@bitblit/ratchet-aws-node-only 6.0.146-alpha → 6.0.147-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -5
- package/src/athena/alb-athena-log-ratchet.spec.ts +36 -0
- package/src/athena/alb-athena-log-ratchet.ts +223 -0
- package/src/athena/athena-ratchet.spec.ts +43 -0
- package/src/athena/athena-ratchet.ts +194 -0
- package/src/build/ratchet-aws-node-only-info.ts +19 -0
- package/src/cli/dynamo-exporter.ts +104 -0
- package/src/cli/ratchet-cli-handler.ts +18 -0
- package/src/cli/site-uploader/site-uploader.ts +143 -0
- package/src/cli/start-instance-and-ssh.ts +70 -0
- package/src/daemon/daemon-like.ts +34 -0
- package/src/daemon/daemon-process-create-options.ts +7 -0
- package/src/daemon/daemon-process-state-public-token.ts +5 -0
- package/src/daemon/daemon-process-state.ts +16 -0
- package/src/daemon/daemon-stream-data-options.ts +6 -0
- package/src/daemon/daemon-util.spec.ts +133 -0
- package/src/daemon/daemon-util.ts +204 -0
- package/src/daemon/daemon.ts +150 -0
- package/src/ec2/ec2-instance-util.ts +60 -0
- package/src/mail/inbound/email-to-db-insert-processor.ts +105 -0
- package/src/mail/inbound/inbound-email-ratchet.spec.ts +28 -0
- package/src/mail/inbound/inbound-email-ratchet.ts +55 -0
- package/src/mail/inbound/parsed-email-processor.ts +6 -0
- package/src/mail/inbound/sample-email-processor.ts +12 -0
- package/src/s3/s3-cache-to-local-disk-ratchet.ts +118 -0
- package/src/s3/s3-cache-to-local-dist-ratchet.spec.ts +31 -0
- package/src/static/albAthenaTableCreate.txt +46 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitblit/ratchet-aws-node-only",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.147-alpha",
|
|
4
4
|
"description": "Common tools for use with AWS (Node only)",
|
|
5
5
|
"note-on-side-effects": "Technically the entries in 'bin' below might be side effects, but they are called explicitly",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
11
|
"files": [
|
|
12
|
+
"src/**",
|
|
12
13
|
"lib/**",
|
|
13
14
|
"bin/**"
|
|
14
15
|
],
|
|
@@ -56,8 +57,8 @@
|
|
|
56
57
|
},
|
|
57
58
|
"license": "Apache-2.0",
|
|
58
59
|
"dependencies": {
|
|
59
|
-
"@bitblit/ratchet-aws": "6.0.
|
|
60
|
-
"@bitblit/ratchet-common": "6.0.
|
|
60
|
+
"@bitblit/ratchet-aws": "6.0.147-alpha",
|
|
61
|
+
"@bitblit/ratchet-common": "6.0.147-alpha"
|
|
61
62
|
},
|
|
62
63
|
"optionalDependencies": {
|
|
63
64
|
"@aws-sdk/client-athena": "3.922.0",
|
|
@@ -76,8 +77,8 @@
|
|
|
76
77
|
"@aws-sdk/client-athena": "^3.922.0",
|
|
77
78
|
"@aws-sdk/client-sts": "^3.922.0",
|
|
78
79
|
"@aws-sdk/types": "^3.922.0",
|
|
79
|
-
"@bitblit/ratchet-aws": "6.0.
|
|
80
|
-
"@bitblit/ratchet-common": "6.0.
|
|
80
|
+
"@bitblit/ratchet-aws": "6.0.147-alpha",
|
|
81
|
+
"@bitblit/ratchet-common": "6.0.147-alpha",
|
|
81
82
|
"@smithy/abort-controller": "^4.2.3",
|
|
82
83
|
"@smithy/smithy-client": "^4.9.1",
|
|
83
84
|
"@smithy/util-waiter": "^4.2.3",
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AthenaRatchet } from './athena-ratchet.js';
|
|
2
|
+
|
|
3
|
+
import { AlbAthenaLogRatchet, AlbLogRecord, AlbLogRecordQuery } from './alb-athena-log-ratchet.js';
|
|
4
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
5
|
+
import { AthenaClient } from '@aws-sdk/client-athena';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
7
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
8
|
+
import { LoggerLevelName } from '@bitblit/ratchet-common/logger/logger-level-name';
|
|
9
|
+
import { TimeZoneRatchet } from '@bitblit/ratchet-common/lang/time-zone-ratchet';
|
|
10
|
+
|
|
11
|
+
describe('#AlbAthenaLogRatchet', function () {
|
|
12
|
+
test('Placeholder', async () => {
|
|
13
|
+
expect(2).toEqual(2);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test.skip('should test a query', async () => {
|
|
17
|
+
Logger.setLevel(LoggerLevelName.debug);
|
|
18
|
+
const athena: AthenaClient = new AthenaClient({ region: 'us-east-1' });
|
|
19
|
+
const s3: S3Client = new S3Client({ region: 'us-east-1' });
|
|
20
|
+
|
|
21
|
+
const outputDir: string = 's3://alb-log-bucket/temp';
|
|
22
|
+
const athRatchet: AthenaRatchet = new AthenaRatchet(athena, s3, outputDir);
|
|
23
|
+
const srv: AlbAthenaLogRatchet = new AlbAthenaLogRatchet(athRatchet, 'alb_logs.log_table');
|
|
24
|
+
|
|
25
|
+
const qry: AlbLogRecordQuery = {
|
|
26
|
+
startTimeEpochMS: TimeZoneRatchet.PACIFIC.startOfTodayEpochMS(),
|
|
27
|
+
endTimeEpochMS: TimeZoneRatchet.PACIFIC.startOfTodayEpochMS() + 1000 * 60 * 10,
|
|
28
|
+
limit: 10,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const result: AlbLogRecord[] = await srv.fetchAlbLogRecords(qry);
|
|
32
|
+
|
|
33
|
+
expect(result).toBeTruthy();
|
|
34
|
+
Logger.info('Got objects : %j', result);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { readFileSync, ReadStream } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { AthenaRatchet } from './athena-ratchet.js';
|
|
4
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
5
|
+
import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet';
|
|
6
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
7
|
+
import { EsmRatchet } from '@bitblit/ratchet-common/lang/esm-ratchet';
|
|
8
|
+
import { CsvRatchet } from '@bitblit/ratchet-node-only/csv/csv-ratchet';
|
|
9
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
10
|
+
import { S3Ratchet } from '@bitblit/ratchet-aws/s3/s3-ratchet';
|
|
11
|
+
|
|
12
|
+
// A class to simplify reading an Athena table based on ALB Logs
|
|
13
|
+
// NOTE: This class only runs on Node since it depends on fs and path
|
|
14
|
+
export class AlbAthenaLogRatchet {
|
|
15
|
+
constructor(
|
|
16
|
+
private athena: AthenaRatchet,
|
|
17
|
+
private athenaTableName: string,
|
|
18
|
+
) {
|
|
19
|
+
RequireRatchet.notNullOrUndefined(athena, 'athena');
|
|
20
|
+
RequireRatchet.notNullOrUndefined(StringRatchet.trimToNull(athenaTableName), 'athenaTableName');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async updatePartitions(
|
|
24
|
+
rootPath: string,
|
|
25
|
+
s3: S3Client,
|
|
26
|
+
startTimeEpochMS: number = new Date().getTime() - 1000 * 60 * 60 * 24,
|
|
27
|
+
endTimeEpochMS: number = new Date().getTime(),
|
|
28
|
+
): Promise<string[]> {
|
|
29
|
+
RequireRatchet.true(S3Ratchet.checkS3UrlForValidity(rootPath), 'root path not valid');
|
|
30
|
+
RequireRatchet.notNullOrUndefined(s3, 's3');
|
|
31
|
+
Logger.info('Updating partitions for %s from %s', this.athenaTableName, rootPath);
|
|
32
|
+
const _bucketName: string = S3Ratchet.extractBucketFromURL(rootPath);
|
|
33
|
+
const _rootKey: string = S3Ratchet.extractKeyFromURL(rootPath);
|
|
34
|
+
|
|
35
|
+
let current: number = startTimeEpochMS;
|
|
36
|
+
const clauses: string[] = [];
|
|
37
|
+
while (current < endTimeEpochMS) {
|
|
38
|
+
const dateUtcVal: string = new Date(current).toISOString().substring(0, 10);
|
|
39
|
+
Logger.info('d:%s', dateUtcVal);
|
|
40
|
+
const dateParts: string[] = dateUtcVal.split('-');
|
|
41
|
+
clauses.push(
|
|
42
|
+
"PARTITION (date_utc_partition='" +
|
|
43
|
+
dateUtcVal +
|
|
44
|
+
"') LOCATION '" +
|
|
45
|
+
rootPath +
|
|
46
|
+
'/' +
|
|
47
|
+
dateParts[0] +
|
|
48
|
+
'/' +
|
|
49
|
+
dateParts[1] +
|
|
50
|
+
'/' +
|
|
51
|
+
dateParts[2] +
|
|
52
|
+
"'",
|
|
53
|
+
);
|
|
54
|
+
current += 1000 * 60 * 60 * 24;
|
|
55
|
+
}
|
|
56
|
+
if (clauses.length > 0) {
|
|
57
|
+
const stmt: string = 'ALTER TABLE ' + this.athenaTableName + ' ADD IF NOT EXISTS \n' + clauses.join('\n');
|
|
58
|
+
await this.athena.runQueryToObjects<any>(stmt);
|
|
59
|
+
} else {
|
|
60
|
+
Logger.warn('Not updating partitions - no time between time clauses');
|
|
61
|
+
}
|
|
62
|
+
return clauses;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public async createTable(rootPath: string, replaceIfPresent: boolean = false): Promise<boolean> {
|
|
66
|
+
RequireRatchet.true(S3Ratchet.checkS3UrlForValidity(rootPath), 'root path not valid');
|
|
67
|
+
let rval: boolean = false;
|
|
68
|
+
Logger.info('Creating ALB table %s', this.athenaTableName);
|
|
69
|
+
if (replaceIfPresent) {
|
|
70
|
+
Logger.info('Replace if present specified, removed old table');
|
|
71
|
+
try {
|
|
72
|
+
await this.athena.runQueryToObjects<any>('drop table ' + this.athenaTableName);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
Logger.info('Drop error : %j', err);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let tableCreateQry: string = readFileSync(
|
|
79
|
+
path.join(EsmRatchet.fetchDirName(import.meta.url), '../static/albAthenaTableCreate.txt'),
|
|
80
|
+
).toString();
|
|
81
|
+
tableCreateQry = StringRatchet.simpleTemplateFill(tableCreateQry, { TABLE_NAME: this.athenaTableName, ALB_LOG_ROOT: rootPath }, true);
|
|
82
|
+
Logger.info('Creating table with %s', tableCreateQry);
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
await this.athena.runQueryToObjects<any>(tableCreateQry);
|
|
86
|
+
rval = true;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
Logger.error('Error creating table : %s', err);
|
|
89
|
+
}
|
|
90
|
+
return rval;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public static async readLogObjectsFromCsvStream(readStream: ReadStream): Promise<AlbLogRecord[]> {
|
|
94
|
+
return CsvRatchet.streamParse(readStream, (p) => p);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public static async readLogObjectsFromFile(fileName: string): Promise<AlbLogRecord[]> {
|
|
98
|
+
return CsvRatchet.fileParse(fileName, (p) => p);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public async fetchAlbLogRecords(qry: AlbLogRecordQuery): Promise<AlbLogRecord[]> {
|
|
102
|
+
const tempFile: string = await this.fetchAlbLogRecordsToFile(qry);
|
|
103
|
+
return AlbAthenaLogRatchet.readLogObjectsFromFile(tempFile);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public async fetchAlbLogRecordsToFile(qry: AlbLogRecordQuery, outputFileName: string = null): Promise<string> {
|
|
107
|
+
Logger.info('Querying %s : %j', this.athenaTableName, qry);
|
|
108
|
+
|
|
109
|
+
let qrySt: string = 'select * from ' + this.athenaTableName + ' where 1=1 ';
|
|
110
|
+
if (qry.startTimeEpochMS) {
|
|
111
|
+
// Dates use the partition for speed and time column for accuracy
|
|
112
|
+
if (qry.startTimeEpochMS) {
|
|
113
|
+
qrySt += " AND time >= '" + new Date(qry.startTimeEpochMS).toISOString() + "'";
|
|
114
|
+
qrySt += " AND date_utc_partition >='" + new Date(qry.startTimeEpochMS).toISOString().substring(0, 10) + "'";
|
|
115
|
+
}
|
|
116
|
+
if (qry.endTimeEpochMS) {
|
|
117
|
+
qrySt += " AND time < '" + new Date(qry.endTimeEpochMS).toISOString() + "'";
|
|
118
|
+
qrySt += " AND date_utc_partition <='" + new Date(qry.endTimeEpochMS).toISOString().substring(0, 10) + "'";
|
|
119
|
+
}
|
|
120
|
+
if (qry.requestUrlFilter) {
|
|
121
|
+
qrySt += " AND request_url LIKE '" + qry.requestUrlFilter + "'";
|
|
122
|
+
}
|
|
123
|
+
if (qry.limit) {
|
|
124
|
+
qrySt += ' LIMIT ' + qry.limit;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const result: string = await this.athena.runQueryToFile(qrySt, null, outputFileName);
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public static readonly CREATE_TABLE_STATEMENT: string =
|
|
133
|
+
'CREATE EXTERNAL TABLE IF NOT EXISTS `{{TABLE NAME}}`(\n' +
|
|
134
|
+
" `type` string COMMENT '',\n" +
|
|
135
|
+
" `time` string COMMENT '',\n" +
|
|
136
|
+
" `elb` string COMMENT '',\n" +
|
|
137
|
+
" `client_ip` string COMMENT '',\n" +
|
|
138
|
+
" `client_port` int COMMENT '',\n" +
|
|
139
|
+
" `target_ip` string COMMENT '',\n" +
|
|
140
|
+
" `target_port` int COMMENT '',\n" +
|
|
141
|
+
" `request_processing_time` double COMMENT '',\n" +
|
|
142
|
+
" `target_processing_time` double COMMENT '',\n" +
|
|
143
|
+
" `response_processing_time` double COMMENT '',\n" +
|
|
144
|
+
" `elb_status_code` string COMMENT '',\n" +
|
|
145
|
+
" `target_status_code` string COMMENT '',\n" +
|
|
146
|
+
" `received_bytes` bigint COMMENT '',\n" +
|
|
147
|
+
" `sent_bytes` bigint COMMENT '',\n" +
|
|
148
|
+
" `request_verb` string COMMENT '',\n" +
|
|
149
|
+
" `request_url` string COMMENT '',\n" +
|
|
150
|
+
" `request_proto` string COMMENT '',\n" +
|
|
151
|
+
" `user_agent` string COMMENT '',\n" +
|
|
152
|
+
" `ssl_cipher` string COMMENT '',\n" +
|
|
153
|
+
" `ssl_protocol` string COMMENT '',\n" +
|
|
154
|
+
" `target_group_arn` string COMMENT '',\n" +
|
|
155
|
+
" `trace_id` string COMMENT '',\n" +
|
|
156
|
+
" `domain_name` string COMMENT '',\n" +
|
|
157
|
+
" `chosen_cert_arn` string COMMENT '',\n" +
|
|
158
|
+
" `matched_rule_priority` string COMMENT '',\n" +
|
|
159
|
+
" `request_creation_time` string COMMENT '',\n" +
|
|
160
|
+
" `actions_executed` string COMMENT '',\n" +
|
|
161
|
+
" `redirect_url` string COMMENT '',\n" +
|
|
162
|
+
" `lambda_error_reason` string COMMENT '',\n" +
|
|
163
|
+
" `target_port_list` string COMMENT '',\n" +
|
|
164
|
+
" `target_status_code_list` string COMMENT '',\n" +
|
|
165
|
+
" `new_field` string COMMENT '')\n" +
|
|
166
|
+
'PARTITIONED BY (\n' +
|
|
167
|
+
' `date_utc_partition` string\n' +
|
|
168
|
+
')\n' +
|
|
169
|
+
'ROW FORMAT SERDE\n' +
|
|
170
|
+
" 'org.apache.hadoop.hive.serde2.RegexSerDe'\n" +
|
|
171
|
+
'WITH SERDEPROPERTIES (\n' +
|
|
172
|
+
' \'input.regex\'=\'([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \\"([^ ]*) ([^ ]*) (- |[^ ]*)\\" \\"([^\\"]*)\\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \\"([^\\"]*)\\" \\"([^\\"]*)\\" \\"([^\\"]*)\\" ([-.0-9]*) ([^ ]*) \\"([^\\"]*)\\" \\"([^\\"]*)\\" \\"([^ ]*)\\" \\"([^s]+)\\" \\"([^s]+)\\"(.*)\')\n' +
|
|
173
|
+
'STORED AS INPUTFORMAT\n' +
|
|
174
|
+
" 'org.apache.hadoop.mapred.TextInputFormat'\n" +
|
|
175
|
+
'OUTPUTFORMAT\n' +
|
|
176
|
+
" 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'\n" +
|
|
177
|
+
'LOCATION\n' +
|
|
178
|
+
" '{{ALB_LOG_ROOT}}'\n";
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface AlbLogRecordQuery {
|
|
182
|
+
startTimeEpochMS?: number;
|
|
183
|
+
endTimeEpochMS?: number;
|
|
184
|
+
requestUrlFilter?: string;
|
|
185
|
+
limit?: number;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Matches the fields that Alb stores natively
|
|
189
|
+
export interface AlbLogRecord {
|
|
190
|
+
type: string;
|
|
191
|
+
time: string;
|
|
192
|
+
elb: string;
|
|
193
|
+
client_ip: string;
|
|
194
|
+
client_port: string;
|
|
195
|
+
target_ip: string;
|
|
196
|
+
target_port: string;
|
|
197
|
+
request_processing_time: string;
|
|
198
|
+
target_processing_time: string;
|
|
199
|
+
response_processing_time: string;
|
|
200
|
+
elb_status_code: string;
|
|
201
|
+
target_status_code: string;
|
|
202
|
+
received_bytes: string;
|
|
203
|
+
sent_bytes: string;
|
|
204
|
+
request_verb: string;
|
|
205
|
+
request_url: string;
|
|
206
|
+
request_proto: string;
|
|
207
|
+
user_agent: string;
|
|
208
|
+
ssl_cipher: string;
|
|
209
|
+
ssl_protocol: string;
|
|
210
|
+
target_group_arn: string;
|
|
211
|
+
trace_id: string;
|
|
212
|
+
domain_name: string;
|
|
213
|
+
chosen_cert_arn: string;
|
|
214
|
+
matched_rule_priority: string;
|
|
215
|
+
request_creation_time: string;
|
|
216
|
+
actions_executed: string;
|
|
217
|
+
redirect_url: string;
|
|
218
|
+
lambda_error_reason: string;
|
|
219
|
+
target_port_list: string;
|
|
220
|
+
target_status_code_list: string;
|
|
221
|
+
new_field: string;
|
|
222
|
+
date_utc_partition: string;
|
|
223
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AthenaRatchet } from './athena-ratchet.js';
|
|
2
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
3
|
+
import {
|
|
4
|
+
AthenaClient,
|
|
5
|
+
GetQueryExecutionCommand,
|
|
6
|
+
GetQueryExecutionOutput,
|
|
7
|
+
StartQueryExecutionCommand,
|
|
8
|
+
StartQueryExecutionOutput,
|
|
9
|
+
} from '@aws-sdk/client-athena';
|
|
10
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
11
|
+
import { mockClient } from 'aws-sdk-client-mock';
|
|
12
|
+
import { beforeEach, describe, expect, test } from 'vitest';
|
|
13
|
+
|
|
14
|
+
let mockAthena;
|
|
15
|
+
let mockS3;
|
|
16
|
+
|
|
17
|
+
describe('#AthenaRatchet', function () {
|
|
18
|
+
mockAthena = mockClient(AthenaClient);
|
|
19
|
+
mockS3 = mockClient(S3Client);
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
mockAthena.reset();
|
|
23
|
+
mockS3.reset();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test.skip('should test a query', async () => {
|
|
27
|
+
const outputDir: string = 's3://your-bucket/your-prefix';
|
|
28
|
+
const qry: string = 'select * from test limit 20000';
|
|
29
|
+
|
|
30
|
+
mockAthena.on(StartQueryExecutionCommand).resolves({ jobName: 'b' } as StartQueryExecutionOutput);
|
|
31
|
+
mockAthena.on(GetQueryExecutionCommand).resolves({
|
|
32
|
+
QueryExecution: { ResultConfiguration: { OutputLocation: 'test' }, Status: { State: 'SUCCEEDED' } },
|
|
33
|
+
} as GetQueryExecutionOutput);
|
|
34
|
+
|
|
35
|
+
const ratchet: AthenaRatchet = new AthenaRatchet(mockAthena, mockS3, outputDir);
|
|
36
|
+
|
|
37
|
+
const result: any[] = await ratchet.runQueryToObjects(qry);
|
|
38
|
+
//const result: string = await ratchet.runQueryToFile(qry, null, 'testfile.csv');
|
|
39
|
+
|
|
40
|
+
expect(result).toBeTruthy();
|
|
41
|
+
Logger.info('Got objects : %j', result);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AthenaClient,
|
|
3
|
+
GetNamedQueryCommand,
|
|
4
|
+
GetNamedQueryCommandOutput,
|
|
5
|
+
GetQueryExecutionCommand,
|
|
6
|
+
GetQueryExecutionCommandOutput,
|
|
7
|
+
ListNamedQueriesCommand,
|
|
8
|
+
ListNamedQueriesCommandOutput,
|
|
9
|
+
NamedQuery,
|
|
10
|
+
Row,
|
|
11
|
+
StartQueryExecutionCommand,
|
|
12
|
+
StartQueryExecutionCommandOutput,
|
|
13
|
+
StartQueryExecutionInput,
|
|
14
|
+
} from '@aws-sdk/client-athena';
|
|
15
|
+
import { GetObjectCommand, GetObjectCommandOutput, GetObjectRequest, S3Client } from '@aws-sdk/client-s3';
|
|
16
|
+
import tmp from 'tmp';
|
|
17
|
+
import fs, { WriteStream } from 'fs';
|
|
18
|
+
import { Readable } from 'stream';
|
|
19
|
+
import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet';
|
|
20
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
21
|
+
import { CsvRatchet } from '@bitblit/ratchet-node-only/csv/csv-ratchet';
|
|
22
|
+
import { PromiseRatchet } from '@bitblit/ratchet-common/lang/promise-ratchet';
|
|
23
|
+
import { StopWatch } from '@bitblit/ratchet-common/lang/stop-watch';
|
|
24
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
25
|
+
|
|
26
|
+
export class AthenaRatchet {
|
|
27
|
+
constructor(
|
|
28
|
+
private athena: AthenaClient,
|
|
29
|
+
private s3: S3Client,
|
|
30
|
+
private outputLocation: string,
|
|
31
|
+
) {
|
|
32
|
+
RequireRatchet.notNullOrUndefined(athena);
|
|
33
|
+
RequireRatchet.notNullOrUndefined(s3);
|
|
34
|
+
RequireRatchet.notNullOrUndefined(outputLocation);
|
|
35
|
+
RequireRatchet.true(outputLocation.startsWith('s3://'));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public static athenaRowsToObject<T>(input: Row[]): T[] {
|
|
39
|
+
const colNames: string[] = input[0].Data.map((d) => d.VarCharValue);
|
|
40
|
+
const temp: Row[] = input.slice(1);
|
|
41
|
+
const rval: T[] = temp.map((t) => {
|
|
42
|
+
const newItem: T = {} as T;
|
|
43
|
+
for (let i = 0; i < t.Data.length; i++) {
|
|
44
|
+
newItem[colNames[i]] = t.Data[i].VarCharValue;
|
|
45
|
+
}
|
|
46
|
+
return newItem;
|
|
47
|
+
});
|
|
48
|
+
return rval;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async fetchQueryIds(): Promise<string[]> {
|
|
52
|
+
const params = {
|
|
53
|
+
// MaxResults: 0,
|
|
54
|
+
NextToken: null,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
let rval: string[] = [];
|
|
58
|
+
let next: ListNamedQueriesCommandOutput = null;
|
|
59
|
+
|
|
60
|
+
do {
|
|
61
|
+
next = await this.athena.send(new ListNamedQueriesCommand(params));
|
|
62
|
+
rval = rval.concat(next.NamedQueryIds);
|
|
63
|
+
params.NextToken = next.NextToken;
|
|
64
|
+
} while (params.NextToken);
|
|
65
|
+
|
|
66
|
+
return rval;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async listQueries(): Promise<NamedQuery[]> {
|
|
70
|
+
const rval: NamedQuery[] = [];
|
|
71
|
+
const ids: string[] = await this.fetchQueryIds();
|
|
72
|
+
Logger.debug('Finding %d items', ids.length);
|
|
73
|
+
for (const id of ids) {
|
|
74
|
+
//for (let i = 0; i < ids.length; i++) {
|
|
75
|
+
const params = {
|
|
76
|
+
NamedQueryId: id,
|
|
77
|
+
};
|
|
78
|
+
const val: GetNamedQueryCommandOutput = await this.athena.send(new GetNamedQueryCommand(params));
|
|
79
|
+
rval.push(val.NamedQuery);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return rval;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public async findQueryByName(name: string): Promise<NamedQuery> {
|
|
86
|
+
const all: NamedQuery[] = await this.listQueries();
|
|
87
|
+
const rval: NamedQuery = all.find((a) => a.Name.toLowerCase() == name.toLowerCase());
|
|
88
|
+
return rval;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public async runQueryToObjects<T>(queryIn: string, queryParams: any = {}, pingTimeMS = 2000): Promise<T[]> {
|
|
92
|
+
Logger.info('Running query to objects');
|
|
93
|
+
const outputLoc = await this.runQueryToOutputLocation(queryIn, queryParams, pingTimeMS);
|
|
94
|
+
Logger.info('Query succeeded, processing file from %s', outputLoc);
|
|
95
|
+
|
|
96
|
+
const bucketName: string = outputLoc.substring(5, outputLoc.indexOf('/', 5));
|
|
97
|
+
const obKey: string = outputLoc.substring(outputLoc.indexOf('/', 5) + 1);
|
|
98
|
+
|
|
99
|
+
const req: GetObjectRequest = {
|
|
100
|
+
Bucket: bucketName,
|
|
101
|
+
Key: obKey,
|
|
102
|
+
};
|
|
103
|
+
const getFileOut: GetObjectCommandOutput = await this.s3.send(new GetObjectCommand(req));
|
|
104
|
+
|
|
105
|
+
const bodyAsString: string = await getFileOut.Body.transformToString();
|
|
106
|
+
|
|
107
|
+
const rval: T[] = await CsvRatchet.stringParse<T>(
|
|
108
|
+
bodyAsString,
|
|
109
|
+
(p) => {
|
|
110
|
+
return p;
|
|
111
|
+
},
|
|
112
|
+
{ columns: true, skip_empty_lines: true },
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return rval;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public async runQueryToFile(queryIn: string, queryParams: any = {}, targetDataFileIn: string = null, pingTimeMS = 2000): Promise<string> {
|
|
119
|
+
Logger.info('Running query to file');
|
|
120
|
+
const outputLoc = await this.runQueryToOutputLocation(queryIn, queryParams, pingTimeMS);
|
|
121
|
+
Logger.info('Query succeeded, pulling file from %s', outputLoc);
|
|
122
|
+
|
|
123
|
+
const bucketName: string = outputLoc.substring(5, outputLoc.indexOf('/', 5));
|
|
124
|
+
const obKey: string = outputLoc.substring(outputLoc.indexOf('/', 5) + 1);
|
|
125
|
+
|
|
126
|
+
const req: GetObjectRequest = {
|
|
127
|
+
Bucket: bucketName,
|
|
128
|
+
Key: obKey,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const targetDataFile: string = targetDataFileIn || tmp.fileSync({ postfix: '.csv', keep: false }).name;
|
|
132
|
+
const fileStream: WriteStream = fs.createWriteStream(targetDataFile);
|
|
133
|
+
const output: GetObjectCommandOutput = await this.s3.send(new GetObjectCommand(req));
|
|
134
|
+
|
|
135
|
+
const readStream: Readable = output.Body as Readable;
|
|
136
|
+
readStream.pipe(fileStream);
|
|
137
|
+
|
|
138
|
+
const rval: string = await PromiseRatchet.resolveOnEvent<string>(readStream, ['finish', 'close'], ['error'], targetDataFile);
|
|
139
|
+
Logger.silly('Response: %s', rval);
|
|
140
|
+
return targetDataFile;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private async runQueryToOutputLocation(queryIn: string, queryParams: any = {}, pingTimeMS = 2000): Promise<string> {
|
|
144
|
+
let rval: string = null;
|
|
145
|
+
const timer: StopWatch = new StopWatch();
|
|
146
|
+
const query: string = StringRatchet.simpleTemplateFill(queryIn, queryParams, true, '{', '}');
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
Logger.info('Starting query : %s', query);
|
|
150
|
+
|
|
151
|
+
const token: string = StringRatchet.createType4Guid();
|
|
152
|
+
const params: StartQueryExecutionInput = {
|
|
153
|
+
QueryString: query,
|
|
154
|
+
ResultConfiguration: {
|
|
155
|
+
/* required */
|
|
156
|
+
OutputLocation: this.outputLocation,
|
|
157
|
+
EncryptionConfiguration: {
|
|
158
|
+
EncryptionOption: 'SSE_S3' /* required */,
|
|
159
|
+
// KmsKey: 'STRING_VALUE'
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
ClientRequestToken: token,
|
|
163
|
+
QueryExecutionContext: {
|
|
164
|
+
Database: 'default',
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const startToken: StartQueryExecutionCommandOutput = await this.athena.send(new StartQueryExecutionCommand(params));
|
|
169
|
+
|
|
170
|
+
const getExecParams: any = {
|
|
171
|
+
QueryExecutionId: startToken.QueryExecutionId,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const finalStates: string[] = ['FAILED', 'CANCELLED', 'SUCCEEDED'];
|
|
175
|
+
let curState: GetQueryExecutionCommandOutput = await this.athena.send(new GetQueryExecutionCommand(getExecParams));
|
|
176
|
+
while (finalStates.indexOf(curState.QueryExecution.Status.State) === -1) {
|
|
177
|
+
await PromiseRatchet.createTimeoutPromise('wait', pingTimeMS);
|
|
178
|
+
Logger.debug('%s : %s : %s', curState.QueryExecution.Status.State, timer.dump(), query);
|
|
179
|
+
curState = await this.athena.send(new GetQueryExecutionCommand(getExecParams));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (curState.QueryExecution.Status.State === 'FAILED') {
|
|
183
|
+
Logger.warn('Query failed : %s', curState.QueryExecution.Status.StateChangeReason);
|
|
184
|
+
} else if (curState.QueryExecution.Status.State === 'SUCCEEDED') {
|
|
185
|
+
rval = curState.QueryExecution.ResultConfiguration.OutputLocation;
|
|
186
|
+
}
|
|
187
|
+
} catch (err) {
|
|
188
|
+
Logger.warn('Failure : %s', err, err);
|
|
189
|
+
}
|
|
190
|
+
Logger.info('Query took %s : %s', timer.dump(), query);
|
|
191
|
+
|
|
192
|
+
return rval;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BuildInformation } from '@bitblit/ratchet-common/build/build-information';
|
|
2
|
+
|
|
3
|
+
export class RatchetAwsNodeOnlyInfo {
|
|
4
|
+
// Empty constructor prevents instantiation
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
6
|
+
private constructor() {}
|
|
7
|
+
|
|
8
|
+
public static buildInformation(): BuildInformation {
|
|
9
|
+
const val: BuildInformation = {
|
|
10
|
+
version: 'LOCAL-SNAPSHOT',
|
|
11
|
+
hash: 'LOCAL-HASH',
|
|
12
|
+
branch: 'LOCAL-BRANCH',
|
|
13
|
+
tag: 'LOCAL-TAG',
|
|
14
|
+
timeBuiltISO: 'LOCAL-TIME-ISO',
|
|
15
|
+
notes: 'LOCAL-NOTES',
|
|
16
|
+
};
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
|
|
2
|
+
import { Logger } from '@bitblit/ratchet-common/logger/logger';
|
|
3
|
+
import { PromiseRatchet } from '@bitblit/ratchet-common/lang/promise-ratchet';
|
|
4
|
+
import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet';
|
|
5
|
+
import fs, { WriteStream } from 'fs';
|
|
6
|
+
import { DynamoRatchet } from '@bitblit/ratchet-aws/dynamodb/dynamo-ratchet';
|
|
7
|
+
|
|
8
|
+
import readline from 'readline';
|
|
9
|
+
import { QueryCommandInput, ScanCommandInput } from '@aws-sdk/lib-dynamodb';
|
|
10
|
+
|
|
11
|
+
export class DynamoExporter {
|
|
12
|
+
// Prevent instantiation
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
14
|
+
private constructor() {}
|
|
15
|
+
|
|
16
|
+
public static async importJsonLFileToTable(dynamo: DynamoRatchet, tableName: string, filename: string): Promise<number> {
|
|
17
|
+
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
18
|
+
RequireRatchet.notNullOrUndefined(tableName, 'tableName');
|
|
19
|
+
RequireRatchet.notNullOrUndefined(filename, 'filename');
|
|
20
|
+
|
|
21
|
+
const fileStream = fs.createReadStream(filename);
|
|
22
|
+
|
|
23
|
+
const rl = readline.createInterface({
|
|
24
|
+
input: fileStream,
|
|
25
|
+
crlfDelay: Infinity,
|
|
26
|
+
});
|
|
27
|
+
// Note: we use the crlfDelay option to recognize all instances of CR LF
|
|
28
|
+
// ('\r\n') in input.txt as a single line break.
|
|
29
|
+
|
|
30
|
+
let rval: number = 0;
|
|
31
|
+
for await (const line of rl) {
|
|
32
|
+
if (rval % 100 === 0) {
|
|
33
|
+
Logger.info('Importing line %d', rval);
|
|
34
|
+
}
|
|
35
|
+
if (StringRatchet.trimToNull(line)) {
|
|
36
|
+
const parsed: any = JSON.parse(line);
|
|
37
|
+
await dynamo.simplePut(tableName, parsed);
|
|
38
|
+
rval++;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return rval;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public static async exportScanToJsonLFile(dynamo: DynamoRatchet, scan: ScanCommandInput, filename: string): Promise<number> {
|
|
46
|
+
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
47
|
+
RequireRatchet.notNullOrUndefined(scan, 'scan');
|
|
48
|
+
RequireRatchet.notNullOrUndefined(filename, 'filename');
|
|
49
|
+
const ws: WriteStream = fs.createWriteStream(filename);
|
|
50
|
+
ws.on('end', () => {
|
|
51
|
+
Logger.debug('Write complete');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const rval: number = await DynamoExporter.exportScanToJsonLWriteStream(dynamo, scan, ws);
|
|
55
|
+
|
|
56
|
+
await PromiseRatchet.resolveOnEvent(ws, ['finish', 'close'], ['error']);
|
|
57
|
+
ws.close();
|
|
58
|
+
return rval;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public static async exportQueryToJsonLFile(dynamo: DynamoRatchet, qry: QueryCommandInput, filename: string): Promise<number> {
|
|
62
|
+
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
63
|
+
RequireRatchet.notNullOrUndefined(qry, 'qry');
|
|
64
|
+
RequireRatchet.notNullOrUndefined(filename, 'filename');
|
|
65
|
+
|
|
66
|
+
const ws: WriteStream = fs.createWriteStream(filename);
|
|
67
|
+
ws.on('end', () => {
|
|
68
|
+
Logger.debug('Write complete');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const rval: number = await DynamoExporter.exportQueryToJsonLWriteStream(dynamo, qry, ws);
|
|
72
|
+
|
|
73
|
+
await PromiseRatchet.resolveOnEvent(ws, ['finish', 'close'], ['error']);
|
|
74
|
+
ws.close();
|
|
75
|
+
return rval;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public static async exportScanToJsonLWriteStream(dynamo: DynamoRatchet, scan: ScanCommandInput, target: WriteStream): Promise<number> {
|
|
79
|
+
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
80
|
+
RequireRatchet.notNullOrUndefined(scan, 'scan');
|
|
81
|
+
RequireRatchet.notNullOrUndefined(target, 'target');
|
|
82
|
+
|
|
83
|
+
const rval: number = await dynamo.fullyExecuteProcessOverScan(scan, async (row) =>
|
|
84
|
+
DynamoExporter.writeItemToJsonLStream(row, target, false),
|
|
85
|
+
);
|
|
86
|
+
return rval;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static async exportQueryToJsonLWriteStream(dynamo: DynamoRatchet, qry: QueryCommandInput, target: WriteStream): Promise<number> {
|
|
90
|
+
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
91
|
+
RequireRatchet.notNullOrUndefined(qry, 'qry');
|
|
92
|
+
RequireRatchet.notNullOrUndefined(target, 'target');
|
|
93
|
+
|
|
94
|
+
const rval: number = await dynamo.fullyExecuteProcessOverQuery(qry, async (row) =>
|
|
95
|
+
DynamoExporter.writeItemToJsonLStream(row, target, false),
|
|
96
|
+
);
|
|
97
|
+
return rval;
|
|
98
|
+
}
|
|
99
|
+
public static writeItemToJsonLStream(item: any, target: WriteStream, includeNulls: boolean = false): void {
|
|
100
|
+
if (!!item || includeNulls) {
|
|
101
|
+
target.write(JSON.stringify(item) + '\n');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AbstractRatchetCliHandler } from '@bitblit/ratchet-node-only/cli/abstract-ratchet-cli-handler';
|
|
2
|
+
import { SiteUploader } from './site-uploader/site-uploader.js';
|
|
3
|
+
import { StartInstanceAndSsh } from './start-instance-and-ssh.js';
|
|
4
|
+
import { BuildInformation } from '@bitblit/ratchet-common/build/build-information';
|
|
5
|
+
import { RatchetAwsNodeOnlyInfo } from '../build/ratchet-aws-node-only-info.js';
|
|
6
|
+
|
|
7
|
+
export class RatchetCliHandler extends AbstractRatchetCliHandler {
|
|
8
|
+
fetchHandlerMap(): Record<string, any> {
|
|
9
|
+
return {
|
|
10
|
+
'site-uploader': SiteUploader.runFromCliArgs,
|
|
11
|
+
'start-instance-and-ssh': StartInstanceAndSsh.runFromCliArgs,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
fetchVersionInfo(): BuildInformation {
|
|
16
|
+
return RatchetAwsNodeOnlyInfo.buildInformation();
|
|
17
|
+
}
|
|
18
|
+
}
|