search.ko.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // Start dashboard lib to move out
  17. var Column = function (size, rows) {
  18. var self = this;
  19. self.size = ko.observable(size);
  20. self.rows = ko.observableArray(rows);
  21. self.klass = ko.computed(function () {
  22. return "card card-home card-column span" + self.size();
  23. });
  24. self.addEmptyRow = function () {
  25. self.addRow();
  26. };
  27. self.addRow = function (row) {
  28. if (typeof row == "undefined" || row == null) {
  29. row = new Row([]);
  30. }
  31. self.rows.push(row);
  32. };
  33. }
  34. var Row = function (widgets) {
  35. var self = this;
  36. self.widgets = ko.observableArray(widgets);
  37. self.addWidget = function (widget) {
  38. self.widgets.push(widget);
  39. };
  40. self.move = function (from, to) {
  41. try {
  42. viewModel.columns()[to].addRow(self);
  43. viewModel.columns()[from].rows.remove(self);
  44. }
  45. catch (exception) {
  46. }
  47. }
  48. self.moveDown = function (col, row) {
  49. var _i = col.rows().indexOf(row);
  50. if (_i < col.rows().length - 1) {
  51. var _arr = col.rows();
  52. col.rows.splice(_i, 2, _arr[_i + 1], _arr[_i]);
  53. }
  54. }
  55. self.moveUp = function (col, row) {
  56. var _i = col.rows().indexOf(row);
  57. if (_i >= 1) {
  58. var _arr = col.rows();
  59. col.rows.splice(_i - 1, 2, _arr[_i], _arr[_i - 1]);
  60. }
  61. }
  62. self.remove = function (col, row) {
  63. col.rows.remove(row);
  64. }
  65. }
  66. // A widget is generic. It has an id that refer to another object (e.g. facet) with the same id.
  67. var Widget = function (size, id, name, widgetType, properties, offset) {
  68. var self = this;
  69. self.size = ko.observable(size).extend({ numeric: 0 });
  70. self.name = ko.observable(name);
  71. self.id = ko.observable(id);
  72. self.widgetType = ko.observable(typeof widgetType != "undefined" && widgetType != null ? widgetType : "empty-widget");
  73. self.properties = ko.observable(typeof properties != "undefined" && properties != null ? properties : {});
  74. self.offset = ko.observable(typeof offset != "undefined" && offset != null ? offset : 0).extend({ numeric: 0 });
  75. self.klass = ko.computed(function () {
  76. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  77. });
  78. self.expand = function () {
  79. self.size(self.size() + 1);
  80. }
  81. self.compress = function () {
  82. self.size(self.size() - 1);
  83. }
  84. self.moveLeft = function () {
  85. self.offset(self.offset() - 1);
  86. }
  87. self.moveRight = function () {
  88. self.offset(self.offset() + 1);
  89. }
  90. self.remove = function (row, widget) {
  91. viewModel.removeWidget(widget);
  92. row.widgets.remove(widget);
  93. }
  94. };
  95. Widget.prototype.clone = function () {
  96. return new Widget(this.size(), UUID(), this.name(), this.widgetType());
  97. };
  98. function fullLayout() {
  99. setLayout([12]);
  100. }
  101. function oneThirdLeftLayout() {
  102. setLayout([3, 9]);
  103. }
  104. function oneThirdRightLayout() { // instead --> full with 1 row = timeline, 2 = 3 pies, 3 = grid result
  105. setLayout([9, 3]);
  106. }
  107. function setLayout(colSizes) {
  108. // Save previous widgets
  109. var _allRows = [];
  110. $(viewModel.columns()).each(function (cnt, col) {
  111. var _tRows = [];
  112. $(col.rows()).each(function (icnt, row) {
  113. if (row.widgets().length > 0) {
  114. _tRows.push(row);
  115. }
  116. });
  117. _allRows = _allRows.concat(_tRows);
  118. });
  119. var _cols = [];
  120. var _highestCol = {
  121. idx: -1,
  122. size: -1
  123. };
  124. $(colSizes).each(function (cnt, size) {
  125. _cols.push(new Column(size, []));
  126. if (size > _highestCol.size) {
  127. _highestCol.idx = cnt;
  128. _highestCol.size = size;
  129. }
  130. });
  131. if (_allRows.length > 0 && _highestCol.idx > -1) {
  132. _cols[_highestCol.idx].rows(_allRows);
  133. }
  134. $(_cols).each(function (cnt, col) {
  135. if (col.rows().length == 0) {
  136. col.rows([new Row([])]);
  137. }
  138. });
  139. viewModel.columns(_cols);
  140. }
  141. function loadLayout(viewModel, json_layout) {
  142. var _columns = [];
  143. $(json_layout).each(function (cnt, json_col) {
  144. var _rows = [];
  145. $(json_col.rows).each(function (rcnt, json_row) {
  146. var row = new Row();
  147. $(json_row.widgets).each(function (wcnt, widget) {
  148. row.addWidget(new Widget(widget.size, widget.id, widget.name, widget.widgetType, widget.properties, widget.offset));
  149. });
  150. _rows.push(row);
  151. });
  152. var column = new Column(json_col.size, _rows);
  153. _columns = _columns.concat(column);
  154. });
  155. viewModel.columns(_columns);
  156. }
  157. // End dashboard lib
  158. var Query = function (vm, query) {
  159. var self = this;
  160. self.q = ko.observable(query.q);
  161. self.fq = query.fq
  162. self.selectFacet = function (facet_json) {
  163. self.fq[facet_json.cat] = facet_json.value;
  164. vm.search();
  165. }
  166. self.unselectFacet = function (facet_json) {
  167. delete self.fq[facet_json.cat];
  168. vm.search();
  169. }
  170. };
  171. var Collection = function (vm, collection) {
  172. var self = this;
  173. self.id = collection.id;
  174. self.name = collection.name;
  175. self.idField = collection.idField;
  176. self.template = ko.mapping.fromJS(collection.template);
  177. self.template.fields.subscribe(function () {
  178. vm.search();
  179. });
  180. self.template.template.subscribe(function () {
  181. vm.search();
  182. });
  183. self.template.isGridLayout.subscribe(function () {
  184. vm.search();
  185. });
  186. self.facets = ko.mapping.fromJS(collection.facets);
  187. self.fields = ko.observableArray(collection.fields);
  188. self.addFacet = function (facet_json) {
  189. self.facets.push(ko.mapping.fromJS({
  190. "id": facet_json.widget_id,
  191. "label": facet_json.name,
  192. "field": facet_json.name, "type": "field"
  193. }));
  194. }
  195. self.removeFacet = function (widget_id) {
  196. $.each(self.facets(), function (index, item) {
  197. if (item.id() == widget_id()) {
  198. self.facets.remove(item);
  199. }
  200. });
  201. }
  202. self.getFacetById = function (facet_id) {
  203. var _facet = null;
  204. $.each(self.facets(), function (index, facet) {
  205. if (facet.id() == facet_id) {
  206. _facet = facet;
  207. }
  208. });
  209. return _facet;
  210. }
  211. self.addDynamicFields = function () {
  212. $.post("/search/index/" + self.id + "/fields/dynamic", {
  213. }, function (data) {
  214. if (data.status == 0) {
  215. $.each(data.dynamic_fields, function (index, field) {
  216. if (self.fields.indexOf(field) == -1) {
  217. self.fields.push(field);
  218. }
  219. });
  220. }
  221. }).fail(function (xhr, textStatus, errorThrown) {});
  222. }
  223. self.addDynamicFields();
  224. };
  225. var SearchViewModel = function (collection_json, query_json) {
  226. var self = this;
  227. // Models
  228. self.collection = new Collection(self, collection_json.collection);
  229. self.query = new Query(self, query_json);
  230. // UI
  231. self.response = ko.observable({});
  232. self.results = ko.observableArray([]);
  233. self.norm_facets = ko.computed(function () {
  234. return self.response().normalized_facets;
  235. });
  236. self.getFacetFromQuery = function (facet_id) {
  237. var _facet = null;
  238. if (self.norm_facets() !== undefined) {
  239. $.each(self.norm_facets(), function (index, norm_facet) {//alert(ko.mapping.toJSON(norm_facet.id + " " + facet_id()));
  240. if (norm_facet.id == facet_id()) {
  241. _facet = norm_facet;
  242. }
  243. });
  244. }
  245. return _facet;
  246. };
  247. self.previewColumns = ko.observable("");
  248. self.columns = ko.observable({});
  249. loadLayout(self, collection_json.layout); // move to init + load dynamic fields?
  250. self.isEditing = ko.observable(false);
  251. self.toggleEditing = function () {
  252. self.isEditing(!self.isEditing());
  253. };
  254. self.isRetrievingResults = ko.observable(false);
  255. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  256. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Results", "resultset-widget"));
  257. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  258. self.draggableArea = ko.observable(new Widget(12, UUID(), "Area Chart", "area-widget"));
  259. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  260. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  261. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  262. self.search = function () {
  263. self.isRetrievingResults(true);
  264. $(".jHueNotify").hide();
  265. $.post("/search/search", {
  266. collection: ko.mapping.toJSON(self.collection),
  267. query: ko.mapping.toJSON(self.query),
  268. layout: ko.mapping.toJSON(self.columns)
  269. }, function (data) {
  270. self.response(data); // If error we should probably update only the facets
  271. self.results.removeAll();
  272. if (data.error) {
  273. $(document).trigger("error", data.error);
  274. }
  275. else {
  276. if (self.collection.template.isGridLayout()) {
  277. // Table view
  278. $.each(data.response.docs, function (index, item) {
  279. var row = [];
  280. var fields = self.collection.template.fields();
  281. // Field selection or whole record
  282. if (fields.length != 0) {
  283. $.each(self.collection.template.fields(), function (index, column) {
  284. row.push(item[column]);
  285. });
  286. } else {
  287. row.push(ko.mapping.toJSON(item));
  288. }
  289. self.results.push(row);
  290. });
  291. }
  292. else {
  293. // Template view
  294. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  295. $.each(data.response.docs, function (index, item) {
  296. addTemplateFunctions(item);
  297. self.results.push(Mustache.render(_mustacheTmpl, item));
  298. });
  299. }
  300. self.isRetrievingResults(false);
  301. }
  302. }).fail(function (xhr, textStatus, errorThrown) {
  303. $(document).trigger("error", xhr.responseText);
  304. });
  305. };
  306. self.removeWidget = function (widget_json) {
  307. self.collection.removeFacet(widget_json.id);
  308. self.search();
  309. }
  310. self.save = function () {
  311. $.post("/search/save", {
  312. collection: ko.mapping.toJSON(self.collection),
  313. layout: ko.mapping.toJSON(self.columns)
  314. },function (data) {
  315. if (data.status == 0) {
  316. $(document).trigger("info", data.message);
  317. }
  318. else {
  319. $(document).trigger("error", data.message);
  320. }
  321. }).fail(function (xhr, textStatus, errorThrown) {
  322. $(document).trigger("error", xhr.responseText);
  323. });
  324. };
  325. };