Skip to content

Commit 8aaaafa

Browse files
committed
refact: spring cleanup
1 parent b59962e commit 8aaaafa

File tree

4 files changed

+172
-234
lines changed

4 files changed

+172
-234
lines changed

src/MetricHandler.js

Lines changed: 61 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import MetricQHistory from '@metricq/history'
33
import Vue from 'vue'
44

55
export class MetricHandler {
6-
constructor (paramRenderer, paramMetricsArr, paramStartTime, paramStopTime, store, metricqBackendConfig) {
6+
constructor (paramRenderer, paramStartTime, paramStopTime, store, metricqBackendConfig) {
77
this.store = store
88
this.renderer = paramRenderer
99
this.startTime = new MetricTimestamp(paramStartTime, 'start')
@@ -13,7 +13,7 @@ export class MetricHandler {
1313
this.WIGGLEROOM_PERCENTAGE = 0.05
1414
this.TIME_MARGIN_FACTOR = 1.00 / 3
1515

16-
this.initializeMetrics(paramMetricsArr)
16+
this.initializeMetrics([])
1717
}
1818

1919
initializeMetrics (initialMetricNames) {
@@ -26,14 +26,14 @@ export class MetricHandler {
2626

2727
doRequest (maxDataPoints) {
2828
const timeMargin = (this.stopTime.getUnix() - this.startTime.getUnix()) * this.TIME_MARGIN_FACTOR
29-
const nonErrorProneMetrics = []
30-
const remainingMetrics = []
29+
const metrics = []
30+
const errorProneMetrics = []
3131
for (const curMetric of this.store.getters['metrics/getAll']()) {
3232
if (curMetric.name.length > 0) {
3333
if (curMetric.errorprone) {
34-
remainingMetrics.push(curMetric.name)
34+
errorProneMetrics.push(curMetric.name)
3535
} else {
36-
nonErrorProneMetrics.push(curMetric.name)
36+
metrics.push(curMetric.name)
3737
}
3838
}
3939
}
@@ -42,32 +42,32 @@ export class MetricHandler {
4242
this.stopTime.getUnix() + timeMargin,
4343
Math.round(maxDataPoints + (maxDataPoints * this.TIME_MARGIN_FACTOR * 2)))
4444
const defaultAggregates = ['min', 'max', 'avg', 'count']
45-
for (let i = 0; i < nonErrorProneMetrics.length; ++i) {
46-
queryObj.target(nonErrorProneMetrics[i], defaultAggregates)
45+
for (let i = 0; i < metrics.length; ++i) {
46+
queryObj.target(metrics[i], defaultAggregates)
4747
}
4848
const startTime = window.performance.now()
4949
if (queryObj.targets.length > 0) {
5050
// TODO: register some callback
5151
// execute query
5252
// TODO: pass parameter nonErrorProneMetrics
5353
queryObj.run().then((dataset) => {
54-
this.handleResponse(nonErrorProneMetrics, dataset, startTime)
54+
this.handleResponse(metrics, dataset, startTime)
5555
}).catch(() => {
56-
console.log('Request failed: ' + nonErrorProneMetrics.join(','))
57-
nonErrorProneMetrics.forEach((curVal) => {
56+
console.log('Request failed: ' + metrics.join(','))
57+
metrics.forEach((curVal) => {
5858
console.log('Marking as faulty: ' + curVal)
5959
this.receivedError(0, curVal)
6060
})
6161
this.doRequest(maxDataPoints)
6262
})
6363
}
64-
for (let i = 0; i < remainingMetrics.length; ++i) {
64+
for (let i = 0; i < errorProneMetrics.length; ++i) {
6565
const queryObj = this.metricQHistory.query(this.startTime.getUnix() - timeMargin,
6666
this.stopTime.getUnix() + timeMargin,
6767
maxDataPoints)
68-
queryObj.target(remainingMetrics[i], defaultAggregates)
68+
queryObj.target(errorProneMetrics[i], defaultAggregates)
6969
queryObj.run().then((dataset) => {
70-
this.handleResponse([remainingMetrics[i]], dataset, startTime)
70+
this.handleResponse([errorProneMetrics[i]], dataset, startTime)
7171
})
7272
}
7373
}
@@ -77,13 +77,14 @@ export class MetricHandler {
7777
const listOfFaultyMetrics = []
7878
let pointCountAgg = null
7979
let pointCountRaw = 0
80-
for (let i = 0; i < requestedMetrics.length; ++i) {
81-
const metricName = requestedMetrics[i]
80+
81+
for (const metric of requestedMetrics) {
8282
const matchingAggregatesObj = {}
8383
let matchingAggregatesCount = 0
84+
8485
for (const curMetricName in myData) {
8586
const splitted = curMetricName.split('/')
86-
if (splitted[0] === requestedMetrics[i]) {
87+
if (splitted[0] === metric) {
8788
matchingAggregatesObj[splitted[1]] = true
8889
matchingAggregatesCount += 1
8990
if (splitted[1] === 'count' || splitted[1] === 'raw') {
@@ -102,31 +103,31 @@ export class MetricHandler {
102103
}
103104
}
104105
}
105-
if (!this.checkIfMetricIsOk(metricName, matchingAggregatesCount, matchingAggregatesObj)) {
106-
listOfFaultyMetrics.push(metricName)
107-
console.log('Metric not ok:' + metricName)
108-
this.receivedError(0, metricName)
106+
107+
if (!this.checkIfMetricIsOk(metric, matchingAggregatesCount, matchingAggregatesObj)) {
108+
listOfFaultyMetrics.push(metric)
109+
console.log('Metric not ok:' + metric)
110+
this.receivedError(0, metric)
109111
}
110112
}
113+
111114
this.store.commit('setAggregatePoints', pointCountAgg)
112115
this.store.commit('setRawPoints', pointCountRaw)
116+
113117
if (listOfFaultyMetrics.length > 0) {
114-
Vue.toasted.error('Fehler mit Metriken: ' + listOfFaultyMetrics.join(', '), this.store.state.toastConfiguration)
118+
Vue.toasted.error('Fehler beim Abfragen von: ' + listOfFaultyMetrics.join(', '), this.store.state.toastConfiguration)
115119
}
120+
116121
this.renderer.renderMetrics(myData, startTime)
117122
}
118123

119124
checkIfMetricIsOk (metricName, aggregateCount, aggregateObj) {
120-
if (!metricName ||
121-
aggregateCount < 1 ||
122-
(!aggregateObj.count && !aggregateObj.raw)) {
123-
return false
124-
}
125-
if (!((aggregateObj.raw && !aggregateObj.min && !aggregateObj.max) ||
126-
(!aggregateObj.raw && aggregateObj.min && aggregateObj.max))) {
125+
if (!metricName || aggregateCount < 1 || (!aggregateObj.count && !aggregateObj.raw)) {
127126
return false
128127
}
129-
return true
128+
129+
// we want (raw xor (min and max))
130+
return aggregateObj.raw !== (aggregateObj.min && aggregateObj.max)
130131
}
131132

132133
searchMetricsPromise (inputStr, metadata = false) {
@@ -135,11 +136,10 @@ export class MetricHandler {
135136

136137
// TODO: move this function to DataCache, maybe?
137138
getAllMinMax () {
138-
const referenceAttribute = 'minmax'
139139
if (this.renderer.graticule.yRangeOverride.type === 'manual') {
140140
return [this.renderer.graticule.yRangeOverride.min, this.renderer.graticule.yRangeOverride.max]
141141
}
142-
let allMinMax = [undefined, undefined]
142+
const result = [Infinity, -Infinity]
143143
const timeFrame = this.renderer.graticule.curTimeRange
144144
for (const curMetric of this.store.getters['metrics/getAll']()) {
145145
if (!curMetric.draw) continue
@@ -154,73 +154,50 @@ export class MetricHandler {
154154
curMinMax = curCache.getAllMinMax(timeFrame[0], timeFrame[1])
155155
}
156156
if (curMinMax) {
157-
if (undefined === allMinMax[0]) {
158-
allMinMax = curMinMax
159-
} else {
160-
if (curMinMax[0] < allMinMax[0]) {
161-
allMinMax[0] = curMinMax[0]
162-
}
163-
if (curMinMax[1] > allMinMax[1]) {
164-
allMinMax[1] = curMinMax[1]
165-
}
157+
if (curMinMax[0] < result[0]) {
158+
result[0] = curMinMax[0]
159+
}
160+
if (curMinMax[1] > result[1]) {
161+
result[1] = curMinMax[1]
166162
}
167163
}
168164
}
169165
}
170166
// add a little wiggle room, so that markers won't be cut off
171-
const delta = allMinMax[1] - allMinMax[0]
172-
allMinMax[0] -= delta * this.WIGGLEROOM_PERCENTAGE
173-
allMinMax[1] += delta * this.WIGGLEROOM_PERCENTAGE
174-
return allMinMax
167+
const delta = result[1] - result[0]
168+
result[0] -= delta * this.WIGGLEROOM_PERCENTAGE
169+
result[1] += delta * this.WIGGLEROOM_PERCENTAGE
170+
return result
175171
}
176172

177-
setTimeRange (paramStartTime, paramStopTime) {
178-
// TODO: check for zoom area if it is too narrow (i.e. less than 1000 ms)
179-
// TODO: sync the aforementioned minimum time window
180-
if (undefined === paramStartTime || paramStartTime instanceof MetricTimestamp) {
181-
paramStartTime = this.startTime.getUnix()
182-
} else {
183-
this.startTime.updateTime(paramStartTime)
173+
setTimeRange (newStartTime, newStopTime) {
174+
if (undefined === newStartTime || newStartTime instanceof MetricTimestamp) {
175+
newStartTime = this.startTime.getUnix()
184176
}
185-
if (undefined === paramStopTime || paramStopTime instanceof MetricTimestamp) {
186-
paramStopTime = this.stopTime.getUnix()
187-
} else {
188-
this.stopTime.updateTime(paramStopTime)
177+
if (undefined === newStopTime || newStopTime instanceof MetricTimestamp) {
178+
newStopTime = this.stopTime.getUnix()
189179
}
190180

191-
if (isNaN(paramStartTime) || isNaN(paramStopTime)) {
181+
if (isNaN(newStartTime) || isNaN(newStopTime)) {
192182
throw new Error('uh oh time is NaN')
193183
}
194-
if (paramStartTime >= paramStopTime) {
195-
throw new Error(`startTime(${paramStartTime}) is not smaller than stopTime(${paramStopTime})`)
184+
if (newStartTime >= newStopTime) {
185+
throw new Error(`startTime(${newStartTime}) is not smaller than stopTime(${newStopTime})`)
196186
}
197187

198-
let timeSuitable = true
199-
if ((paramStopTime - paramStartTime) < this.renderer.graticule.MIN_ZOOM_TIME) {
200-
const oldDelta = paramStopTime - paramStartTime
201-
const newDelta = this.renderer.graticule.MIN_ZOOM_TIME
202-
paramStartTime -= Math.round((newDelta - oldDelta) / 2.00)
203-
paramStopTime += Math.round((newDelta - oldDelta) / 2.00)
204-
timeSuitable = false
188+
if (newStopTime - newStartTime < this.renderer.graticule.MIN_ZOOM_TIME) {
189+
return false
205190
}
206-
if ((paramStopTime - paramStartTime) > this.renderer.graticule.MAX_ZOOM_TIME) {
207-
const oldDelta = paramStopTime - paramStartTime
208-
const newDelta = this.renderer.graticule.MAX_ZOOM_TIME
209-
paramStartTime += Math.round((oldDelta - newDelta) / 2.00)
210-
paramStopTime -= Math.round((oldDelta - newDelta) / 2.00)
211-
timeSuitable = false
191+
if (newStopTime - newStartTime > this.renderer.graticule.MAX_ZOOM_TIME) {
192+
return false
212193
}
213194

214-
this.renderer.updateMetricUrl()
215-
// maybe move this line to MetricQWebView.setPlotRanges()? NAW
216-
window.MetricQWebView.graticule.setTimeRange(this.startTime.getUnix(), this.stopTime.getUnix())
217-
return timeSuitable
218-
// this.lastRangeChangeTime = (new Date()).getTime();
219-
// TODO: return false when intended zoom area is smaller than e.g. 1000 ms
220-
// TODO: define a CONSTANT that is MINIMUM_ZOOM_AREA
195+
this.startTime.updateTime(newStartTime)
196+
this.stopTime.updateTime(newStopTime)
221197

222-
// TODO: call url export here?
223-
// return true;
198+
this.renderer.updateMetricUrl()
199+
this.renderer.graticule.setTimeRange(this.startTime.getUnix(), this.stopTime.getUnix())
200+
return true
224201
}
225202

226203
zoomTimeAtPoint (pointAt, zoomDirection) {
@@ -237,10 +214,10 @@ export class MetricHandler {
237214
return couldZoom
238215
}
239216

240-
receivedError (errorCode, metricBase) {
217+
receivedError (_errorCode, metric) {
241218
// mark a metric so it is being excluded in bulk-requests
242-
if (this.store.getters['metrics/get'](metricBase)) {
243-
this.store.dispatch('metrics/setError', { metricKey: metricBase })
219+
if (this.store.getters['metrics/get'](metric)) {
220+
this.store.dispatch('metrics/setError', { metricKey: metric })
244221
}
245222
}
246223

0 commit comments

Comments
 (0)