Skip to content

Commit

Permalink
Added Line chart component
Browse files Browse the repository at this point in the history
And Line chart component and related controllers, template, views to display Recent Hosts & Recent VMs line charts.
  • Loading branch information
h-kataria committed Aug 27, 2017
1 parent b98133e commit 3fef8cc
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 97 deletions.
114 changes: 114 additions & 0 deletions app/assets/javascripts/components/pf_charts/line-chart.component.js
@@ -0,0 +1,114 @@
angular.module('patternfly.charts').component('pfLineChart', {
bindings: {
config: '<',
chartData: '<',
showXAxis: '<?',
showYAxis: '<?',
setAreaChart: '<?'
},
templateUrl: '/static/pf_charts/line-chart.html',
controller: lineChartController
});
lineChartController.$inject = ['pfUtils']

function lineChartController(pfUtils) {
'use strict';
var vm = this, prevChartData;

vm.updateAll = function () {
// Need to deep watch changes in chart data
prevChartData = angular.copy(vm.chartData);

// Create an ID for the chart based on the chartId in the config if given
if (vm.lineChartId === undefined) {
vm.lineChartId = 'lineChart';
if (vm.config.chartId) {
vm.lineChartId = vm.config.chartId + vm.lineChartId;
}
}

/*
* Setup Axis options. Default is to not show either axis. This can be overridden in two ways:
* 1) in the config, setting showAxis to true will show both axes
* 2) in the attributes showXAxis and showYAxis will override the config if set
*
* By default only line and the tick marks are shown, no labels. This is a line and should be used
* only to show a brief idea of trending. This can be overridden by setting the config.axis options per C3
*/

if (vm.showXAxis === undefined) {
vm.showXAxis = (vm.config.showAxis !== undefined) && vm.config.showAxis;
}

if (vm.showYAxis === undefined) {
vm.showYAxis = (vm.config.showAxis !== undefined) && vm.config.showAxis;
}

vm.defaultConfig = patternfly.c3ChartDefaults().getDefaultLineConfig();
vm.defaultConfig.axis = {
x: {
show: vm.showXAxis === true,
type: 'timeseries',
tick: {
format: function () {
return '';
}
}
},
y: {
show: vm.showYAxis === true,
tick: {
format: function () {
return '';
}
}
}
};

/*
* Setup Chart type option. Default is Line Chart.
*/
if (vm.setAreaChart === undefined) {
vm.setAreaChart = (vm.config.setAreaChart !== undefined) && vm.config.setAreaChart;
}

// Convert the given data to C3 chart format
vm.config.data = vm.getLineData(vm.chartData);

// Override defaults with callers specifications
vm.defaultConfig = pfUtils.merge(vm.defaultConfig, vm.config);

// Will trigger c3 chart generation
vm.chartConfig = pfUtils.merge(vm.defaultConfig, vm.config);
};

/*
* Convert the config data to C3 Data
*/
vm.getLineData = function (chartData) {
var lineData = {
type: vm.setAreaChart ? "area" : "line"
};

if (chartData && chartData.dataAvailable !== false && chartData.xData) {
lineData.x = chartData.xData[0];
// Convert the chartData dictionary into a C3 columns data arrays
lineData.columns = Object.keys(chartData).map(function (key) {
return chartData[key];
});
}

return lineData;
};

vm.$onChanges = function (changesObj) {
vm.updateAll();
};

vm.$doCheck = function () {
// do a deep compare on chartData
if (!angular.equals(vm.chartData, prevChartData)) {
vm.updateAll();
}
};
}
45 changes: 1 addition & 44 deletions app/assets/javascripts/controllers/pf_charts/charts-mixin.js
Expand Up @@ -96,50 +96,10 @@ angular.module('miq.util').factory('chartsMixin', ['pfUtils', function(pfUtils)
},
};

var processHeatmapData = function(heatmapsStruct, data) {
if (data) {
var heatmapsStructData = data.map(function(d) {
var percent = -1;
var tooltip = __("Cluster: ") + d.node + "<br>" + __("Provider: ") + d.provider
if (d.percent === null || d.total === null) {
tooltip += "<br> " + __("Usage: Unknown");
} else {
percent = d.percent
tooltip += "<br>" + __("Usage: ") + sprintf(__("%d%% in use of %d %s total"), (percent * 100).toFixed(0), d.total, d.unit);
}

return {
"id": d.id,
"tooltip": tooltip,
"value": percent
};
})
heatmapsStruct.data = _.sortBy(heatmapsStructData, 'value').reverse()
} else {
heatmapsStruct.data = [];
heatmapsStruct.dataAvailable = false
}

return heatmapsStruct
};

var processUtilizationData = function(data, xDataLabel, yDataLabel) {
return processData(data, xDataLabel, yDataLabel);
};

var processRecentHostsData = function(data, xDataLabel, yDataLabel) {
return processData(data, xDataLabel, yDataLabel);
};

