-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeFunctions.js
91 lines (79 loc) · 2.35 KB
/
writeFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
DATABASE_NAME,
TABLE_NAME,
SQ_ERROR_CONFIGURATION_S3_BUCKET_NAME,
} from "./constants.js";
import * as timestreamSetupHelper from "./utils/timestreamSetup.js";
import * as csvIngestion from "./utils/csv-ingestion.js";
import * as s3Helper from "./utils/s3-helper.js";
import { writeClient } from "./aws-clients.js";
import { WriteRecordsCommand } from "@aws-sdk/client-timestream-write";
const csvFilePath = "sample.csv";
async function writeRecordsFromCSV() {
if (csvFilePath != null) {
await csvIngestion.processCSV(csvFilePath);
}
}
async function setupTimestream() {
await timestreamSetupHelper.createDatabase(DATABASE_NAME);
await s3Helper.createS3Bucket(SQ_ERROR_CONFIGURATION_S3_BUCKET_NAME);
await timestreamSetupHelper.createTable(DATABASE_NAME, TABLE_NAME);
// await timestreamSetupHelper.describeDatabase();
// await timestreamSetupHelper.listDatabases();
// await timestreamSetupHelper.describeTable();
// await timestreamSetupHelper.listTables();
// await timestreamSetupHelper.updateTable();
}
async function writeRecord(data, measureName, measureValue, measureValueType) {
//TODO: Data cleanup
//TODO: Check if database and table exists
const dimensions = [
{
Name: "batteryNumber",
Value: data.batteryNumber.toString(),
},
{
Name: "batteryType",
Value: data.batteryType.toString(),
},
{
Name: "batteryModel",
Value: data.batteryModel.toString(),
},
{
Name: "deviceIMEI",
Value: data.deviceIMEI.toString(),
},
{
Name: "deviceIP",
Value: data.deviceIP.toString(),
},
];
const recordTime = data.recordTime.toString();
const records = [
{
Dimensions: dimensions,
MeasureName: measureName,
MeasureValue: measureValue,
MeasureValueType: measureValueType,
Time: recordTime,
},
{
}
];
const params = {
DatabaseName: DATABASE_NAME,
TableName: TABLE_NAME,
Records: records,
};
try {
const writeRecordsCommand = new WriteRecordsCommand(params);
const response = await writeClient.send(writeRecordsCommand);
console.log("WriteRecords Status: ", response.$metadata.httpStatusCode);
return response;
} catch (err) {
console.log("Error writing records. ", err);
throw err;
}
}
export { writeRecordsFromCSV, setupTimestream, writeRecord };