spark.vm.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.result = new Result(snippet, snippet.result);
  44. // init()
  45. // checkStatus()
  46. self.execute = function() {
  47. $(".jHueNotify").hide();
  48. logGA('/execute/' + self.type());
  49. self.result.clear();
  50. $.post("/spark/api/execute", {
  51. notebook: ko.mapping.toJSON(notebook),
  52. snippet: ko.mapping.toJSON(self)
  53. }, function (data) {
  54. if (data.status == 0) {
  55. $.each(data.handle, function(key, val) {
  56. self.result.handle()[key] = val;
  57. });
  58. self.status('running');
  59. self.checkStatus();
  60. }
  61. else {
  62. $(document).trigger("error", data.message);
  63. }
  64. }).fail(function (xhr, textStatus, errorThrown) {
  65. $(document).trigger("error", xhr.responseText);
  66. });
  67. };
  68. self.checkStatus = function() {
  69. $.post("/spark/api/check_status", {
  70. notebook: ko.mapping.toJSON(notebook),
  71. snippet: ko.mapping.toJSON(self)
  72. }, function (data) {
  73. if (data.status == 0) {
  74. self.status(data.query_status);
  75. if (self.status() == 'running') {
  76. setTimeout(self.checkStatus, 1000);
  77. } else {
  78. self.fetchResult();
  79. }
  80. }
  81. else {
  82. $(document).trigger("error", data.message);
  83. }
  84. }).fail(function (xhr, textStatus, errorThrown) {
  85. $(document).trigger("error", xhr.responseText);
  86. });
  87. };
  88. self.fetchResult = function() {
  89. $.post("/spark/api/fetch_result", {
  90. notebook: ko.mapping.toJSON(notebook),
  91. snippet: ko.mapping.toJSON(self)
  92. }, function (data) {
  93. if (data.status == 0) {
  94. self.result.meta(data.result.meta);
  95. self.result.data(data.result.data);
  96. // move resultsets to n rows
  97. // check if N rows fetched...
  98. }
  99. else {
  100. $(document).trigger("error", data.message);
  101. }
  102. }).fail(function (xhr, textStatus, errorThrown) {
  103. $(document).trigger("error", xhr.responseText);
  104. });
  105. };
  106. self.fetchResultMetadata = function() {
  107. }
  108. self.cancel = function() {
  109. };
  110. }
  111. var Notebook = function (vm, notebook) {
  112. var self = this;
  113. self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : UUID());
  114. self.snippets = ko.observableArray();
  115. self.addSnippet = function(snippet) {
  116. self.snippets.push(new Snippet(self, snippet));
  117. }
  118. self.newSnippet = function() {
  119. self.snippets.push(new Snippet(self, {}));
  120. }
  121. if (notebook.snippets) {
  122. $.each(notebook.snippets, function(index, snippet) {
  123. self.addSnippet(snippet);
  124. });
  125. }
  126. }
  127. function EditorViewModel(notebooks) {
  128. var self = this;
  129. self.notebooks = ko.observableArray();
  130. self.init = function() {
  131. $.each(notebooks, function(index, notebook) {
  132. self.loadNotebook(notebook);
  133. });
  134. }
  135. self.loadNotebook = function(notebook) {
  136. self.notebooks.push(new Notebook(self, notebook));
  137. }
  138. self.newNotebook = function() {
  139. self.notebooks.push(new Notebook(self, {}));
  140. }
  141. self.save = function() {
  142. };
  143. }
  144. function logGA(page) {
  145. if (typeof trackOnGA == 'function') {
  146. trackOnGA('editor/' + page);
  147. }
  148. }