Skip to content

Commit

Permalink
Fixes #13424 - c3 patternfly react implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gailsteiger authored and ohadlevy committed Oct 9, 2016
1 parent 2322228 commit 659d49a
Show file tree
Hide file tree
Showing 60 changed files with 1,399 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015"]
"presets": ["react", "es2015"],
"plugins": ["transform-object-rest-spread", "transform-object-assign"]
}
20 changes: 15 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"ecmaFeatures": {
"globalReturn": true,
"modules": true
},
"plugins": ["react"],

"env": {
"browser": true,
Expand All @@ -19,11 +16,21 @@
"unescape": false,
"window": false,
"$": true,
"_": true
"_": true,
"__": true
},

"parser": "babel-eslint",

"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"globalReturn": true,
"jsx": true
}
},

"rules": {
"block-scoped-var": "error",
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
Expand Down Expand Up @@ -66,6 +73,7 @@
"no-array-constructor": "error",
"no-bitwise": "off",
"no-caller": "error",
"no-case-declarations": "error",
"no-catch-shadow": "off",
"no-cond-assign": "error",
"no-console": "warn",
Expand Down Expand Up @@ -159,6 +167,8 @@
"quote-props": "off",
"quotes": ["error", "single", {"avoidEscape": true}],
"radix": "error",
"react/jsx-uses-react": "error",
"react/jsx-uses-vars": "error",
"semi": ["error", "always"],
"semi-spacing": "off",
"sort-vars": "off",
Expand Down
7 changes: 7 additions & 0 deletions __mocks__/c3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const c3 = {};

c3.generate = () => {
return undefined;
};

export default c3;
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//= require jquery.ui.spinner
//= require scoped_search
//= require bootstrap
//= require patternfly
//= require charts
//= require topbar
//= require two-pane
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ class StatisticsController < ApplicationController
before_action :find_stat, :only => [:show]

def index
@stats = charts
@metadata = charts.map(&:metadata)
end

def show
render :show, :layout => false
respond_to do |format|
format.json do
render :json => {:id => @stat.id, :data => @stat.calculate.map(&:values)}
end
end
end

private
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/reactjs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ReactjsHelper
def mount_react_component(name, selector, data)
javascript_tag defer: 'defer' do
"$(tfm.reactMounter.mount('#{name}', '#{selector}', #{data}));".html_safe
end
end
end
6 changes: 5 additions & 1 deletion app/services/statistics/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Statistics
class Base
attr_reader :search, :title, :count_by
attr_reader :title, :count_by

def initialize(options = {})
@id = options[:id]
Expand All @@ -22,6 +22,10 @@ def url
@url || "statistics/#{id}"
end

def search
"/hosts?search=#{@search}"
end

def metadata
{:id => id, :title => title, :url => url, :search => search}
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/statistics/count_numerical_fact_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def calculate
},
{
:label => _('used memory'),
:data => mem_size - mem_free
:data => (mem_size - mem_free).round(2)
}
]
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/config_reports/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= javascript 'reports' %>
<%= javascript 'reports', 'charts' %>
<% title "#{@config_report.host}"%>

<p class='ra'> <%= _("Reported at %s ") % @config_report.reported_at %> </p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= javascript 'dashboard' %>
<%= javascript 'dashboard', 'charts' %>
<% title _('Overview') %>
<%= title_actions dashboard_actions %>

Expand Down
1 change: 1 addition & 0 deletions app/views/fact_values/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= javascript 'charts' %>
<%
if params[:host_id].present?
title(_("Fact Values | %{host_name}") % {:host_name => params[:host_id]})
Expand Down
1 change: 1 addition & 0 deletions app/views/facts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= javascript 'charts' %>
<% title @fact.name %>

<%= pie_chart @fact.name, _("%s Distribution") % @fact.name.camelize, FactValue.count_each(@fact.name) %>
2 changes: 1 addition & 1 deletion app/views/smart_proxies/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% title _("Smart Proxies") %>
<%= javascript 'proxy_status' %>
<%= javascript 'proxy_status', 'charts' %>
<% title_actions new_link(_("New Smart Proxy")),
documentation_button('4.3SmartProxies') %>

