diff --git a/server_manager/www/app.ts b/server_manager/www/app.ts index 9315d523bc..71c544e20e 100644 --- a/server_manager/www/app.ts +++ b/server_manager/www/app.ts @@ -44,6 +44,8 @@ const DATA_LIMITS_VERSION = '1.1.0'; const CHANGE_HOSTNAME_VERSION = '1.2.0'; const KEY_SETTINGS_VERSION = '1.6.0'; const MAX_ACCESS_KEY_DATA_LIMIT_BYTES = 50 * 10 ** 9; // 50GB +const HOUR_IN_SECS = 60 * 60; +const MONTH_IN_HOURS = 30 * 24; const CANCELLED_ERROR = new Error('Cancelled'); export const LAST_DISPLAYED_SERVER_STORAGE_KEY = 'lastDisplayedServer'; @@ -1044,12 +1046,11 @@ export class App { let totalUserHours = 0; for (const {tunnelTime} of serverMetrics.server) { - // convert to hours - totalUserHours += tunnelTime.seconds / (60 * 60); + totalUserHours += tunnelTime.seconds / HOUR_IN_SECS; } serverView.totalUserHours = totalUserHours; - serverView.totalDevices = serverView.totalUserHours / (30 * 24); + serverView.totalDevices = serverView.totalUserHours / MONTH_IN_HOURS; let totalInboundBytes = 0; for (const {dataTransferred} of serverMetrics.accessKeys) { diff --git a/server_manager/www/shadowbox_server.ts b/server_manager/www/shadowbox_server.ts index 079e37646a..8b8b3ca48c 100644 --- a/server_manager/www/shadowbox_server.ts +++ b/server_manager/www/shadowbox_server.ts @@ -57,8 +57,29 @@ function makeAccessKeyModel(apiAccessKey: AccessKeyJson): server.AccessKey { export class ShadowboxServer implements server.Server { private api: PathApiClient; private serverConfig: ServerConfigJson; - - constructor(private readonly id: string) {} + private supportedEndpoints: { + 'experimental/server/metrics': boolean; + }; + + constructor(private readonly id: string) { + this.id = id; + + this.supportedEndpoints = { + 'experimental/server/metrics': false, + }; + + this.api + .request('experimental/server/metrics') + .then( + () => (this.supportedEndpoints['experimental/server/metrics'] = true) + ) + .catch(error => { + // endpoint is not defined, keep set to false + if (error.response.status === 404) return; + + this.supportedEndpoints['experimental/server/metrics'] = true; + }); + } getId(): string { return this.id; @@ -150,33 +171,30 @@ export class ShadowboxServer implements server.Server { } async getServerMetrics(): Promise { - try { - const result = await this.api.request( + if (this.supportedEndpoints['experimental/server/metrics']) { + return this.api.request( 'experimental/server/metrics' ); + } - return result; - } catch (e) { - // fallback to metrics/transfer endpoint - const result: server.ServerMetricsJson = { - server: [], - accessKeys: [], - }; - - const jsonResponse = - await this.api.request('metrics/transfer'); - - for (const [accessKeyId, bytes] of Object.entries( - jsonResponse.bytesTransferredByUserId - )) { - result.accessKeys.push({ - accessKeyId: Number(accessKeyId), - dataTransferred: {bytes}, - }); - } + const result: server.ServerMetricsJson = { + server: [], + accessKeys: [], + }; + + const jsonResponse = + await this.api.request('metrics/transfer'); - return result; + for (const [accessKeyId, bytes] of Object.entries( + jsonResponse.bytesTransferredByUserId + )) { + result.accessKeys.push({ + accessKeyId: Number(accessKeyId), + dataTransferred: {bytes}, + }); } + + return result; } getName(): string { diff --git a/server_manager/www/testing/models.ts b/server_manager/www/testing/models.ts index 46178ff6e0..e604bc1dc8 100644 --- a/server_manager/www/testing/models.ts +++ b/server_manager/www/testing/models.ts @@ -169,32 +169,9 @@ export class FakeServer implements server.Server { return Promise.resolve(new Map()); } getServerMetrics() { - return Promise.resolve({ - server: [ - { - location: '', - asn: 0, - asOrg: '', - tunnelTime: { - seconds: 0, - }, - dataTransferred: { - bytes: 0, - }, - }, - ], - accessKeys: [ - { - accessKeyId: 0, - tunnelTime: { - seconds: 0, - }, - dataTransferred: { - bytes: 0, - }, - }, - ], - }); + return Promise.reject( + new Error('FakeServer.getServerMetrics not implemented') + ); } addAccessKey() { const accessKey = {