seconds objcet

This commit is contained in:
Daniel LaCosse 2024-09-19 15:58:07 -04:00
parent 0fd1008d65
commit 9b398a9a57
3 changed files with 15 additions and 9 deletions

View file

@ -35,10 +35,10 @@ describe('PrometheusManagerMetrics', () => {
const managerMetrics = new PrometheusManagerMetrics(
new FakeTunnelTimePrometheusClient({US: 1000, CA: 2000})
);
const tunnelTime = await managerMetrics.getTunnelTimeByLocation({hours: 0});
const tunnelTime = await managerMetrics.getTunnelTimeByLocation({seconds: 0});
expect(tunnelTime).toEqual([
{location: 'US', asn: undefined, as_org: undefined, tunnel_time_seconds: 1000},
{location: 'CA', asn: undefined, as_org: undefined, tunnel_time_seconds: 2000},
{location: 'US', asn: undefined, as_org: undefined, tunnel_time: {seconds: 1000}},
{location: 'CA', asn: undefined, as_org: undefined, tunnel_time: {seconds: 2000}},
]);
done();
});

View file

@ -18,14 +18,16 @@ import {DataUsageByUser, DataUsageTimeframe} from '../model/metrics';
export type TunnelTimeDimension = 'access_key' | 'country' | 'asn';
interface TunnelTimeRequest {
hours: number;
seconds: number;
}
interface TunnelTimeResponse {
location?: string;
asn?: number;
as_org?: string;
tunnel_time_seconds: number;
tunnel_time: {
seconds: number;
};
}
export interface ManagerMetrics {
@ -55,16 +57,18 @@ export class PrometheusManagerMetrics implements ManagerMetrics {
return {bytesTransferredByUserId: usage};
}
async getTunnelTimeByLocation({hours}: TunnelTimeRequest): Promise<TunnelTimeResponse[]> {
async getTunnelTimeByLocation({seconds}: TunnelTimeRequest): Promise<TunnelTimeResponse[]> {
const {result} = await this.prometheusClient.query(
`sum(increase(shadowsocks_tunnel_time_seconds_per_location[${hours}h])) by (location, asn, asorg)`
`sum(increase(shadowsocks_tunnel_time_seconds_per_location[${seconds}s])) by (location, asn, asorg)`
);
return result.map((entry) => ({
location: entry.metric['location'],
asn: entry.metric['asn'] !== undefined ? parseInt(entry.metric['asn'], 10) : undefined,
as_org: entry.metric['asorg'],
tunnel_time_seconds: Math.round(parseFloat(entry.value[1])),
tunnel_time: {
seconds: Math.round(parseFloat(entry.value[1])),
},
}));
}
}

View file

@ -604,7 +604,9 @@ export class ShadowsocksManagerService {
async getTunnelTimeByLocation(req: RequestType, res: ResponseType, next: restify.Next) {
try {
logging.debug(`getTunnelTime request ${JSON.stringify(req.params)}`);
const response = await this.managerMetrics.getTunnelTimeByLocation({hours: 30 * 24});
const response = await this.managerMetrics.getTunnelTimeByLocation({
seconds: 30 * 24 * 60 * 60,
});
res.send(HttpSuccess.OK, response);
logging.debug(`getTunnelTime response ${JSON.stringify(response)}`);
return next();