Skip to content

Commit 7f24b74

Browse files
code refactor
1 parent f43136d commit 7f24b74

File tree

5 files changed

+60
-50
lines changed

5 files changed

+60
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ and dashboards in Grafana using data in Dataset. You may want to use this plugin
55
to allow you to visualize Dataset data next to other data sources, for instance
66
when you want to monitor many feeds on a single dashboard.
77

8-
![SystemDashboard](images/SystemDashboard.png)
8+
![SystemDashboard](https://github.com/scalyr/scalyr-grafana-datasource-plugin/blob/go-rewrite-v2/src/img/SystemDashboard.png)
99

1010
With the Dataset plugin, you will be able to create and visualize your log-based
1111
metrics along side all of your other data sources. It's a great way to have a

pkg/plugin/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func (d *DataSetClient) DoFacetValuesRequest(req FacetQuery) (LRQResult, error)
101101
// Repeat ping requests for our query until we get a result with all steps steps complete
102102
// TODO: A timeout or some other way of escaping besides an error
103103
for stepsComplete < stepsTotal {
104-
log.DefaultLogger.Warn(" request", request)
105104
resp, err := d.netClient.Do(request)
106105
if err != nil {
107106
log.DefaultLogger.Warn("error sending request to DataSet", "err", err)

pkg/plugin/lrq_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const OK = "OK"
2828
const TIMEOUT = "TIMEOUT"
2929
const ERROR = "ERROR"
3030

31+
const (
32+
PERCENTAGE string = "PERCENTAGE"
33+
NUMBER string = "NUMBER"
34+
TIMESTAMP string = "TIMESTAMP"
35+
)
36+
3137
type LogOptions struct {
3238
Filter string `json:"filter"`
3339
Ascending bool `json:"ascending"`

pkg/plugin/plugin.go

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"math"
78
"net/http"
89
"strconv"
910
"strings"
@@ -38,7 +39,6 @@ func NewDataSetDatasource(settings backend.DataSourceInstanceSettings) (instance
3839
var unsecure jsonData
3940
err := json.Unmarshal(settings.JSONData, &unsecure)
4041
if err != nil {
41-
log.DefaultLogger.Warn("error marshalling", "err", err)
4242
return nil, err
4343
}
4444
url := unsecure.ScalyrUrl
@@ -117,7 +117,6 @@ func (d *DataSetDatasource) CallResource(ctx context.Context, req *backend.CallR
117117
}
118118
finalResponse := make([]string, len(facetResultData.Facet.Values))
119119
for i, val := range facetResultData.Facet.Values {
120-
log.DefaultLogger.Warn("facetResultData", val.Value, val.Value)
121120
finalResponse[i] = val.Value
122121
}
123122
pb := &FacetResponse{Value: finalResponse}
@@ -210,13 +209,10 @@ func displayPlotData(result LRQResult, response backend.DataResponse) backend.Da
210209
values := make([]float64, len(resultData.XAxis))
211210
for index, value := range resultData.XAxis {
212211
values[index] = resultData.Plots[0].Samples[index] // TODO: handle multiple PlotData objects for Breakdown graphs
213-
log.DefaultLogger.Warn("value: ", "value", value)
214-
times[index] = time.Unix(value/1000, 0) // TODO: we lose the precision of milliseconds here, is this fine?
215-
log.DefaultLogger.Warn("times[index]: ", "times[index]", times[index])
212+
times[index] = time.Unix(value/1000, 0) // TODO: we lose the precision of milliseconds here, is this fine?
216213
}
217214

218215
// add fields.
219-
log.DefaultLogger.Warn("values: ", "values", values)
220216
frame.Fields = append(frame.Fields,
221217
data.NewField("time", nil, times),
222218
data.NewField("values", nil, values),
@@ -229,97 +225,107 @@ func displayPlotData(result LRQResult, response backend.DataResponse) backend.Da
229225

230226
func displayPQData(result LRQResult, response backend.DataResponse) backend.DataResponse {
231227
resultData := TableResultData{}
232-
233228
err := json.Unmarshal(result.Data, &resultData)
234229
if err != nil {
235230
return response
236231
}
237-
if len(resultData.Columns) < 1 {
232+
if len(resultData.Values) < 1 {
238233
return response
239234
}
240235
frame := data.NewFrame("response")
241-
236+
// Iterate over the data to modify the result into dataframe acceptable format
242237
for idx, col := range resultData.Columns {
243-
switch val := col.Type; {
244-
case val == "TIMESTAMP":
245-
tmp := make([]time.Time, len(resultData.Values))
246-
for j, b := range resultData.Values {
247-
timeInInt := int64(b[idx].(float64))
248-
tmp[j] = time.Unix(timeInInt/1000000000, 0)
238+
switch cellType := col.Type; {
239+
case cellType == TIMESTAMP:
240+
res := make([]time.Time, len(resultData.Values))
241+
for i, val := range resultData.Values {
242+
timeInInt := int64(val[idx].(float64))
243+
res[i] = time.Unix(timeInInt/1000000000, 0) // convert nanoseconds to Time.time format
249244
}
250245
frame.Fields = append(frame.Fields,
251-
data.NewField(col.Name, nil, tmp),
246+
data.NewField(col.Name, nil, res),
252247
)
253248
break
254-
case val == "PERCENTAGE":
255-
tmp := make([]string, len(resultData.Values))
256-
for j, b := range resultData.Values {
257-
tmp[j] = strconv.Itoa(b[idx].(int))
249+
case cellType == PERCENTAGE:
250+
res := make([]string, len(resultData.Values))
251+
for i, val := range resultData.Values {
252+
if w, ok := val[idx].(int); ok {
253+
res[i] = strconv.FormatInt(int64(w), 10) + "%"
254+
}
258255
}
259256
frame.Fields = append(frame.Fields,
260-
data.NewField(col.Name, nil, tmp),
257+
data.NewField(col.Name, nil, res),
261258
)
262259
break
263-
case val == "NUMBER" && col.DecimalPlaces > 0:
264-
tmp := make([]float64, len(resultData.Values))
265-
for j, b := range resultData.Values {
266-
switch b[idx].(type) {
260+
case cellType == NUMBER && col.DecimalPlaces > 0:
261+
res := make([]float64, len(resultData.Values))
262+
for i, val := range resultData.Values {
263+
switch val[idx].(type) {
267264
case float32:
268-
tmp[j] = float64(b[idx].(float32))
265+
res[i] = float64(val[idx].(float32))
266+
break
267+
case string:
268+
if val[idx] == "Infinity" {
269+
res[i] = math.Inf(1)
270+
} else if val[idx] == "-Infinity" {
271+
res[i] = math.Inf(-1)
272+
} else if val[idx] == "NaN" {
273+
res[i] = math.NaN()
274+
}
269275
break
270276
default:
271-
tmp[j] = b[idx].(float64)
277+
res[i] = val[idx].(float64)
272278
}
273279
}
274280
frame.Fields = append(frame.Fields,
275-
data.NewField(col.Name, nil, tmp),
281+
data.NewField(col.Name, nil, res),
276282
)
277283
break
278-
case val == "NUMBER" && col.DecimalPlaces <= 0:
279-
tmp := make([]int64, len(resultData.Values))
280-
for j, b := range resultData.Values {
281-
switch b[idx].(type) {
284+
case cellType == NUMBER && col.DecimalPlaces <= 0:
285+
res := make([]int64, len(resultData.Values))
286+
for i, val := range resultData.Values {
287+
switch val[idx].(type) {
282288
case int:
283-
tmp[j] = int64(b[idx].(int))
289+
res[i] = int64(val[idx].(int))
284290
break
285291
case int16:
286-
tmp[j] = int64(b[idx].(int16))
292+
res[i] = int64(val[idx].(int16))
287293
break
288294
case int32:
289-
tmp[j] = int64(b[idx].(int32))
295+
res[i] = int64(val[idx].(int32))
290296
break
291297
case float32:
292-
tmp[j] = int64(b[idx].(float32))
298+
res[i] = int64(val[idx].(float32))
293299
break
294300
case float64:
295-
tmp[j] = int64(b[idx].(float64))
301+
res[i] = int64(val[idx].(float64))
296302
break
297303
default:
298-
tmp[j] = b[idx].(int64)
304+
res[i] = val[idx].(int64)
299305
}
300306
}
301307
frame.Fields = append(frame.Fields,
302-
data.NewField(col.Name, nil, tmp),
308+
data.NewField(col.Name, nil, res),
303309
)
304310
break
305311
default:
306-
tmp := make([]string, len(resultData.Values))
307-
for j, b := range resultData.Values {
308-
switch b[idx].(type) {
312+
res := make([]string, len(resultData.Values))
313+
for i, val := range resultData.Values {
314+
switch val[idx].(type) {
309315
case string:
310-
tmp[j] = b[idx].(string)
316+
res[i] = val[idx].(string)
311317
break
312318
case bool:
313-
if w, ok := b[idx].(bool); ok {
314-
tmp[j] = strconv.FormatBool(w)
319+
if w, ok := val[idx].(bool); ok {
320+
res[i] = strconv.FormatBool(w)
315321
}
316322
break
317323
default:
318324

319325
}
320326
}
321327
frame.Fields = append(frame.Fields,
322-
data.NewField(col.Name, nil, tmp),
328+
data.NewField(col.Name, nil, res),
323329
)
324330
}
325331
}
@@ -332,7 +338,6 @@ func displayPQData(result LRQResult, response backend.DataResponse) backend.Data
332338
// datasource configuration page which allows users to verify that
333339
// a datasource is working as expected.
334340
func (d *DataSetDatasource) CheckHealth(_ context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
335-
// currentTime := time.Now().UnixNano()
336341
request := FacetRequest{
337342
QueryType: "facet",
338343
MaxCount: 1,

src/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/grafana/grafana/master/docs/sources/developers/plugins/plugin.schema.json",
33
"type": "datasource",
4-
"name": "dataset-datasource",
4+
"name": "Dataset",
55
"id": "sentinelone-dataset-datasource",
66
"metrics": true,
77
"backend": true,

0 commit comments

Comments
 (0)