search.ko.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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(facet_json.id);
  682. vm.query.removeFilter(ko.mapping.fromJS({'id': facet_json.id}));
  683. if (facet.properties.gap() != null) { // Bar, line charts don't have gap
  684. facet.properties.gap(facet.properties.initial_gap());
  685. }
  686. vm.search();
  687. }
  688. self.translateSelectedField = function (index, direction) {
  689. var array = self.template.fieldsSelected();
  690. if (direction == 'left') {
  691. self.template.fieldsSelected.splice(index - 1, 2, array[index], array[index - 1]);
  692. } else {
  693. self.template.fieldsSelected.splice(index, 2, array[index + 1], array[index]);
  694. }
  695. };
  696. self.upDownFacetLimit = function (facet_id, direction) {
  697. var facet = self.getFacetById(facet_id);
  698. if (facet.properties.prevLimit == undefined) {
  699. facet.properties.prevLimit = facet.properties.limit();
  700. }
  701. if (direction == 'up') {
  702. facet.properties.limit(facet.properties.limit() + 10);
  703. } else {
  704. facet.properties.limit(facet.properties.limit() - 10);
  705. }
  706. vm.search();
  707. };
  708. };
  709. var NewTemplate = function (vm, initial) {
  710. var self = this;
  711. self.collections = ko.mapping.fromJS(initial.collections);
  712. self.layout = initial.layout;
  713. self.inited = ko.observable(self.collections().length > 0); // No collection if not a new dashboard
  714. self.init = function() {
  715. if (self.inited()) {
  716. // If new dashboard
  717. vm.collection.name.subscribe(function(newValue) {
  718. vm.collection.label(newValue);
  719. vm.collection.switchCollection();
  720. vm.search();
  721. });
  722. } else {
  723. self.syncCollections();
  724. }
  725. if (initial.autoLoad) {
  726. magicLayout(vm);
  727. }
  728. };
  729. self.syncCollections = function () {
  730. $.post("/search/get_collections", {
  731. collection: ko.mapping.toJSON(vm.collection),
  732. }, function (data) {
  733. if (data.status == 0) {
  734. // Sync new and old names
  735. $.each(data.collection, function(index, name) {
  736. if (self.collections.indexOf(name) == -1) {
  737. self.collections.push(name);
  738. }
  739. });
  740. $.each(self.collections(), function(index, collection) {
  741. if (data.collection.indexOf(collection) == -1) {
  742. self.collections.remove(collection);
  743. }
  744. });
  745. }
  746. else {
  747. $(document).trigger("error", data.message);
  748. }
  749. }).fail(function (xhr, textStatus, errorThrown) {
  750. $(document).trigger("error", xhr.responseText);
  751. }).done(function() {
  752. self.inited(true);
  753. });
  754. };
  755. };
  756. var DATE_TYPES = ['date', 'tdate'];
  757. var NUMBER_TYPES = ['int', 'tint', 'long', 'tlong', 'float', 'tfloat', 'double', 'tdouble'];
  758. var RANGE_SELECTABLE_WIDGETS = ['histogram-widget', 'bar-widget', 'line-widget'];
  759. var SearchViewModel = function (collection_json, query_json, initial_json) {
  760. var self = this;
  761. self.intervalOptions = ko.observableArray(ko.bindingHandlers.daterangepicker.INTERVAL_OPTIONS);
  762. // Models
  763. self.collection = new Collection(self, collection_json.collection);
  764. self.query = new Query(self, query_json);
  765. self.initial = new NewTemplate(self, initial_json);
  766. // UI
  767. self.response = ko.observable({});
  768. self.results = ko.observableArray([]);
  769. self.norm_facets = ko.computed(function () {
  770. return self.response().normalized_facets;
  771. });
  772. self.getFacetFromQuery = function (facet_id) {
  773. var _facet = null;
  774. if (self.norm_facets() !== undefined) {
  775. $.each(self.norm_facets(), function (index, norm_facet) {
  776. if (norm_facet.id == facet_id) {
  777. _facet = norm_facet;
  778. }
  779. });
  780. }
  781. return _facet;
  782. };
  783. self.toggledGridlayoutResultChevron = ko.observable(false);
  784. self.enableGridlayoutResultChevron = function() {
  785. self.toggledGridlayoutResultChevron(true);
  786. };
  787. self.disableGridlayoutResultChevron = function() {
  788. self.toggledGridlayoutResultChevron(false);
  789. };
  790. self.fieldAnalyses = ko.observableArray([]);
  791. self.fieldAnalysesName = ko.observableArray("");
  792. self.fieldsAnalysisAttributesNames = ko.computed(function () {
  793. var _fields = [];
  794. $.each(self.collection.template.fieldsAttributes(), function (index, field) {
  795. if (field.name() != self.fieldAnalysesName()){
  796. _fields.push(field.name())
  797. }
  798. });
  799. return _fields;
  800. });
  801. self.previewColumns = ko.observable("");
  802. self.columns = ko.observable([]);
  803. loadLayout(self, collection_json.layout);
  804. self.isEditing = ko.observable(false);
  805. self.toggleEditing = function () {
  806. self.isEditing(! self.isEditing());
  807. };
  808. self.isRetrievingResults = ko.observable(false);
  809. function bareWidgetBuilder(name, type){
  810. return new Widget({
  811. size: 12,
  812. id: UUID(),
  813. name: name,
  814. widgetType: type
  815. });
  816. }
  817. self.draggableHit = ko.observable(bareWidgetBuilder("Hit Count", "hit-widget"));
  818. self.draggableFacet = ko.observable(bareWidgetBuilder("Facet", "facet-widget"));
  819. self.draggableResultset = ko.observable(bareWidgetBuilder("Grid Results", "resultset-widget"));
  820. self.draggableHtmlResultset = ko.observable(bareWidgetBuilder("HTML Results", "html-resultset-widget"));
  821. self.draggableHistogram = ko.observable(bareWidgetBuilder("Histogram", "histogram-widget"));
  822. self.draggableBar = ko.observable(bareWidgetBuilder("Bar Chart", "bar-widget"));
  823. self.draggableMap = ko.observable(bareWidgetBuilder("Map", "map-widget"));
  824. self.draggableLeafletMap = ko.observable(bareWidgetBuilder("Marker Map", "leafletmap-widget"));
  825. self.draggableLine = ko.observable(bareWidgetBuilder("Line Chart", "line-widget"));
  826. self.draggablePie = ko.observable(bareWidgetBuilder("Pie Chart", "pie-widget"));
  827. self.draggableFilter = ko.observable(bareWidgetBuilder("Filter Bar", "filter-widget"));
  828. self.draggableTree = ko.observable(bareWidgetBuilder("Tree", "tree-widget"));
  829. self.draggableHeatmap = ko.observable(bareWidgetBuilder("Heatmap", "heatmap-widget"));
  830. self.availableDateFields = ko.computed(function() {
  831. return $.grep(self.collection.availableFacetFields(), function(field) { return DATE_TYPES.indexOf(field.type()) != -1; });
  832. });
  833. self.availableNumberFields = ko.computed(function() {
  834. return $.grep(self.collection.availableFacetFields(), function(field) { return NUMBER_TYPES.indexOf(field.type()) != -1; });
  835. });
  836. self.availablePivotFields = ko.computed(function() {
  837. return self.collection.fields();
  838. });
  839. function getWidgets(equalsTo) {
  840. return $.map(self.columns(), function (col){return $.map(col.rows(), function(row){ return $.grep(row.widgets(), function(widget){ return equalsTo(widget); });}) ;})
  841. };
  842. self.availableDraggableResultset = ko.computed(function() {
  843. return getWidgets(function(widget) { return ['resultset-widget', 'html-resultset-widget'].indexOf(widget.widgetType()) != -1; }).length == 0;
  844. });
  845. self.availableDraggableLeaflet = ko.computed(function() {
  846. return getWidgets(function(widget) { return ['leafletmap-widget'].indexOf(widget.widgetType()) != -1; }).length == 0;
  847. });
  848. self.availableDraggableFilter = ko.computed(function() {
  849. return getWidgets(function(widget) { return widget.widgetType() == 'filter-widget'; }).length == 0;
  850. });
  851. self.availableDraggableHistogram = ko.computed(function() {
  852. return getWidgets(function(widget) { return widget.widgetType() == 'histogram-widget'; }).length == 0 && self.availableDateFields().length > 0;
  853. });
  854. self.availableDraggableNumbers = ko.computed(function() {
  855. return self.availableNumberFields().length > 0;
  856. });
  857. self.availableDraggableChart = ko.computed(function() {
  858. return self.collection.availableFacetFields().length > 0;
  859. });
  860. self.init = function (callback) {
  861. self.initial.init();
  862. self.collection.syncFields();
  863. self.search(callback);
  864. }
  865. self.searchBtn = function () {
  866. self.query.start(0);
  867. self.search();
  868. };
  869. self.search = function (callback) {
  870. self.isRetrievingResults(true);
  871. $(".jHueNotify").hide();
  872. logGA('search');
  873. // Multi queries
  874. var multiQs = [];
  875. var multiQ = self.query.getMultiq();
  876. if (multiQ != null) {
  877. var facet = {};
  878. var queries = [];
  879. if (multiQ == 'query') {
  880. queries = self.query.qs();
  881. } else {
  882. facet = self.query.getFacetFilter(self.query.selectedMultiq());
  883. queries = $.map(facet.filter(), function(f) { return f.value(); });
  884. }
  885. multiQs = $.map(queries, function(qdata) {
  886. return $.post("/search/get_timeline", {
  887. collection: ko.mapping.toJSON(self.collection),
  888. query: ko.mapping.toJSON(self.query),
  889. facet: ko.mapping.toJSON(facet),
  890. qdata: ko.mapping.toJSON(qdata),
  891. multiQ: multiQ
  892. }, function (data) {return data});
  893. });
  894. }
  895. $.each(self.fieldAnalyses(), function (index, analyse) { // Invalidate stats analysis
  896. analyse.stats.data.removeAll();
  897. });
  898. if (self.getFieldAnalysis()) {
  899. self.getFieldAnalysis().update();
  900. }
  901. $.when.apply($, [
  902. $.post("/search/search", {
  903. collection: ko.mapping.toJSON(self.collection),
  904. query: ko.mapping.toJSON(self.query),
  905. layout: ko.mapping.toJSON(self.columns)
  906. }, function (data) {
  907. if (typeof callback != undefined && callback != null){
  908. callback(data);
  909. }
  910. self.response(data);
  911. self.results.removeAll();
  912. if (data.error) {
  913. $(document).trigger("error", data.error);
  914. }
  915. else {
  916. if (self.collection.template.isGridLayout()) {
  917. // Table view
  918. var _docs = [];
  919. var leafletmap = {};
  920. $.each(data.response.docs, function (index, item) {
  921. var row = [];
  922. var _showDetails = item.showDetails;
  923. var _details = item.details;
  924. delete item["showDetails"];
  925. delete item["details"];
  926. var fields = self.collection.template.fieldsSelected();
  927. // Display selected fields or whole json document
  928. if (fields.length != 0) {
  929. $.each(self.collection.template.fieldsSelected(), function (index, field) {
  930. row.push(item[field]);
  931. });
  932. } else {
  933. row.push(ko.mapping.toJSON(item));
  934. }
  935. if (self.collection.template.leafletmapOn()) {
  936. leafletmap = {
  937. 'latitude': item[self.collection.template.leafletmap.latitudeField()],
  938. 'longitude': item[self.collection.template.leafletmap.longitudeField()],
  939. 'label': self.collection.template.leafletmap.labelField() ? item[self.collection.template.leafletmap.labelField()] : ""
  940. }
  941. }
  942. var doc = {
  943. 'id': item[self.collection.idField()],
  944. 'row': row,
  945. 'showDetails': ko.observable(_showDetails),
  946. 'details': ko.observableArray(_details),
  947. 'leafletmap': leafletmap
  948. };
  949. _docs.push(doc);
  950. });
  951. self.results(_docs);
  952. }
  953. else {
  954. // Template view
  955. var _docs = [];
  956. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.collection.template.template());
  957. $.each(data.response.docs, function (index, item) {
  958. // fix the fields that contain dots in the name
  959. addTemplateFunctions(item);
  960. _docs.push(Mustache.render(_mustacheTmpl, item));
  961. });
  962. self.results(_docs);
  963. }
  964. self.isRetrievingResults(false);
  965. }
  966. })
  967. ].concat(multiQs)
  968. )
  969. .done(function() {
  970. if (arguments[0] instanceof Array) { // If multi queries
  971. var histoFacetId = self.collection.getHistogramFacet().id();
  972. var histoFacet = self.getFacetFromQuery(histoFacetId);
  973. for (var i = 1; i < arguments.length; i++) {
  974. histoFacet.extraSeries.push(arguments[i][0]['series']);
  975. }
  976. self.response.valueHasMutated();
  977. }
  978. })
  979. .fail(function (xhr, textStatus, errorThrown) {
  980. $(document).trigger("error", xhr.responseText);
  981. })
  982. .always(function () {
  983. $('.btn-loading').button('reset');
  984. });
  985. };
  986. self.removeWidget = function (widget_json) {
  987. self.collection.removeFacet(widget_json.id);
  988. self.collection.removeLeaflet(widget_json);
  989. var refresh = self.query.removeFilter(widget_json);
  990. self.removeWidgetById(widget_json.id());
  991. if (refresh) {
  992. self.search();
  993. }
  994. }
  995. self.getWidgetById = function (widget_id) {
  996. var _widget = null;
  997. $.each(self.columns(), function (i, col) {
  998. $.each(col.rows(), function (j, row) {
  999. $.each(row.widgets(), function (z, widget) {
  1000. if (widget.id() == widget_id){
  1001. _widget = widget;
  1002. return false;
  1003. }
  1004. });
  1005. });
  1006. });
  1007. return _widget;
  1008. }
  1009. self.removeWidgetById = function (widget_id) {
  1010. $.each(self.columns(), function (i, col) {
  1011. $.each(col.rows(), function (j, row) {
  1012. $.each(row.widgets(), function (z, widget) {
  1013. if (widget && widget.id() == widget_id){
  1014. row.widgets.remove(widget);
  1015. row.autosizeWidgets();
  1016. return false;
  1017. }
  1018. });
  1019. });
  1020. });
  1021. }
  1022. self.getDocument = function (doc) {
  1023. $.post("/search/get_document", {
  1024. collection: ko.mapping.toJSON(self.collection),
  1025. id: doc.id
  1026. }, function (data) {
  1027. if (data.status == 0) {
  1028. $.each(data.doc.doc, function(key, val) {
  1029. doc['details'].push(ko.mapping.fromJS({
  1030. key: key,
  1031. value: val
  1032. }));
  1033. });
  1034. }
  1035. else if (data.status == 1) {
  1036. $(document).trigger("info", data.message);
  1037. doc['details'].push(ko.mapping.fromJS({
  1038. key: '',
  1039. value: ''
  1040. }));
  1041. }
  1042. else {
  1043. $(document).trigger("error", data.message);
  1044. }
  1045. }).fail(function (xhr, textStatus, errorThrown) {
  1046. $(document).trigger("error", xhr.responseText);
  1047. });
  1048. };
  1049. self.showFieldAnalysis = function() {
  1050. if (self.fieldAnalysesName()) {
  1051. var analyse = self.getFieldAnalysis();
  1052. if (analyse == null) {
  1053. analyse = new FieldAnalysis(self, self.fieldAnalysesName());
  1054. self.fieldAnalyses.push(analyse);
  1055. }
  1056. analyse.update();
  1057. $(document).trigger("shownAnalysis")
  1058. }
  1059. }
  1060. self.getFieldAnalysis = function() {
  1061. var field_name = self.fieldAnalysesName();
  1062. var _analyse = null;
  1063. $.each(self.fieldAnalyses(), function (index, analyse) {
  1064. if (analyse.name() == field_name) {
  1065. _analyse = analyse;
  1066. return false;
  1067. }
  1068. });
  1069. return _analyse;
  1070. }
  1071. self.save = function () {
  1072. $.post("/search/save", {
  1073. collection: ko.mapping.toJSON(self.collection),
  1074. layout: ko.mapping.toJSON(self.columns)
  1075. }, function (data) {
  1076. if (data.status == 0) {
  1077. self.collection.id = data.id;
  1078. $(document).trigger("info", data.message);
  1079. if (window.location.search.indexOf("collection") == -1) {
  1080. window.location.hash = '#collection=' + data.id;
  1081. }
  1082. }
  1083. else {
  1084. $(document).trigger("error", data.message);
  1085. }
  1086. }).fail(function (xhr, textStatus, errorThrown) {
  1087. $(document).trigger("error", xhr.responseText);
  1088. });
  1089. };
  1090. };
  1091. function logGA(page) {
  1092. if (typeof trackOnGA == 'function') {
  1093. trackOnGA('search/' + page);
  1094. }
  1095. }