Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit

Permalink
Show update status in the app.
Browse files Browse the repository at this point in the history
  • Loading branch information
ibz committed Feb 21, 2024
1 parent f8bf42f commit 709978a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
17 changes: 13 additions & 4 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@

@api_blueprint.route('/api/status', methods=['GET'])
def status():
github_releases = requests.get(f"https://api.github.com/repos/{app.config['GITHUB_OWNER']}/{app.config['GITHUB_REPO']}/releases").json()
last_release = max(github_releases, key=lambda r: r['tag_name'])
if bool(int(request.args.get('check_last_release', 0))):
github_releases = requests.get(f"https://api.github.com/repos/{app.config['GITHUB_OWNER']}/{app.config['GITHUB_REPO']}/releases").json()
last_release = max(github_releases, key=lambda r: r['tag_name'])
else:
# don't hit GitHub's API every time we hit /status if we are just waiting for an update to happen!
last_release = None

return jsonify({'running': True,
'release_version': app.config['RELEASE_VERSION'],
'last_release_version': last_release['tag_name'] if last_release else "",
'github_repo_url': f"https://github.com/{app.config['GITHUB_OWNER']}/{app.config['GITHUB_REPO']}"})
'last_release_version': last_release['tag_name'] if last_release else None,
'github_repo_url': f"https://github.com/{app.config['GITHUB_OWNER']}/{app.config['GITHUB_REPO']}",
'update_requested': os.path.isfile(app.config['UPDATE_REQUESTED_FILE']),
'update_running': os.path.isfile(app.config['UPDATE_RUNNING_FILE']),
'update_success': os.path.isfile(app.config['UPDATE_SUCCESS_FILE']),
'update_failed': os.path.isfile(app.config['UPDATE_FAILED_FILE'])})

@api_blueprint.route('/api/update', methods=['PUT'])
@user_required
Expand Down
3 changes: 3 additions & 0 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
GITHUB_REPO = 'plebeian-market'

UPDATE_REQUESTED_FILE = "/state/UPDATE_REQUESTED"
UPDATE_RUNNING_FILE = "/state/UPDATE_RUNNING"
UPDATE_SUCCESS_FILE = "/state/UPDATE_SUCCESS"
UPDATE_FAILED_FILE = "/state/UPDATE_FAILED"

SQLALCHEMY_TRACK_MODIFICATIONS = False
DEBUG = bool(int(os.environ.get("DEBUG", 0)))
Expand Down
63 changes: 53 additions & 10 deletions web/backoffice/src/lib/components/settings/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@
import { page } from "$app/stores";
import { ErrorHandler, getStatus, putUpdate } from "$lib/services/api";
import { Info, token } from "$sharedLib/stores";
import InfoBox from "$lib/components/notifications/InfoBox.svelte";
import ErrorBox from "$lib/components/notifications/ErrorBox.svelte";
export let onSave: () => void = () => {};
let version: string | null = null;
let lastVersion: string | null = null;
let githubRepoUrl: string | null = null;
let updateRequested: boolean | null = null;
let updateRunning: boolean | null = null;
let updateSuccess: boolean | null = null;
let updateFailed: boolean | null = null;
let inRequest = false;
function update() {
inRequest = true;
putUpdate($token,
() => {
Info.set("Update requested!");
inRequest = false;
checkStatus(false);
},
new ErrorHandler(true, () => inRequest = false));
}
let updateButtonActive = version !== lastVersion && !inRequest;
let updateButtonActive = lastVersion !== null && version !== lastVersion && !inRequest && !updateRequested && !updateRunning;
function checkStatus(checkLastRelease: boolean) {
getStatus(checkLastRelease,
(v, lv, g, ureq, urunning, usuccess, ufailed) => {
version = v;
lastVersion = lv;
githubRepoUrl = g;
updateRequested = ureq;
updateRunning = urunning;
updateSuccess = usuccess;
updateFailed = ufailed;
if (updateRequested || updateRunning) {
setTimeout(() => { checkStatus(false); }, 1000);
}
});
}
onMount(async () => {
getStatus((v, lv, g) => { version = v; lastVersion = lv; githubRepoUrl = g; });
checkStatus(true);
});
</script>

Expand All @@ -39,17 +64,35 @@
<h2 class="text-2xl">App</h2>
{/if}

{#if version !== null && lastVersion !== null}
{#if version !== null && version !== ""}
<div class="items-center justify-center mt-8">
<p class="text-2xl">You are currently running<br /><strong>Plebeian Market {version}</strong>.</p>
{#if version === lastVersion}
<p class="text-xl mt-4">This is the most recent version. All is well!</p>
{:else}
<p class="text-2xl mt-4">The last available version is <strong>{lastVersion}</strong>.</p>
{#if githubRepoUrl !== null}
<p class="text-xl mt-4">See what is new <a class="link" target="_blank" href="{githubRepoUrl}/releases/">here</a>!</p>
<p class="text-2xl mb-4">You are currently running<br /><strong>Plebeian Market {version}</strong>.</p>
{#if lastVersion !== null}
{#if version === lastVersion}
<p class="text-xl my-4">This is the most recent version. All is well!</p>
{:else}
<p class="text-2xl my-4">The last available version is <strong>{lastVersion}</strong>.</p>
{#if githubRepoUrl !== null}
<p class="text-xl my-4">See what is new <a class="link" target="_blank" href="{githubRepoUrl}/releases/">here</a>!</p>
{/if}
{/if}
{/if}
{#if updateRequested}
<p class="text-2xl font-bold mt-4">You requested an update. Please wait...</p>
{/if}
{#if updateRunning}
<p class="text-2xl font-bold mt-4">Update running! Please wait...</p>
{/if}
{#if updateSuccess}
<InfoBox>
Update successful!
</InfoBox>
{/if}
{#if updateFailed}
<ErrorBox>
Update failed!
</ErrorBox>
{/if}
</div>
<div class="flex justify-center items-center mt-8 h-15">
<button id="update-pm" class="btn btn-primary" class:btn-disabled={!updateButtonActive} on:click|preventDefault={update}>Update</button>
Expand Down
6 changes: 3 additions & 3 deletions web/backoffice/src/lib/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ async function fetchAPIAsync(path, method, tokenValue, body, contentType) {
return await fetch(`${API_BASE}${path}`, getFetchOptions(method, tokenValue, body, contentType));
}

export function getStatus(successCB: (releaseVersion: string, lastReleaseVersion: string, githubRepoUrl: string) => void, errorHandler = new ErrorHandler()) {
fetchAPI("/status", 'GET', null, null, null,
export function getStatus(checkLastRelease: boolean, successCB: (releaseVersion: string, lastReleaseVersion: string | null, githubRepoUrl: string, updateRequested: boolean, updateRunning: boolean, updateSuccess: boolean, updateFailed: boolean) => void, errorHandler = new ErrorHandler()) {
fetchAPI("/status" + (`?check_last_release=${+checkLastRelease}`), 'GET', null, null, null,
response => {
if (response.status === 200) {
response.json().then(data => {
successCB(data['release_version'], data['last_release_version'], data['github_repo_url']);
successCB(data['release_version'], data['last_release_version'], data['github_repo_url'], data['update_requested'], data['update_running'], data['update_success'], data['update_failed']);
});
} else {
errorHandler.handle(response);
Expand Down

0 comments on commit 709978a

Please sign in to comment.