[TIC-Web] source clean up
[archive/20170607/tools/tic.git] / controller / mariadb.js
1 'use strict';
2
3 var mariaSql = require('mariasql');
4 var JL = require('jsnlog').JL;
5 var logger = JL('mariadb.js');
6 var _ = require('lodash');
7
8 /**
9  * Client
10  */
11 var mariaSqlClient;
12
13 /**
14  * mariadb
15  */
16 var mariadb = mariadb || {};
17
18 mariadb.queries = {
19     'addJob': [
20         'insert into tic_job ',
21         '(job_status, job_deleted, job_hasksfile, job_updater, job_uptime, job_register, job_regtime) ',
22         'values ("READY", false, false, "tic", now(), "tic", now());'
23     ],
24     'getJobById': [
25         'select tic_job.job_id job_id, ',
26             'tic_job.job_status job_status, ',
27             'tic_job.job_image_id job_image_id, ',
28             'tic_image.image_name job_image_name, ',
29             'tic_image.image_size job_image_size, ',
30             'tic_job.job_hasksfile job_hasksfile, ',
31             'tic_job.job_uptime job_uptime ',
32         'from tic_job ',
33         'left join tic_image on tic_job.job_image_id = tic_image.image_id ',
34         'where tic_job.job_deleted = false ',
35         'and job_id = <%= strJobId %>;'
36     ],
37     'getJobsTotalCount': [
38         'select count(job_id) as total_count ',
39         'from tic_job ',
40         'where job_deleted = false;'
41     ],
42     'getJobsAllList': [
43         'select tic_job.job_id job_id, ',
44             'tic_job.job_status job_status, ',
45             'tic_job.job_image_id job_image_id, ',
46             'tic_image.image_name job_image_name, ',
47             'tic_image.image_size job_image_size, ',
48             'tic_job.job_hasksfile job_hasksfile, ',
49             'tic_job.job_uptime job_uptime ',
50         'from tic_job ',
51         'left join tic_image on tic_job.job_image_id = tic_image.image_id ',
52         'where tic_job.job_deleted = false ',
53         'order by job_id desc ',
54         'limit <%= startNum %> , 10;'
55     ],
56     'getImagesAllList': [
57         'select tic_job.job_image_id image_id, ',
58         'tic_job.job_id image_job_id, ',
59         'tic_image.image_name image_name, ',
60         'tic_image.image_size image_size, ',
61         'tic_job.job_hasksfile image_hasksfile, ',
62         'tic_job.job_status image_status, ',
63         'tic_job.job_uptime image_uptime ',
64         'from tic_job inner join tic_image ',
65         'where tic_job.job_image_id = tic_image.image_id ',
66         'and tic_job.job_deleted = false ',
67         'order by tic_job.job_id desc ',
68         'limit <%= startNum %> , 10;'
69     ],
70     'getImagesTotalCount': [
71         'select count(image_id) as total_count ',
72         'from tic_image;',
73     ],
74     'addImage': [
75         'insert into tic_image ',
76         '(',
77             'image_type, ',
78             'image_pid',
79         ') ',
80         'values (',
81             '"<%= imageType %>", ',
82             '"<%= imagePid %>"',
83         ');'
84     ]
85 };
86
87 mariadb.doQuery = function doQuery (queryString, callback) {
88     return mariaSqlClient.query(queryString, callback);
89 };
90
91 /**
92  * JOBS
93  */
94 /**
95  * Edit the Job By Id
96  */
97 mariadb.editJob = function addJob (req, res) {
98     var queryString, strJobId, reqParam,
99     job_status, job_deleted, job_hasksfile, job_image_id;
100
101     function onSuccess (err, rows) {
102         if (err) {
103             logger.error(err);
104             throw err;
105         }
106
107         logger.info('editJob.success');
108         res.json(rows);
109     }
110
111     strJobId = req.params.id;
112     reqParam = req.body;
113     job_status = reqParam.job_status;
114     job_deleted = reqParam.job_deleted;
115     job_hasksfile = reqParam.job_hasksfile;
116     job_image_id = reqParam.job_image_id;
117
118     queryString = 'update tic_job set';
119     if (job_image_id) queryString += ' job_image_id = "' + job_image_id + '",';
120     if (job_status) queryString += ' job_status = "' + job_status + '",';
121     if (job_deleted) queryString += ' job_deleted = ' + job_deleted + ',';
122     if (job_hasksfile) queryString += ' job_hasksfile = ' + job_hasksfile + ',';
123     queryString += ' job_updater = "tic",';
124     queryString += ' job_uptime = now()';
125     queryString += ' where job_id = ' + strJobId + ';';
126
127     logger.info('editJob: query = ' + queryString);
128
129     this.doQuery(queryString, onSuccess);
130 };
131
132 /**
133  * Add The New Job
134  */
135 mariadb.addJob = function addJob (req, res) {
136     var queryString;
137
138     function onSuccess (err, rows) {
139         if (err) {
140             logger.error(err);
141             throw err;
142         }
143
144         logger.info('addJob.success: {job_id: ' + rows.info.insertId + '}');
145         res.json({
146             job_id: rows.info.insertId
147         });
148     }
149
150     queryString = _.join(this.queries['addJob'], '');
151
152     logger.info('addJob: query = ' + queryString);
153
154     // call
155     this.doQuery(queryString, onSuccess);
156 };
157
158 /**
159  * Get The Job By ID
160  */
161 mariadb.getJobById = function getJobById (req, res) {
162     var queryString, strJobId;
163
164     function onSuccess (err, rows) {
165         if (err) {
166             logger.error(err);
167             throw err;
168         }
169         res.json(rows);
170     }
171
172     strJobId = req.params.id;
173
174     queryString = _.template(_.join(this.queries['getJobById'], ''))({
175         strJobId: strJobId
176     });
177
178     logger.info('getJobById: query = ' + queryString);
179
180     // call
181     this.doQuery(queryString, onSuccess);
182 };
183
184 /**
185  * Get Total Count of Job
186  */
187 mariadb.getJobsTotalCount = function getJobsTotalCount (req, res) {
188     var queryString;
189
190     function onSuccess (err, rows) {
191         if (err) {
192             logger.error(err);
193             throw err;
194         }
195         res.json(rows);
196     }
197
198     queryString = _.join(this.queries['getJobsTotalCount'], '');
199
200     logger.info('getJobsTotalCount: query = ' + queryString);
201
202     // call
203     this.doQuery(queryString, onSuccess);
204 };
205
206 /**
207  * Get All Jobs
208  */
209 mariadb.getJobsAllList = function getJobsAllList (req, res) {
210     var queryString, reqParam, pageNum, startNum;
211
212     function onSuccess (err, rows) {
213         if (err) {
214             logger.error(err);
215             throw err;
216         }
217         res.json(rows);
218     }
219
220     // strNum
221     reqParam = req.body;
222     pageNum = reqParam.pageNum;
223     startNum = (pageNum - 1) * 10;
224
225     queryString = _.template(_.join(this.queries['getJobsAllList'], ''))({
226         startNum: startNum
227     });
228
229     logger.info('getJobsAllList: query = ' + queryString);
230
231     // call
232     this.doQuery(queryString, onSuccess);
233 };
234
235
236 /**
237  * IAMGES
238  */
239
240 /**
241  * Get All Images
242  */
243 mariadb.getImagesAllList = function getImagesAllList (req, res) {
244     var queryString, reqParam, pageNum, startNum;
245
246     function onSuccess (err, rows) {
247         if (err) {
248             logger.error(err);
249             throw err;
250         }
251         res.json(rows);
252     }
253
254     // strNum
255     reqParam = req.body;
256     pageNum = reqParam.pageNum;
257     startNum = (pageNum - 1) * 10;
258
259     queryString = _.template(_.join(this.queries['getImagesAllList'], ''))({
260         startNum: startNum
261     });
262
263     logger.info('getImagesAllList: query = ' + queryString);
264
265     // call
266     this.doQuery(queryString, onSuccess);
267 };
268
269 /**
270  * Get Total Count of Job
271  */
272 mariadb.getImagesTotalCount = function getImagesTotalCount (req, res) {
273     var queryString;
274
275     function onSuccess (err, rows) {
276         if (err) {
277             logger.error(err);
278             throw err;
279         }
280         res.json(rows);
281     }
282
283     queryString = _.join(this.queries['getImagesTotalCount'], '');
284
285     logger.info('getImagesTotalCount: query = ' + queryString);
286
287     // call
288     this.doQuery(queryString, onSuccess);
289 };
290
291 /**
292  * Add The New Image
293  */
294 mariadb.addImage = function addImage (req, res) {
295     var queryString, paramObj, imagePid;
296
297     function onSuccess (err, rows) {
298         if (err) {
299             logger.error(err);
300             throw err;
301         }
302
303         logger.info('addJob: result = {image_id: ' + rows.info.insertId + '}');
304         res.json({
305             image_id: rows.info.insertId
306         });
307     }
308
309     paramObj = req.body;
310     imagePid = paramObj.imagePid;
311
312     queryString = _.template(_.join(this.queries['addImage'], ''))({
313         imageType: 'external',
314         imagePid: imagePid
315     });
316
317     logger.info('addJob: query = ' + queryString);
318
319     // call
320     this.doQuery(queryString, onSuccess);
321 };
322
323
324 mariadb.editImage = function editImage (req, res) {
325     var queryString, strJobId, reqParam,
326     job_status, job_deleted, job_hasksfile, job_image_id;
327
328     function onSuccess (err, rows) {
329         if (err) {
330             logger.error(err);
331             throw err;
332         }
333
334         logger.info('editJob.success');
335         res.json(rows);
336     }
337
338     strJobId = req.params.id;
339     reqParam = req.body;
340     job_status = reqParam.job_status;
341     job_deleted = reqParam.job_deleted;
342     job_hasksfile = reqParam.job_hasksfile;
343     job_image_id = reqParam.job_image_id;
344
345     queryString = 'update tic_job set';
346     if (job_image_id) queryString += ' job_image_id = "' + job_image_id + '",';
347     if (job_status) queryString += ' job_status = "' + job_status + '",';
348     if (job_deleted) queryString += ' job_deleted = ' + job_deleted + ',';
349     if (job_hasksfile) queryString += ' job_hasksfile = ' + job_hasksfile + ',';
350     queryString += ' job_updater = "tic",';
351     queryString += ' job_uptime = now()';
352     queryString += ' where job_id = ' + strJobId + ';';
353
354     logger.info('editJob: query = ' + queryString);
355
356     this.doQuery(queryString, onSuccess);
357 };
358
359 /**
360  * DB
361  */
362 /**
363  * Connect
364  */
365 mariadb.connectToClient = function connectToClient () {
366     return new Promise(function (resolve, reject) {
367         // connect
368         mariaSqlClient.connect(function (err) {
369             if (err) {
370                 logger.error('connection error');
371                 logger.error(err);
372                 throw err;
373             }
374             logger.info('connection success');
375         });
376     });
377 };
378
379 /**
380  * Create the Client
381  */
382 mariadb.createClient = function createClient () {
383     return new Promise(function (resolve, reject) {
384         logger.info('create client');
385
386         var mariaSqlConfig;
387
388         // config
389         mariaSqlConfig = {
390             host: '127.0.0.1',
391             user: 'tic',
392             password: 'tic',
393             db: 'pdk',
394             timeout: 60000,
395             trace: true
396         }
397
398         // create
399         mariaSqlClient = new mariaSql(mariaSqlConfig);
400         resolve();
401     });
402 };
403
404 /**
405  * Initialize
406  */
407 mariadb.init = function init () {
408     logger.info('init');
409
410     var self = this;
411     self.createClient()
412     .then(self.connectToClient);
413 };
414
415 mariadb.init();
416
417 module.exports = mariadb;