-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputFunctions.js
222 lines (180 loc) · 6.18 KB
/
outputFunctions.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* File of outputFunctions to be used in index.batchProcess()
*
* Each is named to be used with matching requestBody in requestBodies.js
* Each takes following parameters:
* {report} which is the report generated by batchGet({requestBody})
* {section} which is the JSON object to store processed data from report
*/
// Takes pair of resulting metrics from batchGet
// (ex: current week vs past week)
// and formats it into CDS report and finds %change
function dataToText(currData, pastData) {
// Limits Trailing Zeroes
const parsedCurrData = parseFloat(parseInt(currData).toFixed(2));
const parsedPastData = parseFloat(parseInt(pastData).toFixed(2));
const percentChange = Math.fround(
((parsedCurrData - parsedPastData) / parsedPastData) * 100
).toFixed(2);
if (percentChange > 0)
return ` up ${percentChange}% (${parsedCurrData} vs ${parsedPastData}) ⬆`;
else
return ` down ${percentChange}% (${parsedCurrData} vs ${parsedPastData}) ⬇`;
}
//
function defaultRequestOutput(report, section) {
// De-structures batchGet report
let {
columnHeader: {
dimensions,
metricHeader: { metricHeaderEntries },
},
data,
} = report;
// Puts metric headers into an array
var headers = [];
for (var i = 0, header; (header = metricHeaderEntries[i]); i++) {
headers.push(header.name);
}
// Puts result rows into an array
var rows = [];
for (var i = 0, row; (row = data.rows[i]); i++) {
rows.push(row);
}
console.log('Dimensions: ' + dimensions);
console.log('Metrics: ' + headers);
console.log();
for (i = 0; i < rows.length; i++) {
// Values from current time frame (Ex: This week)
let currValues = rows[i].metrics[0].values;
// Values from last time frame (Ex: last week)
let pastValues = rows[i].metrics[1].values;
// Separates values arrays into discrete values
for (i = 0; i < headers.length; i++) {
let header = headers[i];
let currVal = currValues[i];
let pastVal = pastValues[i];
// Turns raw data into formatted string to be used in weekly report
let format = dataToText(currVal, pastVal);
// Creates new variable in section object, to be read by DOCS API
section[header] = format;
// Test output
//console.log(headers[i] + format);
}
}
// Test output
//console.log(section);
}
//
function defaultChannelGroupingOutput(report, section) {
// De-structures batchGet report
let {
columnHeader: {
dimensions,
metricHeader: { metricHeaderEntries },
},
data,
} = report;
// Puts metric headers into an array
var headers = [];
for (var i = 0, header; (header = metricHeaderEntries[i]); i++) {
headers.push('' + header.name);
}
// Puts result rows into an array
var rows = [];
for (var i = 0, row; i < data.rows.length; i++) {
row = data.rows[i];
rows.push(row);
}
console.log('Dimensions: ' + dimensions);
console.log('Metrics: ' + headers);
console.log();
// Puts string into section object
section['mostPageViewsCameFrom'] = rows[0].dimensions[0];
for (i = 0; i < rows.length; i++) {
// Dimension Label (Ex: Facebook)
let label = rows[i].dimensions[0];
let currVal = rows[i].metrics[0].values[0];
let prevVal = rows[i].metrics[1].values[0];
// pageviews formatted to fixed number of digits
let pageviews = dataToText(currVal, prevVal);
section[label] = pageviews;
}
}
function top10ArticlesOutput(report, section) {
// De-structures batchGet report
let {
columnHeader: {
dimensions,
metricHeader: { metricHeaderEntries },
},
data,
} = report;
// Puts metric headers into an array
var headers = [];
for (var i = 0, header; (header = metricHeaderEntries[i]); i++) {
headers.push('' + header.name);
}
// Puts result rows into an array
var rows = [];
for (var i = 0, row; (row = data.rows[i]); i++) {
rows.push(row);
}
console.log('Dimensions: ' + dimensions);
console.log('Metrics: ' + headers);
console.log();
// String that holds formatted list of top 10 articles
let top10ArticlesFormatted = '';
for (i = 0; i < rows.length; i++) {
// Dimension Label (Ex: Facebook)
let articleLink = rows[i].dimensions[0];
// pageviews formatted to fixed number of digits
let pageviews = Number(rows[i].metrics[0].values[0]);
top10ArticlesFormatted += `${articleLink}\n`;
}
// Puts string into section object
section['top10Articles'] = top10ArticlesFormatted;
}
function percentUsersFromNYCOutput(report, section) {
defaultRequestOutput(report, section);
}
function socialNetworkOutput(report, section) {
// De-structures batchGet report
let {
columnHeader: {
dimensions,
metricHeader: { metricHeaderEntries },
},
data,
} = report;
// Puts metric headers into an array
var headers = [];
for (var i = 0, header; (header = metricHeaderEntries[i]); i++) {
headers.push('' + header.name);
}
// Puts result rows into an array
var rows = [];
for (var i = 0, row; (row = data.rows[i]); i++) {
rows.push(row);
}
console.log('Dimensions: ' + dimensions);
console.log('Metrics: ' + headers);
console.log();
// Puts string into section object
section['mostUsersCameFrom'] = rows[0].dimensions[0];
for (i = 0; i < rows.length; i++) {
// Dimension Label (Ex: Facebook)
let label = rows[i].dimensions[0];
let currVal = rows[i].metrics[0].values[0];
let prevVal = rows[i].metrics[1].values[0];
// pageviews formatted to fixed number of digits
let users = dataToText(currVal, prevVal);
section[label] = users;
}
}
module.exports = {
defaultRequestOutput,
top10ArticlesOutput,
defaultChannelGroupingOutput,
percentUsersFromNYCOutput,
socialNetworkOutput,
};