Skip to content

Commit

Permalink
Make scubaclient return bigints
Browse files Browse the repository at this point in the history
- It won't return string, this will be handled by the program
  using the values.

Issue: SCUBA-217
  • Loading branch information
williamlardier committed Jan 21, 2025
1 parent 4ac6c49 commit 2423473
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
34 changes: 26 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export type ScubaClientParameters = Omit<
};

export type ScubaMetrics = {
objectsTotal: string;
bytesTotal: string;
objectsTotal: bigint;
bytesTotal: bigint;
metricsClass: string;
resourceName: string;
id?: number;
Expand All @@ -60,8 +60,8 @@ export type ScubaHttpError = {

export type GetMetricsBatchResponseDateMetrics = {
date: string;
bytesTotal: string;
objectsTotal: string;
bytesTotal: bigint;
objectsTotal: bigint;
};

export type GetMetricsBatchResponseDateError = {
Expand Down Expand Up @@ -186,7 +186,9 @@ export default class ScubaClient {
const resp = (await this._api.getLatestMetrics(metricsClass, resourceName, body, {
...this._defaultReqOptions,
...options,
})) as any;
})) as unknown as { data: ScubaMetrics };
resp.data.bytesTotal = BigInt(resp.data.bytesTotal);
resp.data.objectsTotal = BigInt(resp.data.objectsTotal);
return resp.data;
}

Expand All @@ -204,7 +206,9 @@ export default class ScubaClient {
const resp = (await this._api.getMetrics(metricsClass, resourceName, dateString, body, {
...this._defaultReqOptions,
...options,
})) as any;
})) as unknown as { data: ScubaMetrics };
resp.data.bytesTotal = BigInt(resp.data.bytesTotal);
resp.data.objectsTotal = BigInt(resp.data.objectsTotal);
return resp.data;
}

Expand All @@ -216,7 +220,19 @@ export default class ScubaClient {
const resp = (await this._api.getMetricsBatch(metricsClass, body, {
...this._defaultReqOptions,
...options,
})) as any;
})) as unknown as { data: GetMetricsBatchResponse };
resp.data.metrics.forEach(resource => {
if ('error' in resource) {
return;
}
resource.metrics?.forEach(metric => {
if ('error' in metric) {
return;
}
metric.bytesTotal = BigInt(metric.bytesTotal);
metric.objectsTotal = BigInt(metric.objectsTotal);
});
});
return resp.data;
}

Expand All @@ -242,7 +258,9 @@ export default class ScubaClient {
const resp = (await this._api.internalGetLatestAccountMetrics(canonicalId, {
...this._defaultReqOptions,
...options,
})) as any;
})) as unknown as { data: ScubaMetrics };
resp.data.bytesTotal = BigInt(resp.data.bytesTotal);
resp.data.objectsTotal = BigInt(resp.data.objectsTotal);
return resp.data;
}
}
53 changes: 30 additions & 23 deletions tests/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import { AddressInfo } from 'net';
import { Server, createServer, IncomingMessage, ServerResponse } from 'http';
import { AxiosError } from 'axios';

import ScubaClient, { GetMetricsBatchResponse, ScubaMetrics, AdminResponseCseq } from '../src/client';
import ScubaClient, { GetMetricsBatchResponse, ScubaMetrics, AdminResponseCseq, GetMetricsBatchResponseResourceMetrics } from '../src/client';
import { AdminActions, GetMetricsBatchBody } from '../src/api';
import { RequiredError } from '../src/base';

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json
// @ts-ignore we extend the prototype for testing purposes
// eslint-disable-next-line no-extend-native
BigInt.prototype.toJSON = function toJSON() {
return this.toString();
};

let response;
class MockScubaServer {
server: Server;
Expand Down Expand Up @@ -71,8 +78,8 @@ class MockScubaServer {
response = <ScubaMetrics>{
metricsClass: urlSections[2],
resourceName: urlSections[3],
objectsTotal: '100',
bytesTotal: '1000',
objectsTotal: 100n,
bytesTotal: 1000n,
date: '2024-12-06',
};

Expand All @@ -89,8 +96,8 @@ class MockScubaServer {
resourceName: resourceNames[0],
metrics: [
{
objectsTotal: '100',
bytesTotal: '1000',
objectsTotal: 100n,
bytesTotal: 1000n,
date: '2024-12-06',
},
],
Expand Down Expand Up @@ -150,11 +157,11 @@ describe('Test client', () => {

describe('Test getLatestMetrics', () => {
it('should return a successful response received by scuba', async () => {
const expectedResponse = {
const expectedResponse = <ScubaMetrics>{
metricsClass,
resourceName,
objectsTotal: '100',
bytesTotal: '1000',
objectsTotal: 100n,
bytesTotal: 1000n,
date: '2024-12-06',
};

Expand All @@ -164,11 +171,11 @@ describe('Test client', () => {
});

it('should handle responses with large numbers', async () => {
const largeNumberResponse = {
const largeNumberResponse = <ScubaMetrics>{
metricsClass,
resourceName,
objectsTotal: '9007199254740992',
bytesTotal: '90071992547409920',
objectsTotal: 9007199254740992n,
bytesTotal: 90071992547409920n,
date: '2024-12-06',
};

Expand All @@ -195,11 +202,11 @@ describe('Test client', () => {

describe('Test getMetrics', () => {
it('should return a successful response received by scuba', async () => {
const expectedResponse = {
const expectedResponse = <ScubaMetrics>{
metricsClass,
resourceName,
objectsTotal: '100',
bytesTotal: '1000',
objectsTotal: 100n,
bytesTotal: 1000n,
date: '2024-12-06',
};

Expand All @@ -209,11 +216,11 @@ describe('Test client', () => {
});

it('should handle responses with large numbers', async () => {
const largeNumberResponse = {
const largeNumberResponse = <ScubaMetrics>{
metricsClass,
resourceName,
objectsTotal: '9007199254740992',
bytesTotal: '90071992547409920',
objectsTotal: 9007199254740992n,
bytesTotal: 90071992547409920n,
date: '2024-12-06',
};

Expand All @@ -240,15 +247,15 @@ describe('Test client', () => {

describe('Test getMetricsBatch', () => {
it('should return a successful response received by scuba', async () => {
const expectedResponse = {
const expectedResponse = <GetMetricsBatchResponse>{
metrics: [
{
metricsClass,
resourceName,
metrics: [
{
objectsTotal: '100',
bytesTotal: '1000',
objectsTotal: 100n,
bytesTotal: 1000n,
date: '2024-12-06',
},
],
Expand All @@ -262,15 +269,15 @@ describe('Test client', () => {
});

it('should handle responses with large numbers', async () => {
const largeNumberResponse = {
const largeNumberResponse = <GetMetricsBatchResponse>{
metrics: [
{
metricsClass,
resourceName,
metrics: [
{
objectsTotal: '9007199254740992', // Number.MAX_SAFE_INTEGER + 1
bytesTotal: '90071992547409920',
objectsTotal: 9007199254740992n, // Number.MAX_SAFE_INTEGER + 1
bytesTotal: 90071992547409920n,
date: '2024-12-06',
},
],
Expand Down

0 comments on commit 2423473

Please sign in to comment.