File: realtime_stats.go

package info (click to toggle)
golang-github-sethvargo-go-fastly 1.2.1%2Bgit20190805.5c6c8bd-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,584 kB
  • sloc: makefile: 71
file content (53 lines) | stat: -rw-r--r-- 1,702 bytes parent folder | download | duplicates (2)
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
package fastly

import "fmt"

// RealtimeStats is a response from Fastly's real-time analytics endpoint
type RealtimeStatsResponse struct {
	Timestamp      uint64          `mapstructure:"Timestamp"`
	Data           []*RealtimeData `mapstructure:"Data"`
	Error          string          `mapstructure:"Error"`
	AggregateDelay uint32          `mapstructure:"AggregateDelay"`
}

// RealtimeData represents combined stats for all Fastly's POPs and aggregate of them.
// It also includes a timestamp of when the stats were recorded
type RealtimeData struct {
	Datacenter map[string]*Stats `mapstructure:"datacenter"`
	Aggregated *Stats            `mapstructure:"aggregated"`
	Recorded   uint64            `mapstructure:"recorded"`
}

// GetRealtimeStatsInput is an input parameter to GetRealtimeStats function
type GetRealtimeStatsInput struct {
	Service   string
	Timestamp uint64
	Limit     uint32
}

// GetRealtimeStats returns realtime stats for a service based on the GetRealtimeStatsInput
// parameter. The realtime stats work in a rolling fasion where first request will return
// a timestamp which should be passed to consequentive call and so on.
// More details at https://docs.fastly.com/api/analytics
func (c *RTSClient) GetRealtimeStats(i *GetRealtimeStatsInput) (*RealtimeStatsResponse, error) {
	if i.Service == "" {
		return nil, ErrMissingService
	}

	path := fmt.Sprintf("/v1/channel/%s/ts/%d", i.Service, i.Timestamp)

	if i.Limit != 0 {
		path = fmt.Sprintf("%s/limit/%d", path, i.Limit)
	}

	resp, err := c.client.Get(path, nil)
	if err != nil {
		return nil, err
	}

	var s *RealtimeStatsResponse
	if err := decodeJSON(&s, resp.Body); err != nil {
		return nil, err
	}
	return s, nil
}