Merge PR #779: Fix calculation of samples for months and years

This commit is contained in:
Jenkins nymea 2022-03-15 13:53:00 +01:00
commit b8729f6f5e
3 changed files with 17 additions and 2 deletions

View File

@ -141,7 +141,7 @@ StatsBase {
var consumer = consumers.get(j)
graphEntry[consumer.id] = 0
}
labelTime = new Date(newestLogTimestamp.getTime() - config.sampleRate * i * 60000)
labelTime = calculateSampleStart(newestLogTimestamp, config.sampleRate, i)
}
// print("Adding entry:", labelTime, config.toLabel(labelTime), JSON.stringify(graphEntry))

View File

@ -143,7 +143,7 @@ StatsBase {
}
labelTime = entry.timestamp
} else {
labelTime = new Date(newestLogTimestamp.getTime() - config.sampleRate * i * 60000)
labelTime = calculateSampleStart(newestLogTimestamp, config.sampleRate, i)
}
// print("Adding entry:", labelTime, graphEntry.consumption, config.toLabel(labelTime))

View File

@ -56,6 +56,21 @@ Item {
}
})
function calculateSampleStart(sampleEnd, sampleRate, sampleCount) {
if (sampleCount === undefined) {
sampleCount = 1
}
var sampleStart = new Date(sampleEnd)
if (sampleRate === EnergyLogs.SampleRate1Month) {
sampleStart.setMonth(sampleEnd.getMonth() - sampleCount)
} else if (sampleRate === EnergyLogs.SampleRate1Year) {
sampleStart.setFullYear(sampleEnd.getFullYear() - sampleCount)
} else {
sampleStart.setTime(sampleEnd.getTime() - (sampleRate * 60000 * sampleCount))
}
return sampleStart
}
function minutesStart() {
var d = new Date();
d.setMinutes(d.getMinutes() - minutesCount + 1, 0, 0)