Expand Down
1 change: 1 addition & 0 deletions app/views/smart_proxies/plugins/_puppet.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% javascript 'charts' %>
<ul id="proxy-puppet-tab" class="nav nav-tabs nav-tabs-pf">
<li class="active"><a href="#puppet_dashboard" data-toggle="tab"><%= _("General")%></a></li>
<li><a href="#puppet_envs" data-toggle="tab"><%= _('Environments') %></a></li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/smart_proxies/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% title(_('Smart Proxy: %s') % @smart_proxy.to_label) %>
<%= javascript 'proxy_status' %>
<%= javascript 'proxy_status', 'charts' %>
<%= smart_proxy_title_actions(@smart_proxy, authorizer) %>
<% service_features = services_tab_features(@smart_proxy) %>
<% tab_features = tabbed_features(@smart_proxy) %>
Expand Down
12 changes: 4 additions & 8 deletions app/views/statistics/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<% title _("Statistics"), _('Statistics') %>
<div id="statistics">
<% @stats.each do |stat| %>
<div data-ajax-url=<%= stat.url %> class='stats-well' data-on-complete='refreshCharts'>
<h4 class='ca pie-title'><%= stat.title %></h4>
<div class='statistics-pie-placeholder small'><span class='spinner spinner-lg spinner-inline'></span></div>
</div>
<% end %>
</div>

<div id="statistics"></div>

<%= mount_react_component('StatisticsChartsList', '#statistics', @metadata.to_json)%>
1 change: 0 additions & 1 deletion app/views/statistics/show.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/trends/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= javascript 'charts' %>
<% title _("Trends") %>
<% title_actions new_link(_("Add Trend Counter")),
documentation_button('4.1.3Trends') %>
Expand Down
1 change: 1 addition & 0 deletions app/views/trends/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= javascript 'charts' %>
<% title(_("Trends for %s") % trend_title(@trend)) %>
<% content_for(:search_bar) { trend_days_filter } %>
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion bundler.d/assets.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
gem 'jquery-turbolinks', '~> 2.1'
gem 'jquery-ui-rails', '< 5.0.0'
gem 'autoprefixer-rails', '~> 5.2'
gem 'patternfly-sass', '~> 2.7'
gem 'patternfly-sass', '~> 3.9'
gem 'gridster-rails', '~> 0.5'
gem 'gettext_i18n_rails_js', '~> 1.0'
gem 'execjs', '>= 1.4.0', '< 3.0'
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

stylesheets = %w( unimported/email.css )

images = %w(editable/clear.png)
images = %w(editable/clear.png patternfly/bg-modal-about-pf.png )

# Add the fonts path
config.assets.paths << Rails.root.join('vendor', 'assets', 'fonts')
Expand Down
2 changes: 1 addition & 1 deletion config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var config = {
test: /(\.png|\.gif)$/,
loader: "url-loader?limit=32767"
}
],
]
},

plugins: [
Expand Down
27 changes: 24 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,43 @@
"description": "Foreman isn't really a node module, these are just dependencies needed to build the webpack bundle. 'dependencies' are the asset libraries in use and 'devDependencies' are used for the build process.",
"private": true,
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-core": "~6.7.2",
"babel-eslint": "^6.1.2",
"babel-jest": "^15.0.0",
"babel-loader": "~6.2.4",
"babel-plugin-transform-object-assign": "^6.8.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "~6.6.0",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.9.0",
"css-loader": "^0.23.1",
"dotenv": "^2.0.0",
"enzyme": "^2.4.1",
"es6-promise": "^3.2.1",
"eslint": "^3.3.1",
"eslint-plugin-react": "^6.1.2",
"expose-loader": "~0.6.0",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.9.0",
"identity-obj-proxy": "^3.0.0",
"jest-cli": "^15.1.1",
"jsdom": "^9.5.0",
"react-addons-test-utils": "^15.3.1",
"stats-webpack-plugin": "^0.2.1",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.9.11",
"webpack-dev-server": "^1.9.0"
},
"dependencies": {
"c3": "^0.4.11",
"datatables.net": "~1.10.12",
"datatables.net-bs": "~1.10.12",
"diff": "~3.0.0",
"events": "^1.1.1",
"flux": "^2.1.1",
"ipaddr.js": "~1.2.0",
"jquery": "~1.11.0",
"jquery-flot": "~0.8.3",
Expand All @@ -37,16 +50,24 @@
"jstz": "~1.0.7",
"lodash": "~4.15.0",
"multiselect": "~0.9.12",
"react": "^15.1.0",
"react-bootstrap": "^0.30.0",
"react-dom": "^15.1.0",
"select2": "~3.5.2-browserify"
},
"scripts": {
"lint": "./node_modules/.bin/eslint -c .eslintrc webpack/assets/**/*.js || exit 0",
"test": "jest"
"test": "node --harmony_proxies node_modules/.bin/jest"
},
"jest": {
"automock": true,
"unmockedModulePathPatterns": [
"jquery"
]
"react",
"node_modules/"
],
"moduleNameMapper": {
"^.+\\.(png|gif)$": "identity-obj-proxy",
"^.+\\.(css)$": "identity-obj-proxy"
}
}
}
4 changes: 2 additions & 2 deletions test/controllers/statistics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class StatisticsControllerTest < ActionController::TestCase
assert_response :success
end

