Files
headscale-webui/helper.py
Marek Pikuła fe7a3667d4 Complete the major refactor
Major part of #73

Unfortunately, it wasn't possible to split it to multiple smaller
commits, since the changes touched the entire application substantially.
Here is a short list of major changes:

1. Create a separate library (headscale-api), which is used as a
   convenient abstraction layer providing Pythonic interface with
   Pydantic. Headscale API is fully asynchronous library, benefitting
   from improved concurrency for backend requests thus increasing page
   load speed, e.g., on "Machines" page.
2. Create a common common, validated with flask-pydantic API passthrough
   layer from GUI to the backend.
3. Move authentication to a separate (auth.py), consolidating the
   functionality in a single place (with better place for expansion in
   the future).
4. Move configuration management to a separate module (config.py). Use
   Pydantic's BaseSettings for reading values from environment, with
   extensive validation and error reporting.
5. Reduce the number of health checks.
    - Now, most are performed during server initialization. If any test
      fails, the server is started in tainted mode, with only the error
      page exposed (thus reducing the surface of attack in invalid
      state).
    - Key checks are implicit in the requests to the backend and
      guarded by `@headscale.key_check_guard` decorator.
    - Key renewal is moved to server-side scheduler.
6. Introduce type hints to the level satisfactory for mypy static
   analysis. Also, enable some other linters in CI and add optional
   pre-commit hooks.
7. Properly handle some error states. Instead of returning success and
   handling different responses, if something fails, there is HTTP error
   code and standard response for it.
8. General formatting, small rewrites for clarity and more idiomatic
   Python constructs.

Signed-off-by: Marek Pikuła <marek.pikula@embevity.com>
2023-04-21 06:02:29 +00:00

153 lines
5.5 KiB
Python

"""Helper functions used for formatting."""
from datetime import timedelta
from enum import StrEnum
from typing import Literal
def pretty_print_duration(
duration: timedelta, delta_type: Literal["expiry", ""] = ""
): # pylint: disable=too-many-return-statements
"""Print a duration in human-readable format."""
days, seconds = duration.days, duration.seconds
hours = days * 24 + seconds // 3600
mins = (seconds % 3600) // 60
secs = seconds % 60
if delta_type == "expiry":
if days > 730:
return "in more than two years"
if days > 365:
return "in more than a year"
if days > 0:
return f"in {days} days" if days > 1 else f"in {days} day"
if hours > 0:
return f"in {hours} hours" if hours > 1 else f"in {hours} hour"
if mins > 0:
return f"in {mins} minutes" if mins > 1 else f"in {mins} minute"
return f"in {secs} seconds" if secs >= 1 or secs == 0 else f"in {secs} second"
if days > 730:
return "over two years ago"
if days > 365:
return "over a year ago"
if days > 0:
return f"{days} days ago" if days > 1 else f"{days} day ago"
if hours > 0:
return f"{hours} hours ago" if hours > 1 else f"{hours} hour ago"
if mins > 0:
return f"{mins} minutes ago" if mins > 1 else f"{mins} minute ago"
return f"{secs} seconds ago" if secs >= 1 or secs == 0 else f"{secs} second ago"
def text_color_duration(
duration: timedelta,
): # pylint: disable=too-many-return-statements
"""Print a color based on duration (imported as seconds)."""
days, seconds = duration.days, duration.seconds
hours = days * 24 + seconds // 3600
mins = (seconds % 3600) // 60
secs = seconds % 60
if days > 30:
return "grey-text "
if days > 14:
return "red-text text-darken-2 "
if days > 5:
return "deep-orange-text text-lighten-1"
if days > 1:
return "deep-orange-text text-lighten-1"
if hours > 12:
return "orange-text "
if hours > 1:
return "orange-text text-lighten-2"
if hours == 1:
return "yellow-text "
if mins > 15:
return "yellow-text text-lighten-2"
if mins > 5:
return "green-text text-lighten-3"
if secs > 30:
return "green-text text-lighten-2"
return "green-text "
def get_color(import_id: int, item_type: Literal["failover", "text", ""] = ""):
"""Get color for users/namespaces."""
# Define the colors... Seems like a good number to start with
match item_type:
case "failover":
colors = [
"teal lighten-1",
"blue lighten-1",
"blue-grey lighten-1",
"indigo lighten-2",
"brown lighten-1",
"grey lighten-1",
"indigo lighten-2",
"deep-orange lighten-1",
"yellow lighten-2",
"purple lighten-2",
]
case "text":
colors = [
"red-text text-lighten-1",
"teal-text text-lighten-1",
"blue-text text-lighten-1",
"blue-grey-text text-lighten-1",
"indigo-text text-lighten-2",
"green-text text-lighten-1",
"deep-orange-text text-lighten-1",
"yellow-text text-lighten-2",
"purple-text text-lighten-2",
"indigo-text text-lighten-2",
"brown-text text-lighten-1",
"grey-text text-lighten-1",
]
case _:
colors = [
"red lighten-1",
"teal lighten-1",
"blue lighten-1",
"blue-grey lighten-1",
"indigo lighten-2",
"green lighten-1",
"deep-orange lighten-1",
"yellow lighten-2",
"purple lighten-2",
"indigo lighten-2",
"brown lighten-1",
"grey lighten-1",
]
return colors[import_id % len(colors)]
class MessageErrorType(StrEnum):
"""Error type for `format_message()."""
WARNING = "warning"
SUCCESS = "success"
ERROR = "error"
INFORMATION = "information"
def format_message(error_type: MessageErrorType, title: str, message: str):
"""Render a "collection" as error/warning/info message."""
content = '<ul class="collection"><li class="collection-item avatar">'
match error_type:
case MessageErrorType.WARNING:
icon = '<i class="material-icons circle yellow">priority_high</i>'
title = f'<span class="title">Warning - {title}</span>'
case MessageErrorType.SUCCESS:
icon = '<i class="material-icons circle green">check</i>'
title = f'<span class="title">Success - {title}</span>'
case MessageErrorType.ERROR:
icon = '<i class="material-icons circle red">warning</i>'
title = f'<span class="title">Error - {title}</span>'
case MessageErrorType.INFORMATION:
icon = '<i class="material-icons circle grey">help</i>'
title = f'<span class="title">Information - {title}</span>'
content += icon + title + message + "</li></ul>"
return content