search.ko.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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() {
  105. setLayout([9, 3]);
  106. }
  107. function magicLayout() {
  108. setLayout([3, 9]);
  109. alert('Hue picked a timeline, filter, result, pie bar, widgets...');
  110. }
  111. function setLayout(colSizes) {
  112. // Save previous widgets
  113. var _allRows = [];
  114. $(viewModel.columns()).each(function (cnt, col) {
  115. var _tRows = [];
  116. $(col.rows()).each(function (icnt, row) {
  117. if (row.widgets().length > 0) {
  118. _tRows.push(row);
  119. }
  120. });
  121. _allRows = _allRows.concat(_tRows);
  122. });
  123. var _cols = [];
  124. var _highestCol = {
  125. idx: -1,
  126. size: -1
  127. };
  128. $(colSizes).each(function (cnt, size) {
  129. _cols.push(new Column(size, []));
  130. if (size > _highestCol.size) {
  131. _highestCol.idx = cnt;
  132. _highestCol.size = size;
  133. }
  134. });
  135. if (_allRows.length > 0 && _highestCol.idx > -1) {
  136. _cols[_highestCol.idx].rows(_allRows);
  137. }
  138. $(_cols).each(function (cnt, col) {
  139. if (col.rows().length == 0) {
  140. col.rows([new Row([])]);
  141. }
  142. });
  143. viewModel.columns(_cols);
  144. }
  145. function loadLayout(viewModel, json_layout) {
  146. var _columns = [];
  147. $(json_layout).each(function (cnt, json_col) {
  148. var _rows = [];
  149. $(json_col.rows).each(function (rcnt, json_row) {
  150. var row = new Row();
  151. $(json_row.widgets).each(function (wcnt, widget) {
  152. row.addWidget(new Widget(widget.size, widget.id, widget.name, widget.widgetType, widget.properties, widget.offset));
  153. });
  154. _rows.push(row);
  155. });
  156. var column = new Column(json_col.size, _rows);
  157. _columns = _columns.concat(column);
  158. });
  159. viewModel.columns(_columns);
  160. }
  161. // End dashboard lib
  162. var Query = function (vm, query) {
  163. var self = this;
  164. self.q = ko.observable(query.q);
  165. self.fq = query.fq
  166. self.selectFacet = function (facet_json) {
  167. self.fq[facet_json.cat] = facet_json.value; // need to add facet id too
  168. vm.search();
  169. }
  170. self.unselectFacet = function (facet_json) {
  171. delete self.fq[facet_json.cat];
  172. vm.search();
  173. }
  174. };
  175. var Collection = function (vm, collection) {
  176. var self = this;
  177. self.id = collection.id;
  178. self.name = collection.name;
  179. self.label = collection.label;
  180. self.idField = collection.idField;
  181. self.template = ko.mapping.fromJS(collection.template);
  182. self.template.fieldsSelected.subscribe(function () {
  183. vm.search();
  184. });
  185. self.template.template.subscribe(function () {
  186. vm.search();
  187. });
  188. self.template.isGridLayout.subscribe(function () {
  189. vm.search();
  190. });
  191. self.facets = ko.mapping.fromJS(collection.facets);
  192. $.each(self.facets(), function (index, facet) {
  193. facet.field.subscribe(function () {
  194. vm.search();
  195. });
  196. });
  197. self.fields = ko.mapping.fromJS(collection.fields);
  198. self.addFacet = function (facet_json) {
  199. $.post("/search/template/" + self.id + "/new_facet", {
  200. "collection": ko.mapping.toJSON(self),
  201. "id": facet_json.widget_id,
  202. "label": facet_json.name,
  203. "field": facet_json.name,
  204. "widget_type": facet_json.widgetType
  205. }, function (data) {
  206. if (data.status == 0) {
  207. var facet = ko.mapping.fromJS(data.facet);
  208. facet.field.subscribe(function () {
  209. vm.search();
  210. });
  211. self.facets.push(facet);
  212. vm.search();
  213. } else {
  214. $(document).trigger("error", data.message);
  215. }
  216. }).fail(function (xhr, textStatus, errorThrown) {});
  217. };
  218. self.removeFacet = function (widget_id) {
  219. $.each(self.facets(), function (index, facet) {
  220. if (facet.id() == widget_id()) {
  221. self.facets.remove(facet);
  222. return false;
  223. }
  224. });
  225. }
  226. self.getFacetById = function (facet_id) {
  227. var _facet = null;
  228. $.each(self.facets(), function (index, facet) {//alert(ko.mapping.toJSON(category));
  229. if (facet.id() == facet_id) {
  230. _facet = facet;
  231. return false;
  232. }
  233. });
  234. return _facet;
  235. }
  236. self.template.fields = ko.computed(function () {
  237. var _fields = [];
  238. $.each(self.template.fieldsAttributes(), function (index, field) {
  239. var position = self.template.fieldsSelected.indexOf(field.name());
  240. if (position != -1) {
  241. _fields[position] = field;
  242. }
  243. });
  244. return _fields;
  245. });
  246. self.addDynamicFields = function () { // + Adding merge smartly if schema updated
  247. $.post("/search/index/" + self.id + "/fields/dynamic", {
  248. }, function (data) {
  249. if (data.status == 0) {
  250. $.each(data.dynamic_fields, function (index, field) {
  251. if (self.fields.indexOf(field) == -1) {
  252. self.fields.push(field);
  253. }
  254. });
  255. }
  256. }).fail(function (xhr, textStatus, errorThrown) {});
  257. }
  258. self.toggleSortColumnGridLayout = function (template_field) {
  259. if (! template_field.sort.direction()) {
  260. template_field.sort.direction('desc');
  261. } else if (template_field.sort.direction() == 'desc') {
  262. template_field.sort.direction('asc');
  263. } else {
  264. template_field.sort.direction(null);
  265. }
  266. vm.search();
  267. };
  268. self.toggleFacet = function (facet_field, event) {
  269. if (facet_field.properties.canRange()) {
  270. if (facet_field.type() == 'field' && facet_field.properties.sort() == 'desc') {
  271. facet_field.type('range');
  272. } else if (facet_field.type() == 'range' && facet_field.properties.sort() == 'desc') {
  273. facet_field.type('field');
  274. }
  275. }
  276. if (facet_field.properties.sort() == 'desc') {
  277. facet_field.properties.sort('asc');
  278. } else {
  279. facet_field.properties.sort('desc');
  280. }
  281. $(event.target).button('loading');
  282. vm.search();
  283. };
  284. };
  285. var SearchViewModel = function (collection_json, query_json) {
  286. var self = this;
  287. // Models
  288. self.collection = new Collection(self, collection_json.collection);
  289. self.query = new Query(self, query_json);
  290. // UI
  291. self.response = ko.observable({});
  292. self.results = ko.observableArray([]);
  293. self.norm_facets = ko.computed(function () {
  294. return self.response().normalized_facets;
  295. });
  296. self.getFacetFromQuery = function (facet_id) {
  297. var _facet = null;
  298. if (self.norm_facets() !== undefined) {
  299. $.each(self.norm_facets(), function (index, norm_facet) {
  300. if (norm_facet.id == facet_id()) {
  301. _facet = norm_facet;
  302. }
  303. });
  304. }
  305. return _facet;
  306. };
  307. self.previewColumns = ko.observable("");
  308. self.columns = ko.observable({});
  309. loadLayout(self, collection_json.layout);
  310. self.isEditing = ko.observable(false);
  311. self.toggleEditing = function () {
  312. self.isEditing(!self.isEditing());
  313. };
  314. self.isRetrievingResults = ko.observable(false);
  315. self.draggableHit = ko.observable(new Widget(12, UUID(), "Hit Count", "hit-widget"));
  316. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  317. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Results", "resultset-widget"));
  318. self.draggableHistogram = ko.observable(new Widget(12, UUID(), "Histogram", "histogram-widget"));
  319. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  320. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  321. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  322. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  323. self.draggableFilter = ko.observable(new Widget(12, UUID(), "Filter Bar", "filter-widget"));
  324. self.init = function () {
  325. //self.collection.addDynamicFields();
  326. self.isEditing(true);
  327. self.search();
  328. }
  329. self.search = function () {
  330. self.isRetrievingResults(true);
  331. $(".jHueNotify").hide();
  332. $.post("/search/search", {
  333. collection: ko.mapping.toJSON(self.collection),
  334. query: ko.mapping.toJSON(self.query),
  335. layout: ko.mapping.toJSON(self.columns)
  336. }, function (data) {
  337. self.response(data); // Content not observable for performance
  338. self.results.removeAll();
  339. if (data.error) {
  340. $(document).trigger("error", data.error);
  341. }
  342. else {
  343. if (self.collection.template.isGridLayout()) {
  344. // Table view
  345. $.each(data.response.docs, function (index, item) {
  346. var row = [];
  347. var fields = self.collection.template.fieldsSelected();
  348. // Field selection or whole record
  349. if (fields.length != 0) {
  350. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  351. row.push(item[field]);
  352. });
  353. } else {
  354. row.push(ko.mapping.toJSON(item));
  355. }
  356. self.results.push(row);
  357. });
  358. }
  359. else {
  360. // Template view
  361. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  362. $.each(data.response.docs, function (index, item) {
  363. addTemplateFunctions(item);
  364. self.results.push(Mustache.render(_mustacheTmpl, item));
  365. });
  366. }
  367. self.isRetrievingResults(false);
  368. }
  369. }).fail(function (xhr, textStatus, errorThrown) {
  370. $(document).trigger("error", xhr.responseText);
  371. }).always(function () {
  372. $('.btn-loading').button('reset');
  373. });
  374. };
  375. self.removeWidget = function (widget_json) {
  376. self.collection.removeFacet(widget_json.id);
  377. self.search();
  378. }
  379. self.save = function () {
  380. $.post("/search/save", {
  381. collection: ko.mapping.toJSON(self.collection),
  382. layout: ko.mapping.toJSON(self.columns)
  383. },function (data) {
  384. if (data.status == 0) {
  385. self.collection.id = data.id;
  386. $(document).trigger("info", data.message);
  387. }
  388. else {
  389. $(document).trigger("error", data.message);
  390. }
  391. }).fail(function (xhr, textStatus, errorThrown) {
  392. $(document).trigger("error", xhr.responseText);
  393. });
  394. };
  395. };