Skip to content

Commit b3da75c

Browse files
committed
Days to keep is new configuration key
1 parent 1f4af93 commit b3da75c

File tree

14 files changed

+131
-140
lines changed

14 files changed

+131
-140
lines changed

README.md

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,47 @@
33
![cnb-cache](cnb-cache.svg?raw=true)
44

55

6-
http://localhost/CNB-cache/src/rate.php?currency=eur - todays EUR rate
7-
http://localhost/CNB-cache/src/rate.php?currency=usd&age=1 - yesterday $ rate
6+
Installation
7+
------------
88

99

10-
## Struktura projektu
10+
```shell
11+
sudo apt install lsb-release wget apt-transport-https bzip2
1112

12-
- **src/app.php**: Hlavní logika aplikace, která načítá potřebné knihovny, nastavuje připojení k databázi MultiFlexi, získává kurzovní lístek z API ČNB a ukládá data do JSON souboru.
13-
- **vendor/**: Adresář obsahující závislosti projektu spravované pomocí Composeru.
14-
- **composer.json**: Konfigurační soubor pro Composer, obsahující informace o závislostech a dalších nastaveních.
1513

16-
## Instalace
14+
wget -qO- https://repo.vitexsoftware.com/keyring.gpg | sudo tee /etc/apt/trusted.gpg.d/vitexsoftware.gpg
15+
echo "deb [signed-by=/etc/apt/trusted.gpg.d/vitexsoftware.gpg] https://repo.vitexsoftware.com $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/vitexsoftware.list
16+
sudo apt update
1717

18-
1. Nainstalujte MultiFlexi podle dokumentace na GitHubu: [MultiFlexi GitHub](https://github.com/VitexSoftware/MultiFlexi).
19-
2. Nainstalujte závislosti pomocí Composeru:
20-
```
21-
composer install
22-
```
18+
sudo apt install cnb-cache-DATABASE
19+
```
2320

24-
## Spuštění aplikace
21+
database can be `mysql` or `sqlite`
2522

26-
Ujistěte se, že máte správně nastavené prostředí a databázi. Poté aplikaci spusťte pomocí příkazu:
27-
```
28-
php src/app.php
29-
```
3023

31-
## Použití
24+
Support the apache2 and lighthttpd web servers:
25+
26+
![Web Servers](webservers.png?raw=true)
27+
28+
First Configure the currencies to be cached
29+
30+
![Currency Chooser](currency-chooser.png?raw=true)
31+
32+
Then set the days to keep the cache
33+
34+
![Days to Keep](daystokeep.png?raw=true)
35+
36+
And finally the cache is initialized:
37+
38+
![Initialization](init.png?raw=true)
39+
40+
Final configuration is stored in `/etc/cnb-cache/cnb-cache.env` file
41+
42+
After installation the currencies listing is available on the `/cnb-cache/` path.
43+
44+
45+
http://localhost/cnb-cache/?currency=eur - todays EUR rate
46+
http://localhost/cnb-cache/?currency=USD&age=yesterday - yesterday $ rate
47+
3248

33-
Před spuštěním aplikace upravte proměnné `$datum` a `$mena` v souboru `src/app.php` podle vašich potřeb. Aplikace stáhne kurzovní lístek pro zadané datum a měnu a uloží jej do souboru `kurzovni_listek.json`.
49+
The systemd-crond service is started and the cache is updated every day at 0:01 AM

currency-chooser.png

17.7 KB
Loading

daystokeep.png

4.33 KB
Loading

debian/cnb-cache.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ db_go || true
1313
db_title Choose currencies to cache
1414
db_input high cnb-cache/currencies || true
1515
db_go || true
16+
17+
db_title Number of days to keep cache
18+
db_input high cnb-cache/keepdays || true
19+
db_go || true

debian/cnb-cache.install

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
src/index.php usr/share/cnb-cache
2+
cnb-cache.svg usr/share/cnb-cache
23
db/migrations/* usr/lib/cnb-cache/db/migrations
34
#db/seeds/* usr/lib/cnb-cache/db/seeds
45
debian/tmp/composer.json usr/lib/cnb-cache

debian/cnb-cache.postinst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,21 @@ configure)
156156
fi
157157
fi
158158

159+
db_get cnb-cache/keepdays || true
160+
keepdays="$RET"
161+
db_go || true
162+
163+
echo "Number of days to keep cache: $keepdays"
164+
165+
# Update or populate KEEP_DAYS key in /etc/cnb-cache/cnb-cache.env with $keepdays
166+
if [ -n "$keepdays" ]; then
167+
if grep -q "^KEEP_DAYS=" /etc/cnb-cache/cnb-cache.env; then
168+
sed -i "s/^KEEP_DAYS=.*/KEEP_DAYS=\"$keepdays\"/" /etc/cnb-cache/cnb-cache.env
169+
else
170+
echo "KEEP_DAYS=\"$keepdays\"" >>/etc/cnb-cache/cnb-cache.env
171+
fi
172+
fi
173+
159174
echo "Initializing cache"
160175

