query_parser.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 influxdb
  15. import (
  16. "time"
  17. api "yunion.io/x/onecloud/pkg/apis/monitor"
  18. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  19. )
  20. type InfluxdbQueryParser struct{}
  21. func (qp *InfluxdbQueryParser) Parse(model *tsdb.Query, dsInfo *tsdb.DataSource) (*Query, error) {
  22. policy := "default"
  23. if model.Policy != "" {
  24. policy = model.Policy
  25. }
  26. alias := model.Alias
  27. tz := model.Tz
  28. measurement := model.Measurement
  29. resultFormat := model.ResultFormat
  30. tags := model.Tags
  31. groupBys, err := qp.parseGroupBy(model.GroupBy)
  32. if err != nil {
  33. return nil, err
  34. }
  35. selects, err := qp.parseSelects(model.Selects)
  36. if err != nil {
  37. return nil, err
  38. }
  39. parsedInterval, err := tsdb.GetIntervalFrom(dsInfo, model, time.Millisecond*1)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &Query{
  44. Measurement: measurement,
  45. Policy: policy,
  46. ResultFormat: resultFormat,
  47. GroupBy: groupBys,
  48. Tags: tags,
  49. Selects: selects,
  50. Interval: parsedInterval,
  51. Alias: alias,
  52. Tz: tz,
  53. }, nil
  54. }
  55. func (qp *InfluxdbQueryParser) parseSelects(selects []api.MetricQuerySelect) ([]*Select, error) {
  56. var result []*Select
  57. for _, selectObj := range selects {
  58. var parts Select
  59. for _, part := range selectObj {
  60. queryPart, err := qp.parseQueryPart(part)
  61. if err != nil {
  62. return nil, err
  63. }
  64. parts = append(parts, *queryPart)
  65. }
  66. result = append(result, &parts)
  67. }
  68. return result, nil
  69. }
  70. func (qp *InfluxdbQueryParser) parseGroupBy(groupBy []api.MetricQueryPart) ([]*QueryPart, error) {
  71. var result []*QueryPart
  72. for _, gb := range groupBy {
  73. queryPart, err := qp.parseQueryPart(gb)
  74. if err != nil {
  75. return nil, err
  76. }
  77. result = append(result, queryPart)
  78. }
  79. return result, nil
  80. }
  81. func (qp *InfluxdbQueryParser) parseQueryPart(part api.MetricQueryPart) (*QueryPart, error) {
  82. return NewQueryPart(part.Type, part.Params)
  83. }