Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/routers/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ const router = new express.Router();
// GET /metric/data/ID?sortBy=-date;key;component;result
// GET /metric/data/ID?page=1
router.get('/metric/data/', auth, [
check('domainid').isMongoId(),
check('page', 'page is required as query parameter').isLength({ min: 1 })
query('domainid').isMongoId(),
query('page', 'page is required as query parameter').isLength({ min: 1 }),
query('result').optional().isBoolean(),
query('key').optional().isLength({ max: 30 }).isString(),
query('environment').optional().isLength({ max: 30 }).isString(),
query('component').optional().isLength({ max: 50 }).isString(),
query('group').optional().isLength({ max: 30 }).isString(),
query('dateBefore').optional().isISO8601(),
query('dateAfter').optional().isISO8601()
], validate, async (req, res) => {
try {
const page = String(req.query.page);
if (isNaN(page)) {
const page = Number(req.query.page);
if (Number.isNaN(page)) {
throw new TypeError('Page value should be a number');
}

Expand All @@ -39,11 +46,11 @@ router.get('/metric/statistics/', auth, [
query('domainid').isMongoId(),
query('statistics', 'add one or more options {swicthers,components,reasons,all} separed by comma').isLength({ min: 3 }),
query('dateGroupPattern', 'e.g. YYYY-MM-DD HH:mm').optional().isLength({ max: 16 }),
query('key').optional().isLength({ max: 30 }),
query('environment').optional().isLength({ max: 30 }),
query('key').optional().isLength({ max: 30 }).isString(),
query('environment').optional().isLength({ max: 30 }).isString(),
query('result').optional().isBoolean(),
query('component').optional().isLength({ max: 50 }),
query('group').optional().isLength({ max: 30 }),
query('component').optional().isLength({ max: 50 }).isString(),
query('group').optional().isLength({ max: 30 }).isString(),
query('dateBefore').optional().isISO8601(),
query('dateAfter').optional().isISO8601()
], validate, async (req, res) => {
Expand Down
19 changes: 11 additions & 8 deletions src/services/metric.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from 'moment';
import { NotFoundError } from '../exceptions/index.js';
import { verifyOwnership } from '../helpers/index.js';
import { verifyOwnership, formatInput } from '../helpers/index.js';
import { EnvType } from '../models/environment.js';
import { Metric } from '../models/metric.js';
import { ActionTypes, RouterTypes } from '../models/permission.js';
Expand Down Expand Up @@ -182,26 +182,29 @@ function buildMetricsFilter(req) {
aggregate.match({ $expr: { $eq: [{ $toString: '$domain' }, args.domain] } });

if (req.query.environment) {
args.environment = req.query.environment;
aggregate.match({ environment: String(req.query.environment) });
const environment = formatInput(String(req.query.environment));
args.environment = environment;
aggregate.match({ environment: environment });
} else {
args.environment = EnvType.DEFAULT;
aggregate.match({ environment: EnvType.DEFAULT });
}

if (req.query.result) {
args.result = req.query.result;
args.result = Boolean(req.query.result);
aggregate.match({ result: String(req.query.result) === 'true' });
}

if (req.query.component) {
args.component = req.query.component;
aggregate.match({ component: String(req.query.component) });
const component = formatInput(String(req.query.component));
args.component = component;
aggregate.match({ component: component });
}

if (req.query.group) {
args.group = req.query.group;
aggregate.match({ group: String(req.query.group) });
const group = formatInput(String(req.query.group), { allowSpace: true });
args.group = group;
aggregate.match({ group: group });
}

if (req.query.dateBefore && !req.query.dateAfter) {
Expand Down