Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATOR-232 - Add latest endpoints #4

Merged
merged 5 commits into from
Apr 10, 2024
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ JOB=consulagentonionoo
FROM=-7d
TO=now
INTERVAL=6h

PORT=3000
3 changes: 3 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: DEV - Build and Deploy

on:
pull_request:
branches:
- development
push:
branches:
- development
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.idea
dist
.env
coverage
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
transform: {'^.+\\.ts?$': 'ts-jest'},
testEnvironment: 'node',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
}
5,204 changes: 4,546 additions & 658 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
"main": "index.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest --coverage"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.11.30",
"@types/supertest": "^6.0.2",
"jest": "^29.7.0",
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.4.3"
},
Expand Down
52 changes: 42 additions & 10 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import QueryString from 'qs';
dotenv.config();

const app = express();
const port = 3000;
const PORT = process.env.PORT ?? 3000;
const vmService = new VictoriaMetricsService(process.env.VICTORIA_METRICS_ADDRESS as string);

const CLUSTER = process.env.CLUSTER ?? 'local';
Expand All @@ -17,27 +17,57 @@ const FROM = process.env.FROM ?? '-7d';
const TO = process.env.TO ?? 'now';
const INTERVAL = process.env.INTERVAL ?? '6h';

app.get('/query-range/:query', async (req, res) => {
await handleQuery(req.params.query, req.query, res);
});
const TOTAL_RELAYS_METRIC = 'total_relays';
const TOTAL_OBSERVED_BANDWIDTH_METRIC = 'total_observed_bandwidth';
const AVERAGE_BANDWIDTH_RATE_METRIC = 'average_bandwidth_rate';

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

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

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

app.get('/total-relays-latest', async (req, res) => {
await handleQuery(buildQuery(TOTAL_RELAYS_METRIC), res);
});

app.get('/total-observed-bandwidth-latest', async (req, res) => {
await handleQuery(buildQuery(TOTAL_OBSERVED_BANDWIDTH_METRIC), res);
});

app.get('/average-bandwidth-rate-latest', async (req, res) => {
await handleQuery(buildQuery(AVERAGE_BANDWIDTH_RATE_METRIC), res);
});

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

async function handleQuery(query: string, params: QueryString.ParsedQs, res: any) {
async function handleQuery(query: string, res: any) {
try {
const vmRawData = await vmService.query(query);
console.log('VM RAW DATA:', vmRawData);

const mappedData = vmRawData.data.result.reduce((acc: any, item: any) => {
acc[item.metric.status] = item.value[1];
return acc;
}, {});
console.log('MAPPED DATA:', mappedData);

res.json(mappedData);
} catch (error) {
console.error(error);
res.status(500).send('Error querying VictoriaMetrics');
}
}

async function handleQueryRange(query: string, params: QueryString.ParsedQs, res: any) {
try {
const from = String(params.from ?? FROM);
const to = String(params.to ?? TO);
Expand All @@ -59,6 +89,8 @@ async function handleQuery(query: string, params: QueryString.ParsedQs, res: any
}
}

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});

export { app };
88 changes: 88 additions & 0 deletions tests/VictoriaMetricsService.test.ts

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions tests/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// @ts-ignore
import request from 'supertest';
import { app } from '../src/app';
import { VictoriaMetricsService } from '../src/VictoriaMetricsService';
import axios from 'axios';

jest.mock('../src/VictoriaMetricsService');
const mockVictoriaMetricsService = VictoriaMetricsService as jest.MockedClass<typeof VictoriaMetricsService>;

