get all record analytics for the whole zone in one request
This commit is contained in:
parent
656988e17f
commit
d2d15701ab
44
cf/cf.go
44
cf/cf.go
@ -57,6 +57,7 @@ type Analytic struct {
|
|||||||
Result struct {
|
Result struct {
|
||||||
Rows int `json:"rows"`
|
Rows int `json:"rows"`
|
||||||
Data []struct {
|
Data []struct {
|
||||||
|
Dimensions []string `json:"dimensions"`
|
||||||
Metrics []int `json:"metrics"`
|
Metrics []int `json:"metrics"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
DataLag int `json:"data_lag"`
|
DataLag int `json:"data_lag"`
|
||||||
@ -154,8 +155,9 @@ func (c *Client) GetRecordAnalytic(record Record) (*Analytic, error) {
|
|||||||
|
|
||||||
q := req.URL.Query()
|
q := req.URL.Query()
|
||||||
q.Add("filters", fmt.Sprintf("queryName==%s", record.Name))
|
q.Add("filters", fmt.Sprintf("queryName==%s", record.Name))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
TBD: Why does this give HTTP 400?
|
TBD: Why does this give HTTP 403?
|
||||||
q.Add("filters", fmt.Sprintf("queryName==%s,queryType==%s", record.Name, record.Type))
|
q.Add("filters", fmt.Sprintf("queryName==%s,queryType==%s", record.Name, record.Type))
|
||||||
*/
|
*/
|
||||||
req.URL.RawQuery = q.Encode()
|
req.URL.RawQuery = q.Encode()
|
||||||
@ -184,3 +186,43 @@ func (c *Client) GetRecordAnalytic(record Record) (*Analytic, error) {
|
|||||||
|
|
||||||
return analytic, nil
|
return analytic, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getRecordAnalytic provides a pointer to an Analytic for the given
|
||||||
|
func (c *Client) GetZoneAnalytic(zone Zone) (*Analytic, error) {
|
||||||
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/zones/%s/dns_analytics/report", c.APIURL, zone.ID), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create new http request: %v", err)
|
||||||
|
}
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.APIKey))
|
||||||
|
|
||||||
|
q := req.URL.Query()
|
||||||
|
q.Add("metrics", "queryCount")
|
||||||
|
q.Add("dimensions", "queryName")
|
||||||
|
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to execute http request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("http response has status '%d' but expected '%d'", resp.StatusCode, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read http response body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
analytic := &Analytic{}
|
||||||
|
err = json.Unmarshal(b, analytic)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal response body to struct: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return analytic, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
19
main.go
19
main.go
@ -44,20 +44,25 @@ func main() {
|
|||||||
}
|
}
|
||||||
log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID)
|
log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID)
|
||||||
|
|
||||||
|
analytic, err := cfClient.GetZoneAnalytic(zone)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to get zone analytic for zone '%s': %v", zone.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
records, err := cfClient.GetRecords(zone)
|
records, err := cfClient.GetRecords(zone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to get records for zone '%s' with ID '%s': %v", zone.Name, zone.ID, err)
|
log.Printf("failed to get records for zone '%s' with ID '%s': %v", zone.Name, zone.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, record := range records {
|
for i := range records {
|
||||||
log.Printf("processing record '%s' of type '%s'", record.Name, record.Type)
|
for _, metric := range analytic.Result.Data {
|
||||||
analytic, err := cfClient.GetRecordAnalytic(record)
|
if records[i].Name != metric.Dimensions[0] {
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to get record report for record '%s' in zone '%s' with ID '%s': %v", record.Name, zone.Name, zone.ID, err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
record.NumberQueries = analytic.Result.Totals.QueryCount
|
records[i].NumberQueries = metric.Metrics[0]
|
||||||
outRows = append(outRows, record)
|
break
|
||||||
|
}
|
||||||
|
outRows = append(outRows, records[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user