Skip to content

Commit

Permalink
feat: query to scan between two dates & type fix
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Rodriguez-Barrios committed Dec 7, 2024
1 parent e2f03d2 commit e9e7393
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
5 changes: 0 additions & 5 deletions packages/client/src/components/tabs/PlaybackTab.tsx

This file was deleted.

1 change: 0 additions & 1 deletion packages/client/src/objects/TabRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import BatteryTab from "@/components/tabs/BatteryTab";
import FaultsTab from "@/components/tabs/FaultsTab";
import MotorTab from "@/components/tabs/MotorTab";
import MpptTab from "@/components/tabs/MpptTab";
import PlaybackTab from "@/components/tabs/PlaybackTab";
import RaceTab from "@/components/tabs/RaceTab";

export interface SolarCarRoutes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export const getFirstAndLastPacket = async (
response,
);
try {
const { firstDate, lastDate } =
const { firstDateUTC, lastDateUTC } =
await backendController.dynamoDB.getFirstAndLastPacketDates();

logger.info(`ENTRY - ${request.method} ${request.url}`);
const data = {
firstDate: firstDate,
lastDate: lastDate,
firstDate: firstDateUTC,
lastDate: lastDateUTC,
message: "OK",
};
logger.info(`EXIT - ${request.method} ${request.url} - ${200}`);
Expand Down
63 changes: 51 additions & 12 deletions packages/server/src/datasources/DynamoDB/DynamoDB.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { start } from "repl";

import { type BackendController } from "@/controllers/BackendController/BackendController";

import {
Expand All @@ -12,7 +14,13 @@ import {
GetItemCommand,
QueryCommand,
} from "@aws-sdk/client-dynamodb";
import { PutCommand } from "@aws-sdk/lib-dynamodb";
import {
PutCommand,
QueryCommandInput,
ScanCommand,
ScanCommandInput,
} from "@aws-sdk/lib-dynamodb";
import { unmarshall } from "@aws-sdk/util-dynamodb";
import type { ILapData, ITelemetryData } from "@shared/helios-types";

if (!process.env.LAP_TABLE_NAME) {
Expand All @@ -27,7 +35,6 @@ const packetTableName = process.env.PACKET_TABLE_NAME;
const lapTableName = process.env.LAP_TABLE_NAME;

const logger = createLightweightApplicationLogger("DynamoDB.ts");

export class DynamoDB implements DynamoDBtypes {
public client: DynamoDBClient;
backendController: BackendController;
Expand All @@ -53,7 +60,7 @@ export class DynamoDB implements DynamoDBtypes {
const command = new GetItemCommand({
Key: {
id: { S: "packet" },
timestamp: { S: date.toString() },
timestamp: { N: date.toString() },
},
TableName: this.packetTableName,
});
Expand All @@ -65,13 +72,45 @@ export class DynamoDB implements DynamoDBtypes {
}
}

public async scanPacketDataBetweenDates(
startUTCDate: Number,
endUTCDate: Number,
) {
try {
let params: ScanCommandInput = {
TableName: this.packetTableName, // Replace with your table name
ScanFilter: {
Timestamp: {
ComparisonOperator: "BETWEEN",
AttributeValueList: [{ N: startUTCDate }, { N: endUTCDate }],
},
},
};

let lastEvaluatedKey;
do {
const command = new ScanCommand(params);
const response = await this.client.send(command);
const items = response.Items
? response.Items.map((item) => unmarshall(item))
: [];

lastEvaluatedKey = response.LastEvaluatedKey;
if (lastEvaluatedKey) {
params.ExclusiveStartKey = lastEvaluatedKey;
}
} while (lastEvaluatedKey);
} catch (error) {
console.error(new Error(" Error Scanning Packets between Dates"));
}
}
// // Helper function to get lap table data
public async getLapData(date: Date) {
public async getLapData(UTCDate: number) {
try {
const command = new GetItemCommand({
Key: {
id: { S: "lap" },
timestamp: { S: date.toString() },
timestamp: { N: UTCDate.toString() },
},
TableName: this.lapTableName,
});
Expand All @@ -92,7 +131,7 @@ export class DynamoDB implements DynamoDBtypes {
Item: {
data: packet,
id: "packet",
timestamp: packet.TimeStamp.toString(),
timestamp: packet.TimeStamp,
},
TableName: this.packetTableName,
});
Expand All @@ -114,7 +153,7 @@ export class DynamoDB implements DynamoDBtypes {
Item: {
data: packet,
id: "lap",
timestamp: packet.timeStamp.toString(),
timestamp: packet.timeStamp,
},
TableName: this.lapTableName,
});
Expand All @@ -131,8 +170,8 @@ export class DynamoDB implements DynamoDBtypes {

// // Helper function getting first and last playback packets
public async getFirstAndLastPacketDates(): Promise<{
firstDate: Date | null;
lastDate: Date | null;
firstDateUTC: Number | null;
lastDateUTC: Number | null;
}> {
try {
const firstCommand = new QueryCommand({
Expand All @@ -156,10 +195,10 @@ export class DynamoDB implements DynamoDBtypes {

const firstResponse = await this.client.send(firstCommand);
const lastResponse = await this.client.send(lastCommand);
const firstDate = new Date(Number(firstResponse.Items[0].timestamp.S));
const lastDate = new Date(Number(lastResponse.Items[0].timestamp.S));
const firstDateUTC = Number(firstResponse.Items[0].timestamp);
const lastDateUTC = Number(lastResponse.Items[0].timestamp);

return { firstDate, lastDate };
return { firstDateUTC, lastDateUTC };
} catch (error) {
throw new Error(error.message);
}
Expand Down

0 comments on commit e9e7393

Please sign in to comment.