Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Support for uploading dashboard definitions.
Browse files Browse the repository at this point in the history
This enables PUT-ing and GET-ing of full dashboard definitions through
/<slug>.json. In this API, the 'dashboards_json' database field is
converted from a JSON string to a native JSON suboject of the Rails
Dashboard model JSON. The full externally exposed JSON looks like this:

{
  "name": "Test Dashboard",
  "created_at": "2014-11-07T00:25:57.000Z",
  "updated_at": "2014-11-07T00:38:23.000Z",
  "dashboard_json": {
    "globalConfig": {
      // ...global configuration...
    },
    "widgets": [
      // ...widget definitions...
    ]
  }
}

Upon PUT, the 'dashboards_json' field is validated against the
JSON-schema definition in the file 'dashboard_schema.json' and then
saved again as a string in a single database field.

Since Rails converts JSON empty arrays to "nil" values by default, this
also upgrades PromDash to Rails 4.1.1, which supports an option to turn
off this automatic conversion. See the following issues about this
regarding security implications (which for now we shouldn't need to
worry about in PromDash):

rails/rails#8832
rails/rails#13420

Further, as we're exposing the dashboard_json as an external API for the
first time, this also includes a lot of naming cleanups in the JSON
fields and the related code.
  • Loading branch information
juliusv committed Nov 7, 2014
1 parent f7cd40a commit 86c09b3
Show file tree
Hide file tree
Showing 41 changed files with 602 additions and 213 deletions.
9 changes: 4 additions & 5 deletions Gemfile
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
gem 'rails', '4.1.7'

gem 'mysql2'

Expand Down Expand Up @@ -29,14 +29,11 @@ group :test, :development do
end

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
gem 'sass-rails', '~> 4.0.4'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby

Expand Down Expand Up @@ -68,3 +65,5 @@ end

#gem 'bootstrap-sass', github: 'thomas-mcdonald/bootstrap-sass', branch: 'master'
gem 'entypo-rails'

