diff --git a/tscli/main.go b/tscli/main.go index 268db3c..61ebc2c 100644 --- a/tscli/main.go +++ b/tscli/main.go @@ -120,7 +120,7 @@ func executeQuery(cmd *cobra.Command, args []string) error { tsi := GoogleCloudMonitoringClient.ListTimeSeries(context.Background(), request) // todo: handle multiple time series - plot, err := tsplot.NewPlotFromTimeSeriesIterator(tsi, "classification") + plot, err := tsplot.NewPlotFromTimeSeriesIterator(tsi, "classification", tsplot.WithXTimeTicks(time.Kitchen), tsplot.WithTitle("decodes"), tsplot.WithXAxisName("UTC")) if err != nil { log.Fatal(err) } diff --git a/tsplot/color_palettes.go b/tsplot/color_palettes.go index a002407..ce09289 100644 --- a/tsplot/color_palettes.go +++ b/tsplot/color_palettes.go @@ -8,8 +8,21 @@ import ( var usedColors = make(map[string]color.RGBA) +// simple colors (subset of golang.org/x/image/colornames) +var availableColors = map[string]color.RGBA{ + "blue": color.RGBA{0x00, 0x00, 0xff, 0xff}, // rgb(0, 0, 255) + "brown": color.RGBA{0xa5, 0x2a, 0x2a, 0xff}, // rgb(165, 42, 42) + "orange": color.RGBA{0xff, 0xa5, 0x00, 0xff}, // rgb(255, 165, 0) + "hotpink": color.RGBA{0xff, 0x69, 0xb4, 0xff}, // rgb(255, 105, 180) + "red": color.RGBA{0xff, 0x00, 0x00, 0xff}, // rgb(255, 0, 0) + "purple": color.RGBA{0x80, 0x00, 0x80, 0xff}, // rgb(128, 0, 128) + "yellow": color.RGBA{0xff, 0xff, 0x00, 0xff}, // rgb(255, 255, 0) + "green": color.RGBA{0x00, 0x80, 0x00, 0xff}, // rgb(0, 128, 0) + +} + func getUnusedColor() color.RGBA { - for k, v := range colornames.Map { + for k, v := range availableColors { if _, ok := usedColors[k]; !ok { usedColors[k] = v return v diff --git a/tsplot/plot.go b/tsplot/plot.go index 4223b03..eae5cec 100644 --- a/tsplot/plot.go +++ b/tsplot/plot.go @@ -1,8 +1,9 @@ package tsplot import ( - monitoring "cloud.google.com/go/monitoring/apiv3/v2" "fmt" + + monitoring "cloud.google.com/go/monitoring/apiv3/v2" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "google.golang.org/api/iterator" @@ -72,7 +73,7 @@ func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*pl return p, nil } -func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, groupByLabel string, opts ...PlotOption) (*plot.Plot, error) { +func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, legendKey string, opts ...PlotOption) (*plot.Plot, error) { p := plot.New() for { timeSeries, err := tsi.Next() @@ -91,7 +92,7 @@ func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, groupByLa // add to legend legendEntry, _ := plotter.NewPolygon() legendEntry.Color = lineColor - p.Legend.Add(timeSeries.GetMetric().GetLabels()[groupByLabel], legendEntry) + p.Legend.Add(timeSeries.GetMetric().GetLabels()[legendKey], legendEntry) p.Y.Max = 400 // todo: make dynamic according to data } @@ -108,7 +109,7 @@ func createLine(dataPoints []*monitoringpb.Point) (*plotter.Line, error) { var XYs plotter.XYs for _, point := range dataPoints { x := point.GetInterval().GetEndTime().GetSeconds() - y := point.GetValue().GetDoubleValue() // todo: This breaks if the valuetype is an int64 + y := point.GetValue().GetDoubleValue() // todo: This breaks if the value type is an int64 XYs = append(XYs, plotter.XY{ X: float64(x), Y: y,