From 6a39bec6f6e97d2f0e7c4deeeb49c360a9536a58 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:49:58 -0400 Subject: [PATCH] feedback --- src/shadowbox/server/api.yml | 29 +++++++++++++++++++++++++ src/shadowbox/server/manager_metrics.ts | 18 +++++++-------- src/shadowbox/server/manager_service.ts | 5 ++++- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/shadowbox/server/api.yml b/src/shadowbox/server/api.yml index 53ef29b3..eb490ec4 100644 --- a/src/shadowbox/server/api.yml +++ b/src/shadowbox/server/api.yml @@ -418,6 +418,35 @@ paths: examples: '0': value: '{"bytesTransferredByUserId":{"1":1008040941,"2":5958113497,"3":752221577}}' + /metrics/tunnel-time-location + get: + description: Returns the tunnel time per location + tags: + - Tunnel time + responses: + '200': + description: The tunnel time per location + content: + application/json: + schema: + type: array + items: + type: object + properties: + location: + type: string + asn: + type: number + as_org: + type: string + tunnel_time: + type: object + properties: + seconds: + type: number + examples: + '0': + value: '[{"location":"US","asn":7922,"as_org":"Comcast Cable Communications, LLC","tunnel_time":{"seconds":2523432}]' /metrics/enabled: get: description: Returns whether metrics is being shared diff --git a/src/shadowbox/server/manager_metrics.ts b/src/shadowbox/server/manager_metrics.ts index 75446546..4db27ac3 100644 --- a/src/shadowbox/server/manager_metrics.ts +++ b/src/shadowbox/server/manager_metrics.ts @@ -15,24 +15,26 @@ import {PrometheusClient} from '../infrastructure/prometheus_scraper'; import {DataUsageByUser, DataUsageTimeframe} from '../model/metrics'; -interface TunnelTimeDuration { +interface Duration { seconds: number; } interface TunnelTimeRequest { - time_window: TunnelTimeDuration; + time_window: Duration; } -interface TunnelTimeResponse { +interface TunnelTimeResponseEntry { location?: string; asn?: number; as_org?: string; - tunnel_time: TunnelTimeDuration; + tunnel_time: Duration; } +type TunnelTimeResponse = TunnelTimeResponseEntry[]; + export interface ManagerMetrics { getOutboundByteTransfer(timeframe: DataUsageTimeframe): Promise; - getTunnelTimeByLocation(request: TunnelTimeRequest): Promise; + getTunnelTimeByLocation(request: TunnelTimeRequest): Promise; } // Reads manager metrics from a Prometheus instance. @@ -57,11 +59,9 @@ export class PrometheusManagerMetrics implements ManagerMetrics { return {bytesTransferredByUserId: usage}; } - async getTunnelTimeByLocation({ - time_window: {seconds}, - }: TunnelTimeRequest): Promise { + async getTunnelTimeByLocation(request: TunnelTimeRequest): Promise { const {result} = await this.prometheusClient.query( - `sum(increase(shadowsocks_tunnel_time_seconds_per_location[${seconds}s])) by (location, asn, asorg)` + `sum(increase(shadowsocks_tunnel_time_seconds_per_location[${request.time_window.seconds}s])) by (location, asn, asorg)` ); return result.map((entry) => ({ diff --git a/src/shadowbox/server/manager_service.ts b/src/shadowbox/server/manager_service.ts index 9e014014..c2f42b61 100644 --- a/src/shadowbox/server/manager_service.ts +++ b/src/shadowbox/server/manager_service.ts @@ -157,7 +157,10 @@ export function bindService( ); apiServer.get(`${apiPrefix}/metrics/transfer`, service.getDataUsage.bind(service)); - apiServer.get(`${apiPrefix}/metrics/tunnel-time`, service.getTunnelTimeByLocation.bind(service)); + apiServer.get( + `${apiPrefix}/metrics/tunnel-time-location`, + service.getTunnelTimeByLocation.bind(service) + ); apiServer.get(`${apiPrefix}/metrics/enabled`, service.getShareMetrics.bind(service)); apiServer.put(`${apiPrefix}/metrics/enabled`, service.setShareMetrics.bind(service));