test 'user with viewer rights should succeed in viewing statistics partial' do
test 'user with viewer rights should succeed in requesting statistics data via ajax' do
@request.session[:user] = users(:one).id
users(:one).roles = [Role.default, Role.find_by_name('Viewer')]
get :show, {:id => 'operatingsystem'}, set_session_user
get :show, {:id => 'operatingsystem', :format=>'json'}, set_session_user
assert_response :success
end
end
18 changes: 9 additions & 9 deletions test/integration/statistic_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'integration_test_helper'

class StatisticIntegrationTest < ActionDispatch::IntegrationTest
class StatisticIntegrationTest < IntegrationTestWithJavascript
test "statistics page" do
visit statistics_path
assert page.has_selector?('h4', :text => "OS Distribution")
assert page.has_selector?('h4', :text => "Architecture Distribution")
assert page.has_selector?('h4', :text => "Environment Distribution")
assert page.has_selector?('h4', :text => "Number of CPUs")
assert page.has_selector?('h4', :text => "Hardware")
assert page.has_selector?('h4', :text => "Class Distribution")
assert page.has_selector?('h4', :text => "Average memory usage")
assert page.has_selector?('h4', :text => "Average swap usage")
assert page.has_selector?('h3', :text => "OS Distribution")
assert page.has_selector?('h3', :text => "Architecture Distribution")
assert page.has_selector?('h3', :text => "Environment Distribution")
assert page.has_selector?('h3', :text => "Number of CPUs")
assert page.has_selector?('h3', :text => "Hardware")
assert page.has_selector?('h3', :text => "Class Distribution")
assert page.has_selector?('h3', :text => "Average memory usage")
assert page.has_selector?('h3', :text => "Average swap usage")
end
end
2 changes: 1 addition & 1 deletion test/integration/top_bar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setup
within("div.navbar-inner") do
click_link("Statistics")
end
assert page.has_selector?('h4', :text => "OS Distribution")
assert page.has_selector?('h1', :text => "Statistics")
end

test "Trends link" do
Expand Down
2 changes: 1 addition & 1 deletion test/unit/statistics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup
test 'it should include an Operating System stat' do
os = Statistics.charts.detect {|s| s.id == 'operatingsystem'}
assert_equal "operatingsystem", os.id
data = { :id=>"operatingsystem", :title=>"OS Distribution", :url=>"statistics/operatingsystem", :search=>"os_title=~VAL~" }
data = { :id=>"operatingsystem", :title=>"OS Distribution", :url=>"statistics/operatingsystem", :search=>"/hosts?search=os_title=~VAL~" }
assert_equal data, os.metadata
end

Expand Down
13 changes: 9 additions & 4 deletions webpack/assets/javascripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ require('./bundle_flot');
require('./bundle_multiselect');
require('./bundle_select2');
require('./bundle_datatables');
window.tfm = {
tools: require('./foreman_tools'),
numFields: require('./jquery.ui.custom_spinners')
};

window.tfm = Object.assign(
window.tfm || {},
{
tools: require('./foreman_tools'),
numFields: require('./jquery.ui.custom_spinners'),
reactMounter: require('./react_app/common/MountingService')
}
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jest.unmock('../foreman_tools');
const tools = require('../foreman_tools');
jest.unmock('./foreman_tools');
const tools = require('./foreman_tools');

describe('iconText', () => {
it('creates a label with the right icon class', () => {
Expand Down
17 changes: 17 additions & 0 deletions webpack/assets/javascripts/react_app/API.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ServerActions from './actions/ServerActions';

export default {
getStatisticsData(url) {
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
jqXHR.originalRequestOptions = originalOptions;
});
$.getJSON(url)
.success(
(rawStatistics, textStatus, jqXHR) => {
ServerActions.receivedStatistics(rawStatistics, textStatus, jqXHR);
})
.error((jqXHR, textStatus, errorThrown) => {
ServerActions.statisticsRequestError(jqXHR, textStatus, errorThrown);
});
}
};
Loading

0 comments on commit 659d49a

Please sign in to comment.