forked from andygrunwald/cachet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
188 lines (159 loc) · 5.77 KB
/
metrics.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
185
186
187
188
package cachet
import (
"fmt"
)
const (
// MetricsViewLastHour means "Default view: Last Hour"
MetricsViewLastHour = 0
// MetricsViewLast12Hours means "Default view: Last 12 Hours"
MetricsViewLast12Hours = 1
// MetricsViewLastWeek means "Default view: Week"
MetricsViewLastWeek = 2
// MetricsViewLastMonth means "Default view: Month"
MetricsViewLastMonth = 3
// MetricsCalculationSum means "Calculation of Metrics: Sum"
MetricsCalculationSum = 0
// MetricsCalculationAverage means "Calculation of Metrics: Average"
MetricsCalculationAverage = 1
// MetricsVisibilityLoggedIn means "Visibility: Visible to authenticated users"
MetricsVisibilityLoggedIn = 0
// MetricsVisibilityPublic means "Visibility: Visible to everybody"
MetricsVisibilityPublic = 1
// MetricsVisibilityHidden means "Visibility: Always hidden"
MetricsVisibilityHidden = 2
// MetricsNoDisplayChart means to not display chart in Status Page
MetricsNoDisplayChart = 0
// MetricsDisplayChart means to display chart in Status Page
MetricsDisplayChart = 1
)
// MetricsService contains REST endpoints that belongs to cachet metrics.
type MetricsService struct {
client *Client
}
// Metric entity reflects one single metric
type Metric struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Suffix string `json:"suffix,omitempty"`
Description string `json:"description,omitempty"`
DefaultValue int `json:"default_value"`
CalcType int `json:"calc_type,omitempty"`
DisplayChart bool `json:"display_chart,omitempty"`
Places int `json:"places,omitempty"`
DefaultView int `json:"default_view,omitempty"`
Threshold int `json:"threshold,omitempty"`
Order int `json:"order,omitempty"`
Visible int `json:"visible,omitemtpy"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
DefaultViewName string `json:"default_view_name,omitempty"`
}
// Point is a single point in a Metric
type Point struct {
ID int `json:"id,omitempty"`
MetricID int `json:"metric_id,omitempty"`
Value int `json:"value,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Counter int `json:"counter,omitempty"`
CalculatedValue int `json:"calculated_value,omitempty"`
}
// MetricResponse reflects the response of /metric call
type MetricResponse struct {
Meta Meta `json:"meta,omitempty"`
Metrics []Metric `json:"data,omitempty"`
}
// MetricQueryParams contains fields to filter returned results
type MetricQueryParams struct {
QueryOptions
}
// metricAPIResponse is an internal type to hide
// some the "data" nested level from the API.
// Some calls (e.g. Get or Create) return the metric in the "data" key.
type metricAPIResponse struct {
Data *Metric `json:"data"`
}
// metricPointsAPIResponse is an internal type to hide
// some the "data" nested level from the API.
// Some calls (e.g. Get or Create) return the metric points in the "data" key.
type metricPointsAPIResponse struct {
Data *[]Point `json:"data"`
}
// metricPointAPIResponse is an internal type to hide
// some the "data" nested level from the API.
// Some calls (e.g. Get or Create) return the metric point in the "data" key.
type metricPointAPIResponse struct {
Data *Point `json:"data"`
}
// GetAll returns all metrics that have been setup.
//
// Docs: https://docs.cachethq.io/reference#get-metrics
func (s *MetricsService) GetAll(filter *MetricQueryParams) (*MetricResponse, *Response, error) {
u := "api/v1/metrics"
v := new(MetricResponse)
u, err := addOptions(u, filter)
if err != nil {
return nil, nil, err
}
resp, err := s.client.Call("GET", u, nil, v)
return v, resp, err
}
// Get returns a single metric, without points.
//
// Docs: https://docs.cachethq.io/reference#get-a-metric
func (s *MetricsService) Get(id int) (*Metric, *Response, error) {
u := fmt.Sprintf("api/v1/metrics/%d", id)
v := new(metricAPIResponse)
resp, err := s.client.Call("GET", u, nil, v)
return v.Data, resp, err
}
// Create a new metric.
//
// Docs: https://docs.cachethq.io/reference#metrics
func (s *MetricsService) Create(m *Metric) (*Metric, *Response, error) {
u := "api/v1/metrics"
v := new(metricAPIResponse)
resp, err := s.client.Call("POST", u, m, v)
return v.Data, resp, err
}
// Delete a metric.
//
// Docs: https://docs.cachethq.io/reference#delete-a-metric
func (s *MetricsService) Delete(id int) (*Response, error) {
u := fmt.Sprintf("api/v1/metrics/%d", id)
resp, err := s.client.Call("DELETE", u, nil, nil)
return resp, err
}
// GetPoints return a list of metric points.
//
// Docs: https://docs.cachethq.io/reference#get-metric-points
func (s *MetricsService) GetPoints(id int) (*[]Point, *Response, error) {
u := fmt.Sprintf("api/v1/metrics/%d/points", id)
v := new(metricPointsAPIResponse)
resp, err := s.client.Call("GET", u, nil, v)
return v.Data, resp, err
}
// AddPoint adds a metric point to a given metric.
//
// Docs: https://docs.cachethq.io/reference#post-metric-points
func (s *MetricsService) AddPoint(id int, value int, timestamp string) (*Point, *Response, error) {
u := fmt.Sprintf("api/v1/metrics/%d/points", id)
v := new(metricPointAPIResponse)
p := struct {
Value int `json:"value"`
Timestamp string `json:"timestamp"`
}{
Value: value,
Timestamp: timestamp,
}
resp, err := s.client.Call("POST", u, p, v)
return v.Data, resp, err
}
// DeletePoint deletes a metric point.
//
// Docs: https://docs.cachethq.io/reference#delete-a-metric-point
func (s *MetricsService) DeletePoint(id, pointID int) (*Response, error) {
u := fmt.Sprintf("api/v1/metrics/%d/points/%d", id, pointID)
resp, err := s.client.Call("DELETE", u, nil, nil)
return resp, err
}