3 var _ = require('lodash');
4 var Metric = require('../../models').Metric;
5 var util = require('util');
8 exports.index = function(req, res) {
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;
17 offset: page * per_page
20 _.forIn(req.query, function(value, key) {
26 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
32 attributes.forEach(function(attribute) {
35 $like: '%' + value + '%'
38 query.where.$or.push(tmp);
45 query.where[key].$like = '%' + value + '%';
50 .findAndCountAll(query)
51 .then(function(result) {
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;
57 res.status(200).send({
61 previous_page: previous_page,
62 total_pages: total_pages
66 .catch(function(err) {
67 res.status(500).send({
68 error: 'Something blew up!'
73 exports.metricValidation = function(req, res) {
74 console.log(req.body);
81 .then(function(metrics) {
83 return res.sendStatus(404);
85 return res.send(metrics);
87 .catch(function(err) {
88 return handleError(res, err);
92 exports.getMetricsByTable = function(req, res) {
93 console.log(req.query);
97 table: req.query.table
100 .then(function(metrics) {
101 console.log(metrics);
103 return res.sendStatus(404);
105 return res.status(200).send(metrics);
107 .catch(function(err) {
108 return handleError(res, err);
112 // Get a single metric
113 exports.show = function(req, res) {
115 .findById(req.params.id)
116 .then(function(metric) {
118 return res.sendStatus(404);
120 return res.send(metric);
122 .catch(function(err) {
123 return handleError(res, err);
127 // Creates a new metric in the DB.
128 exports.create = function(req, res) {
131 .then(function(metric) {
132 return res.status(201).send(metric);
134 .catch(function(err) {
135 return handleError(res, err);
139 // Updates an existing metric in the DB.
140 exports.update = function(req, res) {
150 .then(function(metrics) {
152 return res.sendStatus(404);
154 if (metrics.length > 0) {
155 return res.status(500).send({
156 message: 'MESSAGE_EXIST_METRIC'
168 .then(function(metric) {
170 return res.sendStatus(404);
172 var updated = _.merge(metric, req.body);
175 return res.status(200).send(metric);
177 .catch(function(err) {
178 return handleError(res, err);
181 .catch(function(err) {
182 return handleError(res, err);
185 .catch(function(err) {
186 return handleError(res, err);
190 // Deletes a metric from the DB.
191 exports.destroy = function(req, res) {
193 .findById(req.params.id)
194 .then(function(metric) {
196 return res.sendStatus(404);
198 metric.getZendeskTexts()
199 .then(function(texts) {
200 if (texts.length > 0) {
201 return res.status(500).send({
202 message: 'MESSAGE_METRIC_CONFIGURATION_ASSOCIATED'
207 return res.sendStatus(204);
209 .catch(function(err) {
210 return handleError(res, err);
213 .catch(function(err) {
214 return handleError(res, err);
217 .catch(function(err) {
218 return handleError(res, err);
222 exports.bulkDestroy = function(req, res) {
228 individualHooks: true
231 return res.sendStatus(204);
233 .catch(function(err) {
234 return handleError(res, err);
238 function handleError(res, err) {
239 return res.status(500).send(err);