Skip to content

Commit 0641f63

Browse files
committed
Write pageviews to CSV
1 parent e3f9ccf commit 0641f63

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
*.txt
3-
*.env
3+
*.env
4+
*.csv

index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,32 @@ async function start() {
4848
"mode": "cors"
4949
});
5050

51-
console.log(await res.json());
51+
const response = await res.json();
52+
writeToCSV(response.data, "pageviews");
53+
}
54+
55+
function writeToCSV(data, metric) {
56+
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
57+
const csvWriter = createCsvWriter({
58+
path: metric + '.csv',
59+
header: [
60+
{ id: 'date', title: 'Date' },
61+
{ id: 'timestamp', title: 'Timestamp' },
62+
{ id: 'value', title: 'Count' },
63+
]
64+
});
65+
66+
const exportData = data.map((elem) => {
67+
return {
68+
date: (new Date(elem[0])).toISOString(),
69+
timestamp: elem[0],
70+
value: elem[1],
71+
};
72+
})
73+
74+
csvWriter
75+
.writeRecords(exportData)
76+
.then(() => console.log(`The CSV file for ${metric} was written successfully`));
5277
}
5378

5479
start();

package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"test": "test"
88
},
99
"dependencies": {
10+
"csv-writer": "^1.6.0",
1011
"node-fetch": "^2.6.1"
1112
},
1213
"devDependencies": {},

0 commit comments

Comments
 (0)