Fix issues with totalCostInUSD calculation and service token caching

This commit is contained in:
Simon Larsen 2024-04-05 09:40:36 +01:00
parent 689de266a1
commit 20dff66769
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
2 changed files with 40 additions and 58 deletions

View File

@ -102,6 +102,18 @@ export class Service extends DatabaseService<Model> {
});
if (usageBilling && usageBilling.id) {
let totalCostInUSD: number =
usageBilling.totalCostInUSD?.value || 0;
if (
isNaN(totalCostInUSD) ||
totalCostInUSD === undefined ||
totalCostInUSD === null ||
(typeof totalCostInUSD === 'string' && totalCostInUSD === 'NaN')
) {
totalCostInUSD = 0;
}
await this.updateOneById({
id: usageBilling.id,
data: {
@ -110,9 +122,9 @@ export class Service extends DatabaseService<Model> {
data.dataIngestedInGB
),
totalCostInUSD: new Decimal(
(usageBilling.totalCostInUSD?.value || 0) +
totalCostOfThisOperationInUSD
totalCostInUSD + totalCostOfThisOperationInUSD
),
retainTelemetryDataForDays: data.retentionInDays,
},
props: {
isRoot: true,

View File

@ -5,7 +5,6 @@ import {
} from 'CommonServer/Utils/Express';
import { ProbeExpressRequest } from '../Types/Request';
import BadRequestException from 'Common/Types/Exception/BadRequestException';
import GlobalCache from 'CommonServer/Infrastructure/GlobalCache';
import DiskSize from 'Common/Types/DiskSize';
import ObjectID from 'Common/Types/ObjectID';
import TelemetryUsageBillingService from 'CommonServer/Services/TelemetryUsageBillingService';
@ -48,17 +47,6 @@ export default class TelemetryIngest {
const sizeToGb: number = DiskSize.byteSizeToGB(sizeInBytes);
const cachedServiceId: string | null = await GlobalCache.getString(
'service-token',
serviceTokenInHeader as string
);
const serviceProjectId: string | null = await GlobalCache.getString(
'service-project-id',
serviceTokenInHeader as string
);
if (!cachedServiceId || !serviceProjectId) {
// load from the database and set the cache.
const service: TelemetryService | null =
await TelemetryServiceService.findOneBy({
@ -81,31 +69,13 @@ export default class TelemetryIngest {
throw new BadRequestException('Invalid service token');
}
await GlobalCache.setString(
'service-token',
serviceTokenInHeader as string,
service._id?.toString() as string
);
await GlobalCache.setString(
'service-project-id',
serviceTokenInHeader as string,
service.projectId?.toString() as string
);
(req as TelemetryRequest).serviceId = service.id as ObjectID;
(req as TelemetryRequest).projectId = service.projectId as ObjectID;
(req as TelemetryRequest).dataRententionInDays =
service.retainTelemetryDataForDays || DEFAULT_RETENTION_IN_DAYS;
(req as TelemetryRequest).serviceId = service.id as ObjectID;
(req as TelemetryRequest).projectId =
service.projectId as ObjectID;
(req as TelemetryRequest).dataRententionInDays =
service.retainTelemetryDataForDays ||
DEFAULT_RETENTION_IN_DAYS;
}
(req as TelemetryRequest).serviceId = ObjectID.fromString(
cachedServiceId as string
);
(req as TelemetryRequest).projectId = ObjectID.fromString(
serviceProjectId as string
);
(req as TelemetryRequest).projectId = service.projectId as ObjectID;
// report to Usage Service.
TelemetryUsageBillingService.updateUsageBilling({