search.ko.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. self.qs = ko.mapping.fromJS(query.qs);
  49. self.fqs = ko.mapping.fromJS(query.fqs);
  50. self.start = ko.mapping.fromJS(query.start);
  51. var defaultMultiqGroup = {'id': 'query', 'label': 'query'};
  52. self.multiqs = ko.computed(function () { // List of widgets supporting multiqs
  53. var histogram_id = vm.collection.getHistogramFacet();
  54. return [defaultMultiqGroup].concat(
  55. $.map($.grep(self.fqs(), function(fq, i) {
  56. return (fq.type() == 'field' || fq.type() == 'range') && (histogram_id == null || histogram_id.id() != fq.id());
  57. }), function(fq) { return {'id': fq.id(), 'label': fq.field()} })
  58. );
  59. });
  60. self.selectedMultiq = ko.observable('query');
  61. self.getFacetFilter = function(widget_id) {
  62. var _fq = null;
  63. $.each(self.fqs(), function (index, fq) {
  64. if (fq.id() == widget_id) {
  65. _fq = fq;
  66. return false;
  67. }
  68. });
  69. return _fq;
  70. };
  71. self.getMultiq = ko.computed(function () {
  72. if (self.selectedMultiq()) {
  73. if (self.selectedMultiq() == 'query') {
  74. if (self.qs().length >= 2) {
  75. return 'query';
  76. }
  77. } else {
  78. var facet = self.getFacetFilter(self.selectedMultiq());
  79. if (facet && facet.filter().length > 0) {
  80. return 'facet';
  81. }
  82. }
  83. }
  84. return null;
  85. });
  86. self.addQ = function (data) {
  87. self.qs.push(ko.mapping.fromJS({'q': ''}));
  88. };
  89. self.removeQ = function (query) {
  90. self.qs.remove(query);
  91. };
  92. self.selectedMultiq.subscribe(function () { // To keep below the computed objects!
  93. vm.search();
  94. });
  95. self.toggleFacet = function (data) {
  96. var fq = self.getFacetFilter(data.widget_id);
  97. if (fq == null) {
  98. self.fqs.push(ko.mapping.fromJS({
  99. 'id': data.widget_id,
  100. 'field': data.facet.cat,
  101. 'filter': [{'exclude': data.exclude ? true : false, 'value': data.facet.value}],
  102. 'type': 'field'
  103. }));
  104. } else {
  105. $.each(self.fqs(), function (index, fq) {
  106. if (fq.id() == data.widget_id) {
  107. var f = $.grep(fq.filter(), function(f) { return f.value() == data.facet.value; });
  108. if (f.length > 0) {
  109. fq.filter.remove(f[0]);
  110. if (fq.filter().length == 0) {
  111. self.fqs.remove(fq);
  112. }
  113. } else {
  114. fq.filter.push(ko.mapping.fromJS({'exclude': data.exclude ? true : false, 'value': data.facet.value}));
  115. }
  116. }
  117. });
  118. }
  119. self.start(0);
  120. vm.search();
  121. }
  122. self.togglePivotFacet = function (data) {
  123. data.facet.cat = data.facet.fq_fields;
  124. data.facet.value = data.facet.fq_values;
  125. self.toggleFacet(data);
  126. }
  127. function _toggleSingleTermFacet(data, exclude) {
  128. var fq = getFilterByField(data.val.cat);
  129. var id;
  130. if (fq) {
  131. id = fq.id();
  132. } else {
  133. id = UUID();
  134. }
  135. self.toggleFacet({'widget_id': id, 'facet': {'cat': data.val.cat, 'value': data.val.value}, 'exclude': exclude});
  136. vm.search();
  137. }
  138. self.addSingleTermFacet = function(data) {
  139. _toggleSingleTermFacet(data, false);
  140. }
  141. self.removeSingleTermFacet = function(data) {
  142. _toggleSingleTermFacet(data, true);
  143. }
  144. self.selectRangeFacet = function (data) {
  145. if (data.force != undefined) {
  146. self.removeFilter(ko.mapping.fromJS({'id': data.widget_id, 'dontZoomOut': true}));
  147. }
  148. var fq = self.getFacetFilter(data.widget_id);
  149. if (fq == null) {
  150. self.fqs.push(ko.mapping.fromJS({
  151. 'id': data.widget_id,
  152. 'field': data.cat,
  153. 'filter': [{'exclude': data.exclude ? true : false, 'value': data.from}],
  154. 'properties': [{'from': data.from, 'to': data.to}],
  155. 'type': 'range'
  156. }));
  157. } else {
  158. var f = $.grep(fq.filter(), function(f) { return f.value() == data.from; });
  159. if (f.length > 0) { // Unselect
  160. fq.filter.remove(f[0]);
  161. $.each(fq.properties(), function (index, prop) {
  162. if (prop && prop.from() == data.from) {
  163. fq.properties.remove(prop);
  164. }
  165. });
  166. if (fq.filter().length == 0) {
  167. self.removeFilter(ko.mapping.fromJS({'id': data.widget_id}));
  168. }
  169. } else {
  170. fq.filter.push(ko.mapping.fromJS({'exclude': data.exclude ? true : false, 'value': data.from}));
  171. fq.properties.push(ko.mapping.fromJS({'from': data.from, 'to': data.to}));
  172. }
  173. }
  174. self.start(0);
  175. if (data.no_refresh == undefined) {
  176. vm.search();
  177. }
  178. };
  179. function getFilterByField(field) {
  180. var _fq = null;
  181. $.each(self.fqs(), function (index, fq) {
  182. if (fq.field() == field && fq.id().indexOf('***') == 0) {
  183. _fq= fq;
  184. return false;
  185. }
  186. });
  187. return _fq;
  188. };
  189. self.removeFilter = function (data) {
  190. var found = false;
  191. $.each(self.fqs(), function (index, fq) {
  192. if (fq.id() == data.id()) {
  193. self.fqs.remove(fq);
  194. // Also re-init range select widget
  195. var rangeWidget = vm.collection.getFacetById(fq.id());
  196. if (data.dontZoomOut == undefined && rangeWidget != null && RANGE_SELECTABLE_WIDGETS.indexOf(rangeWidget.widgetType()) != -1 && fq.type() == 'range') {
  197. vm.collection.rangeZoomOut({'id': rangeWidget.id()});
  198. }
  199. found = true;
  200. return false;
  201. }
  202. });
  203. return found;
  204. };
  205. self.paginate = function (direction) {
  206. if (direction == 'next') {
  207. self.start(self.start() + vm.collection.template.rows() * 1.0);
  208. } else {
  209. self.start(self.start() - vm.collection.template.rows() * 1.0);
  210. }
  211. vm.search();
  212. };
  213. };
  214. var FieldAnalysis = function (vm, field_name) {
  215. var self = this;
  216. self.name = ko.observable(field_name);
  217. self.isLoading = ko.observable(true);
  218. self.section = ko.observable('terms');
  219. self.section.subscribe(function () {
  220. self.update();
  221. });
  222. self.terms = ko.mapping.fromJS({'prefix': '', 'data': []});
  223. self.terms.prefix.subscribe(function () {
  224. self.getTerms();
  225. });
  226. self.terms.prefix.extend({rateLimit: {timeout: 2000, method: "notifyWhenChangesStop"}});
  227. self.stats = ko.mapping.fromJS({'facet': '', 'data': []});
  228. var _statsUpdateTimeout = -1;
  229. self.stats.facet.subscribe(function () {
  230. // this is to avoid false positives from typeahead blur
  231. window.clearTimeout(_statsUpdateTimeout);
  232. _statsUpdateTimeout = window.setTimeout(function(){self.getStats()}, 200);
  233. });
  234. self.update = function() {
  235. if (self.section() == 'stats') {
  236. if (self.stats.data().length == 0) {
  237. self.getStats();
  238. }
  239. } else {
  240. if (self.terms.data().length == 0) {
  241. self.getTerms();
  242. }
  243. }
  244. }
  245. self.getTerms = function () {
  246. self.isLoading(true);
  247. self.terms.data.removeAll();
  248. $.post("/search/get_terms", {
  249. collection: ko.mapping.toJSON(vm.collection),
  250. analysis: ko.mapping.toJSON(self)
  251. }, function (data) {
  252. if (data.status == 0) {
  253. if (data.terms != null) {
  254. $.each(data.terms, function (key, val) {
  255. self.terms.data.push({'key': key, 'val': val});
  256. });
  257. }
  258. }
  259. else if (data.status == 1) {
  260. self.terms.data.push({'key': 'Error', 'val': data.message});
  261. }
  262. else {
  263. $(document).trigger("error", data.message);
  264. }
  265. self.isLoading(false);
  266. }).fail(function (xhr, textStatus, errorThrown) {
  267. $(document).trigger("error", xhr.responseText);
  268. });
  269. };
  270. self.getStats = function () {
  271. self.stats.data.removeAll();
  272. self.isLoading(true);
  273. $.post("/search/get_stats", {
  274. collection: ko.mapping.toJSON(vm.collection),
  275. query: ko.mapping.toJSON(vm.query),
  276. analysis: ko.mapping.toJSON(self)
  277. }, function (data) {
  278. if (data.status == 0) {
  279. if (data.stats.stats.stats_fields[self.name()] != null) {
  280. $.each(data.stats.stats.stats_fields[self.name()], function (key, val) {
  281. self.stats.data.push({'key': key, 'val': val});
  282. });
  283. }
  284. }
  285. else if (data.status == 1) {
  286. self.stats.data.push({'key': 'Error', 'val': data.message});
  287. }
  288. else {
  289. $(document).trigger("error", data.message);
  290. }
  291. self.isLoading(false);
  292. }).fail(function (xhr, textStatus, errorThrown) {
  293. $(document).trigger("error", xhr.responseText);
  294. });
  295. };
  296. }
  297. var Collection = function (vm, collection) {
  298. var self = this;
  299. self.id = collection.id;
  300. self.name = ko.mapping.fromJS(collection.name);
  301. self.label = ko.mapping.fromJS(collection.label);
  302. self.enabled = ko.mapping.fromJS(collection.enabled);
  303. self.idField = ko.observable(collection.idField);
  304. self.template = ko.mapping.fromJS(collection.template);
  305. self.template.fieldsSelected.subscribe(function () {
  306. vm.search();
  307. });
  308. self.template.extracode($("<span>").html(self.template.extracode()).text()); // Unescape HTML
  309. self.template.extracode.extend({rateLimit: {timeout: 3000, method: "notifyWhenChangesStop"}});
  310. self.template.template.extend({rateLimit: {timeout: 3000, method: "notifyWhenChangesStop"}});
  311. self.template.template.subscribe(function () {
  312. vm.search();
  313. });
  314. self.template.isGridLayout.subscribe(function () {
  315. vm.results.removeAll();
  316. vm.search();
  317. });
  318. if (self.template.leafletmap.latitudeField == undefined) {
  319. self.template.leafletmap.latitudeField = ko.observable();
  320. }
  321. if (self.template.leafletmap.longitudeField == undefined) {
  322. self.template.leafletmap.longitudeField = ko.observable();
  323. }
  324. if (self.template.leafletmap.labelField == undefined) {
  325. self.template.leafletmap.labelField = ko.observable();
  326. }
  327. self.template.leafletmapOn = ko.computed(function() {
  328. return self.template.leafletmap.latitudeField() != null && self.template.leafletmap.longitudeField() != null;
  329. });
  330. self.template.leafletmap.latitudeField.subscribe(function (newValue) {
  331. if (self.template.leafletmap.longitudeField() != null && newValue != null) {
  332. vm.search();
  333. }
  334. });
  335. self.template.leafletmap.longitudeField.subscribe(function (newValue) {
  336. if (self.template.leafletmap.latitudeField() != null && newValue != null) {
  337. vm.search();
  338. }
  339. });
  340. self.template.leafletmap.labelField.subscribe(function (newValue) {
  341. if (self.template.leafletmapOn()) {
  342. vm.search();
  343. }
  344. });
  345. self.template.selectedVisualField = ko.observable();
  346. self.template.selectedVisualFunction = ko.observable();
  347. self.template.selectedVisualFunction.subscribe(function (newValue) {
  348. var _vf = $("#visualFunctions");
  349. _vf.siblings(".muted").text(_vf.find(":selected").attr("title"));
  350. });
  351. self.template.selectedSourceField = ko.observable();
  352. self.template.selectedSourceFunction = ko.observable();
  353. self.template.selectedSourceFunction.subscribe(function (newValue) {
  354. var _sf = $("#sourceFunctions");
  355. _sf.siblings(".muted").text(_sf.find(":selected").attr("title"));
  356. });
  357. self.template.addFieldToVisual = function () {
  358. $(document).trigger("addFieldToVisual", self.template.selectedVisualField());
  359. };
  360. self.template.addFunctionToVisual = function () {
  361. $(document).trigger("addFunctionToVisual", self.template.selectedVisualFunction());
  362. };
  363. self.template.addFieldToSource = function () {
  364. $(document).trigger("addFieldToSource", self.template.selectedSourceField());
  365. };
  366. self.template.addFunctionToSource = function () {
  367. $(document).trigger("addFunctionToSource", self.template.selectedSourceFunction());
  368. };
  369. self.facets = ko.mapping.fromJS(collection.facets);
  370. $.each(self.facets(), function (index, facet) {
  371. facet.properties.limit.subscribe(function () {
  372. vm.search();
  373. });
  374. });
  375. self.template.rows.subscribe(function() {
  376. vm.search();
  377. });
  378. self.template.rows.extend({rateLimit: {timeout: 1500, method: "notifyWhenChangesStop"}});
  379. self.fields = ko.mapping.fromJS(collection.fields);
  380. self.availableFacetFields = ko.computed(function() {
  381. var facetFieldNames = $.map(self.facets(), function(facet) {
  382. return facet.field();
  383. });
  384. return $.grep(self.fields(), function(field) {
  385. return facetFieldNames.indexOf(field.name()) == -1;
  386. });
  387. });
  388. self.selectedDocument = ko.observable({});
  389. self.addFacet = function (facet_json) {
  390. self.removeFacet(function(){return facet_json.widget_id});
  391. logGA('add_facet/' + facet_json.widgetType);
  392. $.post("/search/template/new_facet", {
  393. "collection": ko.mapping.toJSON(self),
  394. "id": facet_json.widget_id,
  395. "label": facet_json.name,
  396. "field": facet_json.name,
  397. "widget_type": facet_json.widgetType
  398. }, function (data) {
  399. if (data.status == 0) {
  400. var facet = ko.mapping.fromJS(data.facet);
  401. facet.properties.limit.subscribe(function () {
  402. vm.search();
  403. });
  404. self.facets.push(facet);
  405. vm.search();
  406. } else {
  407. $(document).trigger("error", data.message);
  408. }
  409. }).fail(function (xhr, textStatus, errorThrown) {});
  410. };
  411. self.addPivotFacetValue = function(facet) {
  412. var pivot = null;
  413. if (typeof facet.properties.facets_form.field == 'string') { // Hackish but we load back properties as simple objects
  414. pivot = ko.mapping.fromJS({
  415. 'field': facet.properties.facets_form.field,
  416. 'limit': facet.properties.facets_form.limit,
  417. 'mincount': facet.properties.facets_form.mincount
  418. });
  419. facet.properties.facets_form.field = null;
  420. facet.properties.facets_form.limit = 5;
  421. facet.properties.facets_form.mincount = 1;
  422. } else {
  423. if (typeof facet.properties.facets_form.field != 'undefined') {
  424. pivot = ko.mapping.fromJS({
  425. 'field': facet.properties.facets_form.field(),
  426. 'limit': facet.properties.facets_form.limit(),
  427. 'mincount': facet.properties.facets_form.mincount()
  428. });
  429. facet.properties.facets_form.field(null);
  430. facet.properties.facets_form.limit(5);
  431. facet.properties.facets_form.mincount(1);
  432. }
  433. }
  434. if (pivot != null) {
  435. facet.properties.facets.push(pivot);
  436. vm.search();
  437. }
  438. }
  439. self.removePivotFacetValue = function(facet) {
  440. facet['pivot_facet'].properties.facets.remove(facet['value']);
  441. vm.search();
  442. }
  443. self.removeFacet = function (widget_id) {
  444. $.each(self.facets(), function (index, facet) {
  445. if (facet.id() == widget_id()) {
  446. self.facets.remove(facet);
  447. return false;
  448. }
  449. });
  450. }
  451. self.removeLeaflet = function (widget_json) {
  452. if (widget_json.widgetType() == "leafletmap-widget") {
  453. self.template.leafletmap.latitudeField(null);
  454. self.template.leafletmap.longitudeField(null);
  455. self.template.leafletmap.labelField(null);
  456. }
  457. }
  458. self.getFacetById = function (facet_id) {
  459. var _facet = null;
  460. $.each(self.facets(), function (index, facet) {
  461. if (facet.id() == facet_id) {
  462. _facet = facet;
  463. return false;
  464. }
  465. });
  466. return _facet;
  467. }
  468. self.getFacetByType = function (facetType) {
  469. var _facet = null;
  470. $.each(self.facets(), function (index, facet) {
  471. if (facet.widgetType() == facetType) {
  472. _facet = facet;
  473. return false;
  474. }
  475. });
  476. return _facet;
  477. }
  478. self.getHistogramFacet = function () { // Should do multi histogram
  479. return self.getFacetByType('histogram-widget');
  480. }
  481. self.template.fields = ko.computed(function () {
  482. var _fields = [];
  483. $.each(self.template.fieldsAttributes(), function (index, field) {
  484. var position = self.template.fieldsSelected.indexOf(field.name());
  485. if (position != -1) {
  486. _fields[position] = field;
  487. }
  488. });
  489. return _fields;
  490. });
  491. self.template.fieldsNames = ko.computed(function () {
  492. return $.map(self.template.fieldsAttributes(), function(field) {
  493. return field.name();
  494. });
  495. });
  496. self.getTemplateField = function (name) {
  497. var _field = null;
  498. $.each(self.template.fields(), function (index, field) {
  499. if (field.name() == name) {
  500. _field = field;
  501. return false;
  502. }
  503. });
  504. return _field;
  505. };
  506. self.template.fieldsModalFilter = ko.observable(""); // For UI
  507. self.template.fieldsModalType = ko.observable(""); // For UI
  508. self.template.fieldsAttributesFilter = ko.observable(""); // For UI
  509. self.template.filteredModalFields = ko.observableArray();
  510. self.template.filteredAttributeFieldsAll = ko.observable(true);
  511. self.template.filteredAttributeFields = ko.computed(function() {
  512. var _fields = [];
  513. var _iterable = self.template.fieldsAttributes();
  514. if (! self.template.filteredAttributeFieldsAll()){
  515. _iterable = self.template.fields();
  516. }
  517. $.each(_iterable, function (index, field) {
  518. if (self.template.fieldsAttributesFilter() == "" || field.name().toLowerCase().indexOf(self.template.fieldsAttributesFilter().toLowerCase()) > -1){
  519. _fields.push(field);
  520. }
  521. });
  522. return _fields;
  523. });
  524. self.template.availableWidgetFields = ko.computed(function() {
  525. if (self.template.fieldsModalType() == 'histogram-widget') {
  526. return vm.availableDateFields();
  527. }
  528. else if (self.template.fieldsModalType() == 'line-widget') {
  529. return vm.availableNumberFields();
  530. }
  531. else if (self.template.fieldsModalType() == 'tree-widget' || self.template.fieldsModalType() == 'heatmap-widget') {
  532. return vm.availablePivotFields();
  533. }
  534. else {
  535. return self.availableFacetFields();
  536. }
  537. });
  538. self.template.availableWidgetFieldsNames = ko.computed(function() {
  539. return $.map(self.template.availableWidgetFields(), function(field) {
  540. return field.name();
  541. });
  542. });
  543. self.template.fieldsModalFilter.subscribe(function(value) {
  544. var _fields = [];
  545. var _availableFields = self.template.availableWidgetFields();
  546. $.each(_availableFields, function (index, field) {
  547. if (self.template.fieldsModalFilter() == "" || field.name().toLowerCase().indexOf(self.template.fieldsModalFilter().toLowerCase()) > -1){
  548. _fields.push(field);
  549. }
  550. });
  551. self.template.filteredModalFields(_fields);
  552. });
  553. self.switchCollection = function() { // Long term would be to reload the page
  554. $.post("/search/get_collection", {
  555. name: self.name()
  556. }, function (data) {
  557. if (data.status == 0) {
  558. self.idField(data.collection.collection.idField);
  559. self.template.template(data.collection.collection.template.template);
  560. self.template.fieldsAttributes.removeAll();
  561. $.each(data.collection.collection.template.fieldsAttributes, function(index, field) {
  562. self.template.fieldsAttributes.push(ko.mapping.fromJS(field));
  563. });
  564. self.fields.removeAll();
  565. $.each(data.collection.collection.fields, function(index, field) {
  566. self.fields.push(ko.mapping.fromJS(field));
  567. });
  568. }
  569. }).fail(function (xhr, textStatus, errorThrown) {});
  570. };
  571. function diff(A, B) {
  572. return A.filter(function (a) {
  573. return B.indexOf(a) == -1;
  574. });
  575. }
  576. function syncArray(currentObservable, newJson, isDynamic) {
  577. // Get names of fields
  578. var _currentFieldsNames = $.map(
  579. $.grep(currentObservable(), function(field) {
  580. return field.isDynamic() == isDynamic;
  581. }), function(field) {
  582. return field.name();
  583. });
  584. var _newFieldsNames = $.map(
  585. $.grep(newJson, function(field) {
  586. return field.isDynamic == isDynamic;
  587. }), function(field) {
  588. return field.name;
  589. });
  590. var _toDelete = diff(_currentFieldsNames, _newFieldsNames);
  591. var _toAdd = diff(_newFieldsNames, _currentFieldsNames);
  592. // Deleted fields
  593. self.template.fieldsSelected.removeAll(_toDelete);
  594. var bulk = $.grep(currentObservable(), function(field) {
  595. return (_toDelete.indexOf(field.name()) != -1)
  596. });
  597. currentObservable.removeAll(bulk);
  598. // New fields
  599. $.each(newJson, function(index, field) {
  600. if (_toAdd.indexOf(field.name) != -1) {
  601. currentObservable.push(ko.mapping.fromJS(field));
  602. }
  603. });
  604. }
  605. self.syncFields = function() {
  606. $.post("/search/get_collection", {
  607. name: self.name()
  608. }, function (data) {
  609. if (data.status == 0) {
  610. self.idField(data.collection.collection.idField);
  611. syncArray(self.template.fieldsAttributes, data.collection.collection.template.fieldsAttributes, false);
  612. syncArray(self.fields, data.collection.collection.fields, false);
  613. }
  614. // After sync the dynamic fields
  615. self.syncDynamicFields()
  616. }).fail(function (xhr, textStatus, errorThrown) {});
  617. };
  618. self.syncDynamicFields = function () {
  619. $.post("/search/index/fields/dynamic", {
  620. name: self.name()
  621. }, function (data) {
  622. if (data.status == 0) {
  623. syncArray(self.template.fieldsAttributes, data.gridlayout_header_fields, true);
  624. syncArray(self.fields, data.fields, true);
  625. }
  626. }).fail(function (xhr, textStatus, errorThrown) {});
  627. };
  628. self.toggleSortColumnGridLayout = function (template_field) {
  629. if (! template_field.sort.direction()) {
  630. template_field.sort.direction('desc');
  631. } else if (template_field.sort.direction() == 'desc') {
  632. template_field.sort.direction('asc');
  633. } else {
  634. template_field.sort.direction(null);
  635. }
  636. $(document).trigger("setResultsHeight");
  637. vm.search();
  638. };
  639. self.toggleSortFacet = function (facet_field, event) {
  640. if (facet_field.properties.sort() == 'desc') {
  641. facet_field.properties.sort('asc');
  642. } else {
  643. facet_field.properties.sort('desc');
  644. }
  645. $(event.target).button('loading');
  646. vm.search();
  647. };
  648. self.toggleRangeFacet = function (facet_field, event) {
  649. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_field.id})); // Reset filter query
  650. if (facet_field.type() == 'field') {
  651. facet_field.type('range');
  652. } else if (facet_field.type() == 'range') {
  653. facet_field.type('field')
  654. }
  655. $(event.target).button('loading');
  656. vm.search();
  657. };
  658. self.selectTimelineFacet = function (data) {
  659. var facet = self.getFacetById(data.widget_id);
  660. facet.properties.start(data.from);
  661. facet.properties.end(data.to);
  662. vm.query.selectRangeFacet({widget_id: data.widget_id, from: data.from, to: data.to, cat: data.cat, no_refresh: true, force: true});
  663. $.ajax({
  664. type: "POST",
  665. url: "/search/get_range_facet",
  666. data: {
  667. collection: ko.mapping.toJSON(self),
  668. facet: ko.mapping.toJSON(facet),
  669. action: 'select'
  670. },
  671. success: function (data) {
  672. if (data.status == 0) {
  673. facet.properties.gap(data.properties.gap);
  674. }
  675. },
  676. async: false
  677. });
  678. vm.search();
  679. }
  680. self.rangeZoomOut = function (facet_json) {
  681. var facet = self.getFacetById(typeof facet_json.id == "function" ? facet_json.id() : facet_json.id);
  682. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_json.id}));
  683. //if (facet && facet.properties) {
  684. if (facet.properties.gap() != null) { // Bar, line charts don't have gap
  685. facet.properties.gap(facet.properties.initial_gap());
  686. }
  687. if (facet.properties.initial_start() != null) { // Bar and line charts
  688. facet.properties.start(facet.properties.initial_start());
  689. facet.properties.end(facet.properties.initial_end());
  690. }
  691. //}
  692. vm.search();
  693. }
  694. self.translateSelectedField = function (index, direction) {
  695. var array = self.template.fieldsSelected();
  696. if (direction == 'left') {
  697. self.template.fieldsSelected.splice(index - 1, 2, array[index], array[index - 1]);
  698. } else {
  699. self.template.fieldsSelected.splice(index, 2, array[index + 1], array[index]);
  700. }
  701. };
  702. self.upDownFacetLimit = function (facet_id, direction) {
  703. var facet = self.getFacetById(facet_id);
  704. if (facet.properties.prevLimit == undefined) {
  705. facet.properties.prevLimit = facet.properties.limit();
  706. }
  707. if (direction == 'up') {
  708. facet.properties.limit(facet.properties.limit() + 10);
  709. } else {
  710. facet.properties.limit(facet.properties.limit() - 10);
  711. }
  712. vm.search();
  713. };
  714. };
  715. var NewTemplate = function (vm, initial) {
  716. var self = this;
  717. self.collections = ko.mapping.fromJS(initial.collections);
  718. self.layout = initial.layout;
  719. self.inited = ko.observable(self.collections().length > 0); // No collection if not a new dashboard
  720. self.init = function() {
  721. if (self.inited()) {
  722. // If new dashboard
  723. vm.collection.name.subscribe(function(newValue) {
  724. vm.collection.label(newValue);
  725. vm.collection.switchCollection();
  726. vm.search();
  727. });
  728. } else {
  729. self.syncCollections();
  730. }
  731. if (initial.autoLoad) {
  732. magicLayout(vm);
  733. }
  734. };
  735. self.syncCollections = function () {
  736. $.post("/search/get_collections", {
  737. collection: ko.mapping.toJSON(vm.collection),
  738. }, function (data) {
  739. if (data.status == 0) {
  740. // Sync new and old names
  741. $.each(data.collection, function(index, name) {
  742. if (self.collections.indexOf(name) == -1) {
  743. self.collections.push(name);
  744. }
  745. });
  746. $.each(self.collections(), function(index, collection) {
  747. if (data.collection.indexOf(collection) == -1) {
  748. self.collections.remove(collection);
  749. }
  750. });
  751. }
  752. else {
  753. $(document).trigger("error", data.message);
  754. }
  755. }).fail(function (xhr, textStatus, errorThrown) {
  756. $(document).trigger("error", xhr.responseText);
  757. }).done(function() {
  758. self.inited(true);
  759. });
  760. };
  761. };
  762. var DATE_TYPES = ['date', 'tdate'];
  763. var NUMBER_TYPES = ['int', 'tint', 'long', 'tlong', 'float', 'tfloat', 'double', 'tdouble'];
  764. var RANGE_SELECTABLE_WIDGETS = ['histogram-widget', 'bar-widget', 'line-widget'];
  765. var SearchViewModel = function (collection_json, query_json, initial_json) {
  766. var self = this;
  767. self.intervalOptions = ko.observableArray(ko.bindingHandlers.daterangepicker.INTERVAL_OPTIONS);
  768. // Models
  769. self.collection = new Collection(self, collection_json.collection);
  770. self.query = new Query(self, query_json);
  771. self.initial = new NewTemplate(self, initial_json);
  772. // UI
  773. self.response = ko.observable({});
  774. self.results = ko.observableArray([]);
  775. self.norm_facets = ko.computed(function () {
  776. return self.response().normalized_facets;
  777. });
  778. self.getFacetFromQuery = function (facet_id) {
  779. var _facet = null;
  780. if (self.norm_facets() !== undefined) {
  781. $.each(self.norm_facets(), function (index, norm_facet) {
  782. if (norm_facet.id == facet_id) {
  783. _facet = norm_facet;
  784. }
  785. });
  786. }
  787. return _facet;
  788. };
  789. self.toggledGridlayoutResultChevron = ko.observable(false);
  790. self.enableGridlayoutResultChevron = function() {
  791. self.toggledGridlayoutResultChevron(true);
  792. };
  793. self.disableGridlayoutResultChevron = function() {
  794. self.toggledGridlayoutResultChevron(false);
  795. };
  796. self.fieldAnalyses = ko.observableArray([]);
  797. self.fieldAnalysesName = ko.observableArray("");
  798. self.fieldsAnalysisAttributesNames = ko.computed(function () {
  799. var _fields = [];
  800. $.each(self.collection.template.fieldsAttributes(), function (index, field) {
  801. if (field.name() != self.fieldAnalysesName()){
  802. _fields.push(field.name())
  803. }
  804. });
  805. return _fields;
  806. });
  807. self.previewColumns = ko.observable("");
  808. self.columns = ko.observable([]);
  809. loadLayout(self, collection_json.layout);
  810. self.isEditing = ko.observable(false);
  811. self.toggleEditing = function () {
  812. self.isEditing(! self.isEditing());
  813. };
  814. self.isRetrievingResults = ko.observable(false);
  815. function bareWidgetBuilder(name, type){
  816. return new Widget({
  817. size: 12,
  818. id: UUID(),
  819. name: name,
  820. widgetType: type
  821. });
  822. }
  823. self.draggableHit = ko.observable(bareWidgetBuilder("Hit Count", "hit-widget"));
  824. self.draggableFacet = ko.observable(bareWidgetBuilder("Facet", "facet-widget"));
  825. self.draggableResultset = ko.observable(bareWidgetBuilder("Grid Results", "resultset-widget"));
  826. self.draggableHtmlResultset = ko.observable(bareWidgetBuilder("HTML Results", "html-resultset-widget"));
  827. self.draggableHistogram = ko.observable(bareWidgetBuilder("Histogram", "histogram-widget"));
  828. self.draggableBar = ko.observable(bareWidgetBuilder("Bar Chart", "bar-widget"));
  829. self.draggableMap = ko.observable(bareWidgetBuilder("Map", "map-widget"));
  830. self.draggableLeafletMap = ko.observable(bareWidgetBuilder("Marker Map", "leafletmap-widget"));
  831. self.draggableLine = ko.observable(bareWidgetBuilder("Line Chart", "line-widget"));
  832. self.draggablePie = ko.observable(bareWidgetBuilder("Pie Chart", "pie-widget"));
  833. self.draggableFilter = ko.observable(bareWidgetBuilder("Filter Bar", "filter-widget"));
  834. self.draggableTree = ko.observable(bareWidgetBuilder("Tree", "tree-widget"));
  835. self.draggableHeatmap = ko.observable(bareWidgetBuilder("Heatmap", "heatmap-widget"));
  836. self.availableDateFields = ko.computed(function() {
  837. return $.grep(self.collection.availableFacetFields(), function(field) { return DATE_TYPES.indexOf(field.type()) != -1; });
  838. });
  839. self.availableNumberFields = ko.computed(function() {
  840. return $.grep(self.collection.availableFacetFields(), function(field) { return NUMBER_TYPES.indexOf(field.type()) != -1; });
  841. });
  842. self.availablePivotFields = ko.computed(function() {
  843. return self.collection.fields();
  844. });
  845. function getWidgets(equalsTo) {
  846. return $.map(self.columns(), function (col){return $.map(col.rows(), function(row){ return $.grep(row.widgets(), function(widget){ return equalsTo(widget); });}) ;})
  847. };
  848. self.availableDraggableResultset = ko.computed(function() {
  849. return getWidgets(function(widget) { return ['resultset-widget', 'html-resultset-widget'].indexOf(widget.widgetType()) != -1; }).length == 0;
  850. });
  851. self.availableDraggableLeaflet = ko.computed(function() {
  852. return getWidgets(function(widget) { return ['leafletmap-widget'].indexOf(widget.widgetType()) != -1; }).length == 0;
  853. });
  854. self.availableDraggableFilter = ko.computed(function() {
  855. return getWidgets(function(widget) { return widget.widgetType() == 'filter-widget'; }).length == 0;
  856. });
  857. self.availableDraggableHistogram = ko.computed(function() {
  858. return getWidgets(function(widget) { return widget.widgetType() == 'histogram-widget'; }).length == 0 && self.availableDateFields().length > 0;
  859. });
  860. self.availableDraggableNumbers = ko.computed(function() {
  861. return self.availableNumberFields().length > 0;
  862. });
  863. self.availableDraggableChart = ko.computed(function() {
  864. return self.collection.availableFacetFields().length > 0;
  865. });
  866. self.init = function (callback) {
  867. self.initial.init();
  868. self.collection.syncFields();
  869. self.search(callback);
  870. }
  871. self.searchBtn = function () {
  872. self.query.start(0);
  873. self.search();
  874. };
  875. self.search = function (callback) {
  876. self.isRetrievingResults(true);
  877. $(".jHueNotify").hide();
  878. logGA('search');
  879. // Multi queries
  880. var multiQs = [];
  881. var multiQ = self.query.getMultiq();
  882. if (multiQ != null) {
  883. var facet = {};
  884. var queries = [];
  885. if (multiQ == 'query') {
  886. queries = self.query.qs();
  887. } else {
  888. facet = self.query.getFacetFilter(self.query.selectedMultiq());
  889. queries = $.map(facet.filter(), function(f) { return f.value(); });
  890. }
  891. multiQs = $.map(queries, function(qdata) {
  892. return $.post("/search/get_timeline", {
  893. collection: ko.mapping.toJSON(self.collection),
  894. query: ko.mapping.toJSON(self.query),
  895. facet: ko.mapping.toJSON(facet),
  896. qdata: ko.mapping.toJSON(qdata),
  897. multiQ: multiQ
  898. }, function (data) {return data});
  899. });
  900. }
  901. $.each(self.fieldAnalyses(), function (index, analyse) { // Invalidate stats analysis
  902. analyse.stats.data.removeAll();
  903. });
  904. if (self.getFieldAnalysis()) {
  905. self.getFieldAnalysis().update();
  906. }
  907. $.when.apply($, [
  908. $.post("/search/search", {
  909. collection: ko.mapping.toJSON(self.collection),
  910. query: ko.mapping.toJSON(self.query),
  911. layout: ko.mapping.toJSON(self.columns)
  912. }, function (data) {
  913. if (typeof callback != undefined && callback != null){
  914. callback(data);
  915. }
  916. self.response(data);
  917. self.results.removeAll();
  918. if (data.error) {
  919. $(document).trigger("error", data.error);
  920. }
  921. else {
  922. if (self.collection.template.isGridLayout()) {
  923. // Table view
  924. var _docs = [];
  925. var leafletmap = {};
  926. $.each(data.response.docs, function (index, item) {
  927. var row = [];
  928. var _showDetails = item.showDetails;
  929. var _details = item.details;
  930. delete item["showDetails"];
  931. delete item["details"];
  932. var fields = self.collection.template.fieldsSelected();
  933. // Display selected fields or whole json document
  934. if (fields.length != 0) {
  935. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  936. row.push(item[field]);
  937. });
  938. } else {
  939. row.push(ko.mapping.toJSON(item));
  940. }
  941. if (self.collection.template.leafletmapOn()) {
  942. leafletmap = {
  943. 'latitude': item[self.collection.template.leafletmap.latitudeField()],
  944. 'longitude': item[self.collection.template.leafletmap.longitudeField()],
  945. 'label': self.collection.template.leafletmap.labelField() ? item[self.collection.template.leafletmap.labelField()] : ""
  946. }
  947. }
  948. var doc = {
  949. 'id': item[self.collection.idField()],
  950. 'row': row,
  951. 'showDetails': ko.observable(_showDetails),
  952. 'details': ko.observableArray(_details),
  953. 'leafletmap': leafletmap
  954. };
  955. _docs.push(doc);
  956. });
  957. self.results(_docs);
  958. }
  959. else {
  960. // Template view
  961. var _docs = [];
  962. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  963. $.each(data.response.docs, function (index, item) {
  964. // fix the fields that contain dots in the name
  965. addTemplateFunctions(item);
  966. _docs.push(Mustache.render(_mustacheTmpl, item));
  967. });
  968. self.results(_docs);
  969. }
  970. self.isRetrievingResults(false);
  971. }
  972. })
  973. ].concat(multiQs)
  974. )
  975. .done(function() {
  976. if (arguments[0] instanceof Array) { // If multi queries
  977. var histoFacetId = self.collection.getHistogramFacet().id();
  978. var histoFacet = self.getFacetFromQuery(histoFacetId);
  979. for (var i = 1; i < arguments.length; i++) {
  980. histoFacet.extraSeries.push(arguments[i][0]['series']);
  981. }
  982. self.response.valueHasMutated();
  983. }
  984. })
  985. .fail(function (xhr, textStatus, errorThrown) {
  986. $(document).trigger("error", xhr.responseText);
  987. })
  988. .always(function () {
  989. $('.btn-loading').button('reset');
  990. });
  991. };
  992. self.removeWidget = function (widget_json) {
  993. self.collection.removeFacet(widget_json.id);
  994. self.collection.removeLeaflet(widget_json);
  995. var refresh = self.query.removeFilter(widget_json);
  996. self.removeWidgetById(widget_json.id());
  997. if (refresh) {
  998. self.search();
  999. }
  1000. }
  1001. self.getWidgetById = function (widget_id) {
  1002. var _widget = null;
  1003. $.each(self.columns(), function (i, col) {
  1004. $.each(col.rows(), function (j, row) {
  1005. $.each(row.widgets(), function (z, widget) {
  1006. if (widget.id() == widget_id){
  1007. _widget = widget;
  1008. return false;
  1009. }
  1010. });
  1011. });
  1012. });
  1013. return _widget;
  1014. }
  1015. self.removeWidgetById = function (widget_id) {
  1016. $.each(self.columns(), function (i, col) {
  1017. $.each(col.rows(), function (j, row) {
  1018. $.each(row.widgets(), function (z, widget) {
  1019. if (widget && widget.id() == widget_id){
  1020. row.widgets.remove(widget);
  1021. row.autosizeWidgets();
  1022. return false;
  1023. }
  1024. });
  1025. });
  1026. });
  1027. }
  1028. self.getDocument = function (doc) {
  1029. $.post("/search/get_document", {
  1030. collection: ko.mapping.toJSON(self.collection),
  1031. id: doc.id
  1032. }, function (data) {
  1033. if (data.status == 0) {
  1034. $.each(data.doc.doc, function(key, val) {
  1035. doc['details'].push(ko.mapping.fromJS({
  1036. key: key,
  1037. value: val
  1038. }));
  1039. });
  1040. }
  1041. else if (data.status == 1) {
  1042. $(document).trigger("info", data.message);
  1043. doc['details'].push(ko.mapping.fromJS({
  1044. key: '',
  1045. value: ''
  1046. }));
  1047. }
  1048. else {
  1049. $(document).trigger("error", data.message);
  1050. }
  1051. }).fail(function (xhr, textStatus, errorThrown) {
  1052. $(document).trigger("error", xhr.responseText);
  1053. });
  1054. };
  1055. self.showFieldAnalysis = function() {
  1056. if (self.fieldAnalysesName()) {
  1057. var analyse = self.getFieldAnalysis();
  1058. if (analyse == null) {
  1059. analyse = new FieldAnalysis(self, self.fieldAnalysesName());
  1060. self.fieldAnalyses.push(analyse);
  1061. }
  1062. analyse.update();
  1063. $(document).trigger("shownAnalysis")
  1064. }
  1065. }
  1066. self.getFieldAnalysis = function() {
  1067. var field_name = self.fieldAnalysesName();
  1068. var _analyse = null;
  1069. $.each(self.fieldAnalyses(), function (index, analyse) {
  1070. if (analyse.name() == field_name) {
  1071. _analyse = analyse;
  1072. return false;
  1073. }
  1074. });
  1075. return _analyse;
  1076. }
  1077. self.save = function () {
  1078. $.post("/search/save", {
  1079. collection: ko.mapping.toJSON(self.collection),
  1080. layout: ko.mapping.toJSON(self.columns)
  1081. }, function (data) {
  1082. if (data.status == 0) {
  1083. self.collection.id = data.id;
  1084. $(document).trigger("info", data.message);
  1085. if (window.location.search.indexOf("collection") == -1) {
  1086. window.location.hash = '#collection=' + data.id;
  1087. }
  1088. }
  1089. else {
  1090. $(document).trigger("error", data.message);
  1091. }
  1092. }).fail(function (xhr, textStatus, errorThrown) {
  1093. $(document).trigger("error", xhr.responseText);
  1094. });
  1095. };
  1096. };
  1097. function logGA(page) {
  1098. if (typeof trackOnGA == 'function') {
  1099. trackOnGA('search/' + page);
  1100. }
  1101. }