Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / metric / metric.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var Metric = require('../../models').Metric;
5 var util = require('util');
6
7 // Get list of metrics
8 exports.index = function(req, res) {
9
10   var attributes = ['name', 'description'];
11   var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
12   var page = req.query.page ? parseInt(req.query.page, 10) : 0;
13
14   var query = {
15     where: {},
16     limit: per_page,
17     offset: page * per_page
18   };
19
20   _.forIn(req.query, function(value, key) {
21     switch (key) {
22       case 'per_page':
23       case 'page':
24         break;
25       case 'sort_by':
26         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
27         break;
28       case 'sort_order':
29         break;
30       case '$':
31         query.where.$or = [];
32         attributes.forEach(function(attribute) {
33           var tmp = {};
34           tmp[attribute] = {
35             $like: '%' + value + '%'
36           };
37
38           query.where.$or.push(tmp);
39         });
40         break;
41       default:
42         query.where[key] = {
43           $like: {}
44         };
45         query.where[key].$like = '%' + value + '%';
46     }
47   });
48
49   Metric
50     .findAndCountAll(query)
51     .then(function(result) {
52
53       var total_pages = Math.ceil(result.count / per_page);
54       var next_page = total_pages > (query.offset + 1) ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page + 1) : null;
55       var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
56
57       res.status(200).send({
58         count: result.count,
59         rows: result.rows,
60         next_page: next_page,
61         previous_page: previous_page,
62         total_pages: total_pages
63       });
64
65     })
66     .catch(function(err) {
67       res.status(500).send({
68         error: 'Something blew up!'
69       });
70     });
71 };
72
73 exports.metricValidation = function(req, res) {
74   console.log(req.body);
75   Metric
76     .findAll({
77       where: {
78         name: req.body.name
79       }
80     })
81     .then(function(metrics) {
82       if (!metrics) {
83         return res.sendStatus(404);
84       }
85       return res.send(metrics);
86     })
87     .catch(function(err) {
88       return handleError(res, err);
89     });
90 };
91
92 exports.getMetricsByTable = function(req, res) {
93   console.log(req.query);
94   Metric
95     .findAll({
96       where: {
97         table: req.query.table
98       }
99     })
100     .then(function(metrics) {
101       console.log(metrics);
102       if (!metrics) {
103         return res.sendStatus(404);
104       }
105       return res.status(200).send(metrics);
106     })
107     .catch(function(err) {
108       return handleError(res, err);
109     });
110 };
111
112 // Get a single metric
113 exports.show = function(req, res) {
114   Metric
115     .findById(req.params.id)
116     .then(function(metric) {
117       if (!metric) {
118         return res.sendStatus(404);
119       }
120       return res.send(metric);
121     })
122     .catch(function(err) {
123       return handleError(res, err);
124     });
125 };
126
127 // Creates a new metric in the DB.
128 exports.create = function(req, res) {
129   Metric
130     .create(req.body)
131     .then(function(metric) {
132       return res.status(201).send(metric);
133     })
134     .catch(function(err) {
135       return handleError(res, err);
136     });
137 };
138
139 // Updates an existing metric in the DB.
140 exports.update = function(req, res) {
141   Metric
142     .findAll({
143       where: {
144         name: req.body.name,
145         id: {
146           $ne: req.body.id
147         }
148       }
149     })
150     .then(function(metrics) {
151       if (!metrics) {
152         return res.sendStatus(404);
153       }
154       if (metrics.length > 0) {
155         return res.status(500).send({
156           message: 'MESSAGE_EXIST_METRIC'
157         })
158       }
159       if (req.body.id) {
160         delete req.body.id;
161       }
162       Metric
163         .find({
164           where: {
165             id: req.params.id
166           }
167         })
168         .then(function(metric) {
169           if (!metric) {
170             return res.sendStatus(404);
171           }
172           var updated = _.merge(metric, req.body);
173           updated.save()
174             .then(function() {
175               return res.status(200).send(metric);
176             })
177             .catch(function(err) {
178               return handleError(res, err);
179             });
180         })
181         .catch(function(err) {
182           return handleError(res, err);
183         });
184     })
185     .catch(function(err) {
186       return handleError(res, err);
187     });
188 };
189
190 // Deletes a metric from the DB.
191 exports.destroy = function(req, res) {
192   Metric
193     .findById(req.params.id)
194     .then(function(metric) {
195       if (!metric) {
196         return res.sendStatus(404);
197       }
198       metric.getZendeskTexts()
199         .then(function(texts) {
200           if (texts.length > 0) {
201             return res.status(500).send({
202               message: 'MESSAGE_METRIC_CONFIGURATION_ASSOCIATED'
203             });
204           }
205           metric.destroy()
206             .then(function() {
207               return res.sendStatus(204);
208             })
209             .catch(function(err) {
210               return handleError(res, err);
211             });
212         })
213         .catch(function(err) {
214           return handleError(res, err);
215         });
216     })
217     .catch(function(err) {
218       return handleError(res, err);
219     });
220 };
221
222 exports.bulkDestroy = function(req, res) {
223   Metric
224     .destroy({
225       where: {
226         id: req.query.id
227       },
228       individualHooks: true
229     })
230     .then(function() {
231       return res.sendStatus(204);
232     })
233     .catch(function(err) {
234       return handleError(res, err);
235     });
236 };
237
238 function handleError(res, err) {
239   return res.status(500).send(err);
240 }