161176

debian/templates

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ Default: USD, EUR
1818
Choices: AUD, BRL, BGN, CNY, DKK, EUR, PHP, HKD, INR, IDR, ISK, ILS, JPY, ZAR, CAD, KRW, HUF, MYR, MXN, XDR, NOK, NZD, PLN, RON, SGD, SEK, CHF, THB, TRY, USD, GBP
1919
Description: Currencies to cache:
2020
Please choose the currencies that should be cached.
21+
22+
Template: cnb-cache/keepdays
23+
Type: string
24+
Default: 3
25+
Description: Number of days to keep cache:
26+
Please enter the number of days to keep cache.

init.png

248 KB
Loading

src/SpojeNet/Cnb/ExchangeRate.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,36 @@
2020
*
2121
* @author Vitex <[email protected]>
2222
*/
23-
class ExchangeRate extends \Ease\SQL\Engine
24-
{
23+
class ExchangeRate extends \Ease\SQL\Engine {
24+
2525
public static string $baseUrl = 'https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt';
26+
private int $keepdays = 3;
2627
private int $httpcode = 0;
2728

2829
/**
2930
* @var array<string, string>
3031
*/
3132
private array $currencies = [];
3233

33-
public function __construct()
34-
{
34+
public function __construct() {
3535
$this->setMyTable('rates');
3636
$this->setObjectName();
3737
$currencies = explode(',', str_replace(' ', '', \Ease\Shared::cfg('CURRENCIES', 'EUR')));
3838
$this->currencies = array_combine($currencies, $currencies);
39+
$this->keepdays = intval(\Ease\Shared::cfg('KEEP_DAYS', 3));
3940
parent::__construct();
4041
}
4142

42-
public function exchangeRateRaw(string $datum): string
43-
{
44-
$url = self::$baseUrl.'?date='.date('d.m.Y', strtotime($datum));
43+
public function getCurrencyList(): array {
44+
return $this->currencies;
45+
}
46+
47+
public function getKeepDays(): int {
48+
return $this->keepdays;
49+
}
50+
51+
public function exchangeRateRaw(string $datum): string {
52+
$url = self::$baseUrl . '?date=' . date('d.m.Y', strtotime($datum));
4553

4654
$ch = curl_init();
4755
curl_setopt($ch, \CURLOPT_URL, $url);
@@ -56,10 +64,9 @@ public function exchangeRateRaw(string $datum): string
5664
/**
5765
* Convert CNB CSV to Array.
5866
*
59-
* @return array
67+
* @return array<string, array<string, string>>
6068
*/
61-
public function cnbCsv2Data(string $ratesRaw)
62-
{
69+
public function cnbCsv2Data(string $ratesRaw) {
6370
$data = explode("\n", $ratesRaw);
6471

6572
unset($data[0], $data[1]);
@@ -85,8 +92,7 @@ public function cnbCsv2Data(string $ratesRaw)
8592
*
8693
* @return bool
8794
*/
88-
public function storeDay(int $dayBack = 0): void
89-
{
95+
public function storeDay(int $dayBack = 0): void {
9096
$datum = self::dateBeforeDays($dayBack);
9197

9298
foreach ($this->cnbCsv2Data($this->exchangeRateRaw($datum)) as $currencyData) {
@@ -102,21 +108,21 @@ public function storeDay(int $dayBack = 0): void
102108
}
103109
}
104110

105-
public function renderDay(): void
106-
{
111+
public function dropOlder(int $days): void {
112+
if($this->dropFromSQL(['date' => ['<' => self::dateBeforeDays($days)]]) > 0){
113+
$this->addStatusMessage(sprintf(_('Dropped rates older than %d days'), $days), 'success');
114+
}
107115
}
108116

109-
public function shiftDays(): void
110-
{
117+
public function shiftDays(): void {
118+
$this->dropOlder($this->getKeepDays());
111119
}
112120

113-
public function getRateInfo($currency, $age): array
114-
{
121+
public function getRateInfo($currency, $age): array {
115122
return $this->getColumnsFromSQL(['*'], ['code' => $currency, 'date' => self::dateBeforeDays($age)]);
116123
}
117124

118-
public static function dateBeforeDays(int $daysBack): string
119-
{
120-
return date('Y-m-d', strtotime('-'.(string) $daysBack.' day'));
125+
public static function dateBeforeDays(int $daysBack): string {
126+
return date('Y-m-d', strtotime('-' . (string) $daysBack . ' day'));
121127
}
122128
}

src/cnb-exchange-rate.php

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)