Skip to content

Commit

Permalink
functions to find max value of gapi nonsense types
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman committed Dec 12, 2023
1 parent 4477066 commit ca4ce3e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
3 changes: 2 additions & 1 deletion tscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ func executeQuery(cmd *cobra.Command, args []string) error {
log.Fatal(err)
}

plot, err := tsplot.NewPlotFromTimeSeries(ts)
plot, err := tsplot.NewPlotFromTimeSeries(ts, tsplot.WithXTimeTicks(time.Kitchen), tsplot.WithXAxisName("UTC"), tsplot.WithTitle("decodes_raw"))
if err != nil {
log.Fatal(err)
}

saveFile := fmt.Sprintf("%s/test.png", "/tmp/")
return plot.Save(8*vg.Inch, 4*vg.Inch, saveFile)
}
Expand Down
9 changes: 9 additions & 0 deletions tsplot/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ func WithLineFromPoints(pts []*monitoringpb.Point) PlotOption {
p.Add(line)
}
}

func WithColoredLineFromPoints(pts []*monitoringpb.Point, color color.Color) PlotOption {
return func(p *plot.Plot) {
line, _ := createLine(pts)
line.Width = vg.Points(1)
line.Color = color
p.Add(line)
}
}
53 changes: 48 additions & 5 deletions tsplot/plot.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,72 @@
package tsplot

import (
"fmt"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"google.golang.org/genproto/googleapis/api/metric"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)

func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*plot.Plot, error) {
// todo: how do we determine the type of values in the metric? float? double?
// todo: need to abstract max function.
// findMaxFromPoints finds the maximum value of a list of metric points of the GAPI MetricDescriptor Value type.
func findMaxFromPoints(gapiValueType metric.MetricDescriptor_ValueType, points []*monitoringpb.Point) (float64, error) {
var maximum float64

switch gapiValueType {
case metric.MetricDescriptor_INT64:
maximum = findMaxFromInt64Data(points)
case metric.MetricDescriptor_DOUBLE:
maximum = findMaxFromFloat64Data(points)
default:
return 0, fmt.Errorf("cannot operate on value type %q", gapiValueType.String())
}

return maximum, nil

}

// findMaxFromFloat64Data is a helper function to findMaxFromPoints but is specific to the data being of type float64.
// The other helper function findMaxFromInt64Data is also required due to the methods that which Google allows the consumer
// to fetch the data.
func findMaxFromFloat64Data(points []*monitoringpb.Point) float64 {
var maximum float64
points := ts.GetPoints()
for _, v := range points {
cur := v.GetValue().GetDoubleValue()
if cur > maximum {
maximum = cur
}
}
return maximum
}

// findMaxFromInt64Data is a helper function to findMaxFromPoints but is specific to the data being of type float64.
// The other helper function findMaxFromFloat64Data is also required due to the methods that which Google allows the consumer
// to fetch the data.
func findMaxFromInt64Data(points []*monitoringpb.Point) float64 {
var maximum int64
for _, v := range points {
cur := v.GetValue().GetInt64Value()
if cur > maximum {
maximum = cur
}
}
return float64(maximum)
}

func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*plot.Plot, error) {
points := ts.GetPoints()
YMax, err := findMaxFromPoints(ts.GetValueType(), points)
if err != nil {
return nil, err
}

opts = append(opts, WithLineFromPoints(points))
p := plot.New()
for _, opt := range opts {
opt(p)
}

p.Y.Max = maximum + 200
p.Y.Max = YMax + 200
return p, nil
}

Expand Down

0 comments on commit ca4ce3e

Please sign in to comment.