gem 'json-schema'
127 changes: 60 additions & 67 deletions Gemfile.lock
@@ -1,36 +1,37 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.0.0)
actionpack (= 4.0.0)
mail (~> 2.5.3)
actionpack (4.0.0)
activesupport (= 4.0.0)
builder (~> 3.1.0)
erubis (~> 2.7.0)
actionmailer (4.1.7)
actionpack (= 4.1.7)
actionview (= 4.1.7)
mail (~> 2.5, >= 2.5.4)
actionpack (4.1.7)
actionview (= 4.1.7)
activesupport (= 4.1.7)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
actionview (4.1.7)
activesupport (= 4.1.7)
builder (~> 3.1)
erubis (~> 2.7.0)
active_model_serializers (0.8.1)
activemodel (>= 3.0)
activemodel (4.0.0)
activesupport (= 4.0.0)
builder (~> 3.1.0)
activerecord (4.0.0)
activemodel (= 4.0.0)
activerecord-deprecated_finders (~> 1.0.2)
activesupport (= 4.0.0)
arel (~> 4.0.0)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.0)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
activemodel (4.1.7)
activesupport (= 4.1.7)
builder (~> 3.1)
activerecord (4.1.7)
activemodel (= 4.1.7)
activesupport (= 4.1.7)
arel (~> 5.0.0)
activesupport (4.1.7)
i18n (~> 0.6, >= 0.6.9)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
tzinfo (~> 1.1)
addressable (2.3.5)
arel (4.0.0)
atomic (1.1.14)
builder (3.1.4)
arel (5.0.1.20140414130214)
builder (3.2.2)
capybara (2.3.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand All @@ -40,13 +41,6 @@ GEM
childprocess (0.5.3)
ffi (~> 1.0, >= 1.0.11)
coderay (1.0.9)
coffee-rails (4.0.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0.beta, < 5.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.6.3)
daemons (1.1.9)
database_cleaner (0.9.1)
diff-lcs (1.2.5)
Expand All @@ -62,7 +56,7 @@ GEM
railties (>= 3.0.0)
ffi (1.9.5)
hike (1.2.3)
i18n (0.6.5)
i18n (0.6.11)
jasmine-core (2.0.0)
jasmine-rails (0.6.0)
jasmine-core (>= 1.3, < 3.0)
Expand All @@ -76,22 +70,21 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.0)
json-schema (2.4.1)
launchy (2.4.2)
addressable (~> 2.3)
libv8 (3.16.14.3)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (1.25.1)
mime-types (2.4.3)
mini_portile (0.6.0)
minitest (4.7.5)
minitest (5.4.2)
multi_json (1.10.1)
mysql2 (0.3.13)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
phantomjs (1.9.7.0)
polyglot (0.3.3)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
Expand All @@ -101,20 +94,22 @@ GEM
rack
rack-test (0.6.2)
rack (>= 1.0)
rails (4.0.0)
actionmailer (= 4.0.0)
actionpack (= 4.0.0)
activerecord (= 4.0.0)
activesupport (= 4.0.0)
rails (4.1.7)
actionmailer (= 4.1.7)
actionpack (= 4.1.7)
actionview (= 4.1.7)
activemodel (= 4.1.7)
activerecord (= 4.1.7)
activesupport (= 4.1.7)
bundler (>= 1.3.0, < 2.0)
railties (= 4.0.0)
sprockets-rails (~> 2.0.0)
railties (4.0.0)
actionpack (= 4.0.0)
activesupport (= 4.0.0)
railties (= 4.1.7)
sprockets-rails (~> 2.0)
railties (4.1.7)
actionpack (= 4.1.7)
activesupport (= 4.1.7)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.1.0)
rake (10.3.2)
rdoc (3.12.2)
json (~> 1.4)
ref (1.0.5)
Expand All @@ -139,11 +134,12 @@ GEM
rspec-support (= 3.0.0.beta2)
rspec-support (3.0.0.beta2)
rubyzip (1.1.6)
sass (3.2.12)
sass-rails (4.0.0)
railties (>= 4.0.0.beta, < 5.0)
sass (>= 3.1.10)
sprockets-rails (~> 2.0.0)
sass (3.2.19)
sass-rails (4.0.4)
railties (>= 4.0.0, < 5.0)
sass (~> 3.2.2)
sprockets (~> 2.8, < 2.12)
sprockets-rails (~> 2.0)
sdoc (0.3.20)
json (>= 1.1.3)
rdoc (~> 3.10)
Expand All @@ -157,15 +153,15 @@ GEM
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.4.6)
sprockets (2.10.0)
sprockets (2.11.3)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.0.0)
sprockets-rails (2.2.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.8)
therubyracer (0.12.0)
libv8 (~> 3.16.14.0)
Expand All @@ -174,14 +170,11 @@ GEM
daemons (>= 1.0.9)
eventmachine (>= 1.0.0)
rack (>= 1.5.0)
thor (0.18.1)
thread_safe (0.1.3)
atomic
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.38)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.2.1)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
Expand All @@ -195,19 +188,19 @@ PLATFORMS
DEPENDENCIES
active_model_serializers
capybara (~> 2.3.0)
coffee-rails (~> 4.0.0)
database_cleaner
entypo-rails
factory_girl_rails
jasmine-rails
jbuilder (~> 1.2)
jquery-rails
json-schema
launchy
mysql2
pry
rails (= 4.0.0)
rails (= 4.1.7)
rspec-rails (~> 3.0.0.beta2)
sass-rails (~> 4.0.0)
sass-rails (~> 4.0.4)
sdoc
selenium-webdriver
sinatra
Expand Down
34 changes: 17 additions & 17 deletions app/assets/javascripts/angular/controllers/dashboard_ctrl.js
Expand Up @@ -6,8 +6,8 @@ angular.module("Prometheus.controllers")
"$document",
"$location",
"WidgetHeightCalculator",
"UrlConfigEncoder",
"UrlVariablesDecoder",
"URLConfigEncoder",
"URLVariablesDecoder",
"SharedGraphBehavior",
"InputHighlighter",
"ModalService",
Expand All @@ -19,8 +19,8 @@ angular.module("Prometheus.controllers")
$document,
$location,
WidgetHeightCalculator,
UrlConfigEncoder,
UrlVariablesDecoder,
URLConfigEncoder,
URLVariablesDecoder,
SharedGraphBehavior,
InputHighlighter,
ModalService,
Expand Down Expand Up @@ -62,10 +62,10 @@ angular.module("Prometheus.controllers")
$scope.saving = true;
$http.put(window.location.pathname + '.json', {
'dashboard': {
'dashboard_json': angular.toJson({
'globalConfig': $scope.globalConfig,
'widgets': $scope.widgets
})
'dashboard_json': {
'globalConfig': angular.copy($scope.globalConfig),
'widgets': angular.copy($scope.widgets)
}
}
}).error(function(data, status) {
alert("Error saving dashboard.");
Expand Down Expand Up @@ -155,9 +155,9 @@ angular.module("Prometheus.controllers")
title: "Title",
expression: {
id: 0,
server_id: 1,
serverID: 1,
expression: "",
legend_id: 1
legendID: 1
},
type: "pie"
};
Expand All @@ -175,22 +175,22 @@ angular.module("Prometheus.controllers")
$scope.globalConfig.vars = vars;
}, true);

