Skip to content

Commit

Permalink
Merge pull request #12 from lucianavlop/expensive-period-straddling-m…
Browse files Browse the repository at this point in the history
…idnight

Expensive period straddling midnight
  • Loading branch information
daithihearn authored Jul 23, 2023
2 parents d62dad9 + 3b5161e commit 8bf1f28
Show file tree
Hide file tree
Showing 8 changed files with 2,023 additions and 2,030 deletions.
47 changes: 24 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "daithi-dashboard",
"version": "2.1.1",
"version": "2.1.2",
"private": true,
"dependencies": {
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@mui/icons-material": "^5.11.16",
"@mui/material": "^5.12.2",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.24",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.1",
"axios": "^1.3.6",
"chart.js": "^4.2.1",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.14.1",
"@mui/material": "^5.14.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.4",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"axios": "^1.4.0",
"chart.js": "^4.3.0",
"chartjs-adapter-moment": "^1.0.1",
"chartjs-plugin-annotation": "^2.2.1",
"date-fns": "^2.29.3",
"prettier": "^2.8.8",
"chartjs-plugin-annotation": "^3.0.1",
"date-fns": "^2.30.0",
"prettier": "^3.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
"typescript": "^5.1.6",
"web-vitals": "^3.4.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down Expand Up @@ -58,9 +58,10 @@
}
},
"devDependencies": {
"@babel/preset-env": "^7.21.5",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.5",
"babel-jest": "^29.5.0"
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.22.9",
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
"babel-jest": "^29.6.1"
}
}
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"sizes": "512x512"
}
],
"version": "2.1.1",
"version": "2.1.2",
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
Expand Down
61 changes: 20 additions & 41 deletions src/components/PriceChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useMemo, useRef, useState } from "react"
import { getMostExpensivePeriod, getTwoCheapestPeriods } from "utils/PriceUtils"
import { filterAndPadPrices, getMostExpensivePeriod, getTwoCheapestPeriods } from "utils/PriceUtils"
import { format } from "date-fns"
import { Chart, ChartData, ChartOptions } from "chart.js/auto"
import Annotation, { LineAnnotationOptions } from "chartjs-plugin-annotation"
Expand Down Expand Up @@ -28,37 +28,6 @@ export interface DailyChartProps {
showExpensivePeriod: boolean
}

const filterAndPadPrices = (
prices: Price[],
cp: Price[],
now = new Date(),
): (number | null)[] => {
if (cp.length < 1) return []

// If the period has passed return null array
const endOfPeriod = new Date(cp[cp.length - 1].dateTime)
endOfPeriod.setMinutes(59)

if (endOfPeriod.getTime() < now.getTime()) {
return []
}

return prices.map(p => {
const priceHour = new Date(p.dateTime).getHours()
// If the dateTime of item is contained in cp, return the price, else return null
if (
cp.find(cpItem => {
const cpHour = new Date(cpItem.dateTime).getHours()
return cpHour === priceHour || cpHour + 1 === priceHour
})
) {
return p.price
} else {
return null
}
})
}

