-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
824 lines (697 loc) · 22.6 KB
/
main.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
const Apify = require("apify");
const request = require("request-promise");
const DomParser = require("dom-parser");
const xlsx = require("node-xlsx");
const fs = require("fs");
const util = require("util");
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const parser = new DomParser();
const api = {};
api.enan = {
encoding: null
};
api.mzcr = "https://onemocneni-aktualne.mzcr.cz/covid-19";
api.quar = "https://www.cssz.cz/documents/20143/648807/Statistika_hlasene_karanteny_Z209-pocet_pripadu_souhrnne_za_2020_podle_kraju_a_okresu.xlsx/9d3b34d3-5df2-7fd5-264e-be89d267dae2";
api.r0 = "https://api.apify.com/v2/key-value-stores/DO0Mg4d1cPbWhtPSD/records/LATEST?disableRedirect=true";
api.hygDistricts = "https://api.apify.com/v2/key-value-stores/EIolfU3CUlHlpQH5M/records/LATEST?disableRedirect=true";
/**
*
*/
class Helpers {
/**
* Changes time based on parameters, can add or remove days, months, years etc.
* @param {string} input date on input
* @param {integer} days days to add or remove, default 0
* @param {integer} months months to add or remove, default 0
* @param {integer} years years to add or remove, default 0
* @returns {date} returns date
*/
deltaDate(input, days, months, years) {
const date = new Date(input);
date.setDate(date.getDate() + days);
date.setMonth(date.getMonth() + months);
date.setFullYear(date.getFullYear() + years);
return date;
}
/**
* Pads the number in case if someone forgets 0 before, eg 3 becomes 03 etc.
* @param {string} inputNumber number to pad
* @returns {string} returns string with 0
*/
padTheNumber(inputNumber) {
const number = parseInt(inputNumber, 10);
return number > 9 ? `${number}` : `0${number}`;
}
/**
* Sort data by key, to make them in order (sometimes keys are not in order)
* @param {array} original Original data to be sorted by key
* @return {array} returns ordered array
*/
sortData(original) {
const ordered = {};
Object.keys(original).sort().forEach((key) => {
ordered[key] = original[key];
});
return ordered;
}
/**
* Push to array by date
* @param {*} array original array
* @param {*} date date that will be the key for pushed array
* @param {*} arrayPushed array that is going to be pushed into the key (date)
* @return {array} returns final array
*/
pushToArrayByDate(array, date, arrayPushed) {
const newArray = array;
if (newArray[date] === undefined) {
newArray[date] = [];
}
newArray[date].push(arrayPushed);
return newArray;
}
/**
* Parse whole quarantine data, has to be outside, cuz async, need to write
* and read because it's zipped
* @param {object} xlsCrawl object with all the xlsKaranteny data
* @returns {sheet} parsed xls sheet data
*/
async parseQuarantineXLS(xlsCrawl) {
// write file
try {
await writeFile("quarantine.xlsx", xlsCrawl);
} catch (err) {
throw err;
}
// and read file file
let sheet = "";
try {
sheet = await readFile(`${__dirname}/quarantine.xlsx`);
} catch (err) {
throw err;
}
sheet = xlsx.parse(sheet);
return sheet;
}
}
const hlp = new Helpers(); // run automatically
/**
* Get, parse data and prepare array.
* @class
* @classdesc Class is getting and parsing the data, to prepare array that we
* will work on later on.
*/
class GetData {
/**
* @param {oblect} htmlCrawlMZCR object with html dom data
* @param {object} xlsCrawl object with all the xlsKaranteny data
* @param {object} jsonApiR0 object with all the json api data
* @param {object} jsonAPIhygDistricts object with all the jsonAPIhygDistricts data
*/
constructor(htmlCrawlMZCR, xlsCrawl, jsonApiR0, jsonAPIhygDistricts) {
this.htmlMZCR = parser.parseFromString(htmlCrawlMZCR);
this.xlsKaranteny = xlsCrawl; // already parsed before, cuz await
this.jsonApiR0 = jsonApiR0;
this.jsonAPIhygDistricts = jsonAPIhygDistricts;
}
/**
* Function to modify dates in computer readable format with dashes
* @param {string} dateInCzechFormat gets date in czech format eg. 14.03.2020
* @returns {string} computer readable date eg. 2020-03-14
*/
readableDate(dateInCzechFormat) {
const date = dateInCzechFormat.split(".");
const day = hlp.padTheNumber(date[0]);
const month = hlp.padTheNumber(date[1]);
const year = hlp.padTheNumber(date[2]);
const parsableDate = `${year}-${month}-${day}`;
return parsableDate;
}
/**
* Get Attribute from sourceHTML.
* @param {string} query Query Selector, for the ID where attribute with data
* is located
* @returns {string} computer readable date eg. 2020-03-14
*/
DataFromDataAttribute(query) {
let data = {};
const dataTable = this.htmlMZCR.getElementById(query);
if (dataTable) {
const dataTableAttributes = dataTable.getAttribute("data-table");
let dataTableString = dataTableAttributes.toString();
dataTableString = dataTableString.replace(/"/g, "\"");
const dataTableJson = JSON.parse(dataTableString);
data = dataTableJson.body;
}
return data;
}
/**
* Gets Data with total Dead people
* @param {object} sourceHtml source DOM where to look at
* @return {array} returns array with all the data
*/
TotalDied() {
const data = this.DataFromDataAttribute("js-total-died-table-data");
let final = [];
for (let i = 0; i < data.length; i++) {
const day = data[i];
const date = this.readableDate(day[0]);
const prepareData = [day[1], day[2]];
final[date] = prepareData;
}
final = hlp.sortData(final);
return final;
}
/**
* Gets Data with total Positive people
* @return {array} returns array with all the data
*/
TotalPositive() {
const data = this.DataFromDataAttribute("js-cummulative-total-positive-table-data");
let final = [];
for (let i = 0; i < data.length; i++) {
const day = data[i];
const date = this.readableDate(day[0]);
const prepareData = day[1];
final = hlp.pushToArrayByDate(final, date, prepareData);
}
final = hlp.sortData(final);
return final;
}
/**
* Gets Data with total Tested people
* @param {object} sourceHtml source DOM where to look at
* @return {array} returns array with all the data
*/
TotalTests() {
const data = this.DataFromDataAttribute("js-cummulative-total-tests-table-data");
let final = [];
for (let i = 0; i < data.length; i++) {
const day = data[i];
const date = this.readableDate(day[0]);
const prepareData = day[1];
final = hlp.pushToArrayByDate(final, date, prepareData);
}
final = hlp.sortData(final);
return final;
}
/**
* Gets Data with all Hospitalized people
* @param {object} sourceHtml source DOM where to look at
* @return {array} returns array with all the data
*/
HospitalizationData() {
// very unstable
const table = this.htmlMZCR.getElementsByClassName("equipmentTable")[0];
if (!table) {
return [];
}
let final = [];
const tbody = table.childNodes[3];
tbody.childNodes.forEach((row) => {
if (row.nodeName === "tr") {
let index = 0;
let date = "";
const prepareData = [];
row.childNodes.forEach((element) => {
if (element.nodeName === "td") {
if (index === 0) {
date = this.readableDate(element.innerHTML);
}
if (index === 1) {
prepareData[0] = element.innerHTML;
}
if (index === 2) {
prepareData[1] = element.innerHTML;
}
if (index === 3) {
prepareData[2] = element.innerHTML;
}
if (index === 4) {
prepareData[3] = element.innerHTML;
}
if (index === 5) {
prepareData[4] = element.innerHTML;
}
index += 1;
}
});
if (!date.includes(NaN)) {
final = hlp.pushToArrayByDate(final, date, prepareData);
}
}
});
// final = hlp.sortData(final);
return final;
}
/**
* Get all the Quarantine data
* @return {array} returns array with all the data
*/
QuarantineData() {
let kraje = [];
for (let index = 0; index < this.xlsKaranteny.length; index++) {
const list = this.xlsKaranteny[index];
if (list.name === "Statistika podle krajů") {
kraje = list.data;
}
}
const final = [];
const regionsNamed = ["Hlavní město Praha", "Středočeský kraj", "Jihočeský kraj", "Plzeňský kraj", "Karlovarský kraj", "Ústecký kraj", "Liberecký kraj", "Královéhradecký kraj", "Pardubický kraj", "Kraj Vysočina", "Jihomoravský kraj", "Olomoucký kraj", "Zlínský kraj", "Moravskoslezský kraj"];
for (let index = 0; index < kraje.length; index++) {
const row = kraje[index];
if (index > 3) {
if (typeof row !== "undefined" && row.length > 0) {
// the array is defined and has at least one element
const daysSince1900 = row[0];
const total = row[16];
const regions = {};
if (total && total > 0) {
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
let date = (daysSince1900) * oneDay;
date = new Date(date);
date = hlp.deltaDate(date, -2, 0, -70); // cuz Excel is weird
date = date.toISOString().substring(0, 10);
for (let region = 0; region < regionsNamed.length; region++) {
const regionName = regionsNamed[region];
const regionID = region + 1;
regions[regionName] = row[regionID];
}
final[date] = [];
final[date].n = total;
final[date].r = regions;
}
}
}
}
return final;
}
/**
* Gets Data from R0 api
* @return {array} returns array with all the data
*/
ApiR0() {
const apiData = this.jsonApiR0;
const parsed = JSON.parse(apiData);
const {
data
} = parsed;
const final = [];
for (let i = 0; i < data.length; i++) {
const [date, min, med, max] = data[i];
final[date] = [min, med, max];
}
// final = hlp.sortData(final);
return final;
}
/**
* Gets Data from hygiene districts
* @return {array} returns array with all the data
*/
ApiHygieneDistricts() {
const apiData = this.jsonAPIhygDistricts;
const parsed = JSON.parse(apiData);
const {
data
} = parsed;
return data;
}
}
/**
* Glue the data all together
* @class
* @classdesc Class is getting and parsing the data, to prepare array that we will work on later on.
*/
class GlueTogether {
/**
* Glue Together all the information
* @param {class} get Class that parses the info
*/
constructor(get) {
this.totalDied = get.TotalDied();
this.totalCases = get.TotalPositive();
this.totalTests = get.TotalTests();
this.totalHospital = get.HospitalizationData();
this.totalQuarantine = get.QuarantineData();
this.apiR0 = get.ApiR0();
this.apiHygieneDistricts = get.ApiHygieneDistricts();
// original: new Date(2020, 0, 27);
// short one: new Date(2020, 1, 27);
this.oldestDateDaily = new Date(2020, 1, 27);
this.oldestDateRegion = new Date(2020, 1, 27);
this.nowDate = new Date();
this.numberDeathsTotal = 0;
this.numberTestsYesterday = 0;
this.numberTestsTotal = 0;
this.numberCasesYesterday = 0;
this.numberCasesTotal = 0;
this.numberQuarantine = 0;
this.numberHospitalizedNow = 0;
this.numberHospitalizedCritical = [0, ""];
this.numberHospitalizedHealed = [0, ""];
this.numberQuarantineRegions = [];
}
/**
* Get date in the same format
* @param {string} date Date
* @return {array} returns array with all the data
*/
dateInDashFormat(date) {
// getting day in readable format
const dayDate = new Date(date);
const dtf = new Intl.DateTimeFormat("en", {
year: "numeric",
month: "2-digit",
day: "2-digit"
});
const [{
value: mo
}, , {
value: da
}, , {
value: ye
}] = dtf.formatToParts(dayDate);
const day = `${ye}-${mo}-${da}`;
return day;
}
/**
* Test if the data is iterable
* @param {array} checkIfIterable array to test if iterable
* @param {string} day day that works as key
* @return {array} returns true or false
*/
isIterable(checkIfIterable, day) {
// write only if available
if (checkIfIterable) {
if (checkIfIterable[day]) {
return true;
}
}
return false;
}
/**
* All the Cases data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
Cases(day) {
let numberCasesNow = 0;
[this.numberCasesTotal] = this.totalCases[day];
numberCasesNow = this.numberCasesTotal - this.numberCasesYesterday;
this.numberCasesYesterday = this.numberCasesTotal;
const prepareFormattedData = {};
prepareFormattedData.n = numberCasesNow;
prepareFormattedData.t = this.numberCasesTotal;
return prepareFormattedData;
}
/**
* All the Tests data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
Tests(day) {
let numberTestsNow = 0;
[this.numberTestsTotal] = this.totalTests[day];
numberTestsNow = this.numberTestsTotal - this.numberTestsYesterday;
this.numberTestsYesterday = this.numberTestsTotal;
const prepareFormattedData = {};
prepareFormattedData.n = numberTestsNow;
prepareFormattedData.t = this.numberTestsTotal;
return prepareFormattedData;
}
/**
* All the Deaths data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
Deaths(day) {
let numberDeathsNow = 0;
if (this.totalDied[day]) {
[numberDeathsNow, this.numberDeathsTotal] = this.totalDied[day];
}
const prepareFormattedData = {};
prepareFormattedData.n = numberDeathsNow;
prepareFormattedData.t = this.numberDeathsTotal;
return prepareFormattedData;
}
/**
* All the Hospitalized data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
Hospitalized(day) {
const [hospDay] = this.totalHospital[day];
this.numberHospitalizedNow = parseInt(hospDay[0], 10);
this.numberHospitalizedCritical = [parseInt(hospDay[1], 10), hospDay[2]];
this.numberHospitalizedHealed = [parseInt(hospDay[3], 10), hospDay[4]];
const prepareFormattedData = {};
prepareFormattedData.t = this.numberHospitalizedNow;
prepareFormattedData.c = [this.numberHospitalizedCritical[0],
this.numberHospitalizedCritical[1]
];
prepareFormattedData.h = [this.numberHospitalizedHealed[0],
this.numberHospitalizedHealed[1]
];
return prepareFormattedData;
}
/**
* All the Quarantined data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
Quarantined(day) {
this.numberQuarantine = parseInt(this.totalQuarantine[day].n, 10);
const prepareFormattedData = {};
prepareFormattedData.n = this.numberQuarantine;
return prepareFormattedData;
}
/**
* All the R0 data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
R0(day) {
const R0day = { };
[R0day.n] = this.apiR0[day];
[, R0day.d] = this.apiR0[day];
[,, R0day.x] = this.apiR0[day];
return R0day;
}
/**
* All the QuarantinedByRegions data
* @param {string} day day that works as key
* @return {array} returns array with all the data
*/
QuarantinedByRegions(day) {
}
/**
* Create array that keys are differentiated by Day
* @return {array} returns array with all the data
*/
Daily() {
const dailyArray = [];
for (let d = this.oldestDateDaily; d <= this.nowDate; d.setDate(d.getDate() + 1)) {
const day = this.dateInDashFormat(d);
const dailyData = {};
if (this.isIterable(this.totalCases, day)) {
dailyData.c = this.Cases(day);
}
if (this.isIterable(this.totalTests, day)) {
dailyData.t = this.Tests(day);
}
if (this.isIterable(this.totalDied, day)) {
dailyData.d = this.Deaths(day);
}
if (this.isIterable(this.totalHospital, day)) {
dailyData.h = this.Hospitalized(day);
}
if (this.isIterable(this.totalQuarantine, day)) {
dailyData.q = this.Quarantined(day);
}
if (this.isIterable(this.apiR0, day)) {
dailyData.r0 = this.R0(day);
}
dailyArray[day] = dailyData;
}
return dailyArray;
}
/**
* Create array that keys are differentiated by Day
* @return {array} returns array with all the data
*/
Regional() {
const regionalArray = [];
for (let d = this.oldestDateRegion; d <= this.nowDate; d.setDate(d.getDate() + 1)) {
const day = this.dateInDashFormat(d);
if (this.totalQuarantine[day]) {
if (this.totalQuarantine[day].r) {
const regions = this.totalQuarantine[day].r;
Object.keys(regions).forEach((region) => {
if (regionalArray[region] === undefined) {
regionalArray[region] = {};
}
if (regionalArray[region].quarantine === undefined) {
regionalArray[region].quarantine = {};
}
regionalArray[region].quarantine[day] = regions[region];
});
}
}
if (this.apiHygieneDistricts[day]) {
const regions = this.apiHygieneDistricts[day];
Object.keys(regions).forEach((region) => {
const districts = regions[region];
Object.keys(districts).forEach((district) => {
const cases = districts[district];
if (regionalArray[region] === undefined) {
regionalArray[region] = {};
}
if (regionalArray[region].cases === undefined) {
regionalArray[region].cases = {};
}
if (regionalArray[region].cases[district] === undefined) {
regionalArray[region].cases[district] = {};
}
if (regionalArray[region].cases[district][day] === undefined) {
regionalArray[region].cases[district][day] = [];
}
regionalArray[region].cases[district][day] = cases;
});
});
}
}
/* const dailyObject = Object.assign({}, regionalArray);
console.log(JSON.stringify(dailyObject)); */
return regionalArray;
}
}
/**
* Generate some usefull data
* @class
* @classdesc Class to generate predefined data
*/
class Generate {
/**
* Generate schema so new users can have an idea how the data are generated
* @returns {string} generated schema
*/
Schema() {
const day = "day in YYYY-MM-DD";
const schema = {};
schema.daily = {};
schema.daily[day] = {};
schema.daily[day]["[c]ases"] = {
n: "number of cases that day",
t: "total cases"
};
schema.daily[day]["[t]ests"] = {
n: "number of tests that day",
t: "total tests"
};
schema.daily[day]["[d]eaths"] = {
n: "number of deaths that day",
t: "total deaths"
};
schema.daily[day]["[h]ospitalizations"] = {
t: "total hospitalizations",
c: ["number of critical", "percentage of critical"],
h: ["number of healed/released", "percentage of healed/released"]
};
schema.daily[day]["[q]uarantine"] = {
n: "number of quarantined that day"
};
schema.daily[day]["[r0] basic reproduction number"] = {
n: "miN: 5th percentile for estimation from last 7 days",
d: "meD: estimation from last 7 days",
x: "maX: 95th percentile for estimation from last 7 days"
};
schema.regions = {};
schema.regions.region = {};
schema.regions.region.quarantine = {
day: "number of quarantined"
};
schema.regions.region.deaths = {
day: "age group"
};
schema.regions.region.cases = {
district: {
day: "number of cases"
}
};
return schema;
}
/**
* Generate schema so new users can have an idea how the data are generated
* @param {object} dailyArray all the data
* @returns {null} Returns nothing
*/
ConsoleLog(dailyArray) {
const dailyObject = Object.assign({}, dailyArray);
const dailyArrayString = JSON.stringify(dailyObject);
// console.log(dailyArray);
console.log(dailyArrayString.length);
console.log(dailyArrayString);
console.log(this.Schema());
// console.log(JSON.stringify(this.Schema()));
}
}
Apify.main(async () => {
// request all the data
const crawlWebMZCR = await request(api.mzcr);
const xlsCrawlQuarantine = await request(api.quar, api.enan);
const xlsCrawlQuarantineData = await hlp.parseQuarantineXLS(xlsCrawlQuarantine);
const jsonAPIr0 = await request(api.r0);
const jsonAPIhygDistricts = await request(api.hygDistricts);
// parse data from requests
const get = new GetData(crawlWebMZCR, xlsCrawlQuarantineData, jsonAPIr0, jsonAPIhygDistricts);
// glue together all the info
const glue = new GlueTogether(get);
const dailyArray = glue.Daily();
const regionalArray = glue.Regional();
// generate additional info
const gen = new Generate();
const data = {
daily: {},
regions: {}
};
data.daily = Object.assign({}, dailyArray);
data.regions = Object.assign({}, regionalArray);
const schema = gen.Schema();
// And then save output
const output = {
schema,
lastUpdatedAtApify: new Date(),
readMe: "https://www.sablatura.info/covid/api",
data
};
gen.ConsoleLog(output);
const {
keyValueStores
} = Apify.client;
const {
id: storeId
} = await keyValueStores.getOrCreateStore({
storeName: "COVID-CZ-history"
});
Apify.client.setOptions({
storeId
});
await Apify.setValue("OUTPUT", output);
await Apify.setValue("SIZE", JSON.stringify(output).length);
// Just pass the 'key' as the 'keyValueStores' already knows
// in what 'storeId' to look at.
const record = await keyValueStores.getRecord({
key: "LATEST"
});
// Check for empty 'STATE' records
const storeRecord = record && record.body ? record.body : {};
const previousState = typeof storeRecord === "string"
? JSON.parse(storeRecord) : storeRecord;
// It is a good practice to copy objects instead of
// overwriting them. Weird things can happen otherwise.
const nextState = Object.assign({}, previousState, output);
await keyValueStores.putRecord({
key: "LATEST",
body: JSON.stringify(nextState)
});
});