Commit 88f69cbd authored by Phil Hughes's avatar Phil Hughes

Used reduce over forEach

Moved shared date & time formats into constants
parent d7154552
import d3 from 'd3';
export const dateFormat = d3.time.format('%b %d, %Y');
export const timeFormat = d3.time.format('%H:%M%p');
/* global Flash */
import d3 from 'd3'; import d3 from 'd3';
import {
dateFormat,
timeFormat,
} from './constants';
export default class Deployments { export default class Deployments {
constructor(width, height) { constructor(width, height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.data = [];
this.dateFormat = d3.time.format('%b %d, %Y');
this.timeFormat = d3.time.format('%H:%M%p');
this.endpoint = document.getElementById('js-metrics').dataset.deploymentEndpoint; this.endpoint = document.getElementById('js-metrics').dataset.deploymentEndpoint;
...@@ -29,15 +31,16 @@ export default class Deployments { ...@@ -29,15 +31,16 @@ export default class Deployments {
url: this.endpoint, url: this.endpoint,
dataType: 'JSON', dataType: 'JSON',
}) })
.fail(() => new Flash('Error getting deployment information.'))
.done((data) => { .done((data) => {
data.deployments.forEach((deployment) => { this.data = data.deployments.reduce((deploymentDataArray, deployment) => {
const time = new Date(deployment.created_at); const time = new Date(deployment.created_at);
const xPos = Math.floor(this.x(time)); const xPos = Math.floor(this.x(time));
time.setSeconds(this.chartData[0].time.getSeconds()); time.setSeconds(this.chartData[0].time.getSeconds());
if (xPos >= 0) { if (xPos >= 0) {
this.data.push({ deploymentDataArray.push({
id: deployment.id, id: deployment.id,
time, time,
sha: deployment.sha, sha: deployment.sha,
...@@ -46,7 +49,9 @@ export default class Deployments { ...@@ -46,7 +49,9 @@ export default class Deployments {
xPos, xPos,
}); });
} }
});
return deploymentDataArray;
}, []);
this.plotData(); this.plotData();
}); });
...@@ -162,14 +167,14 @@ export default class Deployments { ...@@ -162,14 +167,14 @@ export default class Deployments {
class: 'deploy-info-text', class: 'deploy-info-text',
y: 18, y: 18,
}) })
.text(d => this.dateFormat(d.time)) .text(d => dateFormat(d.time))
.select(this.selectParentNode) .select(this.selectParentNode)
.append('text') .append('text')
.attr({ .attr({
class: 'deploy-info-text text-metric-bold', class: 'deploy-info-text text-metric-bold',
y: 38, y: 38,
}) })
.text(d => this.timeFormat(d.time)); .text(d => timeFormat(d.time));
} }
static toggleDeployTextbox(deploy, key, showInfoBox) { static toggleDeployTextbox(deploy, key, showInfoBox) {
...@@ -201,9 +206,6 @@ export default class Deployments { ...@@ -201,9 +206,6 @@ export default class Deployments {
} }
static refText(d) { static refText(d) {
const isTag = d.tag; return d.tag ? d.ref : d.sha.slice(0, 6);
const refText = isTag ? d.ref : d.sha.slice(0, 6);
return refText;
} }
} }
...@@ -7,14 +7,16 @@ import Deployments from './deployments'; ...@@ -7,14 +7,16 @@ import Deployments from './deployments';
import '../lib/utils/common_utils'; import '../lib/utils/common_utils';
import { formatRelevantDigits } from '../lib/utils/number_utils'; import { formatRelevantDigits } from '../lib/utils/number_utils';
import '../flash'; import '../flash';
import {
dateFormat,
timeFormat,
} from './constants';
const prometheusContainer = '.prometheus-container'; const prometheusContainer = '.prometheus-container';
const prometheusParentGraphContainer = '.prometheus-graphs'; const prometheusParentGraphContainer = '.prometheus-graphs';
const prometheusGraphsContainer = '.prometheus-graph'; const prometheusGraphsContainer = '.prometheus-graph';
const prometheusStatesContainer = '.prometheus-state'; const prometheusStatesContainer = '.prometheus-state';
const metricsEndpoint = 'metrics.json'; const metricsEndpoint = 'metrics.json';
const timeFormat = d3.time.format('%H:%M%p');
const dayFormat = d3.time.format('%b %d, %Y');
const bisectDate = d3.bisector(d => d.time).left; const bisectDate = d3.bisector(d => d.time).left;
const extraAddedWidthParent = 100; const extraAddedWidthParent = 100;
...@@ -256,7 +258,7 @@ class PrometheusGraph { ...@@ -256,7 +258,7 @@ class PrometheusGraph {
const evalTime = timeValueOverlay - d0.time > d1.time - timeValueOverlay; const evalTime = timeValueOverlay - d0.time > d1.time - timeValueOverlay;
const currentData = evalTime ? d1 : d0; const currentData = evalTime ? d1 : d0;
const currentTimeCoordinate = Math.floor(currentGraphProps.xScale(currentData.time)); const currentTimeCoordinate = Math.floor(currentGraphProps.xScale(currentData.time));
const currentDeployXPos = this.deployments.mouseOverDeployInfo(currentTimeCoordinate, key); const currentDeployXPos = this.deployments.mouseOverDeployInfo(currentXCoordinate, key);
const currentPrometheusGraphContainer = `${prometheusGraphsContainer}[graph-type=${key}]`; const currentPrometheusGraphContainer = `${prometheusGraphsContainer}[graph-type=${key}]`;
const maxValueFromData = d3.max(currentGraphProps.data.map(metricValue => metricValue.value)); const maxValueFromData = d3.max(currentGraphProps.data.map(metricValue => metricValue.value));
const maxMetricValue = currentGraphProps.yScale(maxValueFromData); const maxMetricValue = currentGraphProps.yScale(maxValueFromData);
...@@ -317,7 +319,7 @@ class PrometheusGraph { ...@@ -317,7 +319,7 @@ class PrometheusGraph {
x: 8, x: 8,
y: 15, y: 15,
}) })
.text(dayFormat(currentData.time)); .text(dateFormat(currentData.time));
let currentMetricValue = formatRelevantDigits(currentData.value); let currentMetricValue = formatRelevantDigits(currentData.value);
if (key === 'cpu_values') { if (key === 'cpu_values') {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment