-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
413 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { computeCosts } from './cost.js'; | ||
|
||
describe('Cost computer', () => { | ||
it('Should take start and end dates in account', () => { | ||
const result = computeCosts( | ||
[ | ||
{ start: '2024-01-15T00:00:00+01:00', state: 1000, sum: 0 }, | ||
{ start: '2024-01-16T00:00:00+01:00', state: 2000, sum: 0 }, | ||
{ start: '2024-01-17T00:00:00+01:00', state: 3000, sum: 0 }, | ||
{ start: '2024-01-18T00:00:00+01:00', state: 4000, sum: 0 }, | ||
{ start: '2024-01-19T00:00:00+01:00', state: 5000, sum: 0 }, | ||
{ start: '2024-01-20T00:00:00+01:00', state: 6000, sum: 0 }, | ||
], | ||
[ | ||
{ price: 0.1, end_date: '2024-01-16' }, | ||
{ price: 1, start_date: '2024-01-17', end_date: '2024-01-19' }, | ||
{ price: 10, start_date: '2024-01-19' }, | ||
], | ||
); | ||
|
||
expect(result).toEqual([ | ||
{ start: '2024-01-15T00:00:00+01:00', state: 0.1, sum: 0.1 }, | ||
{ start: '2024-01-17T00:00:00+01:00', state: 3, sum: 3 + 0.1 }, | ||
{ start: '2024-01-18T00:00:00+01:00', state: 4, sum: 4 + 3 + 0.1 }, | ||
{ start: '2024-01-19T00:00:00+01:00', state: 50, sum: 50 + 4 + 3 + 0.1 }, | ||
{ start: '2024-01-20T00:00:00+01:00', state: 60, sum: 60 + 50 + 4 + 3 + 0.1 }, | ||
]); | ||
}); | ||
|
||
it('Should take weekday in account', () => { | ||
const result = computeCosts( | ||
[ | ||
{ start: '2024-01-01T00:00:00+01:00', state: 1000, sum: 0 }, | ||
{ start: '2024-01-02T00:00:00+01:00', state: 2000, sum: 0 }, | ||
{ start: '2024-01-03T00:00:00+01:00', state: 3000, sum: 0 }, | ||
{ start: '2024-01-04T00:00:00+01:00', state: 4000, sum: 0 }, | ||
{ start: '2024-01-05T00:00:00+01:00', state: 5000, sum: 0 }, | ||
], | ||
[ | ||
{ price: 2, weekday: ['mon', 'sun'] }, | ||
{ price: 10, weekday: ['wed', 'thu'] }, | ||
], | ||
); | ||
|
||
expect(result).toEqual([ | ||
{ start: '2024-01-01T00:00:00+01:00', state: 2, sum: 2 }, | ||
{ start: '2024-01-03T00:00:00+01:00', state: 30, sum: 30 + 2 }, | ||
{ start: '2024-01-04T00:00:00+01:00', state: 40, sum: 40 + 30 + 2 }, | ||
]); | ||
}); | ||
|
||
it('Should take time in account', () => { | ||
const result = computeCosts( | ||
[ | ||
{ start: '2024-01-01T00:00:00+01:00', state: 500, sum: 0 }, | ||
{ start: '2024-01-01T01:00:00+01:00', state: 1000, sum: 0 }, | ||
{ start: '2024-01-01T02:00:00+01:00', state: 2000, sum: 0 }, | ||
{ start: '2024-01-01T03:00:00+01:00', state: 3000, sum: 0 }, | ||
{ start: '2024-01-01T04:00:00+01:00', state: 4000, sum: 0 }, | ||
{ start: '2024-01-01T05:00:00+01:00', state: 5000, sum: 0 }, | ||
], | ||
[ | ||
{ price: 0.1, before: '01:00' }, | ||
{ price: 1, after: '01:00', before: '03:00' }, | ||
{ price: 10, after: '03:00', before: '04:00' }, | ||
{ price: 100, after: '05:00' }, | ||
], | ||
); | ||
|
||
expect(result).toEqual([ | ||
{ start: '2024-01-01T00:00:00+01:00', state: 0.05, sum: 0.05 }, | ||
{ start: '2024-01-01T01:00:00+01:00', state: 1, sum: 1 + 0.05 }, | ||
{ start: '2024-01-01T02:00:00+01:00', state: 2, sum: 2 + 1 + 0.05 }, | ||
{ start: '2024-01-01T03:00:00+01:00', state: 30, sum: 30 + 2 + 1 + 0.05 }, | ||
{ start: '2024-01-01T05:00:00+01:00', state: 500, sum: 500 + 30 + 2 + 1 + 0.05 }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { StatisticDataPoint } from './format.js'; | ||
import { CostConfig } from './config.js'; | ||
import dayjs from 'dayjs'; | ||
import { info } from './log.js'; | ||
|
||
export function computeCosts(energy: StatisticDataPoint[], costConfigs: CostConfig[]): StatisticDataPoint[] { | ||
const result: StatisticDataPoint[] = []; | ||
|
||
for (const point of energy) { | ||
const matchingCostConfig = findMatchingCostConfig(point, costConfigs); | ||
if (matchingCostConfig) { | ||
const cost = Math.round(matchingCostConfig.price * point.state) / 1000; | ||
result.push({ | ||
start: point.start, | ||
state: cost, | ||
sum: Math.round(1000 * ((result.length === 0 ? 0 : result[result.length - 1].sum) + cost)) / 1000, | ||
}); | ||
} | ||
} | ||
|
||
if (result.length > 0) { | ||
const intervalFrom = dayjs(result[0].start).format('DD/MM/YYYY'); | ||
const intervalTo = dayjs(result[result.length - 1].start).format('DD/MM/YYYY'); | ||
info(`Successfully computed the cost of ${result.length} data points from ${intervalFrom} to ${intervalTo}`); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function findMatchingCostConfig(point: StatisticDataPoint, configs: CostConfig[]): CostConfig { | ||
return configs.find((config) => { | ||
if (!config.price || typeof config.price !== 'number') { | ||
return false; | ||
} | ||
const pointStart = dayjs(point.start); | ||
if (config.start_date) { | ||
const configStartDate = dayjs(config.start_date); | ||
if (pointStart.isBefore(configStartDate)) { | ||
return false; | ||
} | ||
} | ||
if (config.end_date) { | ||
const configEndDate = dayjs(config.end_date); | ||
if (pointStart.isAfter(configEndDate) || pointStart.isSame(configEndDate)) { | ||
return false; | ||
} | ||
} | ||
|
||
if (config.weekday && config.weekday.length > 0) { | ||
const weekday = pointStart.format('ddd').toLowerCase(); | ||
if (!(config.weekday as string[]).includes(weekday)) { | ||
return false; | ||
} | ||
} | ||
|
||
if (config.after) { | ||
const afterHour = +config.after.split(':')[0]; | ||
if (isNaN(afterHour) || pointStart.hour() < afterHour) { | ||
return false; | ||
} | ||
} | ||
|
||
if (config.before) { | ||
const beforeHour = +config.before.split(':')[0]; | ||
if (isNaN(beforeHour) || pointStart.hour() >= beforeHour) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
} |
Oops, something went wrong.