collections.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // Defaults
  2. var SOURCES = [
  3. 'file',
  4. // 'hbase',
  5. // 'hive'
  6. ];
  7. var SOURCE_TYPES = [
  8. 'log',
  9. 'separated',
  10. // 'morphlines'
  11. ];
  12. var FIELD_TYPES = [
  13. "alphaOnlySort",
  14. "ancestor_path",
  15. "boolean",
  16. "currency",
  17. "date",
  18. "descendent_path",
  19. "double",
  20. "float",
  21. "int",
  22. "location",
  23. "location_rpt",
  24. "long",
  25. "lowercase",
  26. "pdate",
  27. "pdouble",
  28. "pfloat",
  29. "pint",
  30. "plong",
  31. "point",
  32. "random",
  33. "string",
  34. "tdate",
  35. "tdouble",
  36. "text_ar",
  37. "text_bg",
  38. "text_ca",
  39. "text_char_norm",
  40. "text_cjk",
  41. "text_cz",
  42. "text_da",
  43. "text_de",
  44. "text_el",
  45. "text_en",
  46. "text_en_splitting",
  47. "text_en_splitting_tight",
  48. "text_es",
  49. "text_eu",
  50. "text_fa",
  51. "text_fi",
  52. "text_fr",
  53. "text_ga",
  54. "text_general",
  55. "text_general_rev",
  56. "text_gl",
  57. "text_greek",
  58. "text_hi",
  59. "text_hu",
  60. "text_hy",
  61. "text_id",
  62. "text_it",
  63. "text_ja",
  64. "text_lv",
  65. "text_nl",
  66. "text_no",
  67. "text_pt",
  68. "text_ro",
  69. "text_ru",
  70. "text_sv",
  71. "text_th",
  72. "text_tr",
  73. "text_ws",
  74. "tfloat",
  75. "tint",
  76. "tlong"
  77. ];
  78. var FIELD_SEPARATORS = [
  79. ',',
  80. '\t',
  81. ];
  82. var FIELD_QUOTE_CHARACTERS = [
  83. '"',
  84. "'"
  85. ];
  86. // View Models
  87. var HiveViewModel = function() {
  88. var self = this;
  89. self.databases = ko.observableArray();
  90. self.database = ko.observable().extend({'errors': null});
  91. self.table = ko.observable().extend({'errors': null});
  92. self.database.subscribe(function() {
  93. self.loadTables();
  94. });
  95. self.table.subscribe(function() {
  96. self.loadColumns();
  97. });
  98. self.loadDatabases = function() {
  99. return $.get("/beeswax/api/autocomplete").done(function(data) {
  100. if (data.databases.length > 0) {
  101. self.databases($.map(data.databases, function(database) { return new HiveDatabase(database); }));
  102. self.database(self.databases()[0]);
  103. }
  104. })
  105. .fail(function (xhr, textStatus, errorThrown) {
  106. $(document).trigger("error", xhr.responseText);
  107. });
  108. };
  109. self.loadTables = function() {
  110. return $.get("/beeswax/api/autocomplete/" + self.database().name()).done(function(data) {
  111. if (data.tables.length > 0) {
  112. self.database().tables($.map(data.tables, function(table) { return new HiveTable(table); }));
  113. self.table(self.database().tables()[0]);
  114. }
  115. })
  116. .fail(function (xhr, textStatus, errorThrown) {
  117. $(document).trigger("error", xhr.responseText);
  118. });
  119. };
  120. self.loadColumns = function() {
  121. return $.get("/beeswax/api/autocomplete/" + self.database().name() + '/' + self.table().name()).done(function(data) {
  122. if (data.columns.length > 0) {
  123. self.table().columns(data.columns);
  124. }
  125. })
  126. .fail(function (xhr, textStatus, errorThrown) {
  127. $(document).trigger("error", xhr.responseText);
  128. });
  129. };
  130. // self.loadDatabases();
  131. };
  132. var HBaseViewModel = function() {
  133. var self = this;
  134. self.clusters = ko.observableArray();
  135. self.cluster = ko.observable().extend({'errors': null});
  136. self.table = ko.observable().extend({'errors': null});
  137. self.indexMapping = {};
  138. self.cluster.subscribe(function() {
  139. self.loadTables();
  140. });
  141. self.loadClusters = function() {
  142. return $.get("/hbase/api/getClusters").done(function(data) {
  143. if (data.data.length > 0) {
  144. self.clusters($.map(data.data, function(cluster) { return new HBaseCluster(cluster.name); }));
  145. self.cluster(self.clusters()[0]);
  146. }
  147. })
  148. .fail(function (xhr, textStatus, errorThrown) {
  149. $(document).trigger("error", xhr.responseText);
  150. });
  151. };
  152. self.loadTables = function() {
  153. return $.get("/hbase/api/getTableList/" + self.cluster().name()).done(function(data) {
  154. if (data.data.length > 0) {
  155. // @TODO(Abe): Check if enabled
  156. self.cluster().tables($.map(data.data, function(table) { return new HBaseTable(table.name); }));
  157. self.table(self.cluster().tables()[0]);
  158. }
  159. })
  160. .fail(function (xhr, textStatus, errorThrown) {
  161. $(document).trigger("error", xhr.responseText);
  162. });
  163. };
  164. // self.loadClusters();
  165. };
  166. var CreateCollectionViewModel = function() {
  167. var self = this;
  168. // Models
  169. self.collection = new Collection();
  170. self.source = ko.observable(SOURCES[0]).extend({'errors': null});
  171. self.fieldSeparator = ko.observable().extend({'errors': null});
  172. self.fieldQuoteCharacter = ko.observable().extend({'errors': null});
  173. self.file = ko.observable().extend({'errors': null});
  174. self.sourceType = ko.observable().extend({'errors': null});
  175. self.morphlines = ko.mapping.fromJS({'name': 'message', 'expression': '%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST:hostname} %{DATA:program}(?:\[%{POSINT:pid}\])?: %{GREEDYDATA:msg}'});
  176. self.morphlines.name = self.morphlines.name.extend({'errors': null});
  177. self.morphlines.expression = self.morphlines.expression.extend({'errors': null});
  178. // UI
  179. self.hive = new HiveViewModel();
  180. self.hbase = new HBaseViewModel();
  181. self.wizard = new Wizard();
  182. self.isLoading = ko.observable();
  183. self.sources = ko.mapping.fromJS(SOURCES);
  184. self.fieldTypes = ko.mapping.fromJS(FIELD_TYPES);
  185. self.fieldSeparators = ko.mapping.fromJS(FIELD_SEPARATORS);
  186. self.fieldQuoteCharacters = ko.mapping.fromJS(FIELD_QUOTE_CHARACTERS);
  187. self.sourceTypes = ko.mapping.fromJS(SOURCE_TYPES);
  188. self.parseFields = function() {
  189. if (self.source() == 'file') {
  190. self.isLoading(true);
  191. return $.post("/indexer/api/fields/parse/", {
  192. 'source': self.source(),
  193. 'type': self.sourceType(),
  194. 'path': self.file(),
  195. 'separator': self.fieldSeparator(),
  196. 'quote': self.fieldQuoteCharacter(),
  197. 'morphlines': ko.mapping.toJSON(self.morphlines)
  198. }).done(function(data) {
  199. if (data.status == 0) {
  200. self.collection.fields(inferFields(data.data, self.collection));
  201. // Find unique key default field
  202. var message = null;
  203. var uniqueKeyFields = ko.utils.arrayFilter(self.collection.fields(), function(field) {
  204. return field.indexed();
  205. });
  206. if (uniqueKeyFields.length > 0) {
  207. self.collection.uniqueKeyField(uniqueKeyFields[0].name());
  208. } else if (self.collection.fields().length > 0) {
  209. self.collection.uniqueKeyField(self.collection.fields().name());
  210. }
  211. } else {
  212. $(document).trigger("error", data.message);
  213. }
  214. self.isLoading(false);
  215. }).fail(function (xhr, textStatus, errorThrown) {
  216. $(document).trigger("error", xhr.responseText);
  217. self.isLoading(false);
  218. });
  219. }
  220. };
  221. self.save = function() {
  222. if (self.wizard.currentPage().validate()) {
  223. self.isLoading(true);
  224. var collection = ko.mapping.toJS(self.collection);
  225. collection.fields = ko.utils.arrayMap(collection.fields, function(field) {
  226. delete field['uniqueKeyField'];
  227. return field;
  228. });
  229. switch(self.source()) {
  230. case 'file':
  231. return $.post("/indexer/api/collections/create/", {
  232. 'collection': ko.mapping.toJSON(collection),
  233. 'type': self.sourceType(),
  234. 'path': self.file(),
  235. 'source': self.source(),
  236. 'separator': self.fieldSeparator(),
  237. 'quote': self.fieldQuoteCharacter()
  238. }).done(function(data) {
  239. if (data.status == 0) {
  240. window.location.href = '/indexer';
  241. } else {
  242. self.isLoading(false);
  243. $(document).trigger("error", data.message);
  244. }
  245. })
  246. .fail(function (xhr, textStatus, errorThrown) {
  247. $(document).trigger("error", xhr.responseText);
  248. self.isLoading(false);
  249. });
  250. case 'hive':
  251. return $.post("/indexer/api/collections/create/", {
  252. 'collection': ko.toJSON(collection),
  253. 'database': self.hive.database().name(),
  254. 'table': self.hive.table().name(),
  255. 'source': self.source()
  256. }).done(function(data) {
  257. if (data.status == 0) {
  258. window.location.href = '/indexer';
  259. } else {
  260. $(document).trigger("error", data.message);
  261. }
  262. self.isLoading(false);
  263. })
  264. .fail(function (xhr, textStatus, errorThrown) {
  265. $(document).trigger("error", xhr.responseText);
  266. self.isLoading(false);
  267. });
  268. }
  269. }
  270. };
  271. };
  272. var EditCollectionViewModel = function() {
  273. var self = this;
  274. // Models
  275. self.collection = ko.observable();
  276. self.source = ko.observable(SOURCES[0]).extend({'errors': null});
  277. self.fieldSeparator = ko.observable().extend({'errors': null});
  278. self.fieldQuoteCharacter = ko.observable().extend({'errors': null});
  279. self.file = ko.observable().extend({'errors': null});
  280. self.sourceType = ko.observable().extend({'errors': null});
  281. // UI
  282. self.wizard = new Wizard();
  283. self.sources = ko.mapping.fromJS(SOURCES);
  284. self.fieldTypes = ko.mapping.fromJS(FIELD_TYPES);
  285. self.sourceTypes = ko.mapping.fromJS(SOURCE_TYPES);
  286. self.fieldSeparators = ko.mapping.fromJS(FIELD_SEPARATORS);
  287. self.fieldQuoteCharacters = ko.mapping.fromJS(FIELD_QUOTE_CHARACTERS);
  288. self.isLoading = ko.observable();
  289. self.fetchFields = function() {
  290. if (self.collection()) {
  291. self.isLoading(true);
  292. return $.get("/indexer/api/collections/" + self.collection().name() + "/fields/").done(function(data) {
  293. if (data.status == 0) {
  294. self.collection().fields(inferFields(data.fields, self.collection()));
  295. self.collection().uniqueKeyField(data.unique_key);
  296. self.sourceType(data.type);
  297. } else {
  298. $(document).trigger("error", data.message);
  299. }
  300. self.isLoading(false);
  301. })
  302. .fail(function (xhr, textStatus, errorThrown) {
  303. $(document).trigger("error", xhr.responseText);
  304. self.isLoading(false);
  305. });
  306. }
  307. };
  308. self.removeCollection = function(collection) {
  309. self.isLoading(true);
  310. var data = [ko.mapping.toJS(self.collection)];
  311. return $.post("/indexer/api/collections/remove/", {
  312. 'collections': ko.mapping.toJSON(data)
  313. }).done(function(data) {
  314. if (data.status == 0) {
  315. window.location.href = '/indexer';
  316. } else {
  317. $(document).trigger("error", data.message);
  318. }
  319. self.isLoading(false);
  320. })
  321. .fail(function (xhr, textStatus, errorThrown) {
  322. $(document).trigger("error", xhr.responseText);
  323. self.isLoading(false);
  324. });
  325. };
  326. self.updateCollection = function() {
  327. self.isLoading(true);
  328. var data = ko.mapping.toJS(self.collection);
  329. return $.post("/indexer/api/collections/" + self.collection().name() + "/update/", {
  330. 'collection': ko.mapping.toJSON(data),
  331. 'type': self.sourceType()
  332. }).done(function(data) {
  333. if (data.status == 0) {
  334. $(document).trigger("info", data.message);
  335. } else {
  336. $(document).trigger("error", data.message);
  337. }
  338. self.isLoading(false);
  339. })
  340. .fail(function (xhr, textStatus, errorThrown) {
  341. $(document).trigger("error", xhr.responseText);
  342. self.isLoading(false);
  343. });
  344. };
  345. self.addData = function() {
  346. self.isLoading(true);
  347. switch(self.source()) {
  348. case 'file':
  349. var data = ko.mapping.toJS(self.collection);
  350. return $.post("/indexer/api/collections/" + self.collection().name() + "/data/", {
  351. 'collection': ko.mapping.toJSON(data),
  352. 'source': self.source(),
  353. 'type': self.sourceType(),
  354. 'path': self.file(),
  355. 'separator': self.fieldSeparator(),
  356. 'quote': self.fieldQuoteCharacter()
  357. }).done(function(data) {
  358. if (data.status == 0) {
  359. window.location.href = '/indexer';
  360. } else {
  361. self.isLoading(false);
  362. $(document).trigger("error", data.message);
  363. }
  364. })
  365. .fail(function (xhr, textStatus, errorThrown) {
  366. self.isLoading(false);
  367. $(document).trigger("error", xhr.responseText);
  368. });
  369. }
  370. };
  371. };
  372. var ManageCollectionsViewModel = function() {
  373. var self = this;
  374. // Models
  375. self.collections = ko.observableArray();
  376. // UI
  377. self.isLoading = ko.observable();
  378. self.filteredCollections = ko.observableArray();
  379. self.selectedCollections = ko.computed(function() {
  380. return ko.utils.arrayFilter(self.collections(), function(collection) {
  381. return collection.selected();
  382. });
  383. });
  384. self.toggleSelectAll = function() {
  385. var direction = !self.selectedCollections().length;
  386. ko.utils.arrayForEach(self.filteredCollections(), function(collection) {
  387. collection.selected(direction);
  388. });
  389. };
  390. self.toggleCollectionSelect = function(collection, e) {
  391. ko.utils.arrayForEach(self.collections(), function(other_collection) {
  392. if(ko.unwrap(other_collection).name() == collection.name()) {
  393. other_collection.selected(!other_collection.selected());
  394. }
  395. });
  396. };
  397. self.importCollection = function(collection, e) {
  398. self.isLoading(true);
  399. return $.post("/indexer/api/collections/import/", {
  400. 'collection': ko.mapping.toJSON(collection)
  401. }).done(function(data) {
  402. self.isLoading(false);
  403. $(document).trigger("info", data.message);
  404. self.fetchCollections();
  405. })
  406. .fail(function (xhr, textStatus, errorThrown) {
  407. self.isLoading(false);
  408. $(document).trigger("error", xhr.responseText);
  409. });
  410. };
  411. self.filterTest = function(obj, filter) {
  412. return ko.unwrap(obj).name().indexOf(filter) != -1;
  413. };
  414. self.fetchCollections = function() {
  415. self.isLoading(true);
  416. return $.get("/indexer/api/collections/").done(function(data) {
  417. if (data.status == 0) {
  418. var collections = [];
  419. ko.utils.arrayForEach(data.collections, function(collection) {
  420. var new_collection = ko.observable(new Collection(collection.name)).extend({'selectable': null});
  421. new_collection().hasHueCollection(collection.hue);
  422. new_collection().hasSolrCollection(collection.solr);
  423. collections.push(new_collection);
  424. });
  425. self.collections(collections);
  426. } else {
  427. $(document).trigger("error", data.message);
  428. }
  429. self.isLoading(false);
  430. })
  431. .fail(function (xhr, textStatus, errorThrown) {
  432. $(document).trigger("error", xhr.responseText);
  433. self.isLoading(false);
  434. });
  435. };
  436. self.removeCollections = function(collection) {
  437. self.isLoading(true);
  438. var collections = self.selectedCollections();
  439. if (collections.length == 0) {
  440. collections = ($.isArray(collection)) ? collection : [collection];
  441. }
  442. return $.post("/indexer/api/collections/remove/", {
  443. 'collections': ko.mapping.toJSON(collections)
  444. }).done(function(data) {
  445. if (data.status == 0) {
  446. // Remove collections
  447. collection_names = ko.utils.arrayMap(collections, function(collection) {
  448. return ko.unwrap(collection).name();
  449. });
  450. var index = 0;
  451. var remove = [];
  452. ko.utils.arrayForEach(self.collections(), function(collection) {
  453. collection = ko.unwrap(collection);
  454. if (collection_names.indexOf(collection.name()) != -1) {
  455. remove.push(index);
  456. }
  457. index++;
  458. });
  459. remove.reverse();
  460. ko.utils.arrayForEach(remove, function(index) {
  461. self.collections.splice(index, 1);
  462. });
  463. } else {
  464. $(document).trigger("error", data.message);
  465. }
  466. self.isLoading(false);
  467. })
  468. .fail(function (xhr, textStatus, errorThrown) {
  469. $(document).trigger("error", xhr.responseText);
  470. self.isLoading(false);
  471. });
  472. };
  473. };
  474. var CollectionsViewModel = function() {
  475. var self = this;
  476. // Models
  477. self.page = ko.observable();
  478. // UI
  479. self.create = new CreateCollectionViewModel();
  480. self.manage = new ManageCollectionsViewModel();
  481. self.edit = new EditCollectionViewModel();
  482. self.isLoading = ko.computed(function() {
  483. return self.create.isLoading() || self.manage.isLoading() || self.edit.isLoading();
  484. });
  485. };
  486. // Utils
  487. function getFileBrowseButton(inputElement) {
  488. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  489. e.preventDefault();
  490. $("#filechooser").jHueFileChooser({
  491. initialPath: inputElement.val(),
  492. onFileChoose: function (filePath) {
  493. inputElement.val(filePath);
  494. inputElement.trigger("change");
  495. $("#chooseFile").modal("hide");
  496. },
  497. selectFolder: false,
  498. createFolder: false
  499. });
  500. $("#chooseFile").modal("show");
  501. });
  502. }
  503. function inferFields(field_data, collection) {
  504. var fields = [];
  505. $.each(field_data, function(index, value) {
  506. // 0 => name
  507. // 1 => type
  508. // 2 => required
  509. // 3 => indexed
  510. // 4 => stored
  511. var field = new Field(collection, value[0], value[1], value[2], value[3]);
  512. field.saved(true);
  513. fields.push(field);
  514. });
  515. return fields;
  516. }