diff --git a/spikes/graphs-fe/chartjs/server/metric.js b/spikes/graphs-fe/chartjs/server/metric.js
index 0d64fad2..b7031178 100644
--- a/spikes/graphs-fe/chartjs/server/metric.js
+++ b/spikes/graphs-fe/chartjs/server/metric.js
@@ -19,7 +19,7 @@ module.exports = (server) => ({
when: new Date().getTime(),
cpu: Math.random() * 100
});
- }, 400);
+ }, 45);
cdm[id] = {
interval,
diff --git a/spikes/graphs-matrix/chartjs/client/actions.js b/spikes/graphs-matrix/chartjs/client/actions.js
index d48d610c..852bf7ea 100644
--- a/spikes/graphs-matrix/chartjs/client/actions.js
+++ b/spikes/graphs-matrix/chartjs/client/actions.js
@@ -2,11 +2,29 @@ const take = require('lodash.take');
const actions = {
'UPDATE_STATS': (state, action) => {
- const data = [action.payload].concat(state[action.subscription] || []);
+ const data = (state[action.subscription] || {
+ cpu: [],
+ mem: [],
+ disk: []
+ });
+
+ const newData = ['cpu', 'mem', 'disk'].reduce((sum, key) => {
+ const item = {
+ ...action.payload.stats[key],
+ when: action.payload.when
+ };
+
+ const prepended = [item].concat(data[key]);
+
+ return {
+ ...sum,
+ [key]: take(prepended, state.windowSize)
+ };
+ }, {});
return {
...state,
- [action.subscription]: take(data, state.windowSize)
+ [action.subscription]: newData
};
}
};
@@ -37,7 +55,7 @@ module.exports.subscribe = (id) => (dispatch, getState) => {
});
return dispatch({
- action: 'SUBSCRIBE',
+ type: 'SUBSCRIBE',
payload: p
});
};
@@ -58,7 +76,7 @@ module.exports.unsubscribe = (id) => (dispatch, getState) => {
});
return dispatch({
- action: 'UNSUBSCRIBE',
+ type: 'UNSUBSCRIBE',
payload: p
});
};
\ No newline at end of file
diff --git a/spikes/graphs-matrix/chartjs/client/chart.js b/spikes/graphs-matrix/chartjs/client/chart.js
deleted file mode 100644
index cae1536f..00000000
--- a/spikes/graphs-matrix/chartjs/client/chart.js
+++ /dev/null
@@ -1,144 +0,0 @@
-const buildArray = require('build-array');
-const Chart = require('chart.js');
-const React = require('react');
-
-// borderSkipped
-// patch `.draw` to support `borderSkipped`:
-// Chart.elements.Rectangle.prototype.draw = function() {
-// var ctx = this._chart.ctx;
-// var vm = this._view;
-//
-// var halfWidth = vm.width / 2,
-// leftX = vm.x - halfWidth,
-// rightX = vm.x + halfWidth,
-// top = vm.base - (vm.base - vm.y),
-// halfStroke = vm.borderWidth / 2;
-//
-// // Canvas doesn't allow us to stroke inside the width so we can
-// // adjust the sizes to fit if we're setting a stroke on the line
-// if (vm.borderWidth) {
-// leftX += halfStroke;
-// rightX -= halfStroke;
-// top += halfStroke;
-// }
-//
-// ctx.beginPath();
-// ctx.fillStyle = vm.backgroundColor;
-// ctx.strokeStyle = vm.borderColor;
-// ctx.lineWidth = vm.borderWidth;
-//
-// var borderSkipped = !Array.isArray(vm.borderSkipped)
-// ? [vm.borderSkipped]
-// : vm.borderSkipped;
-//
-// // Corner points, from bottom-left to bottom-right clockwise
-// // | 1 2 |
-// // | 0 3 |
-// var corners = [
-// [leftX, vm.base, (borderSkipped.indexOf('bottom') >= 0), 'bottom'],
-// [leftX, top, (borderSkipped.indexOf('left') >= 0), 'left'],
-// [rightX, top, (borderSkipped.indexOf('top') >= 0), 'top'],
-// [rightX, vm.base, (borderSkipped.indexOf('right') >= 0), 'right']
-// ];
-//
-// function cornerAt(index) {
-// return corners[index % 4];
-// }
-//
-// // Draw rectangle from 'startCorner'
-// var corner = cornerAt(0);
-// ctx.moveTo(corner[0], corner[1]);
-//
-// for (var i = 1; i < 5; i++) {
-// corner = cornerAt(i);
-// ctx.lineTo(corner[0], corner[1]);
-//
-// if (!corner[2]) {
-// ctx.stroke();
-// }
-// }
-//
-// console.log(corners);
-//
-// ctx.fill();
-// };
-
-module.exports = React.createClass({
- ref: function(name) {
- this._refs = this._refs || {};
-
- return (el) => {
- this._refs[name] = el;
- };
- },
- fromData: function(data) {
- return (data || []).map((d) => {
- return d.cpu;
- });
- },
- componentDidMount: function() {
- const {
- data = [],
- bg,
- border
- } = this.props;
-
- const bars = this.fromData(data);
-
- this._chart = new Chart(this._refs.component, {
- type: 'bar',
- options: {
- elements: {
- rectangle: {
- borderSkipped: ['bottom', 'left', 'right']
- }
- },
- scales: {
- xAxes: [{
- display: false
- }],
- yAxes: [{
- display: false
- }]
- },
- legend: {
- display: false
- }
- },
- data: {
- labels: buildArray(bars.length).map((v, i) => ''),
- datasets: [{
- borderWidth: 1,
- borderColor: border,
- backgroundColor: bg,
- data: bars
- }]
- }
- });
- },
- componentWillReceiveProps: function(nextProps) {
- const {
- data = [],
- bg,
- border
- } = this.props;
-
- const bars = this.fromData(data);
-
- this._chart.data.labels = buildArray(bars.length).map((v, i) => '');
- this._chart.data.datasets[0].backgroundColor = bg;
- this._chart.data.datasets[0].borderColor = border;
- this._chart.data.datasets[0].data = bars;
-
- this._chart.update(0);
- },
- render: function() {
- return (
-
- );
- }
-});
diff --git a/spikes/graphs-matrix/chartjs/client/chart/base.js b/spikes/graphs-matrix/chartjs/client/chart/base.js
new file mode 100644
index 00000000..d3c52441
--- /dev/null
+++ b/spikes/graphs-matrix/chartjs/client/chart/base.js
@@ -0,0 +1,71 @@
+const buildArray = require('build-array');
+const Chart = require('chart.js');
+const React = require('react');
+
+module.exports = React.createClass({
+ ref: function(name) {
+ this._refs = this._refs || {};
+
+ return (el) => {
+ this._refs[name] = el;
+ };
+ },
+ componentDidMount: function() {
+ const {
+ datasets = [],
+ labels = 0,
+ stacked = false,
+ xAxe = false,
+ yAxe = false,
+ legend = false
+ } = this.props;
+
+ const _labels = !Array.isArray(labels)
+ ? buildArray(labels).map((v, i) => '')
+ : labels;
+
+ this._chart = new Chart(this._refs.component, {
+ type: 'bar',
+ stacked: stacked,
+ responsive: true,
+ options: {
+ scales: {
+ xAxes: [{
+ display: xAxe,
+ stacked: stacked
+ }],
+ yAxes: [{
+ display: yAxe,
+ stacked: stacked
+ }]
+ },
+ legend: {
+ display: legend
+ }
+ },
+ data: {
+ labels:
+ datasets: datasets
+ }
+ });
+ },
+ componentWillReceiveProps: function(nextProps) {
+ const {
+ datasets = [],
+ labels = 0
+ } = this.props;
+
+ this._chart.data.datasets = datasets;
+ this._chart.data.labels = buildArray(labels).map((v, i) => '');
+ this._chart.update(0);
+ },
+ render: function() {
+ return (
+
+ );
+ }
+});
diff --git a/spikes/graphs-matrix/chartjs/client/chart/cpu.js b/spikes/graphs-matrix/chartjs/client/chart/cpu.js
new file mode 100644
index 00000000..6df20000
--- /dev/null
+++ b/spikes/graphs-matrix/chartjs/client/chart/cpu.js
@@ -0,0 +1,31 @@
+const buildArray = require('build-array');
+const Chart = require('./base');
+const React = require('react');
+
+const colors = {
+ user: 'rgb(255, 99, 132)',
+ sys: 'rgb(255, 159, 64)'
+};
+
+module.exports = ({
+ data = {},
+ windowSize
+}) => {
+ const datasets = ['user', 'sys'].map((key) => {
+ return {
+ label: key,
+ backgroundColor: colors[key],
+ data: buildArray(windowSize).map((v, i) => ((data[i] || {})[key] || 0))
+ };
+ });
+
+ return (
+