search.ko.js 39 KB

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