1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 06:00:06 +02:00

fix(cp-frontend): custom getPreviousEnd

This commit is contained in:
Sérgio Ramos 2017-08-30 11:40:15 +01:00
parent b918a5bb66
commit 827379806b
4 changed files with 42 additions and 21 deletions

View File

@ -35,6 +35,7 @@
"lodash.isstring": "^4.0.1", "lodash.isstring": "^4.0.1",
"lodash.remove": "^4.7.0", "lodash.remove": "^4.7.0",
"lodash.uniq": "^4.5.0", "lodash.uniq": "^4.5.0",
"lodash.uniqby": "^4.7.0",
"normalized-styled-components": "^1.0.8", "normalized-styled-components": "^1.0.8",
"param-case": "^2.1.1", "param-case": "^2.1.1",
"prop-types": "^15.5.10", "prop-types": "^15.5.10",

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { compose, graphql } from 'react-apollo'; import { compose, graphql } from 'react-apollo';
import get from 'lodash.get';
import moment from 'moment'; import moment from 'moment';
import uniqBy from 'lodash.uniqby';
export const MetricNames = [ export const MetricNames = [
'AVG_MEM_BYTES', 'AVG_MEM_BYTES',
@ -10,22 +10,17 @@ export const MetricNames = [
]; ];
export const withServiceMetricsPolling = ({ export const withServiceMetricsPolling = ({
pollingInterval = 1000 // in milliseconds pollingInterval = 1000, // in milliseconds
getPreviousEnd = () => moment().utc().format()
}) => { }) => {
return WrappedComponent => { return WrappedComponent => {
return class extends Component { return class extends Component {
componentDidMount() { componentDidMount() {
this._poll = setInterval(() => { this._poll = setInterval(() => {
const { loading, error, service, fetchMoreMetrics } = this.props; const { loading, error, service, fetchMoreMetrics } = this.props;
const previousEnd = getPreviousEnd(this.props);
if (!loading && !error && service) { if (previousEnd) {
const previousEnd = get(
service,
'instances[0].metrics[0].end',
moment()
.utc()
.format()
);
fetchMoreMetrics(previousEnd); fetchMoreMetrics(previousEnd);
} }
}, pollingInterval); // TODO this is the polling interval - think about amount is the todo I guess... }, pollingInterval); // TODO this is the polling interval - think about amount is the todo I guess...
@ -63,7 +58,8 @@ export const withServiceMetricsGql = ({
const getNextResult = (previousResult, fetchNextResult) => { const getNextResult = (previousResult, fetchNextResult) => {
const deploymentGroup = fetchNextResult.deploymentGroup; const deploymentGroup = fetchNextResult.deploymentGroup;
const nextResult = {
return {
deploymentGroup: { deploymentGroup: {
...deploymentGroup, ...deploymentGroup,
services: deploymentGroup.services.map(service => ({ services: deploymentGroup.services.map(service => ({
@ -72,18 +68,17 @@ export const withServiceMetricsGql = ({
...instance, ...instance,
metrics: instance.metrics.map(metric => ({ metrics: instance.metrics.map(metric => ({
...metric, ...metric,
metrics: getPreviousMetrics( metrics: uniqBy(getPreviousMetrics(
previousResult, previousResult,
service.id, service.id,
instance.id, instance.id,
metric.name metric.name
).concat(metric.metrics) ).concat(metric.metrics), 'time')
})) }))
})) }))
})) }))
} }
}; };
return nextResult;
}; };
return graphql(gqlQuery, { return graphql(gqlQuery, {

View File

@ -70,6 +70,15 @@ export default compose(
error error
}) })
}), }),
withServiceMetricsPolling({ pollingInterval: 1000 }), withServiceMetricsPolling({
pollingInterval: 1000,
getPreviousEnd: ({ loading, error, service = [] }) => {
if (loading) {
return false;
}
return get(service, 'instances[0].metrics[0].end', moment().utc().format());
}
}),
withNotFound([GqlPaths.DEPLOYMENT_GROUP, GqlPaths.SERVICES]) withNotFound([GqlPaths.DEPLOYMENT_GROUP, GqlPaths.SERVICES])
)(ServiceMetrics); )(ServiceMetrics);

View File

@ -5,13 +5,10 @@ import { connect } from 'react-redux';
import styled from 'styled-components'; import styled from 'styled-components';
import forceArray from 'force-array'; import forceArray from 'force-array';
import sortBy from 'lodash.sortby'; import sortBy from 'lodash.sortby';
import get from 'lodash.get';
import moment from 'moment';
import ServicesQuery from '@graphql/Services.gql'; import ServicesQuery from '@graphql/Services.gql';
import {
processServices,
processInstancesMetrics
} from '@root/state/selectors';
import { toggleServicesQuickActions } from '@root/state/actions'; import { toggleServicesQuickActions } from '@root/state/actions';
import { withNotFound, GqlPaths } from '@containers/navigation'; import { withNotFound, GqlPaths } from '@containers/navigation';
import { LayoutContainer } from '@components/layout'; import { LayoutContainer } from '@components/layout';
@ -23,6 +20,11 @@ import {
withServiceMetricsGql withServiceMetricsGql
} from '@containers/metrics'; } from '@containers/metrics';
import {
processServices,
processInstancesMetrics
} from '@root/state/selectors';
// 'width' of graph, i.e. total duration of time it'll display and truncate data to // 'width' of graph, i.e. total duration of time it'll display and truncate data to
// amount of data we'll need to initially fetch // amount of data we'll need to initially fetch
const GraphDurationSeconds = 90; const GraphDurationSeconds = 90;
@ -182,7 +184,21 @@ export default compose(
error error
}) })
}), }),
withServiceMetricsPolling({ pollingInterval: 1000 }), withServiceMetricsPolling({
pollingInterval: 1000,
getPreviousEnd: ({ loading, error, services = [] }) => {
if (loading) {
return false;
}
const previousEnd = services
.map(service => get(service, 'instances[0].metrics[0].end', null))
.filter(Boolean)
.shift();
return previousEnd || moment().utc().format();
}
}),
UiConnect, UiConnect,
withNotFound([GqlPaths.DEPLOYMENT_GROUP]) withNotFound([GqlPaths.DEPLOYMENT_GROUP])
)(ServiceList); )(ServiceList);