UNPKG

7.07 kBJavaScriptView Raw
1var fs = require('fs'),
2 path = require('path'),
3 extend = require('xtend'),
4 item_manager = require('./item-manager'),
5 logger = require('./logger')();
6
7/**
8 * Return the dashboard config file based on path
9 *
10 * @param {string} dashboardFilePath dashboard path
11 * @return {object} dashboard configuration
12 */
13
14function readDashboard (dashboardFilePath){
15 var dashboardConfig = JSON.parse(fs.readFileSync(dashboardFilePath,"utf-8"));
16
17 if (!dashboardConfig.layout){
18 throw('No layout field found in ' + dashboardFilePath);
19 }
20
21 if (!dashboardConfig.layout.widgets){
22 throw('No widgets field found in ' + dashboardFilePath);
23 }
24 return dashboardConfig;
25}
26
27/**
28 * Returns true if dashboard matches a particular regex filter
29 *
30 * @param {string} dashboardFullPath dashboard full path
31 * @param {string} filter regex
32 * @return {boolean}
33 */
34
35function matchDashboardFilter (dashboardFullPath, filter){
36 var dashboardName = path.basename(dashboardFullPath);
37 return dashboardName.match(filter);
38}
39
40/**
41 * Returns true if job matches a particular regex filter
42 *
43 * @param {string} jobName job name
44 * @param {string} filter regex
45 * @return {boolean}
46 */
47
48function matchJobFilter (jobName, filter){
49 return jobName.match(filter);
50}
51
52/**
53 * Process dashboard, reading all related jobs
54 *
55 * @param {array} allJobs all available jobs
56 * @param {string} dashboardName dashboard name
57 * @param {object} dashboardConfig dashboard config
58 * @param {object} filters filters, if any
59 * @return {array} related jobs
60 */
61function processDashboard (allJobs, dashboardName, dashboardConfig, filters){
62 var jobs = [];
63 for (var i = 0, l = dashboardConfig.layout.widgets.length; i < l ; i++) {
64 var jobItem = dashboardConfig.layout.widgets[i];
65 if (jobItem.job) { // widgets can run without a job, displaying just static html.
66
67 if (filters.jobFilter){
68 if (!matchJobFilter(jobItem.job, filters.jobFilter)){
69 continue;
70 }
71 }
72
73 var candidateJobs = item_manager.resolve_candidates(allJobs, jobItem.job, "jobs", ".js");
74 if (!candidateJobs.length){
75 throw " ERROR RESOLVING JOB " +
76 "\n No job file found for \"" + jobItem.job + "\" in " + dashboardName +
77 "\n Did you pulled all the packages dependencies? (they are git submodules)" +
78 "\n\n $ git submodule init"+
79 "\n $ git submodule update\n";
80 }
81
82 var job = {
83 task : require(candidateJobs[0]),
84 dashboard_name : path.basename(dashboardName, '.json'),
85 widget_item : jobItem,
86 job_name : jobItem.job,
87 configKey : jobItem.config
88 };
89
90 jobs.push(job);
91 }
92 }
93 return jobs;
94}
95
96
97module.exports = {
98
99 /**
100 * Return the jobs for all available dashboards in all the packages
101 *
102 * @param {object} options options object
103 * @param {Function} callback
104 */
105
106 get_jobs : function (options, callback) {
107
108 var packagesPath = options.packagesPath,
109 configPath = options.configPath,
110 filters = options.filters || {};
111
112 var jobs = [];
113 var config_path = path.join(configPath,"/dashboard_common.json");
114 var generalDashboardConfig = {};
115
116 // ----------------------------------------------
117 // general config is optional, but if it exists it needs to be a valid file
118 // ----------------------------------------------
119 if (fs.existsSync(config_path)){
120 try{
121 generalDashboardConfig = JSON.parse(fs.readFileSync(config_path,"utf-8")).config;
122 if (!generalDashboardConfig) throw 'invalid format. config property not found';
123 }
124 catch (e){
125 return callback("ERROR reading general config file..." + config_path);
126 }
127 }
128
129 // ----------------------------------------------
130 // get all dashboards from all packages folder
131 // ----------------------------------------------
132 item_manager.get(packagesPath, "dashboards", ".json", function(err, dashboardConfigFiles){
133 if (err){ return callback(err); }
134
135 // ----------------------------------------------
136 // get all jobs from those packages
137 // ----------------------------------------------
138 item_manager.get(packagesPath, "jobs", ".js", function(err, allJobs){
139 if (err){ return callback(err); }
140
141 for (var d = 0, dl = dashboardConfigFiles.length; d < dl ; d++) {
142 var dashboardFullPath = dashboardConfigFiles[d];
143
144 if (filters.dashboardFilter){
145 if (!matchDashboardFilter(dashboardFullPath, filters.dashboardFilter)){
146 continue;
147 }
148 }
149
150 var dashboardConfig;
151 var dashboardJobs;
152 try {
153 dashboardConfig = readDashboard(dashboardFullPath);
154 dashboardJobs = processDashboard(allJobs, dashboardFullPath, dashboardConfig, filters);
155 }
156 catch (e){
157 return callback (e);
158 }
159
160 // add config to job, extending for the same config key in general config, if any
161 dashboardJobs = dashboardJobs.map(function(job){
162 job.config = extend(generalDashboardConfig[job.configKey], dashboardConfig.config[job.configKey]);
163 return job;
164 });
165
166 jobs = jobs.concat(dashboardJobs);
167 }
168
169 callback(null, jobs);
170 });
171 });
172 },
173
174 /**
175 * Return the jobs for all available dashboards in all the packages
176 *
177 * @param {object} options options object
178 * @param {Function} callback
179 */
180
181 get_job : function (packagesPath,dashboardName,job, callback) {
182 var jobs = [];
183 var generalDashboardConfig = {};
184
185 // Retrieve the dashboard
186 item_manager.get_first(packagesPath, dashboardName, "dashboards", ".json", function(err, dashboard){
187
188 // ----------------------------------------------
189 // get all jobs from those packages
190 // ----------------------------------------------
191 item_manager.get(packagesPath, "jobs", ".js", function(err, allJobs){
192
193 var dashboardConfig;
194 var dashboardJobs;
195 try {
196 dashboardConfig = readDashboard(dashboard);
197 dashboardJobs = processDashboard(allJobs, dashboard, dashboardConfig, {jobFilter:new RegExp(job)});
198 }
199 catch (e){
200 return callback (e);
201 }
202
203
204 // add config to job, extending for the same config key in general config, if any
205 dashboardJobs = dashboardJobs.map(function(job){
206 job.config = extend(generalDashboardConfig[job.configKey], dashboardConfig.config[job.configKey]);
207 return job;
208 });
209
210 if(dashboardJobs.length) {
211 callback(null,dashboardJobs[0]);
212 } else {
213 callback("The job " + job + " couldn't be found in the dashboard.");
214 }
215
216 });
217 });
218 }
219}
\No newline at end of file