1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-15 07:40:07 +02:00
copilot/spikes/graphs-matrix/chartjs/client/actions.js

82 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-11-09 17:54:42 +02:00
const take = require('lodash.take');
2016-11-09 15:58:10 +02:00
const actions = {
'UPDATE_STATS': (state, action) => {
2016-11-11 19:56:42 +02:00
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)
};
}, {});
2016-11-09 15:58:10 +02:00
return {
...state,
2016-11-11 19:56:42 +02:00
[action.subscription]: newData
2016-11-09 15:58:10 +02:00
};
}
};
module.exports = (state, action) => {
return !actions[action.type] ? state : actions[action.type](state, action);
};
module.exports.subscribe = (id) => (dispatch, getState) => {
const {
ws
} = getState();
const p = new Promise((resolve, reject) => {
ws.subscribe(`/stats/${id}`, (update, flag) => {
dispatch({
type: 'UPDATE_STATS',
payload: update,
subscription: id
});
}, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
return dispatch({
2016-11-11 19:56:42 +02:00
type: 'SUBSCRIBE',
2016-11-09 15:58:10 +02:00
payload: p
});
};
module.exports.unsubscribe = (id) => (dispatch, getState) => {
const {
ws
} = getState();
const p = new Promise((resolve, reject) => {
ws.unsubscribe(`/stats/${id}`, null, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
return dispatch({
2016-11-11 19:56:42 +02:00
type: 'UNSUBSCRIBE',
2016-11-09 15:58:10 +02:00
payload: p
});
};