forked from Sunbird-Obsrv/obsrv-api-service
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDataMetricsController.ts
More file actions
39 lines (36 loc) · 1.74 KB
/
DataMetricsController.ts
File metadata and controls
39 lines (36 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Request, Response } from "express";
import _ from "lodash";
import { executeNativeQuery } from "../../connections/druidConnection";
import { ResponseHandler } from "../../helpers/ResponseHandler";
import vaidationSchema from "./DataMetricsValidationSchema.json"
import { schemaValidation } from "../../services/ValidationService";
import logger from "../../logger";
import { obsrvError } from "../../types/ObsrvError";
import axios from "axios";
import { config } from "../../configs/Config";
const getBaseUrl = (url: string) => {
if (_.startsWith(url, "/prom")) return config.query_api.prometheus.url + _.replace(url, "/prom", "")
}
const dataMetrics = async (req: Request, res: Response) => {
const isValidSchema = schemaValidation(req.body, vaidationSchema);
if (!isValidSchema?.isValid) {
logger.error({ message: isValidSchema?.message, code: "INVALID_QUERY" })
throw obsrvError("", "INVALID_QUERY", isValidSchema.message, "BAD_REQUEST", 400)
}
const { query } = req.body || {};
const endpoint = query.url;
if (_.startsWith(endpoint, "/prom")) {
query.url = getBaseUrl(endpoint)
const { url, method, headers = {}, body = {}, params = {}, ...rest } = query;
const apiResponse = await axios.request({ url, method, headers, params, data: body, ...rest })
const data = _.get(apiResponse, "data");
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
return res.json(data);
}
else {
const query = _.get(req, ["body", "query", "body", "query"]);
const response = await executeNativeQuery(query);
ResponseHandler.successResponse(req, res, { status: 200, data: _.get(response, "data") });
}
}
export default dataMetrics;