table.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. // Package query defined types for representing flux query result
  5. package query
  6. import (
  7. "fmt"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. // FluxTableMetadata holds flux query result table information represented by collection of columns.
  13. // Each new table is introduced by annotations
  14. type FluxTableMetadata struct {
  15. position int
  16. columns []*FluxColumn
  17. }
  18. // FluxColumn holds flux query table column properties
  19. type FluxColumn struct {
  20. index int
  21. name string
  22. dataType string
  23. group bool
  24. defaultValue string
  25. }
  26. // FluxRecord represents row in the flux query result table
  27. type FluxRecord struct {
  28. table int
  29. values map[string]interface{}
  30. }
  31. // NewFluxTableMetadata creates FluxTableMetadata for the table on position
  32. func NewFluxTableMetadata(position int) *FluxTableMetadata {
  33. return NewFluxTableMetadataFull(position, make([]*FluxColumn, 0, 10))
  34. }
  35. // NewFluxTableMetadataFull creates FluxTableMetadata
  36. func NewFluxTableMetadataFull(position int, columns []*FluxColumn) *FluxTableMetadata {
  37. return &FluxTableMetadata{position: position, columns: columns}
  38. }
  39. // Position returns position of the table in the flux query result
  40. func (f *FluxTableMetadata) Position() int {
  41. return f.position
  42. }
  43. // Columns returns slice of flux query result table
  44. func (f *FluxTableMetadata) Columns() []*FluxColumn {
  45. return f.columns
  46. }
  47. // AddColumn adds column definition to table metadata
  48. func (f *FluxTableMetadata) AddColumn(column *FluxColumn) *FluxTableMetadata {
  49. f.columns = append(f.columns, column)
  50. return f
  51. }
  52. // Column returns flux table column by index.
  53. // Returns nil if index is out of the bounds.
  54. func (f *FluxTableMetadata) Column(index int) *FluxColumn {
  55. if len(f.columns) == 0 || index < 0 || index >= len(f.columns) {
  56. return nil
  57. }
  58. return f.columns[index]
  59. }
  60. // String returns FluxTableMetadata string dump
  61. func (f *FluxTableMetadata) String() string {
  62. var buffer strings.Builder
  63. for i, c := range f.columns {
  64. if i > 0 {
  65. buffer.WriteString(",")
  66. }
  67. buffer.WriteString("col")
  68. buffer.WriteString(c.String())
  69. }
  70. return buffer.String()
  71. }
  72. // NewFluxColumn creates FluxColumn for position
  73. func NewFluxColumn(index int) *FluxColumn {
  74. return &FluxColumn{index: index}
  75. }
  76. // NewFluxColumnFull creates FluxColumn
  77. func NewFluxColumnFull(dataType string, defaultValue string, name string, group bool, index int) *FluxColumn {
  78. return &FluxColumn{index: index, name: name, dataType: dataType, group: group, defaultValue: defaultValue}
  79. }
  80. // SetDefaultValue sets default value for the column
  81. func (f *FluxColumn) SetDefaultValue(defaultValue string) {
  82. f.defaultValue = defaultValue
  83. }
  84. // SetGroup set group flag for the column
  85. func (f *FluxColumn) SetGroup(group bool) {
  86. f.group = group
  87. }
  88. // SetDataType sets data type for the column
  89. func (f *FluxColumn) SetDataType(dataType string) {
  90. f.dataType = dataType
  91. }
  92. // SetName sets name of the column
  93. func (f *FluxColumn) SetName(name string) {
  94. f.name = name
  95. }
  96. // DefaultValue returns default value of the column
  97. func (f *FluxColumn) DefaultValue() string {
  98. return f.defaultValue
  99. }
  100. // IsGroup return true if the column is grouping column
  101. func (f *FluxColumn) IsGroup() bool {
  102. return f.group
  103. }
  104. // DataType returns data type of the column
  105. func (f *FluxColumn) DataType() string {
  106. return f.dataType
  107. }
  108. // Name returns name of the column
  109. func (f *FluxColumn) Name() string {
  110. return f.name
  111. }
  112. // Index returns index of the column
  113. func (f *FluxColumn) Index() int {
  114. return f.index
  115. }
  116. // String returns FluxColumn string dump
  117. func (f *FluxColumn) String() string {
  118. return fmt.Sprintf("{%d: name: %s, datatype: %s, defaultValue: %s, group: %v}", f.index, f.name, f.dataType, f.defaultValue, f.group)
  119. }
  120. // NewFluxRecord returns new record for the table with values
  121. func NewFluxRecord(table int, values map[string]interface{}) *FluxRecord {
  122. return &FluxRecord{table: table, values: values}
  123. }
  124. // Table returns value of the table column
  125. // It returns zero if the table column is not found
  126. func (r *FluxRecord) Table() int {
  127. return int(intValue(r.values, "table"))
  128. }
  129. // Start returns the inclusive lower time bound of all records in the current table.
  130. // Returns empty time.Time if there is no column "_start".
  131. func (r *FluxRecord) Start() time.Time {
  132. return timeValue(r.values, "_start")
  133. }
  134. // Stop returns the exclusive upper time bound of all records in the current table.
  135. // Returns empty time.Time if there is no column "_stop".
  136. func (r *FluxRecord) Stop() time.Time {
  137. return timeValue(r.values, "_stop")
  138. }
  139. // Time returns the time of the record.
  140. // Returns empty time.Time if there is no column "_time".
  141. func (r *FluxRecord) Time() time.Time {
  142. return timeValue(r.values, "_time")
  143. }
  144. // Value returns the default _value column value or nil if not present
  145. func (r *FluxRecord) Value() interface{} {
  146. return r.ValueByKey("_value")
  147. }
  148. // Field returns the field name.
  149. // Returns empty string if there is no column "_field".
  150. func (r *FluxRecord) Field() string {
  151. return stringValue(r.values, "_field")
  152. }
  153. // Result returns the value of the _result column, which represents result name.
  154. // Returns empty string if there is no column "result".
  155. func (r *FluxRecord) Result() string {
  156. return stringValue(r.values, "result")
  157. }
  158. // Measurement returns the measurement name of the record
  159. // Returns empty string if there is no column "_measurement".
  160. func (r *FluxRecord) Measurement() string {
  161. return stringValue(r.values, "_measurement")
  162. }
  163. // Values returns map of the values where key is the column name
  164. func (r *FluxRecord) Values() map[string]interface{} {
  165. return r.values
  166. }
  167. // ValueByKey returns value for given column key for the record or nil of result has no value the column key
  168. func (r *FluxRecord) ValueByKey(key string) interface{} {
  169. return r.values[key]
  170. }
  171. // String returns FluxRecord string dump
  172. func (r *FluxRecord) String() string {
  173. if len(r.values) == 0 {
  174. return ""
  175. }
  176. i := 0
  177. keys := make([]string, len(r.values))
  178. for k := range r.values {
  179. keys[i] = k
  180. i++
  181. }
  182. sort.Strings(keys)
  183. var buffer strings.Builder
  184. buffer.WriteString(fmt.Sprintf("%s:%v", keys[0], r.values[keys[0]]))
  185. for _, k := range keys[1:] {
  186. buffer.WriteString(",")
  187. buffer.WriteString(fmt.Sprintf("%s:%v", k, r.values[k]))
  188. }
  189. return buffer.String()
  190. }
  191. // timeValue returns time.Time value from values map according to the key
  192. // Empty time.Time value is returned if key is not found
  193. func timeValue(values map[string]interface{}, key string) time.Time {
  194. if val, ok := values[key]; ok {
  195. if t, ok := val.(time.Time); ok {
  196. return t
  197. }
  198. }
  199. return time.Time{}
  200. }
  201. // stringValue returns string value from values map according to the key
  202. // Empty string is returned if key is not found
  203. func stringValue(values map[string]interface{}, key string) string {
  204. if val, ok := values[key]; ok {
  205. if s, ok := val.(string); ok {
  206. return s
  207. }
  208. }
  209. return ""
  210. }
  211. // intValue returns int64 value from values map according to the key
  212. // Zero value is returned if key is not found
  213. func intValue(values map[string]interface{}, key string) int64 {
  214. if val, ok := values[key]; ok {
  215. if i, ok := val.(int64); ok {
  216. return i
  217. }
  218. }
  219. return 0
  220. }