1 | var fs = require('fs'),
|
2 | path = require('path'),
|
3 | extend = require('xtend'),
|
4 | item_manager = require('./item-manager'),
|
5 | logger = require('./logger')();
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function 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 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function matchDashboardFilter (dashboardFullPath, filter){
|
36 | var dashboardName = path.basename(dashboardFullPath);
|
37 | return dashboardName.match(filter);
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function matchJobFilter (jobName, filter){
|
49 | return jobName.match(filter);
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function 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) {
|
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 |
|
97 | module.exports = {
|
98 |
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
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 |
|
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 |
|
131 |
|
132 | item_manager.get(packagesPath, "dashboards", ".json", function(err, dashboardConfigFiles){
|
133 | if (err){ return callback(err); }
|
134 |
|
135 |
|
136 |
|
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 |
|
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 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | get_job : function (packagesPath,dashboardName,job, callback) {
|
182 | var jobs = [];
|
183 | var generalDashboardConfig = {};
|
184 |
|
185 |
|
186 | item_manager.get_first(packagesPath, dashboardName, "dashboards", ".json", function(err, dashboard){
|
187 |
|
188 |
|
189 |
|
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 |
|
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 |