Skip to content

Commit

Permalink
Implement sorting of events via config file (#81)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
podliashanyk and stveit authored Jun 12, 2024
1 parent 3ac00e7 commit 311a477
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ venv.bak/
pip-selfcheck.json

# End of https://www.gitignore.io/api/venv,django,pycharm+all

### Tests ###
# Reports
reports
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ The default timezone for timestamps is ``UTC``. Timezone information can be chan
the ``[howitz]``-section or setting the environment variable ``HOWITZ_TIMEZONE`` to ``LOCAL``. Timezone values other
than ``LOCAL`` and ``UTC`` provided in config will be ignored and fall back to ``UTC``.

Howitz uses caching. You can configure preferred caching type under the ``[flask]``-section.
See `Flask-Caching's configuration docs <https://flask-caching.readthedocs.io/en/latest/#configuring-flask-caching>`_ for available configuration options.
Default cache type is `SimpleCache <https://flask-caching.readthedocs.io/en/latest/#simplecache>`_.


Configuring order in which events are sorted
--------------------------------------------

Sorting method can be changed under the ``[howitz]``-section by adding::

sort_by = "<valid sorting method>"

Valid sorting methods are:

* *raw* - Unchanged order in which Zino server sends events (by ID ascending).
* *lasttrans* - Newest transaction first, all IGNORED at the bottom. Default sorting in curitz.

* *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.
* *down-rev* - Shortest/none downtime first. Identical to an existing method in Ritz TK.
* *down* - Longest downtime first. Identical to an existing method in Ritz TK.
* *upd-rev* - Events with the most recent update date first. Identical to an existing method in Ritz TK.
* *upd* - Events with the oldest update date first. Identical to an existing method in Ritz TK.
* *age-rev* - Oldest events first. Identical to an existing method in Ritz TK.
* *age* - Newest events first. Identical to an existing method in Ritz TK.


Example config-file (for development)
-------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions howitz.min.toml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[flask]
SECRET_KEY =
DEBUG = true
CACHE_TYPE = 'FileSystemCache'
CACHE_DIR = './.howitz_cache'

[howitz]
storage = "./howitz.sqlite3"
Expand Down
3 changes: 3 additions & 0 deletions howitz.toml.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[flask]
SECRET_KEY =
DEBUG = true
CACHE_TYPE = 'FileSystemCache'
CACHE_DIR = './.howitz_cache'

[howitz]
storage = "./howitz.sqlite3"
devmode = true
refresh_interval = 10
timezone='LOCAL'
sort_by="lasttrans"

[zino.connections.default]
server =
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ requires-python = ">=3.8"
dynamic = ["version"]
dependencies = [
"flask",
"flask-caching",
"flask_assets",
"flask-login",
"jinja2",
"zinolib>=1.1.1",
"zinolib>=1.2.0",
"werkzeug<3", # needed by flask-login
"pydantic",
]
Expand Down
7 changes: 6 additions & 1 deletion requirements-frozen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ annotated-types==0.6.0
# via pydantic
blinker==1.7.0
# via flask
cachelib==0.9.0
# via flask-caching
click==8.1.7
# via flask
flask==2.3.3
# via
# flask-assets
# flask-caching
# flask-login
# howitz (pyproject.toml)
flask-assets==2.1.0
# via howitz (pyproject.toml)
flask-caching==2.3.0
# via howitz (pyproject.toml)
flask-login==0.6.3
# via howitz (pyproject.toml)
itsdangerous==2.1.2
Expand Down Expand Up @@ -49,5 +54,5 @@ werkzeug==2.3.8
# flask
# flask-login
# howitz (pyproject.toml)
zinolib==1.1.1
zinolib==1.2.0
# via howitz (pyproject.toml)
12 changes: 12 additions & 0 deletions src/howitz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


from logging.config import dictConfig
from flask_caching import Cache

from flask import Flask, g, redirect, url_for, current_app
from flask.logging import default_handler
Expand Down Expand Up @@ -101,6 +102,17 @@ def unauthorized():
assets.register("css", css)
css.build()

cache_type = app.config.get("CACHE_TYPE")
if not cache_type:
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
cache.init_app(app)
app.logger.warn('Caching config not found, setting up simple caching')
else:
cache = Cache(app)
app.logger.debug('Cache type -> %s', cache_type)
app.cache = cache


# import endpoints/urls
from . import endpoints
app.register_blueprint(endpoints.main)
Expand Down
3 changes: 3 additions & 0 deletions src/howitz/config/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

DEFAULT_STORAGE = "./howitz.sqlite3"
DEFAULT_TIMEZONE = 'UTC'
7 changes: 4 additions & 3 deletions src/howitz/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from pydantic import BaseModel
from pydantic.networks import IPvAnyAddress


DEFAULT_STORAGE = "./howitz.sqlite3"
DEFAULT_TIMEZONE = 'UTC'
from howitz.config.defaults import DEFAULT_TIMEZONE, DEFAULT_STORAGE
from howitz.endpoints import EventSort


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


class DevHowitzConfig(DevServerConfig, DevStorageConfig):
devmode: bool = Literal[True]
refresh_interval: int = 5
timezone: str = DEFAULT_TIMEZONE
sort_by: str = EventSort.DEFAULT
Loading

0 comments on commit 311a477

Please sign in to comment.