Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work on displaying metrics #115

Merged
merged 11 commits into from Sep 17, 2019

Actually draw some graphs

  • Loading branch information...
lawrencegripper committed Sep 4, 2019
commit e2accf70b538c255bca64d61f955c34c92f39d3f
1 go.mod
@@ -20,6 +20,7 @@ require (
github.com/gogo/protobuf v1.2.0 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2 // indirect
github.com/guptarohit/asciigraph v0.4.2-0.20190112130928-1bc9b2452856
github.com/hokaccha/go-prettyjson v0.0.0-20180920040306-f579f869bbfe // indirect
github.com/jroimartin/gocui v0.4.0
github.com/mailru/easyjson v0.0.0-20190221075403-6243d8e04c3f // indirect
4 go.sum
@@ -60,6 +60,10 @@ github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/guptarohit/asciigraph v0.4.1 h1:YHmCMN8VH81BIUIgTg2Fs3B52QDxNZw2RQ6j5pGoSxo=
github.com/guptarohit/asciigraph v0.4.1/go.mod h1:9fYEfE5IGJGxlP1B+w8wHFy7sNZMhPtn59f0RLtpRFM=
github.com/guptarohit/asciigraph v0.4.2-0.20190112130928-1bc9b2452856 h1:6s4PF4AtuPGnGP39pL8iU6/aOe0z4C2F1HxSHI93IFc=
github.com/guptarohit/asciigraph v0.4.2-0.20190112130928-1bc9b2452856/go.mod h1:9fYEfE5IGJGxlP1B+w8wHFy7sNZMhPtn59f0RLtpRFM=
github.com/hokaccha/go-prettyjson v0.0.0-20180920040306-f579f869bbfe h1:MCgzztuoH5LZNr9AkIaicIDvCfACu11KUCCZQnRHDC0=
github.com/hokaccha/go-prettyjson v0.0.0-20180920040306-f579f869bbfe/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
@@ -3,8 +3,10 @@ package handlers
import (
"context"
"encoding/json"
"strings"
"time"

"github.com/guptarohit/asciigraph"
"github.com/lawrencegripper/azbrowse/internal/pkg/style"

"github.com/lawrencegripper/azbrowse/pkg/armclient"
@@ -61,12 +63,13 @@ func (e *MetricsExpander) Expand(ctx context.Context, currentItem *TreeNode) Exp
Display: metric.Name.Value + "\n " + style.Subtle("Unit: "+metric.Unit),
ID: currentItem.ID,
Parentid: currentItem.ID,
ExpandURL: currentItem.ID + "/providers/microsoft.Insights/metrics?timespan=" + time.Now().AddDate(0, 0, -30).Format("2006-01-02T15:04:05.000Z") + "/" + time.Now().Format("2006-01-02T15:04:05.000Z") + "&interval=PT5M&metricnames=" + metric.Name.Value + "&aggregation=" + metric.PrimaryAggregationType + "&metricNamespace=" + metric.Namespace + "&autoadjusttimegrain=true&validatedimensions=false&api-version=2018-01-01",
ExpandURL: currentItem.ID + "/providers/microsoft.Insights/metrics?timespan=" + time.Now().Add(-3*time.Hour).Format("2006-01-02T15:04:05.000Z") + "/" + time.Now().Format("2006-01-02T15:04:05.000Z") + "&interval=PT5M&metricnames=" + metric.Name.Value + "&aggregation=" + metric.PrimaryAggregationType + "&metricNamespace=" + metric.Namespace + "&autoadjusttimegrain=true&validatedimensions=false&api-version=2018-01-01",
ItemType: "metrics.graph",
SubscriptionID: currentItem.SubscriptionID,
Metadata: map[string]string{
"SuppressSwaggerExpand": "true",
"SuppressGenericExpand": "true",
"AggregationType": strings.ToLower(metric.PrimaryAggregationType),
},
})
}
@@ -89,8 +92,22 @@ func (e *MetricsExpander) Expand(ctx context.Context, currentItem *TreeNode) Exp
}
}

var metricResponse armclient.MetricResponse
err = json.Unmarshal([]byte(data), &metricResponse)
if err != nil {
panic(err)
}

graphData := []float64{}
for _, datapoint := range metricResponse.Value[0].Timeseries[0].Data {
value := datapoint[currentItem.Metadata["AggregationType"]].(float64)
graphData = append(graphData, value)
}

graph := asciigraph.Plot(graphData)

return ExpanderResult{
Response: data,
Response: graph,
IsPrimaryResponse: true,
SourceDescription: "MetricsExpander build graph",
}
@@ -37,3 +37,31 @@ type MetricsListResponse struct {
} `json:"dimensions"`
} `json:"value"`
}

// MetricResponse https://docs.microsoft.com/en-us/rest/api/monitor/metrics/list
type MetricResponse struct {
Cost int `json:"cost"`
Timespan string `json:"timespan"`
Interval string `json:"interval"`
Namespace string `json:"namespace"`
Resourceregion string `json:"resourceregion"`
Value []struct {
ID string `json:"id"`
Type string `json:"type"`
Name struct {
Value string `json:"value"`
LocalizedValue string `json:"localizedValue"`
} `json:"name"`
Unit string `json:"unit"`
Timeseries []struct {
Metadatavalues []struct {
Name struct {
Value string `json:"value"`
LocalizedValue string `json:"localizedValue"`
} `json:"name"`
Value string `json:"value"`
} `json:"metadatavalues"`
Data []map[string]interface{} `json:"data"`
} `json:"timeseries"`
} `json:"value"`
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.