|
|
|
@ -69,10 +69,13 @@ class Xapi {
|
|
|
|
|
|
|
|
|
|
if (err && err.code) |
|
|
|
|
res.status(400).json({error: err}); |
|
|
|
|
else |
|
|
|
|
else if (err && err.message) |
|
|
|
|
res.status(500).json({error: 'Internal server error : ' + err.message}); |
|
|
|
|
else |
|
|
|
|
res.status(500).json({error: 'Internal server error : ' + err}); |
|
|
|
|
|
|
|
|
|
next(err); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
asyncMiddleware(fn) { |
|
|
|
@ -416,57 +419,67 @@ class Xapi {
|
|
|
|
|
|
|
|
|
|
async groupBy(req, res) { |
|
|
|
|
|
|
|
|
|
let query = 'select ' + req.query._fields + ',count(*) as count from ?? group by ' + req.query._fields; |
|
|
|
|
let params = []; |
|
|
|
|
let tableName = req.app.locals._tableName; |
|
|
|
|
if (req.query && req.query._fields) { |
|
|
|
|
let query = 'select ' + req.query._fields + ',count(*) as count from ?? group by ' + req.query._fields; |
|
|
|
|
let params = []; |
|
|
|
|
let tableName = req.app.locals._tableName; |
|
|
|
|
|
|
|
|
|
params.push(tableName); |
|
|
|
|
params.push(tableName); |
|
|
|
|
|
|
|
|
|
if (!req.query.sort) { |
|
|
|
|
req.query._sort = {} |
|
|
|
|
req.query._sort = '-count' |
|
|
|
|
} |
|
|
|
|
if (!req.query.sort) { |
|
|
|
|
req.query._sort = {} |
|
|
|
|
req.query._sort = '-count' |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query = query + this.mysql.getOrderByClause(req.query, tableName); |
|
|
|
|
query = query + this.mysql.getOrderByClause(req.query, tableName); |
|
|
|
|
|
|
|
|
|
var results = await this.mysql.exec(query, params); |
|
|
|
|
var results = await this.mysql.exec(query, params); |
|
|
|
|
|
|
|
|
|
res.status(200).json(results); |
|
|
|
|
} else { |
|
|
|
|
res.status(400).json({message: 'Missing _fields query params eg: /api/tableName/groupby?_fields=column1'}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
res.status(200).json(results); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async aggregate(req, res) { |
|
|
|
|
|
|
|
|
|
let tableName = req.app.locals._tableName; |
|
|
|
|
let query = 'select ' |
|
|
|
|
let params = [] |
|
|
|
|
let fields = req.query._fields.split(','); |
|
|
|
|
|
|
|
|
|
for (var i = 0; i < fields.length; ++i) { |
|
|
|
|
if (i) { |
|
|
|
|
query = query + ',' |
|
|
|
|
if (req.query && req.query._fields) { |
|
|
|
|
let tableName = req.app.locals._tableName; |
|
|
|
|
let query = 'select ' |
|
|
|
|
let params = [] |
|
|
|
|
let fields = req.query._fields.split(','); |
|
|
|
|
|
|
|
|
|
for (var i = 0; i < fields.length; ++i) { |
|
|
|
|
if (i) { |
|
|
|
|
query = query + ',' |
|
|
|
|
} |
|
|
|
|
query = query + ' min(??) as ?,max(??) as ?,avg(??) as ?,sum(??) as ?,stddev(??) as ?,variance(??) as ? ' |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('min_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('max_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('avg_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('sum_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('stddev_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('variance_of_' + fields[i]); |
|
|
|
|
} |
|
|
|
|
query = query + ' min(??) as ?,max(??) as ?,avg(??) as ?,sum(??) as ?,stddev(??) as ?,variance(??) as ? ' |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('min_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('max_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('avg_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('sum_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('stddev_of_' + fields[i]); |
|
|
|
|
params.push(fields[i]); |
|
|
|
|
params.push('variance_of_' + fields[i]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
query = query + ' from ??' |
|
|
|
|
params.push(tableName) |
|
|
|
|
query = query + ' from ??' |
|
|
|
|
params.push(tableName) |
|
|
|
|
|
|
|
|
|
var results = await this.mysql.exec(query, params); |
|
|
|
|
var results = await this.mysql.exec(query, params); |
|
|
|
|
|
|
|
|
|
res.status(200).json(results); |
|
|
|
|
res.status(200).json(results); |
|
|
|
|
} else { |
|
|
|
|
res.status(400).json({message:'Missing _fields in query params eg: /api/tableName/groupby?_fields=numericColumn1'}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|