search.ko.js 35 KB

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