spark.vm.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 s4() {
  17. return Math.floor((1 + Math.random()) * 0x10000)
  18. .toString(16)
  19. .substring(1);
  20. }
  21. function UUID() {
  22. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  23. }
  24. var Result = function (snippet, result) {
  25. var self = this;
  26. self.id = ko.observable(typeof result != "undefined" && result.id != "undefined" && result.id != null ? result.id : UUID());
  27. self.type = ko.observable('table');
  28. self.handle = ko.observable({});
  29. self.meta = ko.observableArray();
  30. self.data = ko.observableArray();
  31. self.clear = function() {
  32. //self.handle = ko.observable({});
  33. self.meta.removeAll();
  34. self.data.removeAll();
  35. };
  36. }
  37. var Snippet = function (notebook, snippet) {
  38. var self = this;
  39. self.id = ko.observable(typeof snippet.id != "undefined" && snippet.id != null ? snippet.id : UUID());
  40. self.type = ko.observable('hive-sql');
  41. self.statement = ko.observable('');
  42. self.status = ko.observable('finished');
  43. self.klass = ko.computed(function(){
  44. return 'results '+ self.type();
  45. });
  46. self.result = new Result(snippet, snippet.result);
  47. // init()
  48. // checkStatus()
  49. self.execute = function() {
  50. $(".jHueNotify").hide();
  51. logGA('/execute/' + self.type());
  52. self.result.clear();
  53. $.post("/spark/api/execute", {
  54. notebook: ko.mapping.toJSON(notebook),
  55. snippet: ko.mapping.toJSON(self)
  56. }, function (data) {
  57. if (data.status == 0) {
  58. $.each(data.handle, function(key, val) {
  59. self.result.handle()[key] = val;
  60. });
  61. self.status('running');
  62. self.checkStatus();
  63. }
  64. else {
  65. $(document).trigger("error", data.message);
  66. }
  67. }).fail(function (xhr, textStatus, errorThrown) {
  68. $(document).trigger("error", xhr.responseText);
  69. });
  70. };
  71. self.checkStatus = function() {
  72. $.post("/spark/api/check_status", {
  73. notebook: ko.mapping.toJSON(notebook),
  74. snippet: ko.mapping.toJSON(self)
  75. }, function (data) {
  76. if (data.status == 0) {
  77. self.status(data.query_status);
  78. if (self.status() == 'running') {
  79. setTimeout(self.checkStatus, 1000);
  80. } else {
  81. self.fetchResult();
  82. }
  83. }
  84. else {
  85. $(document).trigger("error", data.message);
  86. }
  87. }).fail(function (xhr, textStatus, errorThrown) {
  88. $(document).trigger("error", xhr.responseText);
  89. });
  90. };
  91. self.fetchResult = function() {
  92. $.post("/spark/api/fetch_result", {
  93. notebook: ko.mapping.toJSON(notebook),
  94. snippet: ko.mapping.toJSON(self)
  95. }, function (data) {
  96. if (data.status == 0) {
  97. self.result.meta(data.result.meta);
  98. self.result.data(data.result.data);
  99. // move resultsets to n rows
  100. // check if N rows fetched...
  101. }
  102. else {
  103. $(document).trigger("error", data.message);
  104. }
  105. }).fail(function (xhr, textStatus, errorThrown) {
  106. $(document).trigger("error", xhr.responseText);
  107. });
  108. };
  109. self.fetchResultMetadata = function() {
  110. }
  111. self.cancel = function() {
  112. };
  113. }
  114. var Notebook = function (vm, notebook) {
  115. var self = this;
  116. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : UUID());
  117. self.snippets = ko.observableArray();
  118. self.addSnippet = function(snippet) {
  119. self.snippets.push(new Snippet(self, snippet));
  120. }
  121. self.newSnippet = function() {
  122. self.snippets.push(new Snippet(self, {}));
  123. }
  124. if (notebook.snippets) {
  125. $.each(notebook.snippets, function(index, snippet) {
  126. self.addSnippet(snippet);
  127. });
  128. }
  129. }
  130. function EditorViewModel(notebooks) {
  131. var self = this;
  132. self.notebooks = ko.observableArray();
  133. self.selectedNotebook = ko.observable();
  134. self.isEditing = ko.observable(false);
  135. self.toggleEditing = function () {
  136. self.isEditing(! self.isEditing());
  137. };
  138. // function bareWidgetBuilder(name, type){
  139. // return new Widget({
  140. // size: 12,
  141. // id: UUID(),
  142. // name: name,
  143. // widgetType: type
  144. // });
  145. // }
  146. //
  147. // self.draggableHive = ko.observable(bareWidgetBuilder("Hive Query", "hive-widget"));
  148. self.init = function() {
  149. $.each(notebooks, function(index, notebook) {
  150. self.loadNotebook(notebook);
  151. if (self.selectedNotebook() == null){
  152. self.selectedNotebook(self.notebooks()[0]);
  153. }
  154. });
  155. }
  156. self.loadNotebook = function(notebook) {
  157. self.notebooks.push(new Notebook(self, notebook));
  158. }
  159. self.newNotebook = function() {
  160. self.notebooks.push(new Notebook(self, {}));
  161. self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
  162. }
  163. self.save = function() {
  164. };
  165. }
  166. function logGA(page) {
  167. if (typeof trackOnGA == 'function') {
  168. trackOnGA('editor/' + page);
  169. }
  170. }