query.editor.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <query-editor-row query-ctrl="ctrl" has-text-edit-mode="true">
  2. <div ng-if="ctrl.target.rawQuery">
  3. <div class="gf-form-inline">
  4. <div class="gf-form gf-form--grow">
  5. <code-editor content="ctrl.target.rawSql" datasource="ctrl.datasource" on-change="ctrl.panelCtrl.refresh()" data-mode="sql" textarea-label="Query Editor">
  6. </code-editor>
  7. </div>
  8. </div>
  9. </div>
  10. <div ng-if="!ctrl.target.rawQuery">
  11. <div class="gf-form-inline">
  12. <div class="gf-form">
  13. <label class="gf-form-label query-keyword width-6">FROM</label>
  14. <metric-segment segment="ctrl.tableSegment" get-options="ctrl.getTableSegments()" on-change="ctrl.tableChanged()"></metric-segment>
  15. <label class="gf-form-label query-keyword width-7">Time column</label>
  16. <metric-segment segment="ctrl.timeColumnSegment" get-options="ctrl.getTimeColumnSegments()" on-change="ctrl.timeColumnChanged()"></metric-segment>
  17. <label class="gf-form-label query-keyword width-9">
  18. Metric column
  19. <info-popover mode="right-normal">Column to be used as metric name for the value column.</info-popover>
  20. </label>
  21. <metric-segment segment="ctrl.metricColumnSegment" get-options="ctrl.getMetricColumnSegments()" on-change="ctrl.metricColumnChanged()"></metric-segment>
  22. </div>
  23. <div class="gf-form gf-form--grow">
  24. <div class="gf-form-label gf-form-label--grow"></div>
  25. </div>
  26. </div>
  27. <div class="gf-form-inline" ng-repeat="selectParts in ctrl.selectParts">
  28. <div class="gf-form">
  29. <label class="gf-form-label query-keyword width-6">
  30. <span ng-show="$index === 0">SELECT</span>&nbsp;
  31. </label>
  32. </div>
  33. <div class="gf-form" ng-repeat="part in selectParts">
  34. <sql-part-editor class="gf-form-label sql-part" part="part" handle-event="ctrl.handleSelectPartEvent(selectParts, part, $event)">
  35. </sql-part-editor>
  36. </div>
  37. <div class="gf-form">
  38. <label class="dropdown"
  39. dropdown-typeahead2="ctrl.selectMenu"
  40. dropdown-typeahead-on-select="ctrl.addSelectPart(selectParts, $item, $subItem)"
  41. button-template-class="gf-form-label query-part"
  42. >
  43. </label>
  44. </div>
  45. <div class="gf-form gf-form--grow">
  46. <div class="gf-form-label gf-form-label--grow"></div>
  47. </div>
  48. </div>
  49. <div class="gf-form-inline">
  50. <div class="gf-form">
  51. <label class="gf-form-label query-keyword width-6">WHERE</label>
  52. </div>
  53. <div class="gf-form" ng-repeat="part in ctrl.whereParts">
  54. <sql-part-editor class="gf-form-label sql-part" part="part" handle-event="ctrl.handleWherePartEvent(ctrl.whereParts, part, $event, $index)">
  55. </sql-part-editor>
  56. </div>
  57. <div class="gf-form">
  58. <metric-segment segment="ctrl.whereAdd" get-options="ctrl.getWhereOptions()" on-change="ctrl.addWhereAction(part, $index)"></metric-segment>
  59. </div>
  60. <div class="gf-form gf-form--grow">
  61. <div class="gf-form-label gf-form-label--grow"></div>
  62. </div>
  63. </div>
  64. <div class="gf-form-inline">
  65. <div class="gf-form">
  66. <label class="gf-form-label query-keyword width-6">
  67. <span>GROUP BY</span>
  68. </label>
  69. <sql-part-editor ng-repeat="part in ctrl.groupParts"
  70. part="part" class="gf-form-label sql-part"
  71. handle-event="ctrl.handleGroupPartEvent(part, $index, $event)">
  72. </sql-part-editor>
  73. </div>
  74. <div class="gf-form">
  75. <metric-segment segment="ctrl.groupAdd" get-options="ctrl.getGroupOptions()" on-change="ctrl.addGroupAction(part, $index)"></metric-segment>
  76. </div>
  77. <div class="gf-form gf-form--grow">
  78. <div class="gf-form-label gf-form-label--grow"></div>
  79. </div>
  80. </div>
  81. </div>
  82. <div class="gf-form-inline">
  83. <div class="gf-form">
  84. <label class="gf-form-label query-keyword" for="format-select-{{ ctrl.target.refId }}">Format As</label>
  85. <div class="gf-form-select-wrapper">
  86. <select id="format-select-{{ ctrl.target.refId }}" class="gf-form-input gf-size-auto" ng-model="ctrl.target.format" ng-options="f.value as f.text for f in ctrl.formats" ng-change="ctrl.refresh()"></select>
  87. </div>
  88. </div>
  89. <div class="gf-form">
  90. <label class="gf-form-label query-keyword pointer" ng-click="ctrl.toggleEditorMode()" ng-show="ctrl.panelCtrl.panel.type !== 'table'">
  91. <span ng-show="ctrl.target.rawQuery">Query Builder</span>
  92. <span ng-hide="ctrl.target.rawQuery">Edit SQL</span>
  93. </label>
  94. </div>
  95. <div class="gf-form">
  96. <label class="gf-form-label query-keyword pointer" ng-click="ctrl.showHelp = !ctrl.showHelp">
  97. Show Help
  98. <icon name="'angle-down'" ng-show="ctrl.showHelp" style="margin-top: 3px;"></icon>
  99. <icon name="'angle-right'" ng-hide="ctrl.showHelp" style="margin-top: 3px;"></icon>
  100. </label>
  101. </div>
  102. <div class="gf-form" ng-show="ctrl.lastQueryMeta">
  103. <label class="gf-form-label query-keyword pointer" ng-click="ctrl.showLastQuerySQL = !ctrl.showLastQuerySQL">
  104. Generated SQL
  105. <icon name="'angle-down'" ng-show="ctrl.showLastQuerySQL" style="margin-top: 3px;"></icon>
  106. <icon name="'angle-right'" ng-hide="ctrl.showLastQuerySQL" style="margin-top: 3px;"></icon>
  107. </label>
  108. </div>
  109. <div class="gf-form gf-form--grow">
  110. <div class="gf-form-label gf-form-label--grow"></div>
  111. </div>
  112. </div>
  113. <div class="gf-form" ng-show="ctrl.showHelp">
  114. <pre class="gf-form-pre alert alert-info">Time series:
  115. - return column named time or time_sec (in UTC), as a unix time stamp or any sql native date data type. You can use the macros below.
  116. - return column(s) with numeric datatype as values
  117. Optional:
  118. - return column named <i>metric</i> to represent the series name.
  119. - If multiple value columns are returned the metric column is used as prefix.
  120. - If no column named metric is found the column name of the value column is used as series name
  121. Resultsets of time series queries need to be sorted by time.
  122. Table:
  123. - return any set of columns
  124. Macros:
  125. - $__time(column) -&gt; UNIX_TIMESTAMP(column) as time_sec
  126. - $__timeEpoch(column) -&gt; UNIX_TIMESTAMP(column) as time_sec
  127. - $__timeFilter(column) -&gt; column BETWEEN FROM_UNIXTIME(1492750877) AND FROM_UNIXTIME(1492750877)
  128. - $__unixEpochFilter(column) -&gt; time_unix_epoch &gt; 1492750877 AND time_unix_epoch &lt; 1492750877
  129. - $__unixEpochNanoFilter(column) -&gt; column &gt;= 1494410783152415214 AND column &lt;= 1494497183142514872
  130. - $__timeGroup(column,'5m'[, fillvalue]) -&gt; cast(cast(UNIX_TIMESTAMP(column)/(300) as signed)*300 as signed)
  131. by setting fillvalue grafana will fill in missing values according to the interval
  132. fillvalue can be either a literal value, NULL or previous; previous will fill in the previous seen value or NULL if none has been seen yet
  133. - $__timeGroupAlias(column,'5m') -&gt; cast(cast(UNIX_TIMESTAMP(column)/(300) as signed)*300 as signed) AS "time"
  134. - $__unixEpochGroup(column,'5m') -&gt; column DIV 300 * 300
  135. - $__unixEpochGroupAlias(column,'5m') -&gt; column DIV 300 * 300 AS "time"
  136. Example of group by and order by with $__timeGroup:
  137. SELECT
  138. $__timeGroupAlias(timestamp_col, '1h'),
  139. sum(value_double) as value
  140. FROM yourtable
  141. GROUP BY 1
  142. ORDER BY 1
  143. Or build your own conditionals using these macros which just return the values:
  144. - $__timeFrom() -&gt; FROM_UNIXTIME(1492750877)
  145. - $__timeTo() -&gt; FROM_UNIXTIME(1492750877)
  146. - $__unixEpochFrom() -&gt; 1492750877
  147. - $__unixEpochTo() -&gt; 1492750877
  148. - $__unixEpochNanoFrom() -&gt; 1494410783152415214
  149. - $__unixEpochNanoTo() -&gt; 1494497183142514872
  150. </pre>
  151. </div>
  152. </div>
  153. <div class="gf-form" ng-show="ctrl.showLastQuerySQL">
  154. <pre class="gf-form-pre">{{ctrl.lastQueryMeta.executedQueryString}}</pre>
  155. </div>
  156. <div class="gf-form" ng-show="ctrl.lastQueryError">
  157. <pre class="gf-form-pre alert alert-error">{{ctrl.lastQueryError}}</pre>
  158. </div>
  159. </query-editor-row>