$scope.syncEntireUrlEncode = function() {
if (!$scope.globalConfig.keepUrlUpdated) {
$scope.globalConfig.encodeEntireUrl = false;
$scope.syncEntireURLEncode = function() {
if (!$scope.globalConfig.keepURLUpdated) {
$scope.globalConfig.encodeEntireURL = false;
}
};

$scope.$watch('globalConfig', function() {
if ($scope.globalConfig.keepUrlUpdated) {
if ($scope.globalConfig.keepURLUpdated) {
if ($scope.globalConfig.range) {
$location.search("range", $scope.globalConfig.range)
}
if ($scope.globalConfig.endTime) {
$location.search("until", (new Date($scope.globalConfig.endTime)).toISOString())
}
if ($scope.globalConfig.encodeEntireUrl) {
UrlConfigEncoder({globalConfig: $scope.globalConfig});
if ($scope.globalConfig.encodeEntireURL) {
URLConfigEncoder({globalConfig: $scope.globalConfig});
}
}
}, true);
Expand Down Expand Up @@ -242,7 +242,7 @@ angular.module("Prometheus.controllers")
$scope.widgets.push(angular.copy($scope.widgetToClone));
};

var searchVars = UrlVariablesDecoder()
var searchVars = URLVariablesDecoder()
if (searchVars.fullscreen) {
$scope.enableFullscreen();
}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/angular/controllers/embed_ctrl.js
@@ -1,4 +1,4 @@
angular.module("Prometheus.controllers").controller('EmbedCtrl',["$scope", "$window", "$timeout", "WidgetHeightCalculator", "UrlConfigEncoder", "FullScreenAspectRatio", "SharedGraphBehavior", function($scope, $window, $timeout, WidgetHeightCalculator, UrlConfigEncoder, FullScreenAspectRatio, SharedGraphBehavior) {
angular.module("Prometheus.controllers").controller('EmbedCtrl',["$scope", "$window", "$timeout", "WidgetHeightCalculator", "URLConfigEncoder", "FullScreenAspectRatio", "SharedGraphBehavior", function($scope, $window, $timeout, WidgetHeightCalculator, URLConfigEncoder, FullScreenAspectRatio, SharedGraphBehavior) {
$window.onresize = function() {
$scope.$apply(function() {
// Need to $apply to propagate aspectRatio change,
Expand Down
8 changes: 4 additions & 4 deletions app/assets/javascripts/angular/controllers/frame_ctrl.js
@@ -1,7 +1,7 @@
angular.module("Prometheus.controllers").controller('FrameCtrl', ["$scope",
"$sce", "$timeout",
"VariableInterpolator",
"UrlHashEncoder",
"URLHashEncoder",
"InputHighlighter",
"WidgetLinkHelper",
"GraphiteTimeConverter",
Expand All @@ -10,7 +10,7 @@ angular.module("Prometheus.controllers").controller('FrameCtrl', ["$scope",
function($scope, $sce,
$timeout,
VariableInterpolator,
UrlHashEncoder,
URLHashEncoder,
InputHighlighter,
WidgetLinkHelper,
GraphiteTimeConverter,
Expand All @@ -29,7 +29,7 @@ angular.module("Prometheus.controllers").controller('FrameCtrl', ["$scope",
graphBlob.globalConfig = dashboardData.globalConfig;
WidgetLinkHelper
.createLink({
encoded_url: UrlHashEncoder(graphBlob),
encoded_url: URLHashEncoder(graphBlob),
graph_title: $scope.getTitle(),
dashboard_name: dashboardName
}, event)
Expand Down Expand Up @@ -92,7 +92,7 @@ angular.module("Prometheus.controllers").controller('FrameCtrl', ["$scope",
return $sce.trustAsResourceUrl(buildFrameURL(url));
};

$scope.updateUrl = function() {
$scope.updateURL = function() {
$scope.frame.url = $scope.urlInput;
};

Expand Down

0 comments on commit 86c09b3

Please sign in to comment.