Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix diff_last aggregate calculation #1634

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default {
return Promise.resolve(getter([]))
}

let boundary = seriesComponents[component.component].includeBoundary?.(component)
const itemPromises = neededItems.map((neededItem) => {
if (this.items[neededItem]) return Promise.resolve(this.items[neededItem])
return this.$oh.api.get(`/rest/items/${neededItem}`).then((item) => {
Expand All @@ -154,7 +155,8 @@ export default {
let query = {
serviceId: component.config.service || undefined,
starttime: seriesStartTime.toISOString(),
endtime: seriesEndTime.subtract(1, 'millisecond').toISOString()
endtime: seriesEndTime.subtract(1, 'millisecond').toISOString(),
boundary: boundary
}

return Promise.all([itemPromises[neededItem], this.$oh.api.get(url, query)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export default (aggregationFunction, arr, idx, values) => {
aggregate = idx < 1 ? NaN : arr[1][0] - values[idx - 1][1][0]
break
case 'diff_last':
aggregate = idx < 1 ? NaN : arr[1][arr[1].length - 1] - values[idx - 1][1][values[idx - 1][1].length - 1]
if (idx < 1) {
aggregate = arr[1][arr[1].length - 1] - arr[1][0]
} else {
aggregate = arr[1][arr[1].length - 1] - values[idx - 1][1][values[idx - 1][1].length - 1]
}
break
case 'average':
aggregate = arr[1].reduce((sum, state) => sum + parseFloat(state), 0) / arr[1].length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,43 @@ function dimensionFromDate (d, dimension, invert) {
}
}

function includeBoundaryFor (component) {
return (!component || !component.config || component.config.aggregationFunction !== 'diff_last') ? undefined : true
}

export default {
neededItems (component) {
if (!component || !component.config || !component.config.item) return []
return [component.config.item]
},
includeBoundary (component) {
return includeBoundaryFor(component)
},
get (component, points, startTime, endTime, chart) {
let dimension1 = component.config.dimension1
let dimension2 = component.config.dimension2
let boundary = includeBoundaryFor(component)

// we'll suppose dimension2 always more granular than dimension1
// e.g. if dimension1=day, dimension2 can be hour but not month
let groupStart = dimension2 || dimension1
if (groupStart === 'weekday' || groupStart === 'isoWeekday') groupStart = 'day'

let itemPoints = points.find(p => p.name === component.config.item).data

if (boundary && itemPoints.length) {
let stime = dayjs(itemPoints[0].time)
let start = stime.startOf(groupStart)
if (!stime.isSame(start)) {
itemPoints.unshift({ time: start.valueOf(), state: NaN })
}
let etime = dayjs(itemPoints[itemPoints.length - 1].time)
if (etime.isSame(etime.endOf(groupStart))) {
itemPoints.splice(-1, 1)
}
}

const itemPoints = points.find(p => p.name === component.config.item).data
const groups = itemPoints.reduce((acc, p) => {
// we'll suppose dimension2 always more granular than dimension1
// e.g. if dimension1=day, dimension2 can be hour but not month
let groupStart = dimension2 || dimension1
if (groupStart === 'weekday' || groupStart === 'isoWeekday') groupStart = 'day'
let start = dayjs(p.time).startOf(groupStart)
if (acc.length && acc[acc.length - 1][0].isSame(start)) {
acc[acc.length - 1][1].push(p.state)
Expand Down