@bitblit/ratchet-aws-node-only 4.0.115-alpha → 4.0.119-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/lib/athena/alb-athena-log-ratchet.d.ts +1 -1
- package/lib/build/ratchet-aws-node-only-info.d.ts +1 -1
- package/lib/cli/dynamo-exporter.d.ts +2 -2
- package/lib/cli/ratchet-cli-handler.d.ts +2 -2
- package/lib/index.d.ts +11 -1
- package/lib/index.mjs +29 -0
- package/lib/index.mjs.map +1 -0
- package/lib/mail/inbound/inbound-email-ratchet.d.ts +2 -2
- package/package.json +14 -28
- package/lib/athena/alb-athena-log-ratchet.js +0 -156
- package/lib/athena/alb-athena-log-ratchet.spec.js +0 -25
- package/lib/athena/athena-ratchet.js +0 -157
- package/lib/athena/athena-ratchet.spec.js +0 -27
- package/lib/build/ratchet-aws-node-only-info.js +0 -14
- package/lib/cli/dynamo-exporter.js +0 -76
- package/lib/cli/ratchet-cli-handler.js +0 -15
- package/lib/cli/site-uploader/site-uploader.js +0 -115
- package/lib/cli/start-instance-and-ssh.js +0 -68
- package/lib/index.js +0 -1
- package/lib/mail/inbound/email-to-db-insert-processor.js +0 -89
- package/lib/mail/inbound/inbound-email-ratchet.js +0 -41
- package/lib/mail/inbound/inbound-email-ratchet.spec.js +0 -18
- package/lib/mail/inbound/parsed-email-processor.js +0 -1
- package/lib/mail/inbound/sample-email-processor.js +0 -8
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference types="node"
|
|
2
|
-
import { S3CacheRatchet } from '@bitblit/ratchet-aws
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { S3CacheRatchet } from '@bitblit/ratchet-aws';
|
|
3
3
|
import { ParsedEmailProcessor } from './parsed-email-processor.js';
|
|
4
4
|
export declare class InboundEmailRatchet {
|
|
5
5
|
private cache;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitblit/ratchet-aws-node-only",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.119-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,
|
|
@@ -8,11 +8,16 @@
|
|
|
8
8
|
"ratchet-aws-cli": "./bin/cli.js"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
|
-
"module": "index.js",
|
|
12
11
|
"files": [
|
|
13
12
|
"lib/*",
|
|
14
13
|
"bin/*"
|
|
15
14
|
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./lib/index.d.ts",
|
|
18
|
+
"import": "./lib/index.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
16
21
|
"contributors": [
|
|
17
22
|
"Christopher Weiss <bitblit@gmail.com>",
|
|
18
23
|
"William Weiss <npm@codification.org>",
|
|
@@ -39,9 +44,9 @@
|
|
|
39
44
|
"docs": "typedoc",
|
|
40
45
|
"lint": "eslint src/**/*.ts",
|
|
41
46
|
"lint-fix": "eslint --fix src/**/*.ts",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"build": "tsc"
|
|
47
|
+
"generate-barrels": "barrelsby -q --delete -d src -e .*\\.spec\\.ts && sed -i 's/\\x27;/.js\\x27;/' src/index.ts",
|
|
48
|
+
"build": "yarn clean && yarn generate-barrels && rollup -c rollup.config.js",
|
|
49
|
+
"force-build": "tsc --build --force"
|
|
45
50
|
},
|
|
46
51
|
"repository": {
|
|
47
52
|
"type": "git",
|
|
@@ -60,35 +65,16 @@
|
|
|
60
65
|
},
|
|
61
66
|
"license": "Apache-2.0",
|
|
62
67
|
"dependencies": {
|
|
63
|
-
"@aws-sdk/client-athena": "3.
|
|
64
|
-
"@
|
|
65
|
-
"@
|
|
66
|
-
"@aws-sdk/client-s3": "3.328.0",
|
|
67
|
-
"@aws-sdk/lib-storage": "3.328.0",
|
|
68
|
-
"@aws-sdk/smithy-client": "3.325.0",
|
|
69
|
-
"@aws-sdk/types": "3.310.0",
|
|
70
|
-
"@bitblit/ratchet-aws": "4.0.115-alpha",
|
|
71
|
-
"@bitblit/ratchet-common": "4.0.115-alpha",
|
|
68
|
+
"@aws-sdk/client-athena": "3.332.0",
|
|
69
|
+
"@bitblit/ratchet-aws": "4.0.119-alpha",
|
|
70
|
+
"@bitblit/ratchet-common": "4.0.119-alpha",
|
|
72
71
|
"mailparser": "3.6.4",
|
|
73
72
|
"mime-types": "2.1.35",
|
|
74
73
|
"tmp": "0.2.1",
|
|
75
|
-
"unzipper": "0.10.
|
|
74
|
+
"unzipper": "0.10.14",
|
|
76
75
|
"walk": "2.3.15"
|
|
77
76
|
},
|
|
78
77
|
"peerDependencies": {
|
|
79
|
-
"@aws-sdk/client-athena": "^3.328.0",
|
|
80
|
-
"@aws-sdk/client-ec2": "^3.328.0",
|
|
81
|
-
"@aws-sdk/client-ec2-instance-connect": "^3.328.0",
|
|
82
|
-
"@aws-sdk/client-s3": "^3.328.0",
|
|
83
|
-
"@aws-sdk/lib-storage": "^3.328.0",
|
|
84
|
-
"@aws-sdk/smithy-client": "^3.328.0",
|
|
85
|
-
"@aws-sdk/types": "^3.310.0",
|
|
86
|
-
"@bitblit/ratchet-aws": "4.0.115-alpha",
|
|
87
|
-
"@bitblit/ratchet-common": "4.0.115-alpha",
|
|
88
|
-
"mailparser": "^3.6.4",
|
|
89
|
-
"mime-types": "^2.1.35",
|
|
90
|
-
"tmp": "^0.2.1",
|
|
91
|
-
"walk": "^2.3.15"
|
|
92
78
|
},
|
|
93
79
|
"devDependencies": {}
|
|
94
80
|
}
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { Logger } from '@bitblit/ratchet-common/lib/logger/logger.js';
|
|
4
|
-
import { RequireRatchet } from '@bitblit/ratchet-common/lib/lang/require-ratchet.js';
|
|
5
|
-
import { StringRatchet } from '@bitblit/ratchet-common/lib/lang/string-ratchet.js';
|
|
6
|
-
import { EsmRatchet } from '@bitblit/ratchet-common/lib/lang/esm-ratchet.js';
|
|
7
|
-
import { S3Ratchet } from '@bitblit/ratchet-aws/lib/s3/s3-ratchet.js';
|
|
8
|
-
import { CsvRatchet } from '@bitblit/ratchet-node-only/lib/csv/csv-ratchet.js';
|
|
9
|
-
export class AlbAthenaLogRatchet {
|
|
10
|
-
athena;
|
|
11
|
-
athenaTableName;
|
|
12
|
-
constructor(athena, athenaTableName) {
|
|
13
|
-
this.athena = athena;
|
|
14
|
-
this.athenaTableName = athenaTableName;
|
|
15
|
-
RequireRatchet.notNullOrUndefined(athena, 'athena');
|
|
16
|
-
RequireRatchet.notNullOrUndefined(StringRatchet.trimToNull(athenaTableName), 'athenaTableName');
|
|
17
|
-
}
|
|
18
|
-
async updatePartitions(rootPath, s3, startTimeEpochMS = new Date().getTime() - 1000 * 60 * 60 * 24, endTimeEpochMS = new Date().getTime()) {
|
|
19
|
-
RequireRatchet.true(S3Ratchet.checkS3UrlForValidity(rootPath), 'root path not valid');
|
|
20
|
-
RequireRatchet.notNullOrUndefined(s3, 's3');
|
|
21
|
-
Logger.info('Updating partitions for %s from %s', this.athenaTableName, rootPath);
|
|
22
|
-
const bucketName = S3Ratchet.extractBucketFromURL(rootPath);
|
|
23
|
-
const rootKey = S3Ratchet.extractKeyFromURL(rootPath);
|
|
24
|
-
let current = startTimeEpochMS;
|
|
25
|
-
const clauses = [];
|
|
26
|
-
while (current < endTimeEpochMS) {
|
|
27
|
-
const dateUtcVal = new Date(current).toISOString().substring(0, 10);
|
|
28
|
-
Logger.info('d:%s', dateUtcVal);
|
|
29
|
-
const dateParts = dateUtcVal.split('-');
|
|
30
|
-
clauses.push("PARTITION (date_utc_partition='" +
|
|
31
|
-
dateUtcVal +
|
|
32
|
-
"') LOCATION '" +
|
|
33
|
-
rootPath +
|
|
34
|
-
'/' +
|
|
35
|
-
dateParts[0] +
|
|
36
|
-
'/' +
|
|
37
|
-
dateParts[1] +
|
|
38
|
-
'/' +
|
|
39
|
-
dateParts[2] +
|
|
40
|
-
"'");
|
|
41
|
-
current += 1000 * 60 * 60 * 24;
|
|
42
|
-
}
|
|
43
|
-
if (clauses.length > 0) {
|
|
44
|
-
const stmt = 'ALTER TABLE ' + this.athenaTableName + ' ADD IF NOT EXISTS \n' + clauses.join('\n');
|
|
45
|
-
await this.athena.runQueryToObjects(stmt);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
Logger.warn('Not updating partitions - no time between time clauses');
|
|
49
|
-
}
|
|
50
|
-
return clauses;
|
|
51
|
-
}
|
|
52
|
-
async createTable(rootPath, replaceIfPresent = false) {
|
|
53
|
-
RequireRatchet.true(S3Ratchet.checkS3UrlForValidity(rootPath), 'root path not valid');
|
|
54
|
-
let rval = false;
|
|
55
|
-
Logger.info('Creating ALB table %s', this.athenaTableName);
|
|
56
|
-
if (replaceIfPresent) {
|
|
57
|
-
Logger.info('Replace if present specified, removed old table');
|
|
58
|
-
try {
|
|
59
|
-
await this.athena.runQueryToObjects('drop table ' + this.athenaTableName);
|
|
60
|
-
}
|
|
61
|
-
catch (err) {
|
|
62
|
-
Logger.info('Drop error : %j', err);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
let tableCreateQry = readFileSync(path.join(EsmRatchet.fetchDirName(import.meta.url), '../static/albAthenaTableCreate.txt')).toString();
|
|
66
|
-
tableCreateQry = tableCreateQry.split('{{TABLE NAME}}').join(this.athenaTableName);
|
|
67
|
-
tableCreateQry = tableCreateQry.split('{{ALB_LOG_ROOT}}').join(rootPath);
|
|
68
|
-
Logger.info('Creating table with %s', tableCreateQry);
|
|
69
|
-
try {
|
|
70
|
-
await this.athena.runQueryToObjects(tableCreateQry);
|
|
71
|
-
rval = true;
|
|
72
|
-
}
|
|
73
|
-
catch (err) {
|
|
74
|
-
Logger.error('Error creating table : %s', err);
|
|
75
|
-
}
|
|
76
|
-
return rval;
|
|
77
|
-
}
|
|
78
|
-
static async readLogObjectsFromCsvStream(readStream) {
|
|
79
|
-
return CsvRatchet.streamParse(readStream, (p) => p);
|
|
80
|
-
}
|
|
81
|
-
static async readLogObjectsFromFile(fileName) {
|
|
82
|
-
return CsvRatchet.fileParse(fileName, (p) => p);
|
|
83
|
-
}
|
|
84
|
-
async fetchAlbLogRecords(qry) {
|
|
85
|
-
const tempFile = await this.fetchAlbLogRecordsToFile(qry);
|
|
86
|
-
return AlbAthenaLogRatchet.readLogObjectsFromFile(tempFile);
|
|
87
|
-
}
|
|
88
|
-
async fetchAlbLogRecordsToFile(qry, outputFileName = null) {
|
|
89
|
-
Logger.info('Querying %s : %j', this.athenaTableName, qry);
|
|
90
|
-
let qrySt = 'select * from ' + this.athenaTableName + ' where 1=1 ';
|
|
91
|
-
if (qry.startTimeEpochMS) {
|
|
92
|
-
if (qry.startTimeEpochMS) {
|
|
93
|
-
qrySt += " AND time >= '" + new Date(qry.startTimeEpochMS).toISOString() + "'";
|
|
94
|
-
qrySt += " AND date_utc_partition >='" + new Date(qry.startTimeEpochMS).toISOString().substring(0, 10) + "'";
|
|
95
|
-
}
|
|
96
|
-
if (qry.endTimeEpochMS) {
|
|
97
|
-
qrySt += " AND time < '" + new Date(qry.endTimeEpochMS).toISOString() + "'";
|
|
98
|
-
qrySt += " AND date_utc_partition <='" + new Date(qry.endTimeEpochMS).toISOString().substring(0, 10) + "'";
|
|
99
|
-
}
|
|
100
|
-
if (qry.requestUrlFilter) {
|
|
101
|
-
qrySt += " AND request_url LIKE '" + qry.requestUrlFilter + "'";
|
|
102
|
-
}
|
|
103
|
-
if (qry.limit) {
|
|
104
|
-
qrySt += ' LIMIT ' + qry.limit;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
const result = await this.athena.runQueryToFile(qrySt, null, outputFileName);
|
|
108
|
-
return result;
|
|
109
|
-
}
|
|
110
|
-
static CREATE_TABLE_STATEMENT = 'CREATE EXTERNAL TABLE IF NOT EXISTS `{{TABLE NAME}}`(\n' +
|
|
111
|
-
" `type` string COMMENT '',\n" +
|
|
112
|
-
" `time` string COMMENT '',\n" +
|
|
113
|
-
" `elb` string COMMENT '',\n" +
|
|
114
|
-
" `client_ip` string COMMENT '',\n" +
|
|
115
|
-
" `client_port` int COMMENT '',\n" +
|
|
116
|
-
" `target_ip` string COMMENT '',\n" +
|
|
117
|
-
" `target_port` int COMMENT '',\n" +
|
|
118
|
-
" `request_processing_time` double COMMENT '',\n" +
|
|
119
|
-
" `target_processing_time` double COMMENT '',\n" +
|
|
120
|
-
" `response_processing_time` double COMMENT '',\n" +
|
|
121
|
-
" `elb_status_code` string COMMENT '',\n" +
|
|
122
|
-
" `target_status_code` string COMMENT '',\n" +
|
|
123
|
-
" `received_bytes` bigint COMMENT '',\n" +
|
|
124
|
-
" `sent_bytes` bigint COMMENT '',\n" +
|
|
125
|
-
" `request_verb` string COMMENT '',\n" +
|
|
126
|
-
" `request_url` string COMMENT '',\n" +
|
|
127
|
-
" `request_proto` string COMMENT '',\n" +
|
|
128
|
-
" `user_agent` string COMMENT '',\n" +
|
|
129
|
-
" `ssl_cipher` string COMMENT '',\n" +
|
|
130
|
-
" `ssl_protocol` string COMMENT '',\n" +
|
|
131
|
-
" `target_group_arn` string COMMENT '',\n" +
|
|
132
|
-
" `trace_id` string COMMENT '',\n" +
|
|
133
|
-
" `domain_name` string COMMENT '',\n" +
|
|
134
|
-
" `chosen_cert_arn` string COMMENT '',\n" +
|
|
135
|
-
" `matched_rule_priority` string COMMENT '',\n" +
|
|
136
|
-
" `request_creation_time` string COMMENT '',\n" +
|
|
137
|
-
" `actions_executed` string COMMENT '',\n" +
|
|
138
|
-
" `redirect_url` string COMMENT '',\n" +
|
|
139
|
-
" `lambda_error_reason` string COMMENT '',\n" +
|
|
140
|
-
" `target_port_list` string COMMENT '',\n" +
|
|
141
|
-
" `target_status_code_list` string COMMENT '',\n" +
|
|
142
|
-
" `new_field` string COMMENT '')\n" +
|
|
143
|
-
'PARTITIONED BY (\n' +
|
|
144
|
-
' `date_utc_partition` string\n' +
|
|
145
|
-
')\n' +
|
|
146
|
-
'ROW FORMAT SERDE\n' +
|
|
147
|
-
" 'org.apache.hadoop.hive.serde2.RegexSerDe'\n" +
|
|
148
|
-
'WITH SERDEPROPERTIES (\n' +
|
|
149
|
-
' \'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' +
|
|
150
|
-
'STORED AS INPUTFORMAT\n' +
|
|
151
|
-
" 'org.apache.hadoop.mapred.TextInputFormat'\n" +
|
|
152
|
-
'OUTPUTFORMAT\n' +
|
|
153
|
-
" 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'\n" +
|
|
154
|
-
'LOCATION\n' +
|
|
155
|
-
" '{{ALB_LOG_ROOT}}'\n";
|
|
156
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { AthenaRatchet } from './athena-ratchet.js';
|
|
2
|
-
import { Logger } from '@bitblit/ratchet-common/lib/logger/logger.js';
|
|
3
|
-
import { LoggerLevelName } from '@bitblit/ratchet-common/lib/logger/logger-level-name.js';
|
|
4
|
-
import { TimeZoneRatchet } from '@bitblit/ratchet-common/lib/lang/time-zone-ratchet.js';
|
|
5
|
-
import { AlbAthenaLogRatchet } from './alb-athena-log-ratchet.js';
|
|
6
|
-
import { S3Client } from '@aws-sdk/client-s3';
|
|
7
|
-
import { AthenaClient } from '@aws-sdk/client-athena';
|
|
8
|
-
describe('#AlbAthenaLogRatchet', function () {
|
|
9
|
-
xit('should test a query', async () => {
|
|
10
|
-
Logger.setLevel(LoggerLevelName.debug);
|
|
11
|
-
const athena = new AthenaClient({ region: 'us-east-1' });
|
|
12
|
-
const s3 = new S3Client({ region: 'us-east-1' });
|
|
13
|
-
const outputDir = 's3://alb-log-bucket/temp';
|
|
14
|
-
const athRatchet = new AthenaRatchet(athena, s3, outputDir);
|
|
15
|
-
const srv = new AlbAthenaLogRatchet(athRatchet, 'alb_logs.log_table');
|
|
16
|
-
const qry = {
|
|
17
|
-
startTimeEpochMS: TimeZoneRatchet.PACIFIC.startOfTodayEpochMS(),
|
|
18
|
-
endTimeEpochMS: TimeZoneRatchet.PACIFIC.startOfTodayEpochMS() + 1000 * 60 * 10,
|
|
19
|
-
limit: 10,
|
|
20
|
-
};
|
|
21
|
-
const result = await srv.fetchAlbLogRecords(qry);
|
|
22
|
-
expect(result).toBeTruthy();
|
|
23
|
-
Logger.info('Got objects : %j', result);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { GetNamedQueryCommand, GetQueryExecutionCommand, ListNamedQueriesCommand, StartQueryExecutionCommand, } from '@aws-sdk/client-athena';
|
|
2
|
-
import { StringRatchet } from '@bitblit/ratchet-common/lib/lang/string-ratchet.js';
|
|
3
|
-
import { Logger } from '@bitblit/ratchet-common/lib/logger/logger.js';
|
|
4
|
-
import { StopWatch } from '@bitblit/ratchet-common/lib/lang/stop-watch.js';
|
|
5
|
-
import { PromiseRatchet } from '@bitblit/ratchet-common/lib/lang/promise-ratchet.js';
|
|
6
|
-
import { GetObjectCommand } from '@aws-sdk/client-s3';
|
|
7
|
-
import tmp from 'tmp';
|
|
8
|
-
import fs from 'fs';
|
|
9
|
-
import { CsvRatchet } from '@bitblit/ratchet-node-only/lib/csv/csv-ratchet.js';
|
|
10
|
-
import { RequireRatchet } from '@bitblit/ratchet-common/lib/lang/require-ratchet.js';
|
|
11
|
-
export class AthenaRatchet {
|
|
12
|
-
athena;
|
|
13
|
-
s3;
|
|
14
|
-
outputLocation;
|
|
15
|
-
constructor(athena, s3, outputLocation) {
|
|
16
|
-
this.athena = athena;
|
|
17
|
-
this.s3 = s3;
|
|
18
|
-
this.outputLocation = outputLocation;
|
|
19
|
-
RequireRatchet.notNullOrUndefined(athena);
|
|
20
|
-
RequireRatchet.notNullOrUndefined(s3);
|
|
21
|
-
RequireRatchet.notNullOrUndefined(outputLocation);
|
|
22
|
-
RequireRatchet.true(outputLocation.startsWith('s3://'));
|
|
23
|
-
}
|
|
24
|
-
static athenaRowsToObject(input) {
|
|
25
|
-
const colNames = input[0].Data.map((d) => d.VarCharValue);
|
|
26
|
-
const temp = input.slice(1);
|
|
27
|
-
const rval = temp.map((t) => {
|
|
28
|
-
const newItem = {};
|
|
29
|
-
for (let i = 0; i < t.Data.length; i++) {
|
|
30
|
-
newItem[colNames[i]] = t.Data[i].VarCharValue;
|
|
31
|
-
}
|
|
32
|
-
return newItem;
|
|
33
|
-
});
|
|
34
|
-
return rval;
|
|
35
|
-
}
|
|
36
|
-
static applyParamsToQuery(query, queryParams) {
|
|
37
|
-
let rval = query;
|
|
38
|
-
if (!!rval && !!queryParams) {
|
|
39
|
-
Object.keys(queryParams).forEach((k) => {
|
|
40
|
-
const val = StringRatchet.safeString(queryParams[k]);
|
|
41
|
-
const kk = '{' + k + '}';
|
|
42
|
-
rval = rval.split(kk).join(val);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return rval;
|
|
46
|
-
}
|
|
47
|
-
async fetchQueryIds() {
|
|
48
|
-
const params = {
|
|
49
|
-
NextToken: null,
|
|
50
|
-
};
|
|
51
|
-
let rval = [];
|
|
52
|
-
let next = null;
|
|
53
|
-
do {
|
|
54
|
-
next = await this.athena.send(new ListNamedQueriesCommand(params));
|
|
55
|
-
rval = rval.concat(next.NamedQueryIds);
|
|
56
|
-
params.NextToken = next.NextToken;
|
|
57
|
-
} while (!!params.NextToken);
|
|
58
|
-
return rval;
|
|
59
|
-
}
|
|
60
|
-
async listQueries() {
|
|
61
|
-
const rval = [];
|
|
62
|
-
const ids = await this.fetchQueryIds();
|
|
63
|
-
Logger.debug('Finding %d items', ids.length);
|
|
64
|
-
for (let i = 0; i < ids.length; i++) {
|
|
65
|
-
const params = {
|
|
66
|
-
NamedQueryId: ids[i],
|
|
67
|
-
};
|
|
68
|
-
const val = await this.athena.send(new GetNamedQueryCommand(params));
|
|
69
|
-
rval.push(val.NamedQuery);
|
|
70
|
-
}
|
|
71
|
-
return rval;
|
|
72
|
-
}
|
|
73
|
-
async findQueryByName(name) {
|
|
74
|
-
const all = await this.listQueries();
|
|
75
|
-
const rval = all.find((a) => a.Name.toLowerCase() == name.toLowerCase());
|
|
76
|
-
return rval;
|
|
77
|
-
}
|
|
78
|
-
async runQueryToObjects(queryIn, queryParams = {}, pingTimeMS = 2000) {
|
|
79
|
-
Logger.info('Running query to objects');
|
|
80
|
-
const outputLoc = await this.runQueryToOutputLocation(queryIn, queryParams, pingTimeMS);
|
|
81
|
-
Logger.info('Query succeeded, processing file from %s', outputLoc);
|
|
82
|
-
const bucketName = outputLoc.substring(5, outputLoc.indexOf('/', 5));
|
|
83
|
-
const obKey = outputLoc.substring(outputLoc.indexOf('/', 5) + 1);
|
|
84
|
-
const req = {
|
|
85
|
-
Bucket: bucketName,
|
|
86
|
-
Key: obKey,
|
|
87
|
-
};
|
|
88
|
-
const getFileOut = await this.s3.send(new GetObjectCommand(req));
|
|
89
|
-
const rval = await CsvRatchet.stringParse(getFileOut.Body.toString(), (p) => {
|
|
90
|
-
return p;
|
|
91
|
-
}, { columns: true, skip_empty_lines: true });
|
|
92
|
-
return rval;
|
|
93
|
-
}
|
|
94
|
-
async runQueryToFile(queryIn, queryParams = {}, targetDataFileIn = null, pingTimeMS = 2000) {
|
|
95
|
-
Logger.info('Running query to file');
|
|
96
|
-
const outputLoc = await this.runQueryToOutputLocation(queryIn, queryParams, pingTimeMS);
|
|
97
|
-
Logger.info('Query succeeded, pulling file from %s', outputLoc);
|
|
98
|
-
const bucketName = outputLoc.substring(5, outputLoc.indexOf('/', 5));
|
|
99
|
-
const obKey = outputLoc.substring(outputLoc.indexOf('/', 5) + 1);
|
|
100
|
-
const req = {
|
|
101
|
-
Bucket: bucketName,
|
|
102
|
-
Key: obKey,
|
|
103
|
-
};
|
|
104
|
-
const targetDataFile = targetDataFileIn || tmp.fileSync({ postfix: '.csv', keep: false }).name;
|
|
105
|
-
const fileStream = fs.createWriteStream(targetDataFile);
|
|
106
|
-
const output = await this.s3.send(new GetObjectCommand(req));
|
|
107
|
-
const readStream = output.Body;
|
|
108
|
-
readStream.pipe(fileStream);
|
|
109
|
-
const rval = await PromiseRatchet.resolveOnEvent(readStream, ['finish', 'close'], ['error'], targetDataFile);
|
|
110
|
-
Logger.silly('Response: %s', rval);
|
|
111
|
-
return targetDataFile;
|
|
112
|
-
}
|
|
113
|
-
async runQueryToOutputLocation(queryIn, queryParams = {}, pingTimeMS = 2000) {
|
|
114
|
-
let rval = null;
|
|
115
|
-
const timer = new StopWatch();
|
|
116
|
-
const query = AthenaRatchet.applyParamsToQuery(queryIn, queryParams);
|
|
117
|
-
try {
|
|
118
|
-
Logger.info('Starting query : %s', query);
|
|
119
|
-
const token = StringRatchet.createType4Guid();
|
|
120
|
-
const params = {
|
|
121
|
-
QueryString: query,
|
|
122
|
-
ResultConfiguration: {
|
|
123
|
-
OutputLocation: this.outputLocation,
|
|
124
|
-
EncryptionConfiguration: {
|
|
125
|
-
EncryptionOption: 'SSE_S3',
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
ClientRequestToken: token,
|
|
129
|
-
QueryExecutionContext: {
|
|
130
|
-
Database: 'default',
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
const startToken = await this.athena.send(new StartQueryExecutionCommand(params));
|
|
134
|
-
const getExecParams = {
|
|
135
|
-
QueryExecutionId: startToken.QueryExecutionId,
|
|
136
|
-
};
|
|
137
|
-
const finalStates = ['FAILED', 'CANCELLED', 'SUCCEEDED'];
|
|
138
|
-
let curState = await this.athena.send(new GetQueryExecutionCommand(getExecParams));
|
|
139
|
-
while (finalStates.indexOf(curState.QueryExecution.Status.State) === -1) {
|
|
140
|
-
await PromiseRatchet.createTimeoutPromise('wait', pingTimeMS);
|
|
141
|
-
Logger.debug('%s : %s : %s', curState.QueryExecution.Status.State, timer.dump(), query);
|
|
142
|
-
curState = await this.athena.send(new GetQueryExecutionCommand(getExecParams));
|
|
143
|
-
}
|
|
144
|
-
if (curState.QueryExecution.Status.State === 'FAILED') {
|
|
145
|
-
Logger.warn('Query failed : %s', curState.QueryExecution.Status.StateChangeReason);
|
|
146
|
-
}
|
|
147
|
-
else if (curState.QueryExecution.Status.State === 'SUCCEEDED') {
|
|
148
|
-
rval = curState.QueryExecution.ResultConfiguration.OutputLocation;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
catch (err) {
|
|
152
|
-
Logger.warn('Failure : %s', err, err);
|
|
153
|
-
}
|
|
154
|
-
Logger.info('Query took %s : %s', timer.dump(), query);
|
|
155
|
-
return rval;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { AthenaRatchet } from './athena-ratchet.js';
|
|
2
|
-
import { Logger } from '@bitblit/ratchet-common/lib/logger/logger.js';
|
|
3
|
-
import { AthenaClient, GetQueryExecutionCommand, StartQueryExecutionCommand, } from '@aws-sdk/client-athena';
|
|
4
|
-
import { S3Client } from '@aws-sdk/client-s3';
|
|
5
|
-
import { mockClient } from 'aws-sdk-client-mock';
|
|
6
|
-
let mockAthena;
|
|
7
|
-
let mockS3;
|
|
8
|
-
describe('#AthenaRatchet', function () {
|
|
9
|
-
mockAthena = mockClient(AthenaClient);
|
|
10
|
-
mockS3 = mockClient(S3Client);
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
mockAthena.reset();
|
|
13
|
-
mockS3.reset();
|
|
14
|
-
});
|
|
15
|
-
xit('should test a query', async () => {
|
|
16
|
-
const outputDir = 's3://your-bucket/your-prefix';
|
|
17
|
-
const qry = 'select * from test limit 20000';
|
|
18
|
-
mockAthena.on(StartQueryExecutionCommand).resolves({ jobName: 'b' });
|
|
19
|
-
mockAthena.on(GetQueryExecutionCommand).resolves({
|
|
20
|
-
QueryExecution: { ResultConfiguration: { OutputLocation: 'test' }, Status: { State: 'SUCCEEDED' } },
|
|
21
|
-
});
|
|
22
|
-
const ratchet = new AthenaRatchet(mockAthena, mockS3, outputDir);
|
|
23
|
-
const result = await ratchet.runQueryToObjects(qry);
|
|
24
|
-
expect(result).toBeTruthy();
|
|
25
|
-
Logger.info('Got objects : %j', result);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export class RatchetAwsNodeOnlyInfo {
|
|
2
|
-
constructor() { }
|
|
3
|
-
static buildInformation() {
|
|
4
|
-
const val = {
|
|
5
|
-
version: 'LOCAL-SNAPSHOT',
|
|
6
|
-
hash: 'LOCAL-HASH',
|
|
7
|
-
branch: 'LOCAL-BRANCH',
|
|
8
|
-
tag: 'LOCAL-TAG',
|
|
9
|
-
timeBuiltISO: 'LOCAL-TIME-ISO',
|
|
10
|
-
notes: 'LOCAL-NOTES',
|
|
11
|
-
};
|
|
12
|
-
return val;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { StringRatchet } from '@bitblit/ratchet-common/lib/lang/string-ratchet.js';
|
|
2
|
-
import { Logger } from '@bitblit/ratchet-common/lib/logger/logger.js';
|
|
3
|
-
import { PromiseRatchet } from '@bitblit/ratchet-common/lib/lang/promise-ratchet.js';
|
|
4
|
-
import { RequireRatchet } from '@bitblit/ratchet-common/lib/lang/require-ratchet.js';
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
import readline from 'readline';
|
|
7
|
-
export class DynamoExporter {
|
|
8
|
-
constructor() { }
|
|
9
|
-
static async importJsonLFileToTable(dynamo, tableName, filename) {
|
|
10
|
-
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
11
|
-
RequireRatchet.notNullOrUndefined(tableName, 'tableName');
|
|
12
|
-
RequireRatchet.notNullOrUndefined(filename, 'filename');
|
|
13
|
-
const fileStream = fs.createReadStream(filename);
|
|
14
|
-
const rl = readline.createInterface({
|
|
15
|
-
input: fileStream,
|
|
16
|
-
crlfDelay: Infinity,
|
|
17
|
-
});
|
|
18
|
-
let rval = 0;
|
|
19
|
-
for await (const line of rl) {
|
|
20
|
-
if (rval % 100 === 0) {
|
|
21
|
-
Logger.info('Importing line %d', rval);
|
|
22
|
-
}
|
|
23
|
-
if (StringRatchet.trimToNull(line)) {
|
|
24
|
-
const parsed = JSON.parse(line);
|
|
25
|
-
await dynamo.simplePut(tableName, parsed);
|
|
26
|
-
rval++;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return rval;
|
|
30
|
-
}
|
|
31
|
-
static async exportScanToJsonLFile(dynamo, scan, filename) {
|
|
32
|
-
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
33
|
-
RequireRatchet.notNullOrUndefined(scan, 'scan');
|
|
34
|
-
RequireRatchet.notNullOrUndefined(filename, 'filename');
|
|
35
|
-
const ws = fs.createWriteStream(filename);
|
|
36
|
-
ws.on('end', () => {
|
|
37
|
-
Logger.debug('Write complete');
|
|
38
|
-
});
|
|
39
|
-
const rval = await DynamoExporter.exportScanToJsonLWriteStream(dynamo, scan, ws);
|
|
40
|
-
await PromiseRatchet.resolveOnEvent(ws, ['finish', 'close'], ['error']);
|
|
41
|
-
ws.close();
|
|
42
|
-
return rval;
|
|
43
|
-
}
|
|
44
|
-
static async exportQueryToJsonLFile(dynamo, qry, filename) {
|
|
45
|
-
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
46
|
-
RequireRatchet.notNullOrUndefined(qry, 'qry');
|
|
47
|
-
RequireRatchet.notNullOrUndefined(filename, 'filename');
|
|
48
|
-
const ws = fs.createWriteStream(filename);
|
|
49
|
-
ws.on('end', () => {
|
|
50
|
-
Logger.debug('Write complete');
|
|
51
|
-
});
|
|
52
|
-
const rval = await DynamoExporter.exportQueryToJsonLWriteStream(dynamo, qry, ws);
|
|
53
|
-
await PromiseRatchet.resolveOnEvent(ws, ['finish', 'close'], ['error']);
|
|
54
|
-
ws.close();
|
|
55
|
-
return rval;
|
|
56
|
-
}
|
|
57
|
-
static async exportScanToJsonLWriteStream(dynamo, scan, target) {
|
|
58
|
-
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
59
|
-
RequireRatchet.notNullOrUndefined(scan, 'scan');
|
|
60
|
-
RequireRatchet.notNullOrUndefined(target, 'target');
|
|
61
|
-
const rval = await dynamo.fullyExecuteProcessOverScan(scan, async (row) => DynamoExporter.writeItemToJsonLStream(row, target, false));
|
|
62
|
-
return rval;
|
|
63
|
-
}
|
|
64
|
-
static async exportQueryToJsonLWriteStream(dynamo, qry, target) {
|
|
65
|
-
RequireRatchet.notNullOrUndefined(dynamo, 'dynamo');
|
|
66
|
-
RequireRatchet.notNullOrUndefined(qry, 'qry');
|
|
67
|
-
RequireRatchet.notNullOrUndefined(target, 'target');
|
|
68
|
-
const rval = await dynamo.fullyExecuteProcessOverQuery(qry, async (row) => DynamoExporter.writeItemToJsonLStream(row, target, false));
|
|
69
|
-
return rval;
|
|
70
|
-
}
|
|
71
|
-
static writeItemToJsonLStream(item, target, includeNulls = false) {
|
|
72
|
-
if (!!item || includeNulls) {
|
|
73
|
-
target.write(JSON.stringify(item) + '\n');
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AbstractRatchetCliHandler } from '@bitblit/ratchet-node-only/lib/cli/abstract-ratchet-cli-handler.js';
|
|
2
|
-
import { SiteUploader } from './site-uploader/site-uploader.js';
|
|
3
|
-
import { StartInstanceAndSsh } from './start-instance-and-ssh.js';
|
|
4
|
-
import { RatchetAwsNodeOnlyInfo } from '../build/ratchet-aws-node-only-info.js';
|
|
5
|
-
export class RatchetCliHandler extends AbstractRatchetCliHandler {
|
|
6
|
-
fetchHandlerMap() {
|
|
7
|
-
return {
|
|
8
|
-
'site-uploader': SiteUploader.runFromCliArgs,
|
|
9
|
-
'start-instance-and-ssh': StartInstanceAndSsh.runFromCliArgs,
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
fetchVersionInfo() {
|
|
13
|
-
return RatchetAwsNodeOnlyInfo.buildInformation();
|
|
14
|
-
}
|
|
15
|
-
}
|