forked from royhobbstn/s3-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-control.js
140 lines (105 loc) · 3.66 KB
/
upload-control.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const AWS = require('aws-sdk');
const S3 = new AWS.S3;
const { dataset } = require('./modules/settings.js');
const Promise = require('bluebird');
const states = require('./modules/states');
const aws_credentials = require('./aws_key.json');
loadPrototype();
const YEAR = '2016';
/****/
AWS.config.update({
region: 'us-west-2',
maxRetries: 0,
retryDelayOptions: {
base: 10000
}
});
/****/
const settings = dataset[YEAR];
const seq_count = parseInt(settings.seq_files, 10);
// all possible combinations
const lambda_invocations = [];
for (let i = 1; i <= seq_count; i++) {
['allgeo', 'trbg'].forEach(geo => {
Object.keys(states).forEach(state => {
const seq = String(i).padStart(3, '0');
lambda_invocations.push({ seq, geo, state, name: `e${YEAR}5${state}0${seq}000_${geo}`, type: 'e' });
lambda_invocations.push({ seq, geo, state, name: `m${YEAR}5${state}0${seq}000_${geo}`, type: 'm' });
});
});
}
// check bucket for existing
const s3_bucket = `s3db-acs-raw-${dataset[YEAR].text}`;
console.log(`Reading keys from bucket: ${s3_bucket}`);
const listAll = require('s3-list-all')({ accessKeyId: aws_credentials.accessKeyId, secretAccessKey: aws_credentials.secretAccessKey });
listAll({ Bucket: s3_bucket, Prefix: '' }, function(err, results) {
if (err) {
console.log(err);
process.exit();
}
console.log(`Found ${results.length} keys.`);
const keys = results.map(d => d.Key.replace('.csv', ''));
// filter out existing from possible
const remaining = lambda_invocations.filter(opt => {
return !(keys.includes(opt.name));
});
console.log(`Missing ${remaining.length} keys.`);
// reduce down to file level by removing the MOE records
const files_to_retrieve = remaining.filter(item => item.type === 'e');
console.log(`Retrieving data from ${files_to_retrieve.length} files.`);
const invoked = Promise.map(files_to_retrieve, (file) => {
return new Promise((resolve, reject) => {
let lambda = new AWS.Lambda({ apiVersion: '2015-03-31' });
const params = {
FunctionName: "s3-db-dev-dataupload",
InvocationType: "Event",
LogType: "None",
Payload: JSON.stringify({
'year': YEAR,
'seq': file.seq,
'geo': file.geo,
'state': file.state
})
};
lambda.invoke(params, function(err, data) {
if (err) {
console.log(err, err.stack);
return reject(err);
}
else {
console.log(data);
return resolve(data);
}
});
});
}, { concurrency: 1 });
Promise.all(invoked).then(() => {
console.log('all lambdas invoked');
})
.catch(err => {
console.log(err);
console.log('something bad happened');
process.exit();
});
});
/*****************/
function loadPrototype() {
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}
}