search.ko.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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.isLoading = ko.observable(false);
  76. self.klass = ko.computed(function () {
  77. return "card card-widget span" + self.size() + (self.offset() * 1 > 0 ? " offset" + self.offset() : "");
  78. });
  79. self.expand = function () {
  80. self.size(self.size() + 1);
  81. }
  82. self.compress = function () {
  83. self.size(self.size() - 1);
  84. }
  85. self.moveLeft = function () {
  86. self.offset(self.offset() - 1);
  87. }
  88. self.moveRight = function () {
  89. self.offset(self.offset() + 1);
  90. }
  91. self.remove = function (row, widget) {
  92. viewModel.removeWidget(widget);
  93. row.widgets.remove(widget);
  94. }
  95. };
  96. Widget.prototype.clone = function () {
  97. return new Widget(this.size(), UUID(), this.name(), this.widgetType());
  98. };
  99. function fullLayout() {
  100. setLayout([12]);
  101. }
  102. function oneThirdLeftLayout() {
  103. setLayout([3, 9]);
  104. }
  105. function oneThirdRightLayout() {
  106. setLayout([9, 3]);
  107. }
  108. function magicLayout() {
  109. setLayout([3, 9]);
  110. alert('Hue picked a timeline, filter, result, pie bar, widgets...');
  111. }
  112. function setLayout(colSizes) {
  113. // Save previous widgets
  114. var _allRows = [];
  115. $(viewModel.columns()).each(function (cnt, col) {
  116. var _tRows = [];
  117. $(col.rows()).each(function (icnt, row) {
  118. if (row.widgets().length > 0) {
  119. _tRows.push(row);
  120. }
  121. });
  122. _allRows = _allRows.concat(_tRows);
  123. });
  124. var _cols = [];
  125. var _highestCol = {
  126. idx: -1,
  127. size: -1
  128. };
  129. $(colSizes).each(function (cnt, size) {
  130. _cols.push(new Column(size, []));
  131. if (size > _highestCol.size) {
  132. _highestCol.idx = cnt;
  133. _highestCol.size = size;
  134. }
  135. });
  136. if (_allRows.length > 0 && _highestCol.idx > -1) {
  137. _cols[_highestCol.idx].rows(_allRows);
  138. }
  139. $(_cols).each(function (cnt, col) {
  140. if (col.rows().length == 0) {
  141. col.rows([new Row([])]);
  142. }
  143. });
  144. viewModel.columns(_cols);
  145. }
  146. function loadLayout(viewModel, json_layout) {
  147. var _columns = [];
  148. $(json_layout).each(function (cnt, json_col) {
  149. var _rows = [];
  150. $(json_col.rows).each(function (rcnt, json_row) {
  151. var row = new Row();
  152. $(json_row.widgets).each(function (wcnt, widget) {
  153. row.addWidget(new Widget(widget.size, widget.id, widget.name, widget.widgetType, widget.properties, widget.offset));
  154. });
  155. _rows.push(row);
  156. });
  157. var column = new Column(json_col.size, _rows);
  158. _columns = _columns.concat(column);
  159. });
  160. viewModel.columns(_columns);
  161. }
  162. // End dashboard lib
  163. var Query = function (vm, query) {
  164. var self = this;
  165. self.q = ko.observable(query.q);
  166. self.fqs = ko.mapping.fromJS(query.fqs);
  167. //[{'wid': 1, 'filter: ['c', 'd'], 'type': 'field', 'field': 'user_location'},
  168. // {'wid': 1, 'filter: [{'from': 1, 'to': 10}], 'type': 'range', 'field': 'created_at'}
  169. // {'wid': 2, ....}
  170. // ]
  171. self.toggleFacet = function (data) {alert(ko.mapping.toJSON(data));
  172. var fq = self.getFacetFilter(data.widget_id);
  173. // Special case when text facet is a range
  174. var _facet = vm.collection.getFacetById(data.widget_id);
  175. if (_facet.type() == 'range') {
  176. self.selectRangeFacet({'widget_id': data.widget_id, 'cat': data.facet.cat, 'from': data.facet.value, 'to': data.facet.value});
  177. return;
  178. }
  179. if (fq == null) {
  180. self.fqs.push(ko.mapping.fromJS({
  181. 'id': data.widget_id,
  182. 'field': data.facet.cat,
  183. 'filter': [data.facet.value],
  184. 'type': 'field'
  185. }));
  186. } else {
  187. $.each(self.fqs(), function (index, fq) {
  188. if (fq.id() == data.widget_id) {
  189. if (fq.filter.indexOf(data.facet.value) > -1) {
  190. fq.filter.remove(data.facet.value);
  191. if (fq.filter().length == 0) {
  192. self.fqs.remove(fq);
  193. }
  194. } else {
  195. fq.filter.push(data.facet.value);
  196. }
  197. }
  198. });
  199. }
  200. vm.search();
  201. }
  202. self.selectRangeFacet = function (data) {
  203. self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
  204. self.fqs.push(ko.mapping.fromJS({
  205. 'id': data.widget_id,
  206. 'field': data.cat,
  207. 'filter': {'from': data.from, 'to': data.to},
  208. 'type': 'range'
  209. }));
  210. vm.search();
  211. }
  212. self.removeFilter = function (data) {
  213. $.each(self.fqs(), function (index, fq) {
  214. if (fq.id() == data.id()) {
  215. self.fqs.remove(fq);
  216. return false;
  217. }
  218. });
  219. vm.search();
  220. }
  221. self.getFacetFilter = function (widget_id) {
  222. var _fq = null;
  223. $.each(self.fqs(), function (index, fq) {
  224. if (fq.id() == widget_id) {
  225. _fq = fq;
  226. return false;
  227. }
  228. });
  229. return _fq;
  230. }
  231. };
  232. var Collection = function (vm, collection) {
  233. var self = this;
  234. self.id = collection.id;
  235. self.name = collection.name;
  236. self.label = collection.label;
  237. self.idField = collection.idField;
  238. self.template = ko.mapping.fromJS(collection.template);
  239. self.template.fieldsSelected.subscribe(function () {
  240. vm.search();
  241. });
  242. self.template.template.subscribe(function () {
  243. vm.search();
  244. });
  245. self.template.isGridLayout.subscribe(function () {
  246. vm.search();
  247. });
  248. self.facets = ko.mapping.fromJS(collection.facets);
  249. $.each(self.facets(), function (index, facet) {
  250. facet.field.subscribe(function () {
  251. vm.search();
  252. });
  253. });
  254. self.fields = ko.mapping.fromJS(collection.fields);
  255. self.addFacet = function (facet_json) {
  256. $.post("/search/template/" + self.id + "/new_facet", {
  257. "collection": ko.mapping.toJSON(self),
  258. "id": facet_json.widget_id,
  259. "label": facet_json.name,
  260. "field": facet_json.name,
  261. "widget_type": facet_json.widgetType
  262. }, function (data) {
  263. if (data.status == 0) {
  264. var facet = ko.mapping.fromJS(data.facet);
  265. facet.field.subscribe(function () {
  266. vm.search();
  267. });
  268. self.facets.push(facet);
  269. vm.search();
  270. } else {
  271. $(document).trigger("error", data.message);
  272. }
  273. }).fail(function (xhr, textStatus, errorThrown) {});
  274. };
  275. self.removeFacet = function (widget_id) {
  276. $.each(self.facets(), function (index, facet) {
  277. if (facet.id() == widget_id()) {
  278. self.facets.remove(facet);
  279. return false;
  280. }
  281. });
  282. }
  283. self.getFacetById = function (facet_id) {
  284. var _facet = null;
  285. $.each(self.facets(), function (index, facet) {
  286. if (facet.id() == facet_id) {
  287. _facet = facet;
  288. return false;
  289. }
  290. });
  291. return _facet;
  292. }
  293. self.template.fields = ko.computed(function () {
  294. var _fields = [];
  295. $.each(self.template.fieldsAttributes(), function (index, field) {
  296. var position = self.template.fieldsSelected.indexOf(field.name());
  297. if (position != -1) {
  298. _fields[position] = field;
  299. }
  300. });
  301. return _fields;
  302. });
  303. self.template.fieldsModalFilter = ko.observable("");
  304. self.template.filteredFieldsAttributes = ko.observableArray(self.template.fieldsAttributes());
  305. self.template.fieldsModalFilter.subscribe(function(value){
  306. var _fields = [];
  307. $.each(self.template.fieldsAttributes(), function (index, field) {
  308. if (field.name().toLowerCase().indexOf(self.template.fieldsModalFilter().toLowerCase()) > -1){
  309. _fields.push(field);
  310. }
  311. });
  312. self.template.filteredFieldsAttributes(_fields);
  313. });
  314. self.addDynamicFields = function () { // + Adding merge smartly if schema updated
  315. $.post("/search/index/" + self.id + "/fields/dynamic", {
  316. }, function (data) {
  317. if (data.status == 0) {
  318. $.each(data.dynamic_fields, function (index, field) {
  319. if (self.fields.indexOf(field) == -1) {
  320. self.fields.push(field);
  321. }
  322. });
  323. }
  324. }).fail(function (xhr, textStatus, errorThrown) {});
  325. }
  326. self.toggleSortColumnGridLayout = function (template_field) {
  327. if (! template_field.sort.direction()) {
  328. template_field.sort.direction('desc');
  329. } else if (template_field.sort.direction() == 'desc') {
  330. template_field.sort.direction('asc');
  331. } else {
  332. template_field.sort.direction(null);
  333. }
  334. vm.search();
  335. };
  336. self.toggleFacet = function (facet_field, event) {
  337. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
  338. if (facet_field.properties.canRange()) {
  339. if (facet_field.type() == 'field' && facet_field.properties.sort() == 'desc') {
  340. facet_field.type('range');
  341. } else if (facet_field.type() == 'range' && facet_field.properties.sort() == 'desc') {
  342. facet_field.type('field');
  343. }
  344. }
  345. if (facet_field.properties.sort() == 'desc') {
  346. facet_field.properties.sort('asc');
  347. } else {
  348. facet_field.properties.sort('desc');
  349. }
  350. $(event.target).button('loading');
  351. vm.search();
  352. };
  353. };
  354. var SearchViewModel = function (collection_json, query_json) {
  355. var self = this;
  356. // Models
  357. self.collection = new Collection(self, collection_json.collection);
  358. self.query = new Query(self, query_json);
  359. // UI
  360. self.response = ko.observable({});
  361. self.results = ko.observableArray([]);
  362. self.norm_facets = ko.computed(function () {
  363. return self.response().normalized_facets;
  364. });
  365. self.getFacetFromQuery = function (facet_id) {
  366. var _facet = null;
  367. if (self.norm_facets() !== undefined) {
  368. $.each(self.norm_facets(), function (index, norm_facet) {
  369. if (norm_facet.id == facet_id()) {
  370. _facet = norm_facet;
  371. }
  372. });
  373. }
  374. return _facet;
  375. };
  376. self.previewColumns = ko.observable("");
  377. self.columns = ko.observable({});
  378. loadLayout(self, collection_json.layout);
  379. self.isEditing = ko.observable(false);
  380. self.toggleEditing = function () {
  381. self.isEditing(!self.isEditing());
  382. };
  383. self.isRetrievingResults = ko.observable(false);
  384. self.draggableHit = ko.observable(new Widget(12, UUID(), "Hit Count", "hit-widget"));
  385. self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
  386. self.draggableResultset = ko.observable(new Widget(12, UUID(), "Results", "resultset-widget"));
  387. self.draggableHistogram = ko.observable(new Widget(12, UUID(), "Histogram", "histogram-widget"));
  388. self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
  389. self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
  390. self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));
  391. self.draggablePie = ko.observable(new Widget(12, UUID(), "Pie Chart", "pie-widget"));
  392. self.draggableFilter = ko.observable(new Widget(12, UUID(), "Filter Bar", "filter-widget"));
  393. self.init = function () {
  394. //self.collection.addDynamicFields();
  395. self.isEditing(true);
  396. self.search();
  397. }
  398. self.search = function () {
  399. self.isRetrievingResults(true);
  400. $(".jHueNotify").hide();
  401. $.post("/search/search", {
  402. collection: ko.mapping.toJSON(self.collection),
  403. query: ko.mapping.toJSON(self.query),
  404. layout: ko.mapping.toJSON(self.columns)
  405. }, function (data) {
  406. self.response(data);
  407. self.results.removeAll();
  408. if (data.error) {
  409. $(document).trigger("error", data.error);
  410. }
  411. else {
  412. if (self.collection.template.isGridLayout()) {
  413. // Table view
  414. $.each(data.response.docs, function (index, item) {
  415. var row = [];
  416. var fields = self.collection.template.fieldsSelected();
  417. // Display selected fields or whole json document
  418. if (fields.length != 0) {
  419. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  420. row.push(item[field]);
  421. });
  422. } else {
  423. row.push(ko.mapping.toJSON(item));
  424. }
  425. self.results.push(row);
  426. });
  427. }
  428. else {
  429. // Template view
  430. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  431. $.each(data.response.docs, function (index, item) {
  432. addTemplateFunctions(item);
  433. self.results.push(Mustache.render(_mustacheTmpl, item));
  434. });
  435. }
  436. self.isRetrievingResults(false);
  437. }
  438. }).fail(function (xhr, textStatus, errorThrown) {
  439. $(document).trigger("error", xhr.responseText);
  440. }).always(function () {
  441. $('.btn-loading').button('reset');
  442. });
  443. };
  444. self.removeWidget = function (widget_json) {
  445. self.collection.removeFacet(widget_json.id);
  446. // todo remove fqs
  447. self.search();
  448. }
  449. self.getWidgetById = function (widget_id) {
  450. var _widget = null;
  451. $.each(self.columns(), function (i, col) {
  452. $.each(col.rows(), function (j, row) {
  453. $.each(row.widgets(), function (z, widget) {
  454. if (widget.id() == widget_id){
  455. _widget = widget;
  456. }
  457. });
  458. });
  459. });
  460. return _widget;
  461. }
  462. self.save = function () {
  463. $.post("/search/save", {
  464. collection: ko.mapping.toJSON(self.collection),
  465. layout: ko.mapping.toJSON(self.columns)
  466. },function (data) {
  467. if (data.status == 0) {
  468. self.collection.id = data.id;
  469. $(document).trigger("info", data.message);
  470. }
  471. else {
  472. $(document).trigger("error", data.message);
  473. }
  474. }).fail(function (xhr, textStatus, errorThrown) {
  475. $(document).trigger("error", xhr.responseText);
  476. });
  477. };
  478. };