Skip to content

Commit

Permalink
feedback - needs test
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellacosse committed Dec 16, 2024
1 parent fb7f672 commit 9c50e42
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 53 deletions.
7 changes: 4 additions & 3 deletions server_manager/www/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down
66 changes: 42 additions & 24 deletions server_manager/www/shadowbox_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<server.ServerMetricsJson>('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;
Expand Down Expand Up @@ -150,33 +171,30 @@ export class ShadowboxServer implements server.Server {
}

async getServerMetrics(): Promise<server.ServerMetricsJson> {
try {
const result = await this.api.request<server.ServerMetricsJson>(
if (this.supportedEndpoints['experimental/server/metrics']) {
return this.api.request<server.ServerMetricsJson>(
'experimental/server/metrics'
);
}

return result;
} catch (e) {
// fallback to metrics/transfer endpoint
const result: server.ServerMetricsJson = {
server: [],
accessKeys: [],
};

const jsonResponse =
await this.api.request<DataUsageByAccessKeyJson>('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<DataUsageByAccessKeyJson>('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 {
Expand Down
29 changes: 3 additions & 26 deletions server_manager/www/testing/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,9 @@ export class FakeServer implements server.Server {
return Promise.resolve(new Map<server.AccessKeyId, number>());
}
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 = {
Expand Down

0 comments on commit 9c50e42

Please sign in to comment.