Skip to content

Commit

Permalink
fix issue where getting dayKeys and todayKey should be based on last …
Browse files Browse the repository at this point in the history
…datapoint date
  • Loading branch information
chienleng committed Sep 22, 2023
1 parent ef9d7c6 commit 60a5d16
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
10 changes: 7 additions & 3 deletions components/Charts/TimeOfDay/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import { CHART_CURVE_SMOOTH, CHART_CURVE_STEP } from '@/constants/chart-options.
import DateDisplay from '@/services/DateDisplay.js'
import DayLines from './DayLines.vue'
import DaySparkLines from './DaySparkLines.vue'
import { getDataBucket, getTimeLabel } from '@/data/transform/time-of-day.js'
import { getDataBucket, getTimeLabel, getDay } from '@/data/transform/time-of-day.js'
import AverageStackedArea from './AverageStackedArea.vue'
export default {
Expand Down Expand Up @@ -136,7 +136,6 @@ export default {
filterPeriod: 'filterPeriod',
hiddenFuelTechs: 'hiddenFuelTechs',
todayKey: 'timeOfDay/todayKey',
selectedToDs: 'timeOfDay/selectedToDs',
chartPowerCurrentUnit: 'chartOptionsPowerEnergy/chartPowerCurrentUnit',
Expand Down Expand Up @@ -164,6 +163,11 @@ export default {
return 1
},
todayKey() {
const lastDate = this.currentDataset[this.currentDataset.length - 1].date
return getDay(lastDate)
},
allDomains() {
const price = []
if (!this.currentDomainPowerEnergy) return []
Expand Down Expand Up @@ -226,7 +230,7 @@ export default {
id: key,
label: getLabel(key)
}
})
})
return datasetKeys
},
Expand Down
22 changes: 11 additions & 11 deletions data/transform/time-of-day.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ export function getDay(d) {
return utcFormat(`%e %b`)(d)
}

function getDayKeys(range) {
export function getDayKeys(range, jsDate) {
const keys = []
let utcCurrent = new Date()
utcCurrent.setUTCDate(utcCurrent.getDate());
let utcCurrent = new Date(jsDate.getTime())
utcCurrent.setUTCHours(0, 0, 0, 0)

for (let i = 0; i < range; i++) {
Expand All @@ -32,9 +31,8 @@ export function getTimeLabel(d) {
return `${hour}${min}${ampm}`
}

function getTimebucket(interval) {
let utcCurrent = new Date()
utcCurrent.setUTCDate(utcCurrent.getDate());
function getTimebucket(interval, jsDate) {
let utcCurrent = new Date(jsDate.getTime())
utcCurrent.setUTCHours(0, 0, 0, 0)
const b = []

Expand Down Expand Up @@ -80,16 +78,17 @@ export function getDataBucket({ data, domain, demandDomain, isPrice, category, p
}
})

// TODO: maybe create dayKeys using dataset start/last time instead
const dayKeys = getDayKeys(range)
const timeBucket = getTimebucket(interval)
const lastPoint = dataset[dataset.length - 1]
const dayKeys = getDayKeys(range, lastPoint.date)
const timeBucket = getTimebucket(interval, lastPoint.date)

dataset.forEach(d => {
const date = d.date
const day = getDay(date)
const findDay = dayKeys.find(k => k === day)
const x = getTimeLabel(date)
const find = timeBucket.find(b => b.x === x)
if (find) {
if (find && findDay) {
find[day] = d.value
}
})
Expand All @@ -105,13 +104,14 @@ export function getDataBucket({ data, domain, demandDomain, isPrice, category, p
b._average = total / keyCount
})


// console.log('domain', domain, data)
// console.log('dataCountWithValues', dataCountWithValues)
// console.log('dataValueSum', dataValueSum)
// console.log('average', dataValueSum / dataCountWithValues)
// console.log('========')

// console.log('average', getAverage({ data, domain, isPrice, demandDomain, category }))

return {
data: timeBucket,
average: getAverage({ data, domain, isPrice, demandDomain, category })
Expand Down
9 changes: 0 additions & 9 deletions store/timeOfDay.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { getDay } from '@/data/transform/time-of-day.js'

export const state = () => ({
todayKey: function() {
const utcCurrent = new Date()
utcCurrent.setUTCDate(utcCurrent.getDate());
utcCurrent.setUTCHours(0, 0, 0, 0)
return getDay(utcCurrent)
}(),
selectedToDs: []
})

export const getters = {
todayKey: (state) => state.todayKey,
selectedToDs: (state) => state.selectedToDs
}

Expand Down

0 comments on commit 60a5d16

Please sign in to comment.