// 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 magicLayout(vm) { loadLayout(vm, vm.initial.layout); $(document).trigger("magicLayout"); } function loadLayout(viewModel, json_layout) { var _columns = []; $(json_layout).each(function (cnt, json_col) { var _rows = []; $(json_col.rows).each(function (rcnt, json_row) { var row = new Row([], viewModel); $(json_row.widgets).each(function (wcnt, widget) { row.addWidget(new Widget({ size:widget.size, id: widget.id, name: widget.name, widgetType: widget.widgetType, properties: widget.properties, offset: widget.offset, loading: true, vm: viewModel })); }); _rows.push(row); }); var column = new Column(json_col.size, _rows); _columns = _columns.concat(column); }); viewModel.columns(_columns); } // End dashboard lib var Node = function (node_json) { var self = this; self.id = ko.observable(typeof node_json.id != "undefined" && node_json.id != null ? node_json.id : UUID()); self.name = ko.observable(typeof node_json.name != "undefined" && node_json.name != null ? node_json.name : ""); } var Workflow = function (vm, workflow) { var self = this; self.id = ko.observable(typeof workflow.id != "undefined" && workflow.id != null ? workflow.id : UUID()); self.name = ko.observable(typeof workflow.name != "undefined" && workflow.name != null ? workflow.name : ""); self.nodes = ko.observableArray([]); self.addNode = function(widget) { if (self.nodes().length == 0) { var node = new Node(ko.mapping.toJS(widget)); self.nodes.push(node); } } self.getNodeById = function (node_id) { var _node = null; $.each(self.nodes(), function (index, node) { if (node.id() == node_id) { _node = node; return false; } }); return _node; } } var WorkflowEditorViewModel = function (layout_json, workflow_json) { var self = this; self.isEditing = ko.observable(true); self.toggleEditing = function () { self.isEditing(! self.isEditing()); }; self.columns = ko.observable([]); self.previewColumns = ko.observable(""); self.workflow = new Workflow(self, workflow_json); self.inited = ko.observable(self.columns().length > 0); self.init = function(callback) { loadLayout(self, layout_json); } self.getWidgetById = function (widget_id) { var _widget = null; $.each(self.columns(), function (i, col) { $.each(col.rows(), function (j, row) { $.each(row.widgets(), function (z, widget) { if (widget.id() == widget_id){ _widget = widget; } }); }); }); return _widget; } self.save = function () { $.post("/impala/dashboard/save", { "dashboard": ko.mapping.toJSON(self.dashboard), "layout": ko.mapping.toJSON(self.columns) }, function (data) { if (data.status == 0) { self.dashboard.id(data.id); $(document).trigger("info", data.message); if (window.location.search.indexOf("dashboard") == -1) { window.location.hash = '#dashboard=' + data.id; } } else { $(document).trigger("error", data.message); } }).fail(function (xhr, textStatus, errorThrown) { $(document).trigger("error", xhr.responseText); }); }; function bareWidgetBuilder(name, type){ return new Widget({ size: 12, id: UUID(), name: name, widgetType: type }); } self.draggableHiveAction = ko.observable(bareWidgetBuilder("Hive Script", "hive-widget")); self.draggablePigAction = ko.observable(bareWidgetBuilder("Pig Script", "pig-widget")); self.draggableJavaAction = ko.observable(bareWidgetBuilder("Java program", "java-widget")); self.draggableMapReduceAction = ko.observable(bareWidgetBuilder("MapReduce job", "mapreduce-widget")); };