-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processData.js
308 lines (272 loc) · 12 KB
/
processData.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const { readDataFile, getDateFileString, mungeData, writeDataFile, getLineSvg, getPassingCwvSvg, getStackedBarSvg, getTrend, getSparkColumnSvg, getAggregations } = require('./helpers');
const RAW_FOLDER = '_raw_data/';
const CACHE_DIR = '_processed_data'
const MIN_ORIGINS = 50
const CLIENTS = ['mobile', 'desktop']
const CREATE_CHARTS = true // set to false when testing other parts
const today = new Date()
const lastMonth = new Date(today.setMonth(today.getMonth() - 1))
// File names are YYYY_MM (e.g., 2024_07)
const currentMonths = Array(6).fill(0).map((_, i) => {
const newDate = lastMonth.setMonth(today.getMonth() - i)
return getDateFileString(new Date(newDate))
}).sort();
const currentMonthsReadable = currentMonths.map(dateStr => {
const parts = dateStr.split('_')
return `${parts[1]}/${parts[0].slice(2)}`
})
const confirmedThemes = readDataFile('currentThemes.json')
// Initialize counters
let themesObj = {}
let ignoredThemes = 0
let unconfirmedThemes = {}
let allOtherOriginsCounts = { mobile: {}, desktop: {} }
CLIENTS.forEach(client => {
currentMonths.forEach(monthStr => { allOtherOriginsCounts[client][monthStr] = 0 })
})
let totalOriginsCounts = { mobile: {}, desktop: {} }
CLIENTS.forEach(client => {
currentMonths.forEach(monthStr => { totalOriginsCounts[client][monthStr] = 0 })
})
console.log("**************************************************");
console.log("*** Beginning data parsing and chart building...");
// For each monthly data file, dump each theme's monthly data into the theme object
currentMonths.forEach(date => {
const file = `${RAW_FOLDER}${date}.json`
const fileData = readDataFile(file)
// Early exit if no file data (file doesn't exist)
if (!fileData[0]) {
console.log(`*** Skipping ${date} file with no data!`);
return
}
fileData.forEach(theme => {
const {client, top_theme_name, origins, id, ...rawPercents} = theme
const monthlyData = mungeData(origins, rawPercents, date, client)
totalOriginsCounts[client][monthlyData.date] = totalOriginsCounts[client][monthlyData.date] + monthlyData.origins
// Set id to null string if null
const theme_store_id = id || 'null'
if (!themesObj[theme_store_id]) {
// check if confirmed theme, if so add it
if (confirmedThemes[theme_store_id]) {
const confirmedName = confirmedThemes[theme_store_id].name
if (confirmedName !== top_theme_name){
console.warn(`THEME NAME MISMATCH FOR ${theme_store_id}: Top theme name is ${top_theme_name} but confirmed theme name is ${confirmedName}. Using confirmed name.`);
}
themesObj[theme_store_id] = {
id: theme_store_id,
name: confirmedName,
sunset: confirmedThemes[theme_store_id].sunset,
slug: confirmedThemes[theme_store_id].slug,
monthlyData: [ monthlyData ]
}
} else {
// If not confirmed, add to other origins count
allOtherOriginsCounts[client][monthlyData.date] = allOtherOriginsCounts[client][monthlyData.date] + monthlyData.origins
// Add to unconfirmed themes list if not already there and have enough origins to matter
if (!unconfirmedThemes[theme_store_id] && monthlyData.origins < MIN_ORIGINS) {
unconfirmedThemes[theme_store_id] = top_theme_name
}
}
} else {
// Confirmed themes already in object, pass new month's data to it
themesObj[id].monthlyData.push(monthlyData)
}
})
})
// Filter out low origins count for last month
const themes = Object.keys(themesObj).map(themeId => themesObj[themeId]).filter(theme => {
const ignore = theme.monthlyData[theme.monthlyData.length - 1].origins < MIN_ORIGINS
if (ignore) {
ignoredThemes++
theme.monthlyData.forEach(data => {
allOtherOriginsCounts[data.client][data.date] = allOtherOriginsCounts[data.client][data.date] + data.origins
});
}
return !ignore
})
// Housekeeping about bad theme names or theme_store_ids and general themes data
if (Object.keys(unconfirmedThemes).length > 1) {
console.error(`*** UNCONFIRMED THEMES (with greater than ${MIN_ORIGINS}):`);
console.log("*** ", {unconfirmedThemes});
}
if (ignoredThemes > 0) {
console.log(`*** Ignored ${ignoredThemes} themes with less than ${MIN_ORIGINS} origins`);
}
console.log(`*** All other origins count is:`);
console.log({allOtherOriginsCounts});
console.log(`*** All origins count is:`);
console.log({totalOriginsCounts});
// Create the themes data object and SVG charts for use in page building
console.log(`*** Creating charts for months: ${currentMonthsReadable.join(', ')}.`);
const themesWithCharts = themes.map(theme => {
const {id, name, monthlyData, sunset, slug} = theme
const metrics = ['origins', 'passingCWV', 'passingLCP', 'needsImproveLCP', 'poorLCP', 'passingCLS', 'needsImproveCLS', 'poorCLS', 'passingINP', 'needsImproveINP', 'poorINP', 'passingTTFB', 'needsImproveTTFB', 'poorTTFB', 'passingFCP', 'needsImproveFCP', 'poorFCP']
// Initialize data object with null arrays. Array indexes are in date order.
const data = { mobile: {}, desktop: {} }
CLIENTS.forEach(client => {
metrics.forEach(metric => { data[client][metric] = Array(6).fill() })
})
monthlyData.forEach(dataset => {
const {client, date, origins, pct_good_cwv, pct_good_lcp, pct_ni_lcp, pct_poor_lcp, pct_good_cls, pct_ni_cls, pct_poor_cls, pct_good_inp, pct_ni_inp, pct_poor_inp, pct_good_ttfb, pct_ni_ttfb, pct_poor_ttfb, pct_good_fcp, pct_ni_fcp, pct_poor_fcp} = dataset
const index = currentMonths.indexOf(date)
if (index >= 0) {
data[client].origins[index] = origins
if (origins >= MIN_ORIGINS) {
data[client].passingCWV[index] = pct_good_cwv
data[client].passingLCP[index] = pct_good_lcp
data[client].needsImproveLCP[index] = pct_ni_lcp
data[client].poorLCP[index] = pct_poor_lcp
data[client].passingCLS[index] = pct_good_cls
data[client].needsImproveCLS[index] = pct_ni_cls
data[client].poorCLS[index] = pct_poor_cls
data[client].passingINP[index] = pct_good_inp
data[client].needsImproveINP[index] = pct_ni_inp
data[client].poorINP[index] = pct_poor_inp
data[client].passingTTFB[index] = pct_good_ttfb
data[client].needsImproveTTFB[index] = pct_ni_ttfb
data[client].poorTTFB[index] = pct_poor_ttfb
data[client].passingFCP[index] = pct_good_fcp
data[client].needsImproveFCP[index] = pct_ni_fcp
data[client].poorFCP[index] = pct_poor_fcp
}
}
})
const charts = {}
if (CREATE_CHARTS) {
Object.keys(data).forEach(client => {
const {origins, passingCWV, passingLCP, needsImproveLCP, poorLCP, passingCLS, needsImproveCLS, poorCLS, passingINP, needsImproveINP, poorINP, passingTTFB, needsImproveTTFB, poorTTFB, passingFCP, needsImproveFCP, poorFCP} = data[client]
charts[client] = {
originsSvg: getLineSvg(origins, currentMonthsReadable),
originsAria: `Origins by month line chart. The data is: ${origins.join(', ')} origins for the months ${currentMonthsReadable.join(', ')}.`,
passingCwvSvg: getPassingCwvSvg(passingCWV, currentMonthsReadable),
passingCwvAria: `Origins Passing All Core Web Vitals bar chart. The data is: ${passingCWV.join(', ')}% passing for the months ${currentMonthsReadable.join(', ')}.`,
lcp: getStackedBarSvg(passingLCP, needsImproveLCP, poorLCP, currentMonthsReadable),
lcpAria: `LCP bar chart. The data is: ${passingLCP.join(', ')}% of origins passing for the months ${currentMonthsReadable.join(', ')}.`,
cls: getStackedBarSvg(passingCLS, needsImproveCLS, poorCLS, currentMonthsReadable),
clsAria: `CLS bar chart. The data is: ${passingCLS.join(', ')}% of origins passing for the months ${currentMonthsReadable.join(', ')}.`,
inp: getStackedBarSvg(passingINP, needsImproveINP, poorINP, currentMonthsReadable),
inpAria: `INP bar chart. The data is: ${passingINP.join(', ')}% of origins passing for the months ${currentMonthsReadable.join(', ')}.`,
ttfb: getStackedBarSvg(passingTTFB, needsImproveTTFB, poorTTFB, currentMonthsReadable),
ttfbAria: `TTFB bar chart. The data is: ${passingTTFB.join(', ')}% of origins passing for the months ${currentMonthsReadable.join(', ')}.`,
fcp: getStackedBarSvg(passingFCP, needsImproveFCP, poorFCP, currentMonthsReadable),
fcpAria: `FCP bar chart. The data is: ${passingFCP.join(', ')}% of origins passing for the months ${currentMonthsReadable.join(', ')}.`,
}
})
}
return {
id,
name,
slug,
sunset,
data,
charts,
}
})
console.log(`*** ${themesWithCharts.length} themes generated.`);
console.log("**************************************************");
const latestTotalOrigins = {
mobile: totalOriginsCounts.mobile[Object.keys(totalOriginsCounts.mobile).sort().reverse()[0]],
desktop: totalOriginsCounts.desktop[Object.keys(totalOriginsCounts.desktop).sort().reverse()[0]]
}
const themesWithChartsAndAggr = themesWithCharts.map((theme, i) => {
const {data, ...rest} = theme
const aggregData = { mobile: {}, desktop: {} }
Object.keys(aggregData).forEach(client => {
const {origins, passingCWV, passingLCP, passingCLS, passingINP} = data[client]
const lastOrigin = origins[origins.length - 1]
const marketSharePct = Math.round(lastOrigin / latestTotalOrigins[client] * 10000) / 100
const passingCWVnum = passingCWV[passingCWV.length - 1] || 0
aggregData[client] = {
origins: lastOrigin,
marketSharePct: marketSharePct.toFixed(2),
passingCWVnum,
passingCWV: passingCWVnum.toFixed(1),
passingCWVchart: getSparkColumnSvg(passingCWVnum, lastMonth),
passingLCP: (passingLCP[passingLCP.length - 1] || 0).toFixed(1),
passingCLS: (passingCLS[passingCLS.length - 1] || 0).toFixed(1),
passingINP: (passingINP[passingINP.length - 1] || 0).toFixed(1),
cwvTrend: getTrend(passingCWV),
lcpTrend: getTrend(passingLCP),
clsTrend: getTrend(passingCLS),
inpTrend: getTrend(passingINP),
}
})
return {
summary: aggregData,
...rest,
}
})
// Create rank fields.....
// Warning: sort mutates
CLIENTS.forEach(client => {
themesWithChartsAndAggr.sort((a, b) => {
// Sorting by largest first (reverse)
if (a.summary[client].origins > b.summary[client].origins) {
return -1;
}
if (a.summary[client].origins < b.summary[client].origins) {
return 1;
}
// a must be equal to b
return 0;
}).map((theme, index) => {
theme.summary[client].marketRank = index + 1
return theme
})
})
CLIENTS.forEach(client => {
themesWithChartsAndAggr.sort((a, b) => {
const aNum = parseFloat(a.summary[client].passingCWV)
const bNum = parseFloat(b.summary[client].passingCWV)
// Sorting by largest first (reverse)
if (aNum > bNum) {
return -1;
}
if (aNum < bNum) {
return 1;
}
// a must be equal to b
return 0;
}).map((theme, index) => {
theme.summary[client].cwvRank = index + 1
return theme
})
})
// Aggregations
const METRICS = [
'passingCWV',
'passingLCP',
'passingCLS',
'passingINP',
]
const READABLE_METRIC = {
passingCWV: 'All Core Web Vitals (CWV)',
passingLCP: 'Largest Contentful Paint (LCP)',
passingCLS: 'Cumulative Layout Shift (CLS)',
passingINP: 'Interaction to Next Paint (INP)',
}
const aggregations = {
mobile: [],
desktop: [],
}
CLIENTS.forEach(client => {
METRICS.forEach(metric => {
const metricValues = themesWithChartsAndAggr.map(theme => parseFloat(theme.summary[client][metric]))
aggregations[client].push({
name: READABLE_METRIC[metric],
...getAggregations(metricValues),
})
})
})
console.log("*** AGGREGATIONS (mobile) ***");
console.log(aggregations.mobile);
console.log("*** AGGREGATIONS (desktop) ***");
console.log(aggregations.desktop);
const output = {
themes: themesWithChartsAndAggr,
date: currentMonthsReadable[currentMonthsReadable.length - 1],
minOrigins: MIN_ORIGINS,
aggregations,
}
const outputFileName = getDateFileString(new Date())
writeDataFile(output, CACHE_DIR, `${outputFileName}.json`, 'Theme data')