impala-dashboard.ko.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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([{'q': ''}]);
  49. self.fqs = ko.mapping.fromJS([]);
  50. self.toggleFacet = function (data) {
  51. var fq = self.getFacetFilter(data.widget.id());
  52. if (fq == null) {
  53. self.fqs.push(ko.mapping.fromJS({
  54. 'id': data.widget.id(),
  55. 'field': data.widget.field(),
  56. 'filter': [data.facet[0]],
  57. 'type': 'field'
  58. }));
  59. } else {
  60. $.each(self.fqs(), function (index, fq) {
  61. if (fq.id() == data.widget.id()) {
  62. if (fq.filter.indexOf(data.facet[0]) > -1) {
  63. fq.filter.remove(data.facet[0]);
  64. if (fq.filter().length == 0) {
  65. self.fqs.remove(fq);
  66. }
  67. } else {
  68. fq.filter.push(data.facet[0]);
  69. }
  70. }
  71. });
  72. }
  73. vm.search();
  74. }
  75. self.getFacetFilter = function (facet_id) {
  76. var _facet = null;
  77. $.each(self.fqs(), function (index, facet) {
  78. if (facet.id() == facet_id) {
  79. _facet = facet;
  80. return false;
  81. }
  82. });
  83. return _facet;
  84. }
  85. }
  86. var Dashboard = function (vm, dashboard) {
  87. var self = this;
  88. self.facets = ko.mapping.fromJS(dashboard.facets);
  89. self.properties = ko.mapping.fromJS(dashboard.properties);
  90. self.dropdownDbs = ko.observableArray([]);
  91. self.selectedDropdownDb = ko.observable("");
  92. self.selectedDropdownDb.subscribe(function(value) {
  93. self.properties()[0].database(self.selectedDropdownDb());
  94. if (self.selectedDropdownDb() && self.selectedDropdownTable()) {
  95. self.updateFields();
  96. }
  97. });
  98. self.dropdownTables = ko.observableArray([]);
  99. self.selectedDropdownTable = ko.observable("");
  100. self.selectedDropdownTable.subscribe(function(value) {
  101. self.properties()[0].table(self.selectedDropdownTable());
  102. if (self.selectedDropdownDb() && self.selectedDropdownTable()) {
  103. self.updateFields();
  104. }
  105. });
  106. self.updateDropdownDatabases = function(databases) {
  107. if (databases) {
  108. var i = databases.indexOf("_impala_builtins"); // Blacklist of system databases
  109. if (i != -1) {
  110. databases.splice(i, 1);
  111. }
  112. self.dropdownDbs(databases);
  113. }
  114. };
  115. self.fields = ko.computed(function () {
  116. if (self.properties() != null && self.properties().length > 0 && self.properties()[0].fields != null){
  117. return self.properties()[0].fields();
  118. }
  119. return [];
  120. });
  121. self.fieldNames = ko.computed(function () {
  122. return $.map(self.fields(), function (field) { return field.name() });
  123. });
  124. self.resultsetShowFieldList = ko.observable(true);
  125. self.resultsetFieldsFilter = ko.observable(""); // For UI
  126. self.resultsetSelectedFields = ko.observableArray(self.fieldNames());
  127. self.resultsetSelectedFields.subscribe(function(value) {
  128. vm.search();
  129. });
  130. self.resultsetFilteredFields = ko.computed(function() {
  131. var _fields = [];
  132. $.each(self.fields(), function (index, field) {
  133. if (self.resultsetFieldsFilter() == "" || field.name().toLowerCase().indexOf(self.resultsetFieldsFilter().toLowerCase()) > -1){
  134. _fields.push(field);
  135. }
  136. });
  137. return _fields;
  138. });
  139. self.selectedNewFacetField = ko.observable();
  140. self.addFacet = function(facet_json) {
  141. $.post("/impala/dashboard/new_facet", {
  142. "dashboard": ko.mapping.toJSON(self),
  143. "facet_json": ko.mapping.toJSON(facet_json),
  144. "field": self.selectedNewFacetField(),
  145. }, function (data) {
  146. if (data.status == 0) {
  147. var facet = ko.mapping.fromJS(data.facet);
  148. facet.properties.limit.subscribe(function () {
  149. vm.search();
  150. });
  151. self.facets.push(facet);
  152. vm.search();
  153. } else {
  154. $(document).trigger("error", data.message);
  155. }
  156. }).fail(function (xhr, textStatus, errorThrown) {});
  157. }
  158. self.getFacetById = function (facet_id) {
  159. var _facet = null;
  160. $.each(self.facets(), function (index, facet) {
  161. if (facet.id == facet_id) {
  162. _facet = facet;
  163. return false;
  164. }
  165. });
  166. return _facet;
  167. }
  168. self.updateFields = function(database, table) {
  169. $.post("/impala/dashboard/get_fields", {
  170. "database": self.properties()[0].database(),
  171. "table": self.properties()[0].table()
  172. }, function (data) {
  173. if (data.status == 0) {
  174. self.properties()[0].fields.removeAll();
  175. self.resultsetSelectedFields.removeAll();
  176. $.each(data.fields, function (index, field) {
  177. self.properties()[0].fields.push(ko.mapping.fromJS(field));
  178. self.resultsetSelectedFields.push(field.name);
  179. });
  180. }
  181. }).fail(function (xhr, textStatus, errorThrown) {});
  182. }
  183. }
  184. var ImpalaDashboardViewModel = function (query_json, dashboard_json, initial_json) {
  185. var self = this;
  186. self.isEditing = ko.observable(true);
  187. self.toggleEditing = function () {
  188. self.isEditing(! self.isEditing());
  189. };
  190. self.isRetrievingResults = ko.observable(false);
  191. self.previewColumns = ko.observable("");
  192. self.columns = ko.observable([]);
  193. loadLayout(self, dashboard_json.layout);
  194. self.query = new Query(self, query_json);
  195. self.dashboard = new Dashboard(self, dashboard_json);
  196. self.initial = initial_json;
  197. self.results = ko.observableArray([]);
  198. self.results_facet = ko.observableArray([]);
  199. self.results_cols = ko.observableArray([]);
  200. self.inited = ko.observable(self.columns().length > 0);
  201. self.init = function(callback) {
  202. if (self.inited()){
  203. self.search();
  204. }
  205. callback();
  206. }
  207. self.search = function (callback) {
  208. self.isRetrievingResults(true);
  209. self.results.removeAll();
  210. var multiQs = $.map(self.dashboard.facets(), function(facet) {
  211. return $.post("/impala/dashboard/query", {
  212. "query": ko.mapping.toJSON(self.query),
  213. "dashboard": ko.mapping.toJSON(self.dashboard),
  214. "layout": ko.mapping.toJSON(self.columns),
  215. "facet": ko.mapping.toJSON(facet),
  216. }, function (data) {return data});
  217. });
  218. $.when.apply($, [
  219. $.post("/impala/dashboard/query", {
  220. "query": ko.mapping.toJSON(self.query),
  221. "dashboard": ko.mapping.toJSON(self.dashboard),
  222. "layout": ko.mapping.toJSON(self.columns),
  223. }, function (data) {
  224. self.isRetrievingResults(false);
  225. if (data.status == 0) {
  226. self.results_cols(data.cols)
  227. $.each(data.data, function (index, row) {
  228. self.results.push(row);
  229. });
  230. } else {
  231. $(document).trigger("error", data.message);
  232. }
  233. })
  234. ].concat(multiQs)
  235. )
  236. .done(function() {
  237. if (multiQs.length > 0) {
  238. for (var i = 1; i < arguments.length; i++) {
  239. var facet = arguments[i][0];
  240. removeFrom(self.results_facet, facet.id);
  241. self.results_facet.push(ko.mapping.fromJS(facet));
  242. }
  243. }
  244. })
  245. .fail(function (xhr, textStatus, errorThrown) {
  246. $(document).trigger("error", errorThrown);
  247. });
  248. };
  249. function removeFrom(collection, item_id) {
  250. $.each(collection(), function (index, item) {
  251. if (item.id() == item_id) {
  252. collection.remove(item);
  253. return false;
  254. }
  255. });
  256. }
  257. self.getFacetFromResult = function (facet_id) {
  258. var _facet = null;
  259. $.each(self.results_facet(), function (index, facet) {
  260. if (facet.id() == facet_id) {
  261. _facet = facet;
  262. return false;
  263. }
  264. });
  265. return _facet;
  266. }
  267. self.getWidgetById = function (widget_id) {
  268. var _widget = null;
  269. $.each(self.columns(), function (i, col) {
  270. $.each(col.rows(), function (j, row) {
  271. $.each(row.widgets(), function (z, widget) {
  272. if (widget.id() == widget_id){
  273. _widget = widget;
  274. }
  275. });
  276. });
  277. });
  278. return _widget;
  279. }
  280. function bareWidgetBuilder(name, type){
  281. return new Widget({
  282. size: 12,
  283. id: UUID(),
  284. name: name,
  285. widgetType: type
  286. });
  287. }
  288. self.draggableFacet = ko.observable(bareWidgetBuilder("Facet", "facet-widget"));
  289. self.draggableResultset = ko.observable(bareWidgetBuilder("Grid Results", "resultset-widget"));
  290. self.draggableHistogram = ko.observable(bareWidgetBuilder("Histogram", "histogram-widget"));
  291. self.draggableBar = ko.observable(bareWidgetBuilder("Bar Chart", "bar-widget"));
  292. self.draggableMap = ko.observable(bareWidgetBuilder("Map", "map-widget"));
  293. self.draggableLine = ko.observable(bareWidgetBuilder("Line Chart", "line-widget"));
  294. self.draggablePie = ko.observable(bareWidgetBuilder("Pie Chart", "pie-widget"));
  295. self.draggableFilter = ko.observable(bareWidgetBuilder("Filter Bar", "filter-widget"));
  296. };