Skip to content

Commit 86460d5

Browse files
committed
feat: update date handling in nodes usage history
- Bump contract library version to 0.3.18 - Modify GetNodesUsageByRangeCommand to accept string dates - Update NodesUsageHistoryController to convert string dates to Date objects - Enhance response model and interface to include date as a Date object - Adjust repository query to group by date and return formatted date
1 parent 42cb9fb commit 86460d5

File tree

7 files changed

+17
-7
lines changed

7 files changed

+17
-7
lines changed

libs/contract/commands/nodes/get-nodes-usage-by-range.command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export namespace GetNodesUsageByRangeCommand {
77
export const TSQ_url = url;
88

99
export const RequestQuerySchema = z.object({
10-
start: z.string().transform((str) => new Date(str)),
11-
end: z.string().transform((str) => new Date(str)),
10+
start: z.string(),
11+
end: z.string(),
1212
});
1313

1414
export type RequestQuery = z.infer<typeof RequestQuerySchema>;
@@ -24,6 +24,7 @@ export namespace GetNodesUsageByRangeCommand {
2424
humanReadableTotal: z.string(),
2525
humanReadableTotalDownload: z.string(),
2626
humanReadableTotalUpload: z.string(),
27+
date: z.string().transform((str) => new Date(str)),
2728
}),
2829
),
2930
});

libs/contract/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@remnawave/backend-contract",
3-
"version": "0.3.14",
3+
"version": "0.3.18",
44
"public": true,
55
"license": "AGPL-3.0-only",
66
"description": "A contract library for Remnawave Backend. It can be used in backend and frontend.",

src/modules/nodes-usage-history/interfaces/get-nodes-usage-by-range.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface IGetNodesUsageByRange {
44
total: bigint;
55
totalDownload: bigint;
66
totalUpload: bigint;
7+
date: string;
78
}

src/modules/nodes-usage-history/models/get-nodes-usage-by-range.response.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ export class GetNodesUsageByRangeResponseModel {
1111
humanReadableTotal: string;
1212
humanReadableTotalDownload: string;
1313
humanReadableTotalUpload: string;
14+
date: Date;
1415

1516
constructor(data: IGetNodesUsageByRange) {
1617
this.nodeUuid = data.nodeUuid;
1718
this.nodeName = data.nodeName;
1819
this.total = Number(data.total);
1920
this.totalDownload = Number(data.totalDownload);
2021
this.totalUpload = Number(data.totalUpload);
22+
this.date = new Date(data.date);
2123
this.humanReadableTotal = prettyBytesUtil(this.total, true, 3, true);
2224
this.humanReadableTotalDownload = prettyBytesUtil(this.totalDownload, true, 3, true);
2325
this.humanReadableTotalUpload = prettyBytesUtil(this.totalUpload, true, 3, true);

src/modules/nodes-usage-history/nodes-usage-history.controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export class NodesUsageHistoryController {
5252
@Query() query: GetNodesUsageByRangeRequestQueryDto,
5353
): Promise<GetNodesUsageByRangeResponseDto> {
5454
const { start, end } = query;
55-
const result = await this.nodesUsageHistoryService.getNodesUsageByRange(start, end);
55+
const result = await this.nodesUsageHistoryService.getNodesUsageByRange(
56+
new Date(start),
57+
new Date(end),
58+
);
5659

5760
const data = errorHandler(result);
5861
return {

src/modules/nodes-usage-history/nodes-usage-history.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class NodesUsageHistoryService {
3030
startDate,
3131
endDate,
3232
);
33+
3334
return {
3435
isOk: true,
3536
response: nodesUsage.map(

src/modules/nodes-usage-history/repositories/nodes-usage-history.repository.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ export class NodesUsageHistoryRepository implements ICrud<NodesUsageHistoryEntit
113113
n.name as "nodeName",
114114
COALESCE(SUM(h."total_bytes"), 0) as total,
115115
COALESCE(SUM(h."download_bytes"), 0) as "totalDownload",
116-
COALESCE(SUM(h."upload_bytes"), 0) as "totalUpload"
116+
COALESCE(SUM(h."upload_bytes"), 0) as "totalUpload",
117+
DATE_TRUNC('day', h."created_at") as "date"
117118
FROM nodes n
118-
LEFT JOIN "nodes_usage_history" h ON h."node_uuid" = n.uuid
119+
INNER JOIN "nodes_usage_history" h ON h."node_uuid" = n.uuid
119120
AND h."created_at" >= ${start}
120121
AND h."created_at" <= ${end}
121-
GROUP BY n.uuid, n.name
122+
GROUP BY n.uuid, n.name, DATE_TRUNC('day', h."created_at")
123+
ORDER BY "date" ASC
122124
`;
123125
}
124126
}

0 commit comments

Comments
 (0)