fix site id
This commit is contained in:
@@ -10,11 +10,10 @@ import {
|
||||
useEconomicsDaily,
|
||||
useEconomicsIntervals,
|
||||
} from '../hooks/useEconomics'
|
||||
import { useSiteStatus } from '../hooks/useSiteStatus'
|
||||
import { pragueCalendarDay } from '../lib/pragueDate'
|
||||
import type { DailyEconomics } from '../types/economics'
|
||||
|
||||
const SITE_ID = 1
|
||||
|
||||
function currentMonth(): string {
|
||||
const today = pragueCalendarDay()
|
||||
return today.slice(0, 7)
|
||||
@@ -204,11 +203,14 @@ function DailyRow({
|
||||
}
|
||||
|
||||
export default function Economics() {
|
||||
const { site: siteRow, ready: siteReady, error: siteError } = useSiteStatus()
|
||||
const siteId = siteRow?.site_id ?? null
|
||||
|
||||
const [month, setMonth] = useState(currentMonth)
|
||||
const [expandedDay, setExpandedDay] = useState<string | null>(null)
|
||||
|
||||
const { days, hasGreenBonus, loading, error, reload } = useEconomicsDaily(SITE_ID, month)
|
||||
const { points } = useEconomicsChart(SITE_ID, month)
|
||||
const { days, hasGreenBonus, loading, error, reload } = useEconomicsDaily(siteId, month)
|
||||
const { points } = useEconomicsChart(siteId, month)
|
||||
|
||||
const summary = useMemo(() => {
|
||||
if (days.length === 0) return null
|
||||
@@ -222,12 +224,13 @@ export default function Economics() {
|
||||
|
||||
const handleLockToggle = useCallback(
|
||||
async (row: DailyEconomics) => {
|
||||
if (siteId == null) return
|
||||
try {
|
||||
if (row.is_locked) {
|
||||
await unlockDay(SITE_ID, row.day)
|
||||
await unlockDay(siteId, row.day)
|
||||
toast.success(`Den ${row.day} odemčen`)
|
||||
} else {
|
||||
await lockDay(SITE_ID, row.day)
|
||||
await lockDay(siteId, row.day)
|
||||
toast.success(`Den ${row.day} zamčen`)
|
||||
}
|
||||
reload()
|
||||
@@ -235,11 +238,24 @@ export default function Economics() {
|
||||
toast.error('Operace se nezdařila')
|
||||
}
|
||||
},
|
||||
[reload],
|
||||
[reload, siteId],
|
||||
)
|
||||
|
||||
return (
|
||||
<main className="mx-auto max-w-7xl space-y-6 px-4 py-6 md:px-8">
|
||||
{siteReady && siteRow && (
|
||||
<p className="text-xs text-slate-500">
|
||||
Lokalita: <span className="text-slate-400">{siteRow.site_code}</span> (id {siteRow.site_id}) — ekonomika vychází z{' '}
|
||||
<code className="rounded bg-slate-800 px-1">audit_interval</code>, ne z raw telemetrie.
|
||||
</p>
|
||||
)}
|
||||
{siteError && (
|
||||
<div className="rounded-lg border border-amber-900/50 bg-amber-950/30 px-3 py-2 text-sm text-amber-200">
|
||||
{siteError} — API ekonomiky potřebuje stejnou lokalitu jako Přehled (PostgREST{' '}
|
||||
<code className="rounded bg-slate-900 px-1">vw_site_status</code>).
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Month selector */}
|
||||
<div className="flex items-center gap-4">
|
||||
<button
|
||||
@@ -300,10 +316,30 @@ export default function Economics() {
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
{loading ? (
|
||||
{!siteReady ? (
|
||||
<div className="py-12 text-center text-sm text-slate-500">Načítání lokality…</div>
|
||||
) : siteId == null ? (
|
||||
<div className="py-12 px-4 text-center text-sm text-slate-500">
|
||||
Není dostupná lokalita z přehledu. Zkontrolujte PostgREST a tabulku{' '}
|
||||
<code className="rounded bg-slate-800 px-1">ems.vw_site_status</code>.
|
||||
</div>
|
||||
) : loading ? (
|
||||
<div className="py-12 text-center text-sm text-slate-500">Načítání…</div>
|
||||
) : days.length === 0 ? (
|
||||
<div className="py-12 text-center text-sm text-slate-500">Žádná data pro tento měsíc</div>
|
||||
<div className="mx-auto max-w-lg py-10 px-4 text-center text-sm text-slate-400">
|
||||
<p className="mb-2 font-medium text-slate-300">Žádná data pro tento měsíc</p>
|
||||
<p className="mb-2">
|
||||
Záložka čte jen to, co je v <code className="rounded bg-slate-800 px-1">ems.audit_interval</code> (15min agregace po
|
||||
jobu audit filler). Telemetrie a ceny samy o sobě tabulku audit nenaplní.
|
||||
</p>
|
||||
<p className="mb-2">Zkuste jiný měsíc (šipky), pokud máte historii jinde než v aktuálním měsíci.</p>
|
||||
<p>
|
||||
Backfill na DB (psql):{' '}
|
||||
<code className="mt-1 block break-all rounded bg-slate-900 p-2 text-left text-xs text-slate-300">
|
||||
SELECT ems.fn_fill_audit_range(<site_id>, '2024-01-01'::timestamptz, now());
|
||||
</code>
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
@@ -328,7 +364,7 @@ export default function Economics() {
|
||||
key={row.day}
|
||||
row={row}
|
||||
hasGreenBonus={hasGreenBonus}
|
||||
siteId={SITE_ID}
|
||||
siteId={siteId}
|
||||
expanded={expandedDay === row.day}
|
||||
onToggle={() => setExpandedDay((prev) => (prev === row.day ? null : row.day))}
|
||||
onLockToggle={() => handleLockToggle(row)}
|
||||
|
||||
Reference in New Issue
Block a user