More days, safer caching
This commit is contained in:
parent
b00617768b
commit
6bdf83a971
2 changed files with 157 additions and 86 deletions
|
@ -92,12 +92,132 @@
|
||||||
// Reload data every hour (3600000 milliseconds)
|
// Reload data every hour (3600000 milliseconds)
|
||||||
setInterval(loadAllData, 3600000);
|
setInterval(loadAllData, 3600000);
|
||||||
};
|
};
|
||||||
|
async function createTableRows() {
|
||||||
|
const tbody = document.getElementById('prices-table-body');
|
||||||
|
tbody.innerHTML = '';
|
||||||
|
const today = new Date();
|
||||||
|
const tomorrow = new Date(today);
|
||||||
|
tomorrow.setDate(today.getDate() + 1);
|
||||||
|
const tomorrowString = formatDate(tomorrow);
|
||||||
|
// Check if tomorrow's data is available
|
||||||
|
let tomorrowAvailable = false;
|
||||||
|
try {
|
||||||
|
const resp = await fetch('/price/day/' + tomorrowString + '?num_cheapest_hours=8');
|
||||||
|
const data = await resp.json();
|
||||||
|
if (!data.detail || data.detail !== "prices not found") {
|
||||||
|
tomorrowAvailable = true;
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
let rowCount = tomorrowAvailable ? 15 : 14;
|
||||||
|
for (let offset = 0; offset < rowCount; offset++) {
|
||||||
|
let date;
|
||||||
|
let label;
|
||||||
|
if (tomorrowAvailable && offset === 0) {
|
||||||
|
date = tomorrow;
|
||||||
|
label = `Zítra (${tomorrowString})`;
|
||||||
|
} else {
|
||||||
|
date = new Date(today);
|
||||||
|
date.setDate(today.getDate() - (tomorrowAvailable ? offset - 1 : offset));
|
||||||
|
const dateString = formatDate(date);
|
||||||
|
label = offset === (tomorrowAvailable ? 1 : 0) ? `Dnes (${dateString})` : dateString;
|
||||||
|
}
|
||||||
|
const dateString = formatDate(date);
|
||||||
|
const rowId = `row-${dateString}`;
|
||||||
|
const tr = document.createElement('tr');
|
||||||
|
tr.id = rowId;
|
||||||
|
// Date cell
|
||||||
|
const dateCell = document.createElement('td');
|
||||||
|
dateCell.className = 'px-1 bg-neutral-200 w-16';
|
||||||
|
dateCell.id = `date-${dateString}`;
|
||||||
|
dateCell.innerText = label;
|
||||||
|
tr.appendChild(dateCell);
|
||||||
|
// Hour cells
|
||||||
|
for (let i = 0; i < 24; i++) {
|
||||||
|
const td = document.createElement('td');
|
||||||
|
td.className = 'px-1';
|
||||||
|
td.id = `${dateString}-${i}`;
|
||||||
|
td.innerText = '-';
|
||||||
|
tr.appendChild(td);
|
||||||
|
}
|
||||||
|
tbody.appendChild(tr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadData(date, isToday) {
|
||||||
|
fetch('/price/day/' + date + '?num_cheapest_hours=8')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.detail == "prices not found") {
|
||||||
|
for (let i = 0; i < 24; i++) {
|
||||||
|
document.getElementById(`${date}-${i}`).innerText = "-";
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let i = 0; i < 24; i++) {
|
||||||
|
let extra_class = "";
|
||||||
|
let border = "border-dotted";
|
||||||
|
if (isToday && i == new Date().getHours()) {
|
||||||
|
extra_class = " font-bold text-xl";
|
||||||
|
border = "border-solid";
|
||||||
|
}
|
||||||
|
let value = Math.round(data.total.hours[i] * 100) / 100;
|
||||||
|
let td = document.getElementById(`${date}-${i}`);
|
||||||
|
if (data.cheapest_hours_by_average.hours.includes(i)) {
|
||||||
|
td.className = `px-1 ${border} border-4 border-lime-400 ${getBgColor(value)}${extra_class}`;
|
||||||
|
} else if (data.most_expensive_hours_by_average.hours.includes(i)) {
|
||||||
|
td.className = `px-1 ${border} border-4 border-rose-400 ${getBgColor(value)}${extra_class}`;
|
||||||
|
} else {
|
||||||
|
if (extra_class != "") {
|
||||||
|
extra_class = " border-4 border-solid border-gray-500";
|
||||||
|
}
|
||||||
|
td.className = `px-1 ${getBgColor(value)}${extra_class}`;
|
||||||
|
}
|
||||||
|
td.innerText = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadAllData() {
|
||||||
|
await createTableRows();
|
||||||
|
const today = new Date();
|
||||||
|
const tomorrow = new Date(today);
|
||||||
|
tomorrow.setDate(today.getDate() + 1);
|
||||||
|
const tomorrowString = formatDate(tomorrow);
|
||||||
|
// Check if tomorrow's data is available
|
||||||
|
let tomorrowAvailable = false;
|
||||||
|
try {
|
||||||
|
const resp = await fetch('/price/day/' + tomorrowString + '?num_cheapest_hours=8');
|
||||||
|
const data = await resp.json();
|
||||||
|
if (!data.detail || data.detail !== "prices not found") {
|
||||||
|
tomorrowAvailable = true;
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
let rowCount = tomorrowAvailable ? 15 : 14;
|
||||||
|
for (let offset = 0; offset < rowCount; offset++) {
|
||||||
|
let date;
|
||||||
|
if (tomorrowAvailable && offset === 0) {
|
||||||
|
date = tomorrow;
|
||||||
|
} else {
|
||||||
|
date = new Date(today);
|
||||||
|
date.setDate(today.getDate() - (tomorrowAvailable ? offset - 1 : offset));
|
||||||
|
}
|
||||||
|
const dateString = formatDate(date);
|
||||||
|
loadData(dateString, (tomorrowAvailable ? offset === 1 : offset === 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.onload = function() {
|
||||||
|
loadAllData();
|
||||||
|
setInterval(loadAllData, 3600000);
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-neutral-800">
|
<body class="bg-neutral-800">
|
||||||
<table class="table border-collapse border border-slate-500 text-center w-full">
|
<table class="table border-collapse border border-slate-500 text-center w-full" id="prices-table">
|
||||||
|
<thead>
|
||||||
<tr class="bg-neutral-400">
|
<tr class="bg-neutral-400">
|
||||||
<th class="px-1"></th>
|
<th class="px-1">Datum</th>
|
||||||
<th class="px-1">00</th>
|
<th class="px-1">00</th>
|
||||||
<th class="px-1">01</th>
|
<th class="px-1">01</th>
|
||||||
<th class="px-1">02</th>
|
<th class="px-1">02</th>
|
||||||
|
@ -123,60 +243,9 @@
|
||||||
<th class="px-1">22</th>
|
<th class="px-1">22</th>
|
||||||
<th class="px-1">23</th>
|
<th class="px-1">23</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
</thead>
|
||||||
<td id="today" class="px-1 bg-neutral-200 w-16">Dnes</td>
|
<tbody id="prices-table-body">
|
||||||
<td id="today0" class="px-1">-</td>
|
</tbody>
|
||||||
<td id="today1" class="px-1">-</td>
|
|
||||||
<td id="today2" class="px-1">-</td>
|
|
||||||
<td id="today3" class="px-1">-</td>
|
|
||||||
<td id="today4" class="px-1">-</td>
|
|
||||||
<td id="today5" class="px-1">-</td>
|
|
||||||
<td id="today6" class="px-1">-</td>
|
|
||||||
<td id="today7" class="px-1">-</td>
|
|
||||||
<td id="today8" class="px-1">-</td>
|
|
||||||
<td id="today9" class="px-1">-</td>
|
|
||||||
<td id="today10" class="px-1">-</td>
|
|
||||||
<td id="today11" class="px-1">-</td>
|
|
||||||
<td id="today12" class="px-1">-</td>
|
|
||||||
<td id="today13" class="px-1">-</td>
|
|
||||||
<td id="today14" class="px-1">-</td>
|
|
||||||
<td id="today15" class="px-1">-</td>
|
|
||||||
<td id="today16" class="px-1">-</td>
|
|
||||||
<td id="today17" class="px-1">-</td>
|
|
||||||
<td id="today18" class="px-1">-</td>
|
|
||||||
<td id="today19" class="px-1">-</td>
|
|
||||||
<td id="today20" class="px-1">-</td>
|
|
||||||
<td id="today21" class="px-1">-</td>
|
|
||||||
<td id="today22" class="px-1">-</td>
|
|
||||||
<td id="today23" class="px-1">-</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td id="tomorrow" class="px-1 bg-neutral-200 w-16">Zítra</td>
|
|
||||||
<td id="tomorrow0" class="px-1">-</td>
|
|
||||||
<td id="tomorrow1" class="px-1">-</td>
|
|
||||||
<td id="tomorrow2" class="px-1">-</td>
|
|
||||||
<td id="tomorrow3" class="px-1">-</td>
|
|
||||||
<td id="tomorrow4" class="px-1">-</td>
|
|
||||||
<td id="tomorrow5" class="px-1">-</td>
|
|
||||||
<td id="tomorrow6" class="px-1">-</td>
|
|
||||||
<td id="tomorrow7" class="px-1">-</td>
|
|
||||||
<td id="tomorrow8" class="px-1">-</td>
|
|
||||||
<td id="tomorrow9" class="px-1">-</td>
|
|
||||||
<td id="tomorrow10" class="px-1">-</td>
|
|
||||||
<td id="tomorrow11" class="px-1">-</td>
|
|
||||||
<td id="tomorrow12" class="px-1">-</td>
|
|
||||||
<td id="tomorrow13" class="px-1">-</td>
|
|
||||||
<td id="tomorrow14" class="px-1">-</td>
|
|
||||||
<td id="tomorrow15" class="px-1">-</td>
|
|
||||||
<td id="tomorrow16" class="px-1">-</td>
|
|
||||||
<td id="tomorrow17" class="px-1">-</td>
|
|
||||||
<td id="tomorrow18" class="px-1">-</td>
|
|
||||||
<td id="tomorrow19" class="px-1">-</td>
|
|
||||||
<td id="tomorrow20" class="px-1">-</td>
|
|
||||||
<td id="tomorrow21" class="px-1">-</td>
|
|
||||||
<td id="tomorrow22" class="px-1">-</td>
|
|
||||||
<td id="tomorrow23" class="px-1">-</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -33,15 +33,17 @@ def get_energy_prices(d: datetime.date=datetime.date.today(), no_cache:bool=Fals
|
||||||
|
|
||||||
if not hours or no_cache:
|
if not hours or no_cache:
|
||||||
r = requests.get(url_energy.format(date_str))
|
r = requests.get(url_energy.format(date_str))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for raw in r.json()["data"]["dataLine"][1]["point"]:
|
for raw in r.json()["data"]["dataLine"][1]["point"]:
|
||||||
hours[str(int(raw["x"])-1)] = raw["y"]
|
hours[str(int(raw["x"])-1)] = raw["y"]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise PriceNotFound()
|
raise PriceNotFound()
|
||||||
|
|
||||||
|
# Only cache if all 24 hours are present
|
||||||
|
if len(hours) == 24:
|
||||||
with open(cache_file, "w") as f:
|
with open(cache_file, "w") as f:
|
||||||
f.write(json.dumps(hours))
|
f.write(json.dumps(hours))
|
||||||
|
# If incomplete, do not cache, but return what is available
|
||||||
|
|
||||||
return hours
|
return hours
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue