-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport-historical-data.ts
executable file
·81 lines (68 loc) · 2.07 KB
/
import-historical-data.ts
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
#!/usr/bin/env ts-node
import { Command } from "commander";
import { AthenaImporter } from "../src/data-import/AthenaImporter";
import { log } from "../src/log";
import { initConfig } from "../src/lib/init-config";
initConfig();
const program = new Command();
program
.name("import-historical-data")
.description("Import historical data for an account")
.requiredOption("--account-id <string>", "The account ID to import")
.option(
"--hours-back <number>",
"Number of hours back to import",
(value: string) => {
const num = Number(value);
if (isNaN(num) || num <= 0) {
throw new Error(
"Invalid argument. Please provide a positive number of hours.",
);
}
return num;
},
1,
)
.option(
"--checkly-api-key <string>",
"Checkly API key",
process.env.CHECKLY_API_KEY,
)
.option(
"--athena-api-key <string>",
"Athena API key",
process.env.CHECKLY_API_KEY,
)
.option(
"--athena-endpoint-url <string>",
"Athena endpoint URL",
process.env.ATHENA_ACCESS_ENDPOINT_URL,
)
.helpOption("--help", "Show this help message");
program.parse(process.argv);
const options = program.opts();
const accountId = options.accountId;
const hoursBack = options.hoursBack; // Already converted to number by our custom parser
const checklyApiKey = options.checklyApiKey;
const athenaApiKey = options.athenaApiKey;
const athenaEndpointUrl = options.athenaEndpointUrl;
const hoursAgo = (hoursBack: number): Date =>
new Date(Date.now() - hoursBack * 60 * 60 * 1000);
const main = async () => {
log.info({ hoursBack, accountId }, "Starting to import data");
const importer = new AthenaImporter({
accountId: accountId,
checklyApiKey: checklyApiKey!,
athenaApiKey: athenaApiKey!,
athenaAccessEndpointUrl: athenaEndpointUrl!,
});
const fromDate = hoursAgo(hoursBack);
const toDate = new Date();
await importer.importAccountData(fromDate, toDate);
log.info("Import completed.");
process.exit(0);
};
main().catch((err) => {
console.error("Import failed:", err);
process.exit(1);
});