query_endpoint.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tsdb
  15. import (
  16. "context"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/apis/monitor"
  19. )
  20. type TsdbQueryEndpoint interface {
  21. Query(ctx context.Context, ds *DataSource, query *TsdbQuery) (*Response, error)
  22. FilterMeasurement(ctx context.Context, ds *DataSource, from, to string, ms *monitor.InfluxMeasurement, tagFilter *monitor.MetricQueryTag) (*monitor.InfluxMeasurement, error)
  23. FillSelect(query *monitor.AlertQuery, isAlert bool) *monitor.AlertQuery
  24. FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string, isAlert bool) *monitor.AlertQuery
  25. }
  26. var registry map[string]GetTsdbQueryEndpointFn
  27. type GetTsdbQueryEndpointFn func(dsInfo *DataSource) (TsdbQueryEndpoint, error)
  28. func init() {
  29. registry = make(map[string]GetTsdbQueryEndpointFn)
  30. }
  31. var (
  32. ErrorNotFoundExecutorDataSource error = errors.Error("Not find executor for data source")
  33. )
  34. func getDataSourceFunc(dsType string) (GetTsdbQueryEndpointFn, error) {
  35. fn, exists := registry[dsType]
  36. if !exists {
  37. return nil, errors.Wrapf(ErrorNotFoundExecutorDataSource, "type: %s", dsType)
  38. }
  39. return fn, nil
  40. }
  41. func GetTsdbQueryEndpointFor(dsInfo *DataSource) (TsdbQueryEndpoint, error) {
  42. fn, err := getDataSourceFunc(dsInfo.Type)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "getDataSourceFunc")
  45. }
  46. executor, err := fn(dsInfo)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "construct datasource query endpoint")
  49. }
  50. return executor, nil
  51. }
  52. func IsValidDataSource(dsType string) error {
  53. _, err := getDataSourceFunc(dsType)
  54. return err
  55. }
  56. func RegisterTsdbQueryEndpoint(dataSourceType string, fn GetTsdbQueryEndpointFn) {
  57. registry[dataSourceType] = fn
  58. }