Skip to content

Commit

Permalink
ATOR-193 - Fix service class
Browse files Browse the repository at this point in the history
  • Loading branch information
yumirkov committed Mar 29, 2024
1 parent 342770b commit ba9e267
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
11 changes: 6 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import express from 'express';
import QueryString from 'qs';
import { Service } from './service';

const app = express();
const port = 3000;
const service = require('./service');
const service = new Service();

app.get('/total-relays', async (req, res) => {
await handleRequest("total_relays", req.query, res);
await handleRequest('total_relays', req.query, res);
});

app.get('/total-observed-bandwidth', async (req, res) => {
await handleRequest("total_observed_bandwidth", req.query, res);
await handleRequest('total_observed_bandwidth', req.query, res);
});

app.get('/average-bandwidth-rate', async (req, res) => {
await handleRequest("average_bandwidth_rate", req.query, res);
await handleRequest('average_bandwidth_rate', req.query, res);
});

app.listen(port, () => {
Expand All @@ -31,4 +32,4 @@ async function handleRequest(metric: string, params: QueryString.ParsedQs, res:
console.error(error);
res.status(500).send('Error querying VictoriaMetrics');
}
}
}
41 changes: 21 additions & 20 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ const FROM = process.env.FROM ?? '-7d';
const TO = process.env.TO ?? 'now';
const INTERVAL = process.env.INTERVAL ?? '6h';

function buildQuery(metric: string): string {
return `${metric}{cluster="${CLUSTER}", env="${ENV}", instance="${ONIONOO_INSTANCE}", job="${JOB}"}`;
export class Service {
async handleRequest(metric: string, params: QueryString.ParsedQs) {
const from = String(params.from ?? FROM);
const to = String(params.to ?? TO);
const interval = String(params.interval ?? INTERVAL);

const vmRawData = await vmClient.query_range(this.buildQuery(metric), from, to, interval);
console.log(vmRawData);

// Transform the response
const mappedData = vmRawData.data.result.reduce((acc: any, item: any) => {
acc[item.metric.status] = item.values;
return acc;
}, {});

return mappedData;
}

buildQuery(metric: string): string {
return `${metric}{cluster="${CLUSTER}", env="${ENV}", instance="${ONIONOO_INSTANCE}", job="${JOB}"}`;
}
}

async function handleRequest(query: string, params: QueryString.ParsedQs) {
const from = String(params.from ?? FROM);
const to = String(params.to ?? TO);
const interval = String(params.interval ?? INTERVAL);

const vmRawData = await vmClient.query_range(query, from, to, interval);
console.log(vmRawData);

// Transform the response
const mappedData = vmRawData.data.result.reduce((acc: any, item: any) => {
acc[item.metric.status] = item.values;
return acc;
}, {});

return mappedData;
}

0 comments on commit ba9e267

Please sign in to comment.