Skip to content

Commit

Permalink
fix edge-case by removing more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bmario committed Sep 29, 2024
1 parent 6110ccd commit e77a9d5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 76 deletions.
72 changes: 13 additions & 59 deletions src/data-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,6 @@ export class DataCache {
return false
}

getAllValuesAtTime (timeAt) {
const valueArr = []
for (let i = 0; i < this.metrics.length; ++i) {
for (const curAggregate in this.metrics[i].series) {
if (this.metrics[i].series[curAggregate] &&
this.metrics[i].series[curAggregate].points.length > 0 &&
store.getters['metrics/getMetricDrawState'](this.metrics[i].name).draw) {
const result = this.metrics[i].series[curAggregate].getValueAtTimeAndIndex(timeAt)
if (result) {
valueArr.push([
result[0],
result[1],
this.metrics[i].series[curAggregate],
this.metrics[i].name,
curAggregate
])
}
}
}
}
return valueArr
}

deleteMetric (metricName) {
for (let i = 0; i < this.metrics.length; ++i) {
if (metricName === this.metrics[i].name) {
Expand Down Expand Up @@ -441,43 +418,20 @@ class Series {
}
}
const closestPointIndex = closestIndex
if (this.points[closestPointIndex].time !== timeAt &&
this.styleOptions &&
this.styleOptions.connect &&
this.styleOptions.connect !== 'none') {
let betterIndex = closestPointIndex
if (this.styleOptions.connect === 'next') {
if (this.points[betterIndex].time > timeAt) {
--betterIndex
}
} else if (this.styleOptions.connect === 'last') {
if (this.points[betterIndex].time < timeAt) {
++betterIndex
}
} else if (this.styleOptions.connect === 'direct') { // linear-interpolation
let firstPoint, secondPoint
if ((timeAt < this.points[betterIndex].time && betterIndex < 0) || (betterIndex + 1) >= this.points.length) {
firstPoint = this.points[betterIndex - 1]
secondPoint = this.points[betterIndex]
} else {
firstPoint = this.points[betterIndex]
secondPoint = this.points[betterIndex + 1]
}
const timeDelta = secondPoint.time - firstPoint.time
const valueDelta = secondPoint.value - firstPoint.value
return [timeAt, firstPoint.value + valueDelta * ((timeAt - firstPoint.time) / timeDelta), betterIndex]
}
if (betterIndex < 0) {
betterIndex = 0
return undefined
} else if (betterIndex >= this.points.length) {
betterIndex = this.points.length - 1
return undefined
}
return [this.points[betterIndex].time, this.points[betterIndex].value, betterIndex]
} else {
return [this.points[closestPointIndex].time, this.points[closestPointIndex].value, closestPointIndex]

if (closestPointIndex === 0 &&
this.points[closestPointIndex].time > timeAt
) {
return undefined
}

if (closestPointIndex === this.points.length - 1 &&
this.points[closestPointIndex].time < timeAt
) {
return undefined
}

return [this.points[closestPointIndex].time, this.points[closestPointIndex].value, closestPointIndex]
}

addPoint (newPoint, isBigger) {
Expand Down
50 changes: 33 additions & 17 deletions src/interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ function uiInteractLegend (evtObj) {
return
}

const timeAt = curPoint[0]

window.MetricQWebView.graticule.draw(false)

const myCtx = window.MetricQWebView.graticule.ctx
Expand All @@ -159,33 +161,47 @@ function uiInteractLegend (evtObj) {
const metricsArray = []
let maxNameWidth = 0
let maxValueWidth = 0
const allValuesAtTime = window.MetricQWebView.graticule.data.getAllValuesAtTime(curPoint[0])

for (let i = 0; i < allValuesAtTime.length; ++i) {
const newEntry = { metric: allValuesAtTime[i][2] }
for (const metric of Object.values(window.MetricQWebView.graticule.data.metrics)) {
const metricDrawState = store.getters['metrics/getMetricDrawState'](metric.name)

if (!metricDrawState.draw) continue

let curText
if (metric.series.raw !== undefined) {
const value = metric.series.raw.getValueAtTimeAndIndex(timeAt)
if (value === undefined) continue

curText = (Number(value[1])).toFixed(3)
} else if (metric.series.min !== undefined &&
metric.series.max !== undefined &&
metric.series.avg !== undefined) {
const min = metric.series.min.getValueAtTimeAndIndex(timeAt)
const max = metric.series.max.getValueAtTimeAndIndex(timeAt)
const avg = metric.series.avg.getValueAtTimeAndIndex(timeAt)
if (min === undefined || max === undefined || avg === undefined) continue

let curText = ''
if (allValuesAtTime[i][4] === 'raw') {
curText = (Number(allValuesAtTime[i][1])).toFixed(3)
} else {
curText = ''
const metricDrawState = store.getters['metrics/getMetricDrawState'](allValuesAtTime[i][3])
if (metricDrawState.drawMin) {
curText += '▼ ' + (Number(allValuesAtTime[i][1])).toFixed(3) + ' | '
curText += '▼ ' + (Number(min[1])).toFixed(3) + ' | '
}
if (metricDrawState.drawAvg) {
curText += ' ⌀ ' + (Number(allValuesAtTime[i + 2][1])).toFixed(3)
curText += ' ⌀ ' + (Number(avg[1])).toFixed(3)
}
if (metricDrawState.drawMax) {
curText += ' | ▲ ' + (Number(allValuesAtTime[i + 1][1])).toFixed(3)
curText += ' | ▲ ' + (Number(max[1])).toFixed(3)
}
i += 2
} else {
continue
}

newEntry.curText = curText
newEntry.name = allValuesAtTime[i][3]
newEntry.curTextWidth = myCtx.measureText(curText).width
newEntry.nameWidth = myCtx.measureText(allValuesAtTime[i][3]).width
const newEntry = {
metric: metric,
curText: curText,
name: metric.name,
curTextWidth: myCtx.measureText(curText).width,
nameWidth: myCtx.measureText(metric.name).width
}

if (newEntry.curTextWidth > maxValueWidth) {
maxValueWidth = newEntry.curTextWidth
Expand Down Expand Up @@ -249,7 +265,7 @@ function drawHoverText (myCtx, metricsArray, curXPosOnCanvas, maxValueWidth, max
}
for (let i = 0; i < metricsArray.length; ++i) {
const y = offsetTop + i * verticalDiff
myCtx.fillStyle = metricsArray[i].metric.styleOptions.color
myCtx.fillStyle = metricsArray[i].metric.color
myCtx.globalAlpha = 0.4
myCtx.fillRect(curXPosOnCanvas + offsetMid - offsetRight - borderPadding, y, maxValueWidth + maxNameWidth + (offsetMid + borderPadding) * 2, 20)
myCtx.fillStyle = '#000000'
Expand Down

0 comments on commit e77a9d5

Please sign in to comment.