Skip to content

Commit

Permalink
Merge 841a566 into 588be2b
Browse files Browse the repository at this point in the history
  • Loading branch information
crnjan authored Jan 17, 2023
2 parents 588be2b + 841a566 commit cf993c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
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

0 comments on commit cf993c8

Please sign in to comment.