_form.html.erb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <% if @query.errors.any? %>
  2. <div class="alert alert-danger"><%= @query.errors.full_messages.first %></div>
  3. <% end %>
  4. <%= form_for @query, url: (@query.persisted? ? query_path(@query, variable_params) : queries_path(variable_params)), html: {class: "the_form", autocomplete: "off"} do |f| %>
  5. <div class="row">
  6. <div class="col-xs-8">
  7. <div class= "form-group">
  8. <%= f.hidden_field :statement %>
  9. <div id="editor-container">
  10. <div id="editor"><%= @query.statement %></div>
  11. </div>
  12. </div>
  13. <div class="form-group text-right">
  14. <div class="pull-left" style="margin-top: 6px;">
  15. <%= link_to "Back", :back %>
  16. <% if Blazer.data_sources.size == 1 %>
  17. <span class="text-muted" style="margin-left: 20px;"> Use {start_time} and {end_time} for time ranges</span>
  18. <% end %>
  19. </div>
  20. <%= f.select :data_source, Blazer.data_sources.values.map { |ds| [ds.name, ds.id] }, {}, class: ("hide" if Blazer.data_sources.size == 1), style: "width: 140px;" %>
  21. <div id="tables" style="display: inline-block; width: 260px; margin-right: 10px;" class="hide">
  22. <%= render partial: "tables" %>
  23. </div>
  24. <script>
  25. function updatePreviewSelect() {
  26. $("#tables").load("<%= tables_queries_path %>?" + $.param({data_source: $("#query_data_source").val()}));
  27. }
  28. updatePreviewSelect();
  29. $("#query_data_source").selectize().change(updatePreviewSelect);
  30. </script>
  31. <%= link_to "Run", "#", class: "btn btn-info", id: "run", style: "vertical-align: top;" %>
  32. </div>
  33. </div>
  34. <div class="col-xs-4">
  35. <div class="form-group">
  36. <%= f.label :name %>
  37. <%= f.text_field :name, class: "form-control" %>
  38. </div>
  39. <div class="form-group">
  40. <%= f.label :description %>
  41. <%= f.text_area :description, placeholder: "Optional", style: "height: 80px;", class: "form-control" %>
  42. </div>
  43. <div class="text-right">
  44. <% if @query.persisted? %>
  45. <%= link_to "Delete", query_path(@query), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
  46. <%= f.submit "Fork", class: "btn btn-info" %>
  47. <% end %>
  48. <%= f.submit @query.persisted? ? "Update" : "Create", class: "btn btn-success" %>
  49. </div>
  50. <% if @query.persisted? %>
  51. <% dashboards_count = @query.dashboards.count %>
  52. <% checks_count = @query.checks.count %>
  53. <% words = [] %>
  54. <% words << pluralize(dashboards_count, "dashboard") if dashboards_count > 0 %>
  55. <% words << pluralize(checks_count, "check") if checks_count > 0 %>
  56. <% if words.any? %>
  57. <div class="alert alert-info" style="margin-top: 10px; padding: 8px 12px;">
  58. Part of <%= words.to_sentence %>. Be careful when editing.
  59. </div>
  60. <% end %>
  61. <% end %>
  62. </div>
  63. </div>
  64. <% end %>
  65. <div id="results"></div>
  66. <script>
  67. var editor = ace.edit("editor");
  68. editor.setTheme("ace/theme/twilight");
  69. editor.getSession().setMode("ace/mode/sql");
  70. editor.setOptions({
  71. enableBasicAutocompletion: false,
  72. enableSnippets: false,
  73. enableLiveAutocompletion: false,
  74. highlightActiveLine: false,
  75. fontSize: 12,
  76. minLines: 10
  77. });
  78. editor.renderer.setShowGutter(true);
  79. editor.renderer.setPrintMarginColumn(false);
  80. editor.renderer.setPadding(10);
  81. editor.getSession().setUseWrapMode(true);
  82. editor.commands.addCommand({
  83. name: 'run',
  84. bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
  85. exec: function(editor) {
  86. $("#run").click();
  87. },
  88. readOnly: false // false if this command should not apply in readOnly mode
  89. });
  90. // http://stackoverflow.com/questions/11584061/
  91. function adjustHeight() {
  92. var lines = editor.getSession().getScreenLength();
  93. if (lines < 9) {
  94. lines = 9;
  95. }
  96. var newHeight = (lines + 1) * 16;
  97. $("#editor").height(newHeight.toString() + "px");
  98. editor.resize();
  99. };
  100. function getSQL() {
  101. var selectedText = editor.getSelectedText();
  102. var text = selectedText.length == 0 ? editor.getValue() : selectedText;
  103. return text.replace(/\n/g, "\r\n");
  104. }
  105. editor.getSession().on("change", adjustHeight);
  106. adjustHeight();
  107. $("#editor").show();
  108. editor.focus();
  109. var error_line = null;
  110. var xhr;
  111. var params = <%= raw blazer_json_escape(variable_params.to_json) %>;
  112. $("#run").click(function (e) {
  113. e.preventDefault();
  114. if (error_line) {
  115. editor.getSession().removeGutterDecoration(error_line - 1, "error");
  116. error_line = null;
  117. }
  118. $("#results").html('<p class="text-muted">Loading...</p>');
  119. if (xhr) {
  120. xhr.abort();
  121. }
  122. var data = $.extend({}, params, {statement: getSQL(), data_source: $("#query_data_source").val()});
  123. xhr = runQuery(data, function (data) {
  124. $("#results").html(data);
  125. error_line = /LINE (\d+)/g.exec($("#results").find('.alert-danger').text());
  126. if (error_line) {
  127. error_line = parseInt(error_line[1], 10);
  128. editor.getSession().addGutterDecoration(error_line - 1, "error");
  129. editor.scrollToLine(error_line, true, true, function () {});
  130. editor.gotoLine(error_line, 0, true);
  131. editor.focus();
  132. }
  133. });
  134. });
  135. if ($("#query_statement").val() != "") {
  136. $("#run").click();
  137. }
  138. $(document).on("change", "#table_names", function () {
  139. var val = $(this).val();
  140. if (val.length > 0) {
  141. editor.setValue("SELECT * FROM " + val + " LIMIT 10");
  142. $("#run").click();
  143. }
  144. });
  145. $("form.the_form").on("submit", function() {
  146. $("#query_statement").val(editor.getValue());
  147. return true;
  148. });
  149. preventBackspaceNav();
  150. </script>