Skip to content

Commit 311a477

Browse files
podliashanykstveit
andauthored
Implement sorting of events via config file (#81)
* Raw sorting implementation Bad code and not all sorting results are equivalent to sorting results in nowadays Zino TK * Finish sorting implementation All sorting methods are implemented and work as intended * Add sort_by to config Sorting method is configured as sort_by (str) under [howitz] section. * Save configured sorting method in session * Add basic documentation about implemented sorting methods * Remove unecessary underscores double underscore in _value_ holds some meaning for Enum, but the other ones seem unecessary * Add sorting tests * Move config consts to a separate file In order to avoid circular imports * Stop implicit None return when sorting * Fix docs * Rename enum * Fix errors in tests * Run tests with app context Otherwise, the logger.debug statement in sorted_events() fails with RuntimeError * Document get_priority function * Add reports folder to gitignore Contains xml files with coverage reports and results * Add tests for get_priority helper * Specify param type in get_priority * Use event's own method for determining whether it is down * Simplify get_priority helper * Add more tests for sort_events helper * Bump zinolib to 1.2.0 * Add flask-caching dependency * Configure SimpleCache for the app * Use flask cache to re-sort event list if there are relevant ntie changes * Rename sort_by default method to raw in config * Polish if statements * Split elifs from sort_events() into separate functions * Make caching configurable * Move cache config to flask section --------- Co-authored-by: Simon Oliver Tveit <[email protected]>
1 parent 3ac00e7 commit 311a477

File tree

11 files changed

+556
-27
lines changed

11 files changed

+556
-27
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ venv.bak/
7777
pip-selfcheck.json
7878

7979
# End of https://www.gitignore.io/api/venv,django,pycharm+all
80+
81+
### Tests ###
82+
# Reports
83+
reports

README.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,31 @@ The default timezone for timestamps is ``UTC``. Timezone information can be chan
257257
the ``[howitz]``-section or setting the environment variable ``HOWITZ_TIMEZONE`` to ``LOCAL``. Timezone values other
258258
than ``LOCAL`` and ``UTC`` provided in config will be ignored and fall back to ``UTC``.
259259

260+
Howitz uses caching. You can configure preferred caching type under the ``[flask]``-section.
261+
See `Flask-Caching's configuration docs <https://flask-caching.readthedocs.io/en/latest/#configuring-flask-caching>`_ for available configuration options.
262+
Default cache type is `SimpleCache <https://flask-caching.readthedocs.io/en/latest/#simplecache>`_.
263+
264+
265+
Configuring order in which events are sorted
266+
--------------------------------------------
267+
268+
Sorting method can be changed under the ``[howitz]``-section by adding::
269+
270+
sort_by = "<valid sorting method>"
271+
272+
Valid sorting methods are:
273+
274+
* *raw* - Unchanged order in which Zino server sends events (by ID ascending).
275+
* *lasttrans* - Newest transaction first, all IGNORED at the bottom. Default sorting in curitz.
276+
277+
* *severity* - Events of same color grouped together. The most severe (red) at the top and ignored at the bottom. Existing method in Ritz TK, but it is called 'default' there.
278+
* *down-rev* - Shortest/none downtime first. Identical to an existing method in Ritz TK.
279+
* *down* - Longest downtime first. Identical to an existing method in Ritz TK.
280+
* *upd-rev* - Events with the most recent update date first. Identical to an existing method in Ritz TK.
281+
* *upd* - Events with the oldest update date first. Identical to an existing method in Ritz TK.
282+
* *age-rev* - Oldest events first. Identical to an existing method in Ritz TK.
283+
* *age* - Newest events first. Identical to an existing method in Ritz TK.
284+
260285

261286
Example config-file (for development)
262287
-------------------------------------

howitz.min.toml.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[flask]
22
SECRET_KEY =
33
DEBUG = true
4+
CACHE_TYPE = 'FileSystemCache'
5+
CACHE_DIR = './.howitz_cache'
46

57
[howitz]
68
storage = "./howitz.sqlite3"

howitz.toml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
[flask]
22
SECRET_KEY =
33
DEBUG = true
4+
CACHE_TYPE = 'FileSystemCache'
5+
CACHE_DIR = './.howitz_cache'
46

57
[howitz]
68
storage = "./howitz.sqlite3"
79
devmode = true
810
refresh_interval = 10
911
timezone='LOCAL'
12+
sort_by="lasttrans"
1013

1114
[zino.connections.default]
1215
server =

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ requires-python = ">=3.8"
1717
dynamic = ["version"]
1818
dependencies = [
1919
"flask",
20+
"flask-caching",
2021
"flask_assets",
2122
"flask-login",
2223
"jinja2",
23-
"zinolib>=1.1.1",
24+
"zinolib>=1.2.0",
2425
"werkzeug<3", # needed by flask-login
2526
"pydantic",
2627
]

requirements-frozen.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ annotated-types==0.6.0
88
# via pydantic
99
blinker==1.7.0
1010
# via flask
11+
cachelib==0.9.0
12+
# via flask-caching
1113
click==8.1.7
1214
# via flask
1315
flask==2.3.3
1416
# via
1517
# flask-assets
18+
# flask-caching
1619
# flask-login
1720
# howitz (pyproject.toml)
1821
flask-assets==2.1.0
1922
# via howitz (pyproject.toml)
23+
flask-caching==2.3.0
24+
# via howitz (pyproject.toml)
2025
flask-login==0.6.3
2126
# via howitz (pyproject.toml)
2227
itsdangerous==2.1.2
@@ -49,5 +54,5 @@ werkzeug==2.3.8
4954
# flask
5055
# flask-login
5156
# howitz (pyproject.toml)
52-
zinolib==1.1.1
57+
zinolib==1.2.0
5358
# via howitz (pyproject.toml)

src/howitz/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from logging.config import dictConfig
5+
from flask_caching import Cache
56

67
from flask import Flask, g, redirect, url_for, current_app
78
from flask.logging import default_handler
@@ -101,6 +102,17 @@ def unauthorized():
101102
assets.register("css", css)
102103
css.build()
103104

105+
cache_type = app.config.get("CACHE_TYPE")
106+
if not cache_type:
107+
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
108+
cache.init_app(app)
109+
app.logger.warn('Caching config not found, setting up simple caching')
110+
else:
111+
cache = Cache(app)
112+
app.logger.debug('Cache type -> %s', cache_type)
113+
app.cache = cache
114+
115+
104116
# import endpoints/urls
105117
from . import endpoints
106118
app.register_blueprint(endpoints.main)

src/howitz/config/defaults.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
DEFAULT_STORAGE = "./howitz.sqlite3"
3+
DEFAULT_TIMEZONE = 'UTC'

src/howitz/config/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
from pydantic import BaseModel
44
from pydantic.networks import IPvAnyAddress
55

6-
7-
DEFAULT_STORAGE = "./howitz.sqlite3"
8-
DEFAULT_TIMEZONE = 'UTC'
6+
from howitz.config.defaults import DEFAULT_TIMEZONE, DEFAULT_STORAGE
7+
from howitz.endpoints import EventSort
98

109

1110
class ServerConfig(BaseModel):
@@ -30,9 +29,11 @@ class HowitzConfig(ServerConfig, StorageConfig):
3029
devmode: bool = Literal[False]
3130
refresh_interval: int = 5
3231
timezone: str = DEFAULT_TIMEZONE
32+
sort_by: str = EventSort.DEFAULT
3333

3434

3535
class DevHowitzConfig(DevServerConfig, DevStorageConfig):
3636
devmode: bool = Literal[True]
3737
refresh_interval: int = 5
3838
timezone: str = DEFAULT_TIMEZONE
39+
sort_by: str = EventSort.DEFAULT

0 commit comments

Comments
 (0)