const DailyChart: React.FC<DailyChartProps> = ({
prices,
median,
Expand Down Expand Up @@ -106,7 +75,6 @@ const DailyChart: React.FC<DailyChartProps> = ({
}, [prices, showCurrentPrice])

const chartOptions = useMemo(() => {
// const currentTime = format(new Date(), dateFormat)
const chartOptions: ChartOptions = {
plugins: {
annotation: {
Expand Down Expand Up @@ -199,26 +167,37 @@ const DailyChart: React.FC<DailyChartProps> = ({
const cp = getTwoCheapestPeriods(prices, 3)

return [
filterAndPadPrices(prices, cp[0]),
filterAndPadPrices(prices, cp[1]),
filterAndPadPrices(cp[0]),
filterAndPadPrices(cp[1]),
]
}, [prices, showCheapPeriod])

const expensivePeriod = useMemo(() => {
if (!showExpensivePeriod) return []

const ep = getMostExpensivePeriod(prices, 3)
return filterAndPadPrices(prices, ep)
return filterAndPadPrices(ep)
}, [prices, showExpensivePeriod])

const paddedPrices = useMemo(() => {
if (prices.length === 0) return []
const last = prices[prices.length - 1]
const pp = [...prices]
pp.push({
price: last.price,
dateTime: last.dateTime.slice(0, -8) + "24:00:00",
})
return pp
}, [prices])

const averageDataset = useMemo(
() => Array<number>(prices.length).fill(median),
[prices, median],
() => Array<number>(paddedPrices.length).fill(median),
[paddedPrices, median],
)

const chartData: ChartData<"line", (number | null)[]> = useMemo(() => {
return {
labels: prices.map(item =>
labels: paddedPrices.map(item =>
format(new Date(item.dateTime), dateFormat),
),
datasets: [
Expand Down Expand Up @@ -272,7 +251,7 @@ const DailyChart: React.FC<DailyChartProps> = ({
},
{
label: "Precio",
data: prices.map(item => item.price),
data: paddedPrices.map(item => item.price),
borderColor: theme.palette.info.main,
backgroundColor: hexToRGBA(theme.palette.info.main, 0.4),
pointRadius: 0,
Expand All @@ -294,7 +273,7 @@ const DailyChart: React.FC<DailyChartProps> = ({
cheapPeriods,
dateFormat,
expensivePeriod,
prices,
paddedPrices,
theme,
])

Expand Down
5 changes: 0 additions & 5 deletions src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ const DashboardContent: React.FC = () => {
const fetchData = async () => {
const prices = await getPrices(currentDate, currentDate)
if (prices.length === 0) return
const last = prices[prices.length - 1]
prices.push({
price: last.price,
dateTime: last.dateTime.slice(0, -8) + "24:00:00",
})
setPricesToday(prices)
}
fetchData()
Expand Down
122 changes: 122 additions & 0 deletions src/test/data/prices20230723.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[
{
"id": "f714ac292245fd90f08238f5bab0447a",
"dateTime": "2023-07-23T00:00:00",
"price": 0.14366
},
{
"id": "bab5c42b619ad51a2d2f290b9dea00e3",
"dateTime": "2023-07-23T01:00:00",
"price": 0.14076
},
{
"id": "729f2951499d258c0fb3656ee14aa710",
"dateTime": "2023-07-23T02:00:00",
"price": 0.13388
},
{
"id": "78d60b56a16ce93ee9c2a046d2ff51f1",
"dateTime": "2023-07-23T03:00:00",
"price": 0.13332
},
{
"id": "cbf9d7983f41f1fb3486ffbce4dc45bb",
"dateTime": "2023-07-23T04:00:00",
"price": 0.13083
},
{
"id": "1d82a0d39766bfe17be486d96213979a",
"dateTime": "2023-07-23T05:00:00",
"price": 0.13181
},
{
"id": "bfe6a9836f6d9f42ed91af2d38e97b44",
"dateTime": "2023-07-23T06:00:00",
"price": 0.13063
},
{
"id": "595510a2ccf71fc1fceac9559ac1aada",
"dateTime": "2023-07-23T07:00:00",
"price": 0.12559
},
{
"id": "67153ba8fd0dc492247078dafe589cb8",
"dateTime": "2023-07-23T08:00:00",
"price": 0.11813
},
{
"id": "0ab3a7e366b55c2ad1fb26021b41c9cb",
"dateTime": "2023-07-23T09:00:00",
"price": 0.11104
},
{
"id": "ad8a29620b29c66457b3e7ea1d41c00e",
"dateTime": "2023-07-23T10:00:00",
"price": 0.07005
},
{
"id": "8557944f8016c5ecf0156f10b1ed8108",
"dateTime": "2023-07-23T11:00:00",
"price": 0.048100000000000004
},
{
"id": "6c92e956d66fc47910712b6abeb80ab8",
"dateTime": "2023-07-23T12:00:00",
"price": 0.028149999999999998
},
{
"id": "f5a46e31a66496a8f9712e196236765f",
"dateTime": "2023-07-23T13:00:00",
"price": 0.02595
},
{
"id": "6e63f30a9d79d93748377b8df4d0b859",
"dateTime": "2023-07-23T14:00:00",
"price": 0.02596
},
{
"id": "8a736cc832061667ddf163429079a9d5",
"dateTime": "2023-07-23T15:00:00",
"price": 0.02557
},
{
"id": "a72f7ba2452a55bb9d6d3ddc734f19df",
"dateTime": "2023-07-23T16:00:00",
"price": 0.02553
},
{
"id": "48d82ee926fe06aea2d818f53c073966",
"dateTime": "2023-07-23T17:00:00",
"price": 0.031149999999999997
},
{
"id": "435308da76222703875052248c44e477",
"dateTime": "2023-07-23T18:00:00",
"price": 0.05197
},
{
"id": "88d3c0e3dcad32862e931a8b397a1de3",
"dateTime": "2023-07-23T19:00:00",
"price": 0.10915000000000001
},
{
"id": "6c8d7f1741b837d195f0661c96f6819e",
"dateTime": "2023-07-23T20:00:00",
"price": 0.12919
},
{
"id": "b14a7bf97c418986c5af55ad79b157f1",
"dateTime": "2023-07-23T21:00:00",
"price": 0.14206
},
{
"id": "b2bccb7fb13ae214973eb4909e353c1f",
"dateTime": "2023-07-23T22:00:00",
"price": 0.157
},
{
"id": "83d26efb860b6bff36b06481189983f9",
"dateTime": "2023-07-23T23:00:00",
"price": 0.15965000000000001
}
]
32 changes: 32 additions & 0 deletions src/utils/PriceUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import {
getCheapestPeriod,
getTwoCheapestPeriods,
getMostExpensivePeriod,
filterAndPadPrices,
} from "./PriceUtils"
import prices20230512 from "test/data/prices20230512.json"
import prices20230513 from "test/data/prices20230513.json"
import prices20230723 from "test/data/prices20230723.json"

describe("getCheapestPeriod", () => {
test("should return empty array if prices is empty", () => {
Expand Down Expand Up @@ -180,4 +182,34 @@ describe("getMostExpensivePeriod", () => {
},
])
})

test("Expensive period straddles midnight", () => {
expect(getMostExpensivePeriod(prices20230723, 3)).toEqual([
{
"id": "b14a7bf97c418986c5af55ad79b157f1",
"dateTime": "2023-07-23T21:00:00",
"price": 0.14206
},
{
"id": "b2bccb7fb13ae214973eb4909e353c1f",
"dateTime": "2023-07-23T22:00:00",
"price": 0.157
},
{
"id": "83d26efb860b6bff36b06481189983f9",
"dateTime": "2023-07-23T23:00:00",
"price": 0.15965000000000001
}])
})
})

describe("filterAndPadPrices", () => {
test("should return empty array if prices is empty", () => {
expect(filterAndPadPrices([])).toEqual([])
})

test("If the period has passed return null array", () => {
expect(filterAndPadPrices(prices20230513)).toEqual([])
})

})
Loading

0 comments on commit 8bf1f28

Please sign in to comment.