search.ko.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function magicLayout(vm) {
  17. loadLayout(vm, vm.initial.layout);
  18. $(document).trigger("magicLayout");
  19. }
  20. function loadLayout(viewModel, json_layout) {
  21. var _columns = [];
  22. $(json_layout).each(function (cnt, json_col) {
  23. var _rows = [];
  24. $(json_col.rows).each(function (rcnt, json_row) {
  25. var row = new Row([], viewModel);
  26. $(json_row.widgets).each(function (wcnt, widget) {
  27. row.addWidget(new Widget({
  28. size:widget.size,
  29. id: widget.id,
  30. name: widget.name,
  31. widgetType: widget.widgetType,
  32. properties: widget.properties,
  33. offset: widget.offset,
  34. loading: true,
  35. vm: viewModel
  36. }));
  37. });
  38. _rows.push(row);
  39. });
  40. var column = new Column(json_col.size, _rows);
  41. _columns = _columns.concat(column);
  42. });
  43. viewModel.columns(_columns);
  44. }
  45. // End dashboard lib
  46. var Query = function (vm, query) {
  47. var self = this;
  48. // current select
  49. self.qs = ko.mapping.fromJS([{'q': ''}]);
  50. self.fqs = ko.mapping.fromJS([{'q': ''}]);
  51. }
  52. var Dashboard = function (vm, dashboard) {
  53. // id from DB
  54. var self = this;
  55. self.facets = ko.observable(dashboard.facets);
  56. self.addFacet = function(data) {
  57. }
  58. self.getFacetById = function (facet_id) {
  59. var _facet = null;
  60. $.each(self.facets(), function (index, facet) {
  61. if (facet.id == facet_id) {
  62. _facet = facet;
  63. return false;
  64. }
  65. });
  66. return _facet;
  67. }
  68. }
  69. var TestViewModel = function (query_json, dashboard_json) {
  70. var self = this;
  71. self.isEditing = ko.observable(false);
  72. self.toggleEditing = function () {
  73. self.isEditing(! self.isEditing());
  74. };
  75. self.previewColumns = ko.observable("");
  76. self.columns = ko.observable([]);
  77. loadLayout(self, dashboard_json.layout);
  78. self.query = new Query(self, query_json);
  79. self.dashboard = new Dashboard(self, dashboard_json);
  80. self.results = ko.observableArray([]);
  81. self.results_facet = ko.observableArray([]);
  82. self.search = function (callback) {
  83. self.results.removeAll();
  84. var multiQs = $.map(self.dashboard.facets(), function(facet) {
  85. return $.post("/impala/query", {
  86. "query": ko.mapping.toJSON(self.query),
  87. "dashboard": ko.mapping.toJSON(self.dashboard),
  88. "layout": ko.mapping.toJSON(self.columns),
  89. "facet": ko.mapping.toJSON(facet),
  90. }, function (data) {return data});
  91. });
  92. $.when.apply($, [
  93. $.post("/impala/query", {
  94. "query": ko.mapping.toJSON(self.query),
  95. "dashboard": ko.mapping.toJSON(self.dashboard),
  96. "layout": ko.mapping.toJSON(self.columns),
  97. }, function (data) {
  98. if (data.status == 0) {
  99. $.each(data.data, function (index, row) {
  100. self.results.push(row);
  101. });
  102. } else {
  103. $(document).trigger("error", data.message);
  104. }
  105. })
  106. ].concat(multiQs)
  107. )
  108. .done(function() {
  109. if (arguments.length > 1) { // If multi queries
  110. for (var i = 1; i < arguments.length; i++) {
  111. var facet = arguments[i][0];
  112. removeFrom(self.results_facet, facet.id);
  113. self.results_facet.push(ko.mapping.fromJS(facet));
  114. }
  115. }
  116. })
  117. .fail(function (xhr, textStatus, errorThrown) {
  118. $(document).trigger("error", data.message);
  119. });
  120. };
  121. function removeFrom(collection, item_id) {
  122. $.each(collection(), function (index, item) {
  123. if (item.id() == item_id) {
  124. collection.remove(item);
  125. return false;
  126. }
  127. });
  128. }
  129. self.getFacetFromResult = function (facet_id) {
  130. var _facet = null;
  131. $.each(self.results_facet(), function (index, facet) {
  132. if (facet.id() == facet_id) {
  133. _facet = facet;
  134. return false;
  135. }
  136. });
  137. return _facet;
  138. }
  139. function bareWidgetBuilder(name, type){
  140. return new Widget({
  141. size: 12,
  142. id: UUID(),
  143. name: name,
  144. widgetType: type
  145. });
  146. }
  147. self.draggableResultset = ko.observable(bareWidgetBuilder("Grid Results", "resultset-widget"));
  148. self.draggablePie = ko.observable(bareWidgetBuilder("Pie Chart", "pie-widget"));
  149. };