-
Notifications
You must be signed in to change notification settings - Fork 0
/
prices.cpp
81 lines (64 loc) · 1.99 KB
/
prices.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "prices.h"
#include "app.h"
#include "args.h"
#include "cache.h"
#include "common.h"
#include "nordpool.h"
#include <QDateTime>
#include <fmt/format.h>
namespace El {
Prices::Prices(App const &app, QObject *parent)
: QObject(parent)
, _app(app)
, _cache(new Cache{app, this})
{}
bool Prices::load(QString const ®ion, QDateTime const &start, QDateTime const &end)
{
auto const start_h = to_hours(start);
auto const end_h = to_hours(end);
// try cached prices first
try {
_prices = _cache->get_prices(region, start_h, end_h);
}
catch (Exception const &ex) {
fmt::print("WARNING: hindade pärimine vahemälust ebaõnnestu: {}\n", ex.what());
}
// check the result
if (!_prices.empty() && !_prices.has_holes() && start_h >= _prices.start_time_h() && end_h <= _prices.end_time_h()) {
if (_app.args().verbose()) {
fmt::print("Kasutan vahemälusse salvestatud hindasid\n");
}
return true;
}
// request missing prices from Nord Pool
auto const missing_blocks = _prices.get_missing_blocks(start_h, end_h);
NordPool np{_app};
for (auto const &period : missing_blocks) {
PriceBlocks p;
try {
p = np.get_prices(region, period.start_h, period.end_h);
}
catch (Exception const &ex) {
fmt::print(stderr, "ERROR: Nord Pool hindade küsimine ebaõnnestus: {}\n", ex.what());
return false;
}
// update cache
try {
_cache->store_prices(region, p);
}
catch (Exception const &ex) {
fmt::print("WARNING: Nord Pool hindade salvestamine vahemälusse ebaõnnestus: {}\n", ex.what());
}
_prices.append(std::move(p));
}
return true;
}
std::optional<double> Prices::get_price(QDateTime const &time) const
{
auto const value = _prices.get_price(to_hours(time));
if (!value) {
return {};
}
return *value / 1000.0;
}
} // namespace El