describe('GET /total-relays-latest', () => {
it('responds with json', async () => {
const mockData = {"data":{"resultType":"vector","result":[{"metric":{"__name__":"total_relays","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"all"},"value":[1712316485,"43"]},{"metric":{"__name__":"total_relays","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"offline"},"value":[1712316485,"26"]},{"metric":{"__name__":"total_relays","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"online"},"value":[1712316485,"17"]}]}};
mockVictoriaMetricsService.prototype.query.mockResolvedValue(mockData);
const response = await request(app).get('/total-relays-latest');
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body).toEqual({"all": "43", "offline": "26", "online": "17"});
});
});

describe('GET /total-observed-bandwidth-latest', () => {
it('responds with json', async () => {
const mockData = {"data":{"resultType":"vector","result":[{"metric":{"__name__":"total_observed_bandwidth","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"all"},"value":[1712497632,"43138495"]},{"metric":{"__name__":"total_observed_bandwidth","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"offline"},"value":[1712497632,"3738891"]},{"metric":{"__name__":"total_observed_bandwidth","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"online"},"value":[1712497632,"39399604"]}]}};
mockVictoriaMetricsService.prototype.query.mockResolvedValue(mockData);
const response = await request(app).get('/total-observed-bandwidth-latest');
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body).toEqual({"all": "43138495", "offline": "3738891", "online": "39399604"});
});
});

describe('GET /average-bandwidth-rate-latest', () => {
it('responds with json', async () => {
const mockData = {"data":{"resultType":"vector","result":[{"metric":{"__name__":"average_bandwidth_rate","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"all"},"value":[1712497728,"508379136"]},{"metric":{"__name__":"average_bandwidth_rate","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"offline"},"value":[1712497728,"9402231.466666667"]},{"metric":{"__name__":"average_bandwidth_rate","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"online"},"value":[1712497728,"948652875.2941176"]}]}};
mockVictoriaMetricsService.prototype.query.mockResolvedValue(mockData);
const response = await request(app).get('/average-bandwidth-rate-latest');
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body).toEqual({"all": "508379136", "offline": "9402231.466666667", "online": "948652875.2941176"});
});
});

describe('GET /total-relays', () => {
it('responds with json', async () => {
const mockData = {"data":{"resultType":"matrix","result":[{"metric":{"__name__":"total_relays","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"all"},"values":[[1712316678,"43"],[1712316978,"44"]]},{"metric":{"__name__":"total_relays","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"offline"},"values":[[1712316678,"26"],[1712316978,"27"]]},{"metric":{"__name__":"total_relays","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"online"},"values":[[1712316678,"17"],[1712316978,"18"]]}]}};
mockVictoriaMetricsService.prototype.query_range.mockResolvedValue(mockData);
const response = await request(app).get('/total-relays');
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body).toEqual({"all": [[1712316678,"43"],[1712316978,"44"]], "offline": [[1712316678,"26"],[1712316978,"27"]], "online": [[1712316678,"17"],[1712316978,"18"]]});
});
});

describe('GET /total-observed-bandwidth', () => {
it('responds with json', async () => {
const mockData = {"data":{"resultType":"matrix","result":[{"metric":{"__name__":"total_observed_bandwidth","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"all"},"values":[[1712497652,"43138495"],[1712497952,"43138495"]]},{"metric":{"__name__":"total_observed_bandwidth","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"offline"},"values":[[1712497652,"3738891"],[1712497952,"3738891"]]},{"metric":{"__name__":"total_observed_bandwidth","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"online"},"values":[[1712497652,"39399604"],[1712497952,"39399604"]]}]}};
mockVictoriaMetricsService.prototype.query_range.mockResolvedValue(mockData);
const response = await request(app).get('/total-observed-bandwidth');
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body).toEqual({"all": [[1712497652,"43138495"],[1712497952,"43138495"]], "offline": [[1712497652,"3738891"],[1712497952,"3738891"]], "online": [[1712497652,"39399604"],[1712497952,"39399604"]]});
});
});

describe('GET /average-bandwidth-rate', () => {
it('responds with json', async () => {
const mockData = {"data":{"resultType":"matrix","result":[{"metric":{"__name__":"average_bandwidth_rate","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"all"},"values":[[1712497700,"508379136"],[1712498000,"508379136"]]},{"metric":{"__name__":"average_bandwidth_rate","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"offline"},"values":[[1712497700,"9402231.466666667"],[1712498000,"9402231.466666667"]]},{"metric":{"__name__":"average_bandwidth_rate","cluster":"local","env":"main","instance":"10.1.244.1:9090","job":"consulagentonionoo","status":"online"},"values":[[1712497700,"948652875.2941176"],[1712498000,"948652875.2941176"]]}]}};
mockVictoriaMetricsService.prototype.query_range.mockResolvedValue(mockData);
const response = await request(app).get('/average-bandwidth-rate');
expect(response.statusCode).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body).toEqual({"all": [[1712497700,"508379136"],[1712498000,"508379136"]], "offline": [[1712497700,"9402231.466666667"],[1712498000,"9402231.466666667"]], "online": [[1712497700,"948652875.2941176"],[1712498000,"948652875.2941176"]]});
});
})
Loading