Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site records clean-up #453

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions api/system/info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import datetime
from typing import Any
from urllib.parse import urlparse

Expand Down Expand Up @@ -154,15 +155,20 @@ async def get_user_sites(

# Get all sites the user is registered to or the current site
query = """
SELECT id, data FROM sites
SELECT id, last_seen, data FROM sites
WHERE id = $1 OR data->'users' ? $2
"""

async for row in Postgres.iterate(query, query_id, user_name):
site = SiteInfo(id=row["id"], **row["data"])
data = row["data"]
data.pop("last_seen", None)
site = SiteInfo(id=row["id"], last_seen=row["last_seen"], **data)
if current_site and site.id == current_site.id:
# record matches the current site
current_site_exists = True
now = datetime.datetime.now()
now = now.replace(tzinfo=datetime.timezone.utc)

if user_name not in site.users:
current_site.users.update(site.users)
current_needs_update = True
Expand All @@ -173,6 +179,10 @@ async def get_user_sites(
current_needs_update = True
elif site.version != current_site.version:
current_needs_update = True
elif (not site.last_seen) or (
site.last_seen < now - datetime.timedelta(days=1)
):
current_needs_update = True
# do not add the current site to the list,
# we'll insert it at the beginning at the end of the loop
continue
Expand All @@ -184,12 +194,15 @@ async def get_user_sites(
if current_needs_update or not current_site_exists:
logging.debug(f"Registering to site {current_site.id}", user=user_name)
mdata = current_site.dict()
mdata.pop("last_seen", None)
mid = mdata.pop("id")
await Postgres.execute(
"""
INSERT INTO sites (id, data)
VALUES ($1, $2) ON CONFLICT (id)
DO UPDATE SET data = EXCLUDED.data
DO UPDATE SET
data = EXCLUDED.data,
last_seen = now()
""",
mid,
mdata,
Expand Down Expand Up @@ -237,13 +250,15 @@ async def get_additional_info(user: UserEntity, request: Request):

current_site: SiteInfo | None = None
with contextlib.suppress(ValidationError):
print("Headers", request.headers)
current_site = SiteInfo(
id=request.headers.get("x-ayon-site-id"),
platform=request.headers.get("x-ayon-platform"),
hostname=request.headers.get("x-ayon-hostname"),
version=request.headers.get("x-ayon-version"),
users=[user.name],
)
print("Got site info", current_site)

sites = await get_user_sites(user.name, current_site)

Expand Down
7 changes: 5 additions & 2 deletions api/system/sites.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from fastapi import Query

from ayon_server.api.dependencies import CurrentUser
Expand All @@ -13,6 +15,7 @@ class SiteInfo(OPModel):
hostname: str = Field(..., title="Machine hostname")
version: str = Field(..., title="Ayon version")
users: set[str] = Field(..., title="List of users")
last_seen: datetime | None = Field(None, title="Last used timestamp")


@router.get("/system/sites", tags=["System"])
Expand All @@ -24,9 +27,9 @@ async def get_sites(
"""Get list of sites"""

result: list[SiteInfo] = []
query = "SELECT id, data FROM sites"
query = "SELECT id, data, last_seen FROM sites"
async for row in Postgres.iterate(query):
site = SiteInfo(id=row["id"], **row["data"])
site = SiteInfo(id=row["id"], last_seen=row["last_seen"], **row["data"])

if platform and site.platform != platform:
continue
Expand Down
16 changes: 16 additions & 0 deletions schemas/migrations/00000005.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
----------------
-- AYON 1.6.x --
----------------

DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_name = 'sites'
AND column_name = 'last_seen'
) THEN
ALTER TABLE IF EXISTS sites
ADD COLUMN last_seen TIMESTAMPTZ DEFAULT NOW();
END IF;
END $$;
1 change: 1 addition & 0 deletions schemas/schema.public.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS bundle_staging_idx ON bundles(is_staging) WHER

CREATE TABLE IF NOT EXISTS public.sites(
id VARCHAR NOT NULL PRIMARY KEY,
last_seen TIMESTAMPTZ NOT NULL DEFAULT NOW(),
data JSONB NOT NULL DEFAULT '{}'::JSONB
);

Expand Down