Skip to content

Commit

Permalink
[38] Update server uptime live within the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinHeist committed Aug 5, 2024
1 parent 349bc1d commit f86fe6a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
11 changes: 8 additions & 3 deletions app/models/preferences.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from logging import Logger
from os import environ
from pathlib import Path
Expand Down Expand Up @@ -56,7 +57,8 @@ class Preferences:
__read_only = (
'is_docker', 'file', 'asset_directory', 'card_type_directory',
'remote_card_types', 'local_card_types', 'invalid_connections',
'currently_running_sync',
'currently_running_sync', 'current_db_schema', 'current_version',
'server_boot_time',
)

__slots__ = (
Expand All @@ -77,7 +79,8 @@ class Preferences:
'imported_blueprints', 'colorblind_mode', 'library_unique_cards',
'invalid_connections', 'home_page_table_view', 'reduced_animations',
'currently_running_sync', 'interactive_card_previews',
'delete_unsynced_series', 'imagemagick_executable',
'delete_unsynced_series', 'imagemagick_executable', 'current_db_schema',
'server_boot_time',
)


Expand All @@ -97,8 +100,9 @@ def __init__(self, file: Path) -> None:
self.file = file
self.file.parent.mkdir(exist_ok=True)

# Pars file
# Parse file
self.parse_file(self.read_file())
self.server_boot_time = datetime.now()

# Initialize paths
self.asset_directory: Path = Path(self.asset_directory)
Expand Down Expand Up @@ -288,6 +292,7 @@ def parse_file(self, obj: object) -> None:
# Set attributes not parsed from the object
self.current_version = Version(self.VERSION_FILE.read_text().strip())
self.available_version: Optional[Version] = None
self.current_db_schema: Optional[str] = None

# Write object to file
self.commit()
Expand Down
5 changes: 3 additions & 2 deletions app/templates/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,10 @@ function populateBlueprintCard(card, blueprint, blueprintId) {
/**
* Get a time-difference string from now to `previousRun`.
* @param {Date} previousRun Previous date to format the text relative from.
* @param {?boolean} addAgo When to add "ago" to the end of the text.
* @returns String describing how along ago `previousRun` was.
*/
function timeDiffString(previousRun) {
function timeDiffString(previousRun, addAgo=true) {
const previous = new Date(previousRun);

// Get current time
Expand All @@ -595,7 +596,7 @@ function timeDiffString(previousRun) {
if (diffSeconds % 60 > 1) { timeUnits.push(`${diffSeconds%60} seconds`); }
else if (diffSeconds % 60 > 0) { timeUnits.push(`<${diffSeconds%60} second`); }

return timeUnits.slice(0, 2).join(', ') + ' ago';
return timeUnits.slice(0, 2).join(', ') + (addAgo ? ' ago' : '');
}

/**
Expand Down
15 changes: 15 additions & 0 deletions app/templates/js/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {Date} When the server was booted */
const bootTime = new Date('{{ preferences.server_boot_time }}');

/**
* Update the uptime text on the page.
*/
function updateUptimeText() {
const diff = timeDiffString(bootTime, false);
document.querySelector('[data-value="uptime"]').innerText = diff;
}

function initAll() {
updateUptimeText();
setInterval(updateUptimeText, 1000);
}
14 changes: 2 additions & 12 deletions app/templates/system.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ <h1 class="ui header">System Details</h1>
<div class="item">
<div class="content">
<div class="header">Database Schema</div>
<div class="description">{{ preferences.current_db_schema }} test</div>
<div class="description">{{ preferences.current_db_schema }}</div>
</div>
</div>

<div class="item">
<div class="content">
<div class="header">Uptime</div>
<div class="description">{{ preferences.server_uptime }} test</div>
<div class="description" data-value="uptime">{{ preferences.server_boot_time }}</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -103,18 +103,8 @@ <h2 class="ui header">Links</h2>
</div>
</div>
</div>
<div class="ui list">
<a class="item" href="https://titlecardmaker.com/" target="_blank">Website</a>
<a class="item">Who is our user?</a>
<a class="item">Where is our office located?</a>
</div>

</div>
</div>

<script>

</script>
</body>

</html>
30 changes: 27 additions & 3 deletions modules/YamlReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ def _is_specified(self, *attributes: str) -> bool:
return True


def _parse_card_type(self, card_type: str, /) -> Optional[BaseCardType]:
@staticmethod
def parse_card_type(card_type: str, /) -> Optional[BaseCardType]:
"""
Read the card_type specification for this object. This first
looks at the locally implemented types in the TitleCard class,
then attempts to create a RemoteCardType from the specification.
This can be either a local file to inject, or a GitHub-hosted
remote file to download and inject. This updates this object's
`valid` attribute (if invalid).
remote file to download and inject.
Args:
card_type: The value of card_type to read/parse.
Expand All @@ -155,10 +155,34 @@ def _parse_card_type(self, card_type: str, /) -> Optional[BaseCardType]:
# If known card type, use class from hard-coded dict
if card_type in TitleCard.CARD_TYPES:
return TitleCard.CARD_TYPES[card_type]

# Try as RemoteCardtype
if (remote_card_type := RemoteCardType(card_type)).valid:
return remote_card_type.card_class

return None


def _parse_card_type(self, card_type: str, /) -> Optional[BaseCardType]:
"""
Read the card_type specification for this object. This first
looks at the locally implemented types in the TitleCard class,
then attempts to create a RemoteCardType from the specification.
This can be either a local file to inject, or a GitHub-hosted
remote file to download and inject. This updates this object's
`valid` attribute (if invalid).
Args:
card_type: The value of card_type to read/parse.
Returns:
Subclass of `BaseCardType` which is indicated by the given
card type identifier string.
"""

if (ct := YamlReader.parse_card_type(card_type)):
return ct

log.error(f'Invalid card type "{card_type}"')
self.valid = False
return None
Expand Down
2 changes: 1 addition & 1 deletion modules/ref/version_webui
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0-alpha.11.0-webui37
v2.0-alpha.11.0-webui38

0 comments on commit f86fe6a

Please sign in to comment.