From 224eff0d3df9d006ef429bd99ce0c5ff351e23ef Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 25 Dec 2023 19:21:04 +0000 Subject: [PATCH] Update launch.json and ObjectID.ts, add debugger statement, modify BillingService.test.ts, Statement.ts, and NavBar.tsx, and update OTelIngest.ts --- .vscode/launch.json | 42 ------------------- Common/Types/DiskSize.ts | 13 ++++++ Common/Types/ObjectID.ts | 4 ++ CommonServer/API/BaseAnalyticsAPI.ts | 1 + .../Tests/Services/BillingService.test.ts | 9 ---- .../Utils/AnalyticsDatabase/Statement.ts | 18 +++++++- Dashboard/src/Components/NavBar/NavBar.tsx | 4 +- .../obj/staticwebassets.pack.sentinel | 1 + Ingestor/API/OTelIngest.ts | 26 ++++++++++++ 9 files changed, 63 insertions(+), 55 deletions(-) create mode 100644 Common/Types/DiskSize.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index d63bacf556..13db89b525 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -181,48 +181,6 @@ "restart": true, "autoAttachChildProcesses": true }, - { - "address": "127.0.0.1", - "localRoot": "${workspaceFolder}/Alert", - "name": "Alert: Debug with Docker", - "port": 9133, - "remoteRoot": "/usr/src/app", - "request": "attach", - "skipFiles": [ - "/**" - ], - "type": "node", - "restart": true, - "autoAttachChildProcesses": true - }, - { - "address": "127.0.0.1", - "localRoot": "${workspaceFolder}/Alert", - "name": "Integration: Debug with Docker", - "port": 9134, - "remoteRoot": "/usr/src/app", - "request": "attach", - "skipFiles": [ - "/**" - ], - "type": "node", - "restart": true, - "autoAttachChildProcesses": true - }, - { - "address": "127.0.0.1", - "localRoot": "${workspaceFolder}/licensing", - "name": "Licensing: Debug with Docker", - "port": 9233, - "remoteRoot": "/usr/src/app", - "request": "attach", - "skipFiles": [ - "/**" - ], - "type": "node", - "restart": true, - "autoAttachChildProcesses": true - }, { "address": "127.0.0.1", "localRoot": "${workspaceFolder}/HttpTestServer", diff --git a/Common/Types/DiskSize.ts b/Common/Types/DiskSize.ts new file mode 100644 index 0000000000..d777cdee21 --- /dev/null +++ b/Common/Types/DiskSize.ts @@ -0,0 +1,13 @@ +export default class DiskSize { + public static byteSizeToGB(byteSize: number): number { + return byteSize / 1024 / 1024 / 1024; + } + + public static byteSizeToMB(byteSize: number): number { + return byteSize / 1024 / 1024; + } + + public static byteSizeToKB(byteSize: number): number { + return byteSize / 1024; + } +} \ No newline at end of file diff --git a/Common/Types/ObjectID.ts b/Common/Types/ObjectID.ts index 55beaaa58a..2cef711208 100644 --- a/Common/Types/ObjectID.ts +++ b/Common/Types/ObjectID.ts @@ -23,6 +23,10 @@ export default class ObjectID extends DatabaseProperty { this.id = id; } + + public get value() : string { + return this._id.toString(); + } public equals(other: ObjectID): boolean { return this.id.toString() === other.id.toString(); diff --git a/CommonServer/API/BaseAnalyticsAPI.ts b/CommonServer/API/BaseAnalyticsAPI.ts index 67b52df756..8db076d49c 100644 --- a/CommonServer/API/BaseAnalyticsAPI.ts +++ b/CommonServer/API/BaseAnalyticsAPI.ts @@ -85,6 +85,7 @@ export default class BaseAnalyticsAPI< next: NextFunction ) => { try { + debugger; await this.getList(req, res); } catch (err) { next(err); diff --git a/CommonServer/Tests/Services/BillingService.test.ts b/CommonServer/Tests/Services/BillingService.test.ts index ec9fd47510..7446f7f38b 100644 --- a/CommonServer/Tests/Services/BillingService.test.ts +++ b/CommonServer/Tests/Services/BillingService.test.ts @@ -34,7 +34,6 @@ import { Subscription, } from '../TestingUtils/Services/Types'; import { ActiveMonitoringMeteredPlan } from '../../Types/Billing/MeteredPlan/AllMeteredPlans'; -import Database from '../TestingUtils/Database'; describe('BillingService', () => { let billingService: BillingService; @@ -43,22 +42,14 @@ describe('BillingService', () => { customer.id.toString() ); - let database!: Database; - beforeEach( async () => { jest.clearAllMocks(); billingService = mockIsBillingEnabled(true); - database = new Database(); - await database.createAndConnect(); }, 10 * 1000 // 10 second timeout because setting up the DB is slow ); - afterEach(async () => { - await database.disconnectAndDropDatabase(); - }); - describe('Customer Management', () => { describe('createCustomer', () => { it('should create a customer when valid data is provided', async () => { diff --git a/CommonServer/Utils/AnalyticsDatabase/Statement.ts b/CommonServer/Utils/AnalyticsDatabase/Statement.ts index ae7f9678ba..693d9c0ed9 100644 --- a/CommonServer/Utils/AnalyticsDatabase/Statement.ts +++ b/CommonServer/Utils/AnalyticsDatabase/Statement.ts @@ -2,6 +2,7 @@ import { BaseQueryParams } from '@clickhouse/client'; import { integer } from '@elastic/elasticsearch/lib/api/types'; import { RecordValue } from 'Common/AnalyticsModels/CommonModel'; import TableColumnType from 'Common/Types/AnalyticsDatabase/TableColumnType'; +import ObjectID from 'Common/Types/ObjectID'; import { inspect } from 'util'; /** @@ -17,7 +18,7 @@ export class Statement implements BaseQueryParams { public constructor( private strings: string[] = [''], private values: Array = [] - ) {} + ) { } public get query(): string { let query: string = this.strings.reduce( @@ -46,9 +47,22 @@ export class Statement implements BaseQueryParams { } public get query_params(): Record { + + debugger; + return Object.fromEntries( this.values.map((v: StatementParameter | string, i: integer) => { - return [`p${i}`, typeof v === 'string' ? v : v.value]; + let finalValue: any = v; + + if (typeof v === 'string') { + finalValue = v; + } else if (v.value instanceof ObjectID) { + finalValue = v.value.toString(); + } else { + finalValue = v.value; + } + + return [`p${i}`, finalValue]; }) ); } diff --git a/Dashboard/src/Components/NavBar/NavBar.tsx b/Dashboard/src/Components/NavBar/NavBar.tsx index 403998113f..e68bc08d12 100644 --- a/Dashboard/src/Components/NavBar/NavBar.tsx +++ b/Dashboard/src/Components/NavBar/NavBar.tsx @@ -134,7 +134,7 @@ const DashboardNavbar: FunctionComponent = ( ), }} > - {/* = ( onClick={() => { forceHideMoreMenu(); }} - /> */} + /> { try { + + + // size of req.body in bytes. + const sizeInBytes: number = Buffer.byteLength( + JSON.stringify(req.body) + ); + + let productType: ProductType; + + const sizeToGb: number = DiskSize.byteSizeToGB(sizeInBytes); + if (req.baseUrl === '/otel/v1/traces') { req.body = TracesData.decode(req.body); + productType = ProductType.Traces; } else if (req.baseUrl === '/otel/v1/logs') { req.body = LogsData.decode(req.body); + productType = ProductType.Logs; } else if (req.baseUrl === '/otel/v1/metrics') { req.body = MetricsData.decode(req.body); + productType = ProductType.Metrics; } else { throw new BadRequestException('Invalid URL: ' + req.baseUrl); } @@ -137,6 +154,15 @@ router.use( // report to Usage Service. + UsageBillingService.updateUsageBilling({ + projectId: (req as OtelRequest).projectId, + productType: productType, + usageCount: sizeToGb, + }).catch((err: Error) => { + logger.error("Failed to update usage billing for OTel"); + logger.error(err); + }); + next(); } catch (err) { return next(err);