var processRecentVmsData = function(data, xDataLabel, yDataLabel) {
return processData(data, xDataLabel, yDataLabel);
};

var processData = function(data, xDataLabel, yDataLabel) {
if (!data) {
return { dataAvailable: false }
}

data.xData.unshift(xDataLabel);
data.yData.unshift(yDataLabel);
return data;
Expand All @@ -149,10 +109,7 @@ angular.module('miq.util').factory('chartsMixin', ['pfUtils', function(pfUtils)
dashboardHeatmapChartHeight: 90,
nodeHeatMapUsageLegendLabels: ['< 70%', '70-80%', '80-90%', '> 90%'],
chartConfig: chartConfig,
processHeatmapData: processHeatmapData,
processUtilizationData: processUtilizationData,
processRecentHostsData: processRecentHostsData,
processRecentVmsData: processRecentVmsData,
processData: processData,
dailyTimeTooltip: dailyTimeTooltip
};
}]);
@@ -0,0 +1,23 @@
/* global miqHttpInject */
angular.module( 'patternfly.charts' ).controller( 'recentHostsLineChartController', ['$scope', 'pfUtils', '$q', 'providerId', '$http', 'chartsMixin', function( $scope, pfUtils, $q, providerId, $http, chartsMixin ) {
var vm = this;

var init = function() {
ManageIQ.angular.scope = vm;
vm.config = chartsMixin.chartConfig['recentHostsConfig'];
var url = '/ems_infra_dashboard/recent_hosts_data/' + providerId;
var hostsDataPromise = $http.get(url).then(function(response) {
vm.data = response.data.data;
});

$q.all([hostsDataPromise]).then(function() {
vm.data = chartsMixin.processData(vm.data.recentHosts, 'dates', vm.data.recentHosts.config.label)
});

vm.custShowXAxis = false;
vm.custShowYAxis = false;
vm.custAreaChart = true;
}

init();
}]);
@@ -0,0 +1,23 @@
/* global miqHttpInject */
angular.module( 'patternfly.charts' ).controller( 'recentVmsLineChartController', ['$scope', 'pfUtils', '$q', 'providerId', '$http', 'chartsMixin', function( $scope, pfUtils, $q, providerId, $http, chartsMixin ) {
var vm = this;

var init = function() {
ManageIQ.angular.scope = vm;
vm.config = chartsMixin.chartConfig['recentVmsConfig'];
var url = '/ems_infra_dashboard/recent_vms_data/' + providerId;
var vmsDataPromise = $http.get(url).then(function(response) {
vm.data = response.data.data;
});

$q.all([vmsDataPromise]).then(function() {
vm.data = chartsMixin.processData(vm.data.recentVms, 'dates', vm.data.recentVms.config.label)
});

vm.custShowXAxis = false;
vm.custShowYAxis = false;
vm.custAreaChart = true;
}

init();
}]);
Expand Up @@ -15,35 +15,6 @@ angular.module( 'patternfly.charts' ).controller('utilizationTrendChartControlle
});

vm.title = "Global Utilization";

// vm.config = {
// title: 'Memory',
// units: 'GB'
// };
// vm.donutConfig = {
// chartId: 'chartA',
// thresholds: {'warning': '60', 'error': '90'}
// };
// vm.sparklineConfig = {
// 'chartId': 'exampleSparkline',
// 'tooltipType': 'default',
// 'units': 'GB'
// };

// var today = new Date();
// var dates = ['dates'];
// for (var d = 20 - 1; d >= 0; d--) {
// dates.push(new Date(today.getTime() - (d * 24 * 60 * 60 * 1000)));
// }
// vm.data = {
// dataAvailable: true,
// used: 76,
// total: 100,
// xData: dates,
// yData: ['used', '10', '20', '30', '20', '30', '10', '14', '20', '25', '68', '54', '56', '78', '56', '67', '88',
// '76', '65', '87', '76']
// };

vm.centerLabel = 'used';
vm.custShowXAxis = false;
vm.custShowYAxis = false;
Expand Down Expand Up @@ -74,7 +45,7 @@ angular.module( 'patternfly.charts' ).controller('utilizationTrendChartControlle
};
} else {
metricsDataStruct.data[keys[i]] = {
'data': processData(data[keys[i]], 'dates', chartsMixin.chartConfig[keys[i] + 'UsageConfig'].units),
'data': chartsMixin.processData(data[keys[i]], 'dates', chartsMixin.chartConfig[keys[i] + 'UsageConfig'].units),
'config': {
'title': chartsMixin.chartConfig[keys[i] + 'UsageConfig'].title,
'units': chartsMixin.chartConfig[keys[i] + 'UsageConfig'].units
Expand All @@ -90,15 +61,5 @@ angular.module( 'patternfly.charts' ).controller('utilizationTrendChartControlle
return metricsDataStruct.data
};

var processData = function(data, xDataLabel, yDataLabel) {
if (!data) {
return { data: {dataAvailable: false} }
}

data.xData.unshift(xDataLabel);
data.yData.unshift(yDataLabel);
return data;
};

init();
}]);
8 changes: 4 additions & 4 deletions app/controllers/ems_infra_dashboard_controller.rb
Expand Up @@ -21,11 +21,11 @@ def cluster_metrics_data
end

def recent_hosts_data
render :json => {:data => recent_hosts_data(params[:id])}
render :json => {:data => recent_hosts(params[:id])}
end

def recent_vms_data
render :json => {:data => recent_vms_data(params[:id])}
render :json => {:data => recent_vms(params[:id])}
end

def ems_utilization_data
Expand All @@ -42,11 +42,11 @@ def cluster_heatmap_data(ems_id)
EmsInfraDashboardService.new(ems_id, self).cluster_heatmap_data
end

def recent_hosts_data(ems_id)
def recent_hosts(ems_id)
EmsInfraDashboardService.new(ems_id, self).recent_hosts_data
end

def recent_vms_data(ems_id)
def recent_vms(ems_id)
EmsInfraDashboardService.new(ems_id, self).recent_vms_data
end

Expand Down
28 changes: 19 additions & 9 deletions app/services/ems_infra_dashboard_service.rb
Expand Up @@ -30,7 +30,7 @@ def recent_hosts_data

def recent_vms_data
{
:recentVms => recentHosts
:recentVms => recentVms
}.compact
end

Expand Down Expand Up @@ -158,20 +158,30 @@ def heatmaps
def recentHosts
# Get recent hosts
all_hosts = recentRecords(Host)
{
:xData => all_hosts.keys,
:yData => all_hosts.values.map,
:title => openstack? ? _('Recent Nodes') : _('Recent Hosts'),
:label => openstack? ? _('Nodes') : _('Hosts'),
return { :dataAvailable => false } if all_hosts.blank?
{
:dataAvailable => true,
:xData => all_hosts.keys,
:yData => all_hosts.values.map,
:config => {
:title => openstack? ? _('Recent Nodes') : _('Recent Hosts'),
:label => openstack? ? _('Nodes') : _('Hosts'),
}
}
end

def recentVms
# Get recent VMs
all_vms = recentRecords(VmOrTemplate)
return { :dataAvailable => false } if all_vms.blank?
{
:xData => all_vms.keys,
:yData => all_vms.values.map
:dataAvailable => true,
:xData => all_vms.keys,
:yData => all_vms.values.map,
:config => {
:title => _('Recent VMs'),
:label => _('VMs'),
}
}
end

Expand Down Expand Up @@ -229,7 +239,7 @@ def daily_provider_metrics

@daily_metrics ||= Metric::Helper.find_for_interval_name('daily', tp)
.where(:resource => (@ems || ManageIQ::Providers::InfraManager.all))
.where('timestamp > ?', 30000.days.ago.utc).order('timestamp')
.where('timestamp > ?', 30.days.ago.utc).order('timestamp')
end

def openstack?
Expand Down
9 changes: 9 additions & 0 deletions app/views/ems_infra/_recent-hosts-line-chart.html.haml
@@ -0,0 +1,9 @@
%div{'ng-controller' => "recentHostsLineChartController as vm", :class => "row", :style => "display:inline-block; width: 100%;"}
%div{:class => "col-md-6"}
%h3
{{vm.data.config.title}}
.row.row-tile-pf
.col-xs-6.col-sm-6.col-md-6.example-trend-container-hosts
%pf-line-chart{'config' => "vm.config", 'chart-data' => "vm.data", 'set-area-chart' => "vm.custAreaChart", 'show-x-axis' => "vm.custShowXAxis", 'show-y-axis' => "vm.custShowYAxis"}
%span.trend-footer-pf{"ng-class" => "{ 'chart-transparent-text': vm.data.dataAvailable === false }"}
= _("Last 30 Days")
9 changes: 9 additions & 0 deletions app/views/ems_infra/_recent-vms-line-chart.html.haml
@@ -0,0 +1,9 @@
%div{'ng-controller' => "recentVmsLineChartController as vm", :class => "row", :style => "display:inline-block; width: 100%;"}
%div{:class => "col-md-6"}
%h3
{{vm.data.config.title}}
.row.row-tile-pf
.col-xs-6.col-sm-6.col-md-6.example-trend-container-vms
%pf-line-chart{'config' => "vm.config", 'chart-data' => "vm.data", 'set-area-chart' => "vm.custAreaChart", 'show-x-axis' => "vm.custShowXAxis", 'show-y-axis' => "vm.custShowYAxis"}
%span.trend-footer-pf{"ng-class" => "{ 'chart-transparent-text': vm.data.dataAvailable === false }"}
= _("Last 30 Days")

0 comments on commit 3fef8cc

Please sign in to comment.