forked from 18F/analytics-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.js
More file actions
173 lines (148 loc) · 5 KB
/
lambda.js
File metadata and controls
173 lines (148 loc) · 5 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const generateAnalyticsReporterData = async (event) =>
{
console.log('GenerateAnalyticsReport triggered');
/// parameters may be right in env, or may be part of sns message
var params = event;
if ( event && 'Records' in event
&& 0 in event.Records
&& 'Sns' in event.Records[0]
&& 'Message' in event.Records[0].Sns
&& event.Records[0].Sns.Message )
{
/// Message from Sns may be a json object
try {
console.log('Parameters From SNS:'+event.Records[0].Sns.Message);
params = JSON.parse( event.Records[0].Sns.Message );
} catch (err) {
console.log('SNS Message is not json, ignoring input');
}
}
var env_prefix = null;
if ( params && 'ENVIRONMENT' in params )
{
env_prefix = params.ENVIRONMENT;
} else if ( 'ENVIRONMENT' in process.env ) {
env_prefix = process.env.ENVIRONMENT;
} else {
env_prefix = 'prod';
}
await buildEnvFromParamterStore(env_prefix);
var reports = [
{"id":"ga:147714046","path":"analytics/raw-data"},
{"id":"ga:147749852","path":"analytics/raw-data/gobierno"},
{"id":"ga:147777730","path":"analytics/raw-data/usagov"}
];
if ( params && 'ANALYTICS_REPORTS' in params ) {
reports = params.ANALYTICS_REPORTS;
} else if ( 'ANALYTICS_REPORTS' in process.env ) {
reports = JSON.parse(process.env.ANALYTICS_REPORTS);
}
process.env.ANALYTICS_REPORTS_PATH = './reports/usagov-analytics.json';
var frequency = null;
if ( params && "FREQUENCY" in params )
{
frequency = params.FREQUENCY;
}
await runReports(reports,frequency);
// var csv = false;
// if ( params && "CSV" in params )
// {
// csv = params.CSV;
// }
// await runReports(reports,frequency,csv);
}
const runReports = async ( reports, frequency ) =>
{
var successes = 0;
for ( var r=0; r<reports.length; r++ )
{
console.log('Report '+reports[r].id+' generating');
await runReport( reports[r], frequency );
}
return successes;
}
const runReport = async (report, frequency) =>
{
var analyticsCommand = './bin/analytics --publish';
if ( frequency )
{
analyticsCommand += ' --frequency '+frequency;
}
// if ( csv )
// {
// analyticsCommand += ' --csv';
// }
process.env.AWS_CACHE_TIME = 0;
process.env.ANALYTICS_REPORT_IDS = report.id;
process.env.AWS_BUCKET_PATH = report.path;
var analyticsCommandJSON = analyticsCommand;
console.log(analyticsCommandJSON);
await exec(analyticsCommandJSON, { stdio: 'inherit' });
console.log('JSON Report '+report.id+' generation finished');
analyticsCommandCSV = analyticsCommand + ' --csv';
console.log(analyticsCommandCSV);
await exec(analyticsCommandCSV, { stdio: 'inherit' });
console.log('CSV Report '+report.id+' generation finished');
// const { stdoutJSON, stderrJSON } = await exec(analyticsCommandJSON);
// if ( stderrJSON )
// {
// console.log('JSON Report '+report.id+' errored: '+stderrJSON);
// }
// console.log('JSON Report '+report.id+' generated '+stdoutJSON);
// analyticsCommandCSV = analyticsCommand + ' --csv';
// console.log(analyticsCommandCSV);
// const { stdoutCSV, stderrCSV } = await exec(analyticsCommandCSV);
// if ( stderrJson )
// {
// console.log('CSV Report '+report.id+' errored: '+stderrCSV);
// }
// console.log('CSV Report '+report.id+' generated '+stdoutCSV);
return true;
}
const buildEnvFromParamterStore = async ( env_prefix ) =>
{
/// set region
var AWS = require('aws-sdk');
if ( 'AWS_REGION' in process.env )
{
AWS.config.update({region:process.env.AWS_REGION});
} else if ( 'AWS_DEFAULT_REGION' in process.env ) {
AWS.config.update({region:process.env.AWS_DEFAULT_REGION});
} else {
AWS.config.update({region:'us-east-1'});
}
var param_prefix = "/project_app_usa/";
/// wrap in //
if ( ! param_prefix.match(/\/$/) )
{
param_prefix += '/';
}
if ( ! param_prefix.match(/^\//) )
{
param_prefix = '/'+param_prefix;
}
/// append environment name
param_prefix += env_prefix;
if ( ! param_prefix.match(/\/$/) )
{
param_prefix += '/';
}
param_prefix += 'analytics_reporters/';
var ssm = new AWS.SSM();
return ssm.getParametersByPath({
Path: param_prefix,
Recursive: true,
WithDecryption: true,
}).promise().then( data => {
for ( var i=0; i<data.Parameters.length; i++ )
{
var envVar = data.Parameters[i].Name.replace(param_prefix,"");
process.env[envVar] = data.Parameters[i].Value;
}
}).catch( err => {
console.error(err, err.stack);
});
}
exports.generateAnalyticsReporterData = generateAnalyticsReporterData