1
1
const fetch = require ( 'node-fetch' ) ;
2
2
const core = require ( '@actions/core' ) ;
3
3
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 } ) ;
6
6
7
7
const startDate = new Date ( ) ;
8
8
startDate . setHours ( 0 ) ;
@@ -35,6 +35,10 @@ if (timezone < 0) {
35
35
async function start ( ) {
36
36
getMetric ( "pageviews" ) ;
37
37
getMetric ( "visitors" ) ;
38
+ getMetric ( "pages" ) ;
39
+ getMetric ( "bandwidth" ) ;
40
+ getMetric ( "not_found" ) ;
41
+ getMetric ( "sources" ) ;
38
42
}
39
43
40
44
async function getMetric ( metric ) {
@@ -51,33 +55,50 @@ async function getMetric(metric) {
51
55
"method" : "GET" ,
52
56
"mode" : "cors"
53
57
} ) ;
54
-
58
+
55
59
const response = await res . json ( ) ;
56
60
writeToCSV ( response . data , metric ) ;
57
- } catch ( e ) {
58
- console . error ( "Request failed" . url ) ;
61
+ } catch ( e ) {
62
+ console . error ( "Request failed" . url ) ;
59
63
console . error ( e ) ;
60
64
}
61
65
}
62
66
63
67
function writeToCSV ( data , metric ) {
64
68
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
+
65
87
const csvWriter = createCsvWriter ( {
66
88
path : metric + '.csv' ,
67
- header : [
68
- { id : 'date' , title : 'Date' } ,
69
- { id : 'timestamp' , title : 'Timestamp' } ,
70
- { id : 'value' , title : 'Count' } ,
71
- ]
89
+ header
72
90
} ) ;
73
91
74
92
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
+ }
81
102
} )
82
103
83
104
csvWriter
0 commit comments