/
heatmap_chart.go
308 lines (284 loc) · 9.62 KB
/
heatmap_chart.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package signalform
import (
"encoding/json"
"fmt"
"math"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
func heatmapChartResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"synced": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether the resource in SignalForm and SignalFx are identical or not. Used internally for syncing.",
},
"last_updated": &schema.Schema{
Type: schema.TypeFloat,
Computed: true,
Description: "Latest timestamp the resource was updated",
},
"resource_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: CHART_URL,
Description: "API URL of the chart",
},
"url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "URL of the chart",
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Name of the chart",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Description of the chart (Optional)",
},
"program_text": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Signalflow program text for the chart. More info at \"https://developers.signalfx.com/docs/signalflow-overview\"",
},
"unit_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "(Metric by default) Must be \"Metric\" or \"Binary\"",
},
"minimum_resolution": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "The minimum resolution (in seconds) to use for computing the underlying program",
},
"max_delay": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "How long (in seconds) to wait for late datapoints",
ValidateFunc: validateMaxDelayValue,
},
"disable_sampling": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "(false by default) If false, samples a subset of the output MTS, which improves UI performance",
},
"group_by": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Properties to group by in the heatmap (in nesting order)",
},
"sort_by": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateSortBy,
Description: "The property to use when sorting the elements. Must be prepended with + for ascending or - for descending (e.g. -foo)",
},
"color_range": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "Values and color for the color range. Example: colorRange : { min : 0, max : 100, color : \"blue\" }",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_value": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
Default: -math.MaxFloat32,
Description: "The minimum value within the coloring range",
},
"max_value": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
Default: math.MaxFloat32,
Description: "The maximum value within the coloring range",
},
"color": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The color range to use. Must be either \"gray\", \"blue\", \"navy\", \"orange\", \"yellow\", \"magenta\", \"purple\", \"violet\", \"lilac\", \"green\", \"aquamarine\"",
ValidateFunc: validateHeatmapChartColor,
},
},
},
},
"color_scale": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "Single color range including both the color to display for that range and the borders of the range",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"gt": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
Default: math.MaxFloat32,
Description: "Indicates the lower threshold non-inclusive value for this range",
},
"gte": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
Default: math.MaxFloat32,
Description: "Indicates the lower threshold inclusive value for this range",
},
"lt": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
Default: math.MaxFloat32,
Description: "Indicates the upper threshold non-inculsive value for this range",
},
"lte": &schema.Schema{
Type: schema.TypeFloat,
Optional: true,
Default: math.MaxFloat32,
Description: "Indicates the upper threshold inclusive value for this range",
},
"color": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The color to use. Must be either \"gray\", \"blue\", \"navy\", \"orange\", \"yellow\", \"magenta\", \"purple\", \"violet\", \"lilac\", \"green\", \"aquamarine\"",
ValidateFunc: validateHeatmapChartColor,
},
},
},
},
"hide_timestamp": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "(false by default) Whether to show the timestamp in the chart",
},
},
Create: heatmapchartCreate,
Read: heatmapchartRead,
Update: heatmapchartUpdate,
Delete: heatmapchartDelete,
}
}
/*
Use Resource object to construct json payload in order to create an Heatmap chart
*/
func getPayloadHeatmapChart(d *schema.ResourceData) ([]byte, error) {
payload := map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"programText": sanitizeProgramText(d.Get("program_text").(string)),
}
viz := getHeatmapOptionsChart(d)
if len(viz) > 0 {
payload["options"] = viz
}
return json.Marshal(payload)
}
func getHeatmapColorRangeOptions(d *schema.ResourceData) map[string]interface{} {
item := make(map[string]interface{})
colorRange := d.Get("color_range").(*schema.Set).List()
for _, options := range colorRange {
options := options.(map[string]interface{})
if val, ok := options["min_value"]; ok {
if val.(float64) != -math.MaxFloat32 {
item["min"] = val.(float64)
}
}
if val, ok := options["max_value"]; ok {
if val.(float64) != math.MaxFloat32 {
item["max"] = val.(float64)
}
}
color := options["color"].(string)
for _, colorStruct := range ChartColorsSlice {
if color == colorStruct.name {
item["color"] = colorStruct.name
break
}
}
}
return item
}
func getHeatmapOptionsChart(d *schema.ResourceData) map[string]interface{} {
viz := make(map[string]interface{})
viz["type"] = "Heatmap"
if val, ok := d.GetOk("unit_prefix"); ok {
viz["unitPrefix"] = val.(string)
}
programOptions := make(map[string]interface{})
if val, ok := d.GetOk("minimum_resolution"); ok {
programOptions["minimumResolution"] = val.(int) * 1000
}
if val, ok := d.GetOk("max_delay"); ok {
programOptions["maxDelay"] = val.(int) * 1000
}
programOptions["disableSampling"] = d.Get("disable_sampling").(bool)
viz["programOptions"] = programOptions
if groupByOptions, ok := d.GetOk("group_by"); ok {
viz["groupBy"] = groupByOptions.([]interface{})
}
if sortProperty, ok := d.GetOk("sort_by"); ok {
sortBy := sortProperty.(string)
viz["sortProperty"] = sortBy[1:]
if strings.HasPrefix(sortBy, "+") {
viz["sortDirection"] = "Ascending"
} else {
viz["sortDirection"] = "Descending"
}
}
if colorRangeOptions := getHeatmapColorRangeOptions(d); len(colorRangeOptions) > 0 {
viz["colorBy"] = "Range"
viz["colorRange"] = colorRangeOptions
} else if colorScaleOptions := getColorScaleOptions(d); len(colorScaleOptions) > 0 {
viz["colorBy"] = "Scale"
viz["colorScale2"] = colorScaleOptions
}
viz["timestampHidden"] = d.Get("hide_timestamp").(bool)
return viz
}
func heatmapchartCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*signalformConfig)
payload, err := getPayloadHeatmapChart(d)
if err != nil {
return fmt.Errorf("Failed creating json payload: %s", err.Error())
}
return resourceCreate(CHART_API_URL, config.AuthToken, payload, d)
}
func heatmapchartRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*signalformConfig)
url := fmt.Sprintf("%s/%s", CHART_API_URL, d.Id())
return resourceRead(url, config.AuthToken, d)
}
func heatmapchartUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*signalformConfig)
payload, err := getPayloadHeatmapChart(d)
if err != nil {
return fmt.Errorf("Failed creating json payload: %s", err.Error())
}
url := fmt.Sprintf("%s/%s", CHART_API_URL, d.Id())
return resourceUpdate(url, config.AuthToken, payload, d)
}
func heatmapchartDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*signalformConfig)
url := fmt.Sprintf("%s/%s", CHART_API_URL, d.Id())
return resourceDelete(url, config.AuthToken, d)
}
/*
Validates the color_range field against a list of allowed words.
*/
func validateHeatmapChartColor(v interface{}, k string) (we []string, errors []error) {
value := v.(string)
keys := make([]string, 0, len(ChartColorsSlice))
found := false
for _, item := range ChartColorsSlice {
if value == item.name {
found = true
}
keys = append(keys, item.name)
}
if !found {
joinedColors := strings.Join(keys, ",")
errors = append(errors, fmt.Errorf("%s not allowed; must be either %s", value, joinedColors))
}
return
}