models.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "fmt"
  17. "sort"
  18. "strings"
  19. "yunion.io/x/pkg/util/sets"
  20. api "yunion.io/x/onecloud/pkg/apis/monitor"
  21. )
  22. type TsdbQuery struct {
  23. TimeRange *TimeRange
  24. Queries []*Query
  25. Debug bool
  26. }
  27. type Query struct {
  28. RefId string
  29. api.MetricQuery
  30. DataSource DataSource
  31. MaxDataPoints int64
  32. IntervalMs int64
  33. }
  34. type Response struct {
  35. Results map[string]*QueryResult `json:"results"`
  36. Message string `json:"message,omitempty"`
  37. }
  38. type QueryResult struct {
  39. Error error `json:"-"`
  40. ErrorString string `json:"error,omitempty"`
  41. RefId string `json:"ref_id"`
  42. Meta api.QueryResultMeta `json:"meta"`
  43. Series api.TimeSeriesSlice `json:"series"`
  44. Tables []*Table `json:"tables"`
  45. Dataframes [][]byte `json:"dataframes"`
  46. }
  47. func NewTimeSeries(
  48. name string,
  49. rawName string,
  50. columns []string,
  51. points api.TimeSeriesPoints,
  52. tags map[string]string,
  53. ) *api.TimeSeries {
  54. return &api.TimeSeries{
  55. RawName: rawName,
  56. Columns: columns,
  57. Name: name,
  58. Points: points,
  59. Tags: tags,
  60. }
  61. }
  62. type Table struct {
  63. Columns []TableColumn `json:"columns"`
  64. Rows []RowValues `json:"rows"`
  65. }
  66. type TableColumn struct {
  67. Text string `json:"text"`
  68. }
  69. type RowValues []interface{}
  70. func NewQueryResult() *QueryResult {
  71. return &QueryResult{
  72. Series: make(api.TimeSeriesSlice, 0),
  73. }
  74. }
  75. func FormatRawName(idx int, name string, groupByTags []string, tags map[string]string, diffTagKeys sets.String) string {
  76. // when group by tag specified
  77. /*if len(groupByTags) != 0 {
  78. for key, val := range tags {
  79. if strings.Contains(strings.Join(groupByTags, ","), key) {
  80. return val
  81. }
  82. }
  83. }*/
  84. genHint := func(k, v string) string {
  85. return fmt.Sprintf("%s=%s", k, v)
  86. }
  87. hintNames := sets.NewString()
  88. hints := sets.NewString()
  89. tagKeys := sets.NewString()
  90. for _, tagKey := range api.MEASUREMENT_TAG_KEYWORD {
  91. tagKeys.Insert(tagKey)
  92. }
  93. tagKeys = tagKeys.Union(diffTagKeys)
  94. for _, tagKey := range tagKeys.List() {
  95. if tagV, ok := tags[tagKey]; ok {
  96. gHint := genHint(tagKey, tagV)
  97. if strings.Contains(tagKey, "name") && !sets.NewString(groupByTags...).Has(tagKey) {
  98. hintNames.Insert(gHint)
  99. } else {
  100. hints.Insert(gHint)
  101. }
  102. }
  103. }
  104. if len(hints) == 0 {
  105. // try id
  106. for key, val := range tags {
  107. if strings.Contains(key, "id") && len(val) != 0 {
  108. hints.Insert(genHint(key, val))
  109. }
  110. }
  111. }
  112. if len(hints) == 0 {
  113. // if hints is empty at last, return index hint
  114. return fmt.Sprintf("unknown-%d-%s", idx, name)
  115. }
  116. sortNL := hintNames.List()
  117. sort.Strings(sortNL)
  118. sortL := hints.List()
  119. sort.Strings(sortL)
  120. sortNL = append(sortNL, sortL...)
  121. return fmt.Sprintf("{%s}", strings.Join(sortNL, ","))
  122. }