Skip to content

Commit 43f76ca

Browse files
authored
Merge pull request #1 from NiklasMerz/pages
Parse more metrics
2 parents a341738 + ed89c15 commit 43f76ca

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

index.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const fetch = require('node-fetch');
22
const core = require('@actions/core');
33

4-
const token = process.env.NETLIFY_TOKEN || core.getInput('netlify-token', {required: true});
5-
const siteId = process.env.NETLIFY_SITE_ID || core.getInput('netlify-site-id', {required: true});
4+
const token = process.env.NETLIFY_TOKEN || core.getInput('netlify-token', { required: true });
5+
const siteId = process.env.NETLIFY_SITE_ID || core.getInput('netlify-site-id', { required: true });
66

77
const startDate = new Date();
88
startDate.setHours(0);
@@ -35,6 +35,10 @@ if (timezone < 0) {
3535
async function start() {
3636
getMetric("pageviews");
3737
getMetric("visitors");
38+
getMetric("pages");
39+
getMetric("bandwidth");
40+
getMetric("not_found");
41+
getMetric("sources");
3842
}
3943

4044
async function getMetric(metric) {
@@ -51,33 +55,50 @@ async function getMetric(metric) {
5155
"method": "GET",
5256
"mode": "cors"
5357
});
54-
58+
5559
const response = await res.json();
5660
writeToCSV(response.data, metric);
57-
} catch(e) {
58-
console.error("Request failed". url);
61+
} catch (e) {
62+
console.error("Request failed".url);
5963
console.error(e);
6064
}
6165
}
6266

6367
function writeToCSV(data, metric) {
6468
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
69+
70+
let header = [];
71+
try {
72+
if (data[0] instanceof Array) {
73+
header = [
74+
{ id: 'date', title: 'date' },
75+
{ id: 'timestamp', title: 'timestamp' },
76+
{ id: 'value', title: 'count' },
77+
];
78+
} else {
79+
for (const key in data[0]) {
80+
header.push({ id: key, title: key });
81+
}
82+
}
83+
} catch (e) {
84+
console.error("Element parsing failed", e);
85+
}
86+
6587
const csvWriter = createCsvWriter({
6688
path: metric + '.csv',
67-
header: [
68-
{ id: 'date', title: 'Date' },
69-
{ id: 'timestamp', title: 'Timestamp' },
70-
{ id: 'value', title: 'Count' },
71-
]
89+
header
7290
});
7391

7492
const exportData = data.map((elem) => {
75-
console.log(metric, elem);
76-
return {
77-
date: (new Date(elem[0])).toISOString(),
78-
timestamp: elem[0],
79-
value: elem[1],
80-
};
93+
if (elem instanceof Array) {
94+
return {
95+
date: (new Date(elem[0])).toISOString(),
96+
timestamp: elem[0],
97+
value: elem[1],
98+
};
99+
} else {
100+
return elem;
101+
}
81102
})
82103

83104
csvWriter

0 commit comments

Comments
 (0)