|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "time" |
| 13 | + |
| 14 | + simplejson "github.com/bitly/go-simplejson" |
| 15 | + "github.com/grafana/grafana-plugin-model/go/datasource" |
| 16 | + hclog "github.com/hashicorp/go-hclog" |
| 17 | + plugin "github.com/hashicorp/go-plugin" |
| 18 | +) |
| 19 | + |
| 20 | +// StravaPlugin implements the Grafana backend interface and forwards queries to the StravaDatasource |
| 21 | +type StravaPlugin struct { |
| 22 | + plugin.NetRPCUnsupportedPlugin |
| 23 | + logger hclog.Logger |
| 24 | + datasourceCache *Cache |
| 25 | +} |
| 26 | + |
| 27 | +// ZabbixDatasource stores state about a specific datasource and provides methods to make |
| 28 | +// requests to the Zabbix API |
| 29 | +type StravaDatasource struct { |
| 30 | + url *url.URL |
| 31 | + authToken string |
| 32 | + dsInfo *datasource.DatasourceInfo |
| 33 | + queryCache *Cache |
| 34 | + logger hclog.Logger |
| 35 | + httpClient *http.Client |
| 36 | +} |
| 37 | + |
| 38 | +// newZabbixDatasource returns an initialized ZabbixDatasource |
| 39 | +func newStravaDatasource(dsInfo *datasource.DatasourceInfo) (*StravaDatasource, error) { |
| 40 | + zabbixURLStr := dsInfo.GetUrl() |
| 41 | + zabbixURL, err := url.Parse(zabbixURLStr) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + return &StravaDatasource{ |
| 47 | + url: zabbixURL, |
| 48 | + dsInfo: dsInfo, |
| 49 | + queryCache: NewCache(10*time.Minute, 10*time.Minute), |
| 50 | + httpClient: &http.Client{ |
| 51 | + Transport: &http.Transport{ |
| 52 | + TLSClientConfig: &tls.Config{ |
| 53 | + Renegotiation: tls.RenegotiateFreelyAsClient, |
| 54 | + }, |
| 55 | + Proxy: http.ProxyFromEnvironment, |
| 56 | + Dial: (&net.Dialer{ |
| 57 | + Timeout: 30 * time.Second, |
| 58 | + KeepAlive: 30 * time.Second, |
| 59 | + }).Dial, |
| 60 | + TLSHandshakeTimeout: 10 * time.Second, |
| 61 | + ExpectContinueTimeout: 1 * time.Second, |
| 62 | + MaxIdleConns: 100, |
| 63 | + IdleConnTimeout: 90 * time.Second, |
| 64 | + }, |
| 65 | + Timeout: time.Duration(time.Second * 30), |
| 66 | + }, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (p *StravaPlugin) NewStravaDatasource(dsInfo *datasource.DatasourceInfo) (*StravaDatasource, error) { |
| 71 | + ds, err := newStravaDatasource(dsInfo) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + ds.logger = p.logger |
| 77 | + return ds, nil |
| 78 | +} |
| 79 | + |
| 80 | +// Query receives requests from the Grafana backend. Requests are filtered by query type and sent to the |
| 81 | +// applicable StravaDatasource. |
| 82 | +func (p *StravaPlugin) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (resp *datasource.DatasourceResponse, err error) { |
| 83 | + StravaDS, err := p.GetDatasource(tsdbReq) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + queryType, err := GetQueryType(tsdbReq) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + switch queryType { |
| 94 | + case "StravaAPI": |
| 95 | + resp, err = StravaDS.StravaAPIQuery(ctx, tsdbReq) |
| 96 | + default: |
| 97 | + err = errors.New("Query not implemented") |
| 98 | + return BuildErrorResponse(err), nil |
| 99 | + } |
| 100 | + |
| 101 | + return |
| 102 | +} |
| 103 | + |
| 104 | +func (ds *StravaDatasource) StravaAPIQuery(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) { |
| 105 | + response := &datasource.DatasourceResponse{ |
| 106 | + Results: []*datasource.QueryResult{ |
| 107 | + &datasource.QueryResult{ |
| 108 | + RefId: "stravaAPI", |
| 109 | + MetaJson: "", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + return response, nil |
| 114 | +} |
| 115 | + |
| 116 | +// GetDatasource Returns cached datasource or creates new one |
| 117 | +func (p *StravaPlugin) GetDatasource(tsdbReq *datasource.DatasourceRequest) (*StravaDatasource, error) { |
| 118 | + dsInfoHash := HashDatasourceInfo(tsdbReq.GetDatasource()) |
| 119 | + |
| 120 | + if cachedData, ok := p.datasourceCache.Get(dsInfoHash); ok { |
| 121 | + if cachedDS, ok := cachedData.(*StravaDatasource); ok { |
| 122 | + return cachedDS, nil |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + dsInfo := tsdbReq.GetDatasource() |
| 127 | + if p.logger.IsDebug() { |
| 128 | + p.logger.Debug(fmt.Sprintf("Datasource cache miss (Org %d Id %d '%s' %s)", dsInfo.GetOrgId(), dsInfo.GetId(), dsInfo.GetName(), dsInfoHash)) |
| 129 | + } |
| 130 | + |
| 131 | + ds, err := p.NewStravaDatasource(dsInfo) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + p.datasourceCache.Set(dsInfoHash, ds) |
| 137 | + return ds, nil |
| 138 | +} |
| 139 | + |
| 140 | +// GetQueryType determines the query type from a query or list of queries |
| 141 | +func GetQueryType(tsdbReq *datasource.DatasourceRequest) (string, error) { |
| 142 | + queryType := "query" |
| 143 | + if len(tsdbReq.Queries) > 0 { |
| 144 | + firstQuery := tsdbReq.Queries[0] |
| 145 | + queryJSON, err := simplejson.NewJson([]byte(firstQuery.ModelJson)) |
| 146 | + if err != nil { |
| 147 | + return "", err |
| 148 | + } |
| 149 | + queryType = queryJSON.Get("queryType").MustString("query") |
| 150 | + } |
| 151 | + return queryType, nil |
| 152 | +} |
| 153 | + |
| 154 | +// BuildResponse transforms a Strava API response to a DatasourceResponse |
| 155 | +func BuildResponse(responseData interface{}) (*datasource.DatasourceResponse, error) { |
| 156 | + jsonBytes, err := json.Marshal(responseData) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + return &datasource.DatasourceResponse{ |
| 162 | + Results: []*datasource.QueryResult{ |
| 163 | + &datasource.QueryResult{ |
| 164 | + RefId: "StravaAPI", |
| 165 | + MetaJson: string(jsonBytes), |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, nil |
| 169 | +} |
| 170 | + |
| 171 | +// BuildErrorResponse creates a QueryResult that forwards an error to the front-end |
| 172 | +func BuildErrorResponse(err error) *datasource.DatasourceResponse { |
| 173 | + return &datasource.DatasourceResponse{ |
| 174 | + Results: []*datasource.QueryResult{ |
| 175 | + &datasource.QueryResult{ |
| 176 | + RefId: "StravaAPI", |
| 177 | + Error: err.Error(), |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | +} |
0 commit comments