-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (155 loc) · 3.7 KB
/
main.go
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
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
type RequestGroup1d struct {
Dimensions struct {
Date string `json:"date"`
} `json:"dimensions"`
Sum struct {
Requests int `json:"requests"`
PageViews int `json:"pageViews"`
} `json:"sum"`
Uniq struct {
Uniques int `json:"uniques"`
} `json:"uniq"`
}
type GraphQLRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
func main() {
log.SetOutput(os.Stderr)
token := os.Getenv("CF_TOKEN")
if token == "" {
panic("CF_TOKEN environment variable is not set")
}
var outFileName = flag.String("out", "", "The output csv filename to write to")
var zoneId = flag.String("zone", "", "The CF zone id")
flag.Parse()
if *outFileName == "" {
panic("out parameter is not set")
}
if *zoneId == "" {
panic("zone parameter is not set")
}
csvFile, err := os.OpenFile(*outFileName, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer csvFile.Close()
csvReader := csv.NewReader(csvFile)
records, err := csvReader.ReadAll()
if err != nil {
panic(err)
}
// seek csvfile to the end
_, err = csvFile.Seek(0, 2)
if err != nil {
panic(err)
}
csvWriter := csv.NewWriter(csvFile)
today := time.Now()
startDate := ""
if len(records) == 0 {
// if no records, start from 1 year ago
startDate = today.Add(-31539600 * time.Second).Format("2006-01-02")
// write header to csv file
err := csvWriter.Write([]string{"date", "requests", "pageViews", "uniques"})
if err != nil {
panic(err)
}
csvWriter.Flush()
log.Printf("Empty CSV detected, starting from %s", startDate)
} else {
startDate = records[len(records)-1][0]
log.Printf("Existing CSV file detected, starting from %s", startDate)
}
query := `
query($zoneId: string, $date: string) {
viewer {
zones(filter: {zoneTag: $zoneId }) {
httpRequests1dGroups(
filter: {
date_gt : $date
}
orderBy: [date_ASC]
limit: 10000
) {
dimensions { date }
sum {
requests,
pageViews,
}
uniq {
uniques
}
}
}
}
}
`
reqBytes, err := json.Marshal(&GraphQLRequest{
Query: query,
Variables: map[string]interface{}{
"zoneId": zoneId,
"date": startDate,
},
})
fmt.Println(string(reqBytes))
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/graphql", bytes.NewReader(reqBytes))
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("GraphQL request failed", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
panic("bad status code")
}
responseHolder := struct {
Data struct {
Viewer struct {
Zones []struct {
HttpRequests1dGroups []RequestGroup1d `json:"httpRequests1dGroups"`
} `json:"zones"`
} `json:"viewer"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}{}
err = json.NewDecoder(resp.Body).Decode(&responseHolder)
if err != nil {
panic(err)
}
if len(responseHolder.Errors) > 0 {
panic(responseHolder.Errors[0].Message)
}
for _, item := range responseHolder.Data.Viewer.Zones {
for i, group := range item.HttpRequests1dGroups {
if i != len(item.HttpRequests1dGroups)-1 {
log.Println(group.Dimensions.Date, group.Sum.Requests, group.Sum.PageViews, group.Uniq.Uniques)
err = csvWriter.Write([]string{group.Dimensions.Date, fmt.Sprintf("%d", group.Sum.Requests), fmt.Sprintf("%d", group.Sum.PageViews), fmt.Sprintf("%d", group.Uniq.Uniques)})
if err != nil {
panic(err)
}
}
}
}
csvWriter.Flush()
}