search.ko.js 34 KB

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