Auto select day if less than 24h data for new sites.
This commit is contained in:
+2
-1
@@ -197,7 +197,8 @@ Because it is a real page, fetch-navigation handles it like any other internal
|
||||
link: clicking the 📊 pen (or any link to `/_a`) fetches the server-rendered
|
||||
HTML, swaps the dynamic regions and mounts the Vue analytics app in place. The
|
||||
range selector updates the URL hash (`#week` etc.) so links to a specific
|
||||
range can be shared.
|
||||
range can be shared. When the URL has no hash, the server defaults to `day`
|
||||
if analytics history spans less than 24 hours, otherwise `week`.
|
||||
|
||||
`AnalyticsView.vue` is no longer a full-screen overlay; the `body.analytics-open`
|
||||
page-chrome hiding and `#/analytics/<range>` hash routing have been removed.
|
||||
|
||||
@@ -9,7 +9,9 @@ let app = null
|
||||
export function mount(container) {
|
||||
if (app) return
|
||||
app = createApp(AnalyticsView, {
|
||||
initialRange: location.hash.slice(1) || 'week',
|
||||
initialRange: location.hash.slice(1)
|
||||
|| container.dataset.initialRange
|
||||
|| 'week',
|
||||
})
|
||||
app.mount(container)
|
||||
}
|
||||
|
||||
+19
-2
@@ -22,7 +22,7 @@ import shutil
|
||||
import socket
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from email.utils import format_datetime
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
@@ -711,6 +711,17 @@ class AnalyticsPing(BaseModel):
|
||||
read: int = 0
|
||||
|
||||
|
||||
def _analytics_initial_range() -> str:
|
||||
"""Default analytics range: day when history is shorter than 24 h."""
|
||||
visits = analytics_store.data.visits
|
||||
if not visits:
|
||||
return "week"
|
||||
earliest = min(v.start for v in visits)
|
||||
if datetime.now(UTC) - earliest < timedelta(hours=24):
|
||||
return "day"
|
||||
return "week"
|
||||
|
||||
|
||||
@app.get("/_a", response_model=None)
|
||||
async def analytics_page(request: Request) -> HTMLResponse:
|
||||
"""Render the analytics viewer as a normal site page at /_a.
|
||||
@@ -721,7 +732,13 @@ async def analytics_page(request: Request) -> HTMLResponse:
|
||||
"""
|
||||
return HTMLResponse(
|
||||
views.render_analytics(
|
||||
data.menu, data.brand, data.custom_css, data.theme, data.favicon, data.brand_html
|
||||
data.menu,
|
||||
data.brand,
|
||||
data.custom_css,
|
||||
data.theme,
|
||||
data.favicon,
|
||||
data.brand_html,
|
||||
initial_range=_analytics_initial_range(),
|
||||
),
|
||||
headers={"cache-control": "no-cache"},
|
||||
)
|
||||
|
||||
+2
-1
@@ -681,6 +681,7 @@ def render_analytics(
|
||||
theme: str = "",
|
||||
favicon: str = "",
|
||||
brand_html: str = "",
|
||||
initial_range: str = "week",
|
||||
) -> str:
|
||||
"""Render the analytics viewer as a normal page at /_a."""
|
||||
page_scripts, page_stylesheets = _page_assets()
|
||||
@@ -689,7 +690,7 @@ def render_analytics(
|
||||
stylesheets = page_stylesheets + analytics_stylesheets
|
||||
doc = E.article
|
||||
with doc:
|
||||
doc.div(id="analytics-app")
|
||||
doc.div(id="analytics-app", **{"data-initial-range": initial_range})
|
||||
return str(
|
||||
_layout(
|
||||
scripts,
|
||||
|
||||
Reference in New Issue
Block a user