Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellacosse committed Nov 21, 2024
1 parent c563dee commit e75da55
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/shadowbox/server/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ paths:
responses:
'204':
description: Access key limit deleted successfully.
/server/metrics:
/experimental/server/metrics:
get:
tags: Server
parameters:
Expand Down
14 changes: 7 additions & 7 deletions src/shadowbox/server/manager_service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as restify from 'restify';
import {InMemoryConfig, JsonConfig} from '../infrastructure/json_config';
import {AccessKey, AccessKeyRepository, DataLimit} from '../model/access_key';
import {ManagerMetrics} from './manager_metrics';
import {bindService, ShadowsocksManagerService, convertTimeRangeToHours} from './manager_service';
import {bindService, ShadowsocksManagerService, convertTimeRangeToSeconds} from './manager_service';
import {FakePrometheusClient, FakeShadowsocksServer} from './mocks/mocks';
import {AccessKeyConfigJson, ServerAccessKeyRepository} from './server_access_key';
import {ServerConfigJson} from './server_config';
Expand Down Expand Up @@ -1202,15 +1202,15 @@ describe('bindService', () => {

describe('convertTimeRangeToHours', () => {
it('properly parses time ranges', () => {
expect(convertTimeRangeToHours('30d')).toEqual(30 * 24);
expect(convertTimeRangeToHours('20h')).toEqual(20);
expect(convertTimeRangeToHours('3w')).toEqual(7 * 3 * 24);
expect(convertTimeRangeToSeconds('30d')).toEqual(30 * 24 * 60 * 60);
expect(convertTimeRangeToSeconds('20h')).toEqual(20 * 60 * 60);
expect(convertTimeRangeToSeconds('3w')).toEqual(7 * 3 * 24 * 60 * 60);
});

it('throws when an invalid time range is provided', () => {
expect(() => convertTimeRangeToHours('30dd')).toThrow();
expect(() => convertTimeRangeToHours('hi mom')).toThrow();
expect(() => convertTimeRangeToHours('1j')).toThrow();
expect(() => convertTimeRangeToSeconds('30dd')).toThrow();
expect(() => convertTimeRangeToSeconds('hi mom')).toThrow();
expect(() => convertTimeRangeToSeconds('1j')).toThrow();
});
});

Expand Down
23 changes: 12 additions & 11 deletions src/shadowbox/server/manager_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function bindService(

apiServer.put(`${apiPrefix}/name`, service.renameServer.bind(service));
apiServer.get(`${apiPrefix}/server`, service.getServer.bind(service));
apiServer.get(`${apiPrefix}/server/metrics`, service.getServerMetrics.bind(service));
apiServer.get(`${apiPrefix}/experimental/server/metrics`, service.getServerMetrics.bind(service));
apiServer.put(
`${apiPrefix}/server/access-key-data-limit`,
service.setDefaultDataLimit.bind(service)
Expand Down Expand Up @@ -190,21 +190,22 @@ function redirect(url: string): restify.RequestHandlerType {
};
}

export function convertTimeRangeToHours(timeRange: string): number {
const TIME_RANGE_UNIT_TO_HOURS_MULTIPLYER = {
h: 1,
d: 24,
w: 7 * 24,
export function convertTimeRangeToSeconds(timeRange: string): number {
const TIME_RANGE_UNIT_TO_SECONDS_MULTIPLYER = {
s: 1,
h: 60 * 60,
d: 24 * 60 * 60,
w: 7 * 24 * 60 * 60,
};

const timeRangeValue = Number(timeRange.slice(0, -1));
const timeRangeUnit = timeRange.slice(-1);

if (isNaN(timeRangeValue) || !TIME_RANGE_UNIT_TO_HOURS_MULTIPLYER[timeRangeUnit]) {
if (isNaN(timeRangeValue) || !TIME_RANGE_UNIT_TO_SECONDS_MULTIPLYER[timeRangeUnit]) {
throw new TypeError(`Invalid time range: ${timeRange}`);
}

return timeRangeValue * TIME_RANGE_UNIT_TO_HOURS_MULTIPLYER[timeRangeUnit];
return timeRangeValue * TIME_RANGE_UNIT_TO_SECONDS_MULTIPLYER[timeRangeUnit];
}

function validateAccessKeyId(accessKeyId: unknown): string {
Expand Down Expand Up @@ -629,22 +630,22 @@ export class ShadowsocksManagerService {
async getServerMetrics(req: RequestType, res: ResponseType, next: restify.Next) {
logging.debug(`getServerMetrics request ${JSON.stringify(req.params)}`);

let hours;
let seconds;
try {
if (!req.query?.since) {
return next(
new restifyErrors.MissingParameterError({statusCode: 400}, 'Parameter `since` is missing')
);
}

hours = convertTimeRangeToHours(req.query.since as string);
seconds = convertTimeRangeToSeconds(req.query.since as string);
} catch (error) {
logging.error(error);
return next(new restifyErrors.InvalidArgumentError({statusCode: 400}, error.message));
}

try {
const response = await this.managerMetrics.getServerMetrics({seconds: hours * 60 * 60});
const response = await this.managerMetrics.getServerMetrics({seconds});
res.send(HttpSuccess.OK, response);
logging.debug(`getServerMetrics response ${JSON.stringify(response)}`);
return next();
Expand Down

0 comments on commit e75da55

Please sign in to comment.