| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- // Licensed to Cloudera, Inc. under one
- // or more contributor license agreements. See the NOTICE file
- // distributed with this work for additional information
- // regarding copyright ownership. Cloudera, Inc. licenses this file
- // to you under the Apache License, Version 2.0 (the
- // "License"); you may not use this file except in compliance
- // with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000)
- .toString(16)
- .substring(1);
- }
- function UUID() {
- return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
- }
- var Result = function (snippet, result) {
- var self = this;
- self.id = ko.observable(typeof result != "undefined" && result.id != "undefined" && result.id != null ? result.id : UUID());
- self.type = ko.observable('table');
- self.handle = ko.observable({});
- self.meta = ko.observableArray();
- self.data = ko.observableArray();
-
- self.clear = function() {
- //self.handle = ko.observable({});
- self.meta.removeAll();
- self.data.removeAll();
- };
- }
- var Snippet = function (notebook, snippet) {
- var self = this;
-
- self.id = ko.observable(typeof snippet.id != "undefined" && snippet.id != null ? snippet.id : UUID());
- self.type = ko.observable('hive-sql');
- self.statement = ko.observable('');
- self.status = ko.observable('finished');
- self.klass = ko.computed(function(){
- return 'results '+ self.type();
- });
-
- self.result = new Result(snippet, snippet.result);
-
- // init()
- // checkStatus()
-
-
- self.execute = function() {
- $(".jHueNotify").hide();
- logGA('/execute/' + self.type());
-
- self.result.clear();
-
- $.post("/spark/api/execute", {
- notebook: ko.mapping.toJSON(notebook),
- snippet: ko.mapping.toJSON(self)
- }, function (data) {
- if (data.status == 0) {
- $.each(data.handle, function(key, val) {
- self.result.handle()[key] = val;
- });
- self.status('running');
- self.checkStatus();
- }
- else {
- $(document).trigger("error", data.message);
- }
- }).fail(function (xhr, textStatus, errorThrown) {
- $(document).trigger("error", xhr.responseText);
- });
- };
- self.checkStatus = function() {
- $.post("/spark/api/check_status", {
- notebook: ko.mapping.toJSON(notebook),
- snippet: ko.mapping.toJSON(self)
- }, function (data) {
- if (data.status == 0) {
- self.status(data.query_status);
-
- if (self.status() == 'running') {
- setTimeout(self.checkStatus, 1000);
- } else {
- self.fetchResult();
- }
- }
- else {
- $(document).trigger("error", data.message);
- }
- }).fail(function (xhr, textStatus, errorThrown) {
- $(document).trigger("error", xhr.responseText);
- });
- };
- self.fetchResult = function() {
- $.post("/spark/api/fetch_result", {
- notebook: ko.mapping.toJSON(notebook),
- snippet: ko.mapping.toJSON(self)
- }, function (data) {
- if (data.status == 0) {
- self.result.meta(data.result.meta);
- self.result.data(data.result.data);
- // move resultsets to n rows
- // check if N rows fetched...
- }
- else {
- $(document).trigger("error", data.message);
- }
- }).fail(function (xhr, textStatus, errorThrown) {
- $(document).trigger("error", xhr.responseText);
- });
- };
- self.fetchResultMetadata = function() {
-
- }
-
- self.cancel = function() {
- };
- }
- var Notebook = function (vm, notebook) {
- var self = this;
- self.id = ko.observable(typeof notebook.id != "undefined" && notebook.id != null ? notebook.id : UUID());
- self.snippets = ko.observableArray();
- self.addSnippet = function(snippet) {
- self.snippets.push(new Snippet(self, snippet));
- }
- self.newSnippet = function() {
- self.snippets.push(new Snippet(self, {}));
- }
-
- if (notebook.snippets) {
- $.each(notebook.snippets, function(index, snippet) {
- self.addSnippet(snippet);
- });
- }
- }
- function EditorViewModel(notebooks) {
- var self = this;
- self.notebooks = ko.observableArray();
- self.selectedNotebook = ko.observable();
- self.isEditing = ko.observable(false);
- self.toggleEditing = function () {
- self.isEditing(! self.isEditing());
- };
- // function bareWidgetBuilder(name, type){
- // return new Widget({
- // size: 12,
- // id: UUID(),
- // name: name,
- // widgetType: type
- // });
- // }
- //
- // self.draggableHive = ko.observable(bareWidgetBuilder("Hive Query", "hive-widget"));
- self.init = function() {
- $.each(notebooks, function(index, notebook) {
- self.loadNotebook(notebook);
- if (self.selectedNotebook() == null){
- self.selectedNotebook(self.notebooks()[0]);
- }
- });
- }
- self.loadNotebook = function(notebook) {
- self.notebooks.push(new Notebook(self, notebook));
- }
- self.newNotebook = function() {
- self.notebooks.push(new Notebook(self, {}));
- self.selectedNotebook(self.notebooks()[self.notebooks().length - 1]);
- }
-
- self.save = function() {
- };
- }
- function logGA(page) {
- if (typeof trackOnGA == 'function') {
- trackOnGA('editor/' + page);
- }
- }
|