mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(admin): show the allowance of place search, address search and routing in the TREK API card
GET /api/admin/service-identity lists places, photon and valhalla, each with today's allowance read fresh from /v1/quota of its own host (cached for 20 seconds per host) and whether this instance asks it right now: Photon only beside the public Nominatim, the TREK Valhalla only where the routing choice comes to it. Photon and Valhalla are asked only with a key and only while TREK_SERVICES_HOSTS lists them. The last allowance an answer stated is kept per host for when a host does not answer. The card lists the three services under each other in the look it had, a service this instance does not ask dimmed and named as such.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-ADMIN-SVCID-001 to FE-ADMIN-SVCID-008
|
||||
// FE-ADMIN-SVCID-001 to FE-ADMIN-SVCID-010
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import type { ServiceIdentityStatus } from '@trek/shared';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
@@ -13,11 +13,16 @@ vi.mock('../../i18n', async (importOriginal) => ({
|
||||
useTranslation: () => ({ t: (key: string) => key, locale: 'en-US' }),
|
||||
}));
|
||||
|
||||
const QUOTA = { limit: 10_000, used: 2_500, remaining: 7_500, resetAt: '2026-10-30T00:00:00.000Z' };
|
||||
const CONNECTED: ServiceIdentityStatus = {
|
||||
state: 'connected',
|
||||
tier: 'std',
|
||||
keySource: 'registered',
|
||||
quota: { limit: 10_000, used: 2_500, remaining: 7_500, resetAt: '2026-10-30T00:00:00.000Z' },
|
||||
services: [
|
||||
{ service: 'places', inUse: true, quota: QUOTA },
|
||||
{ service: 'photon', inUse: false, quota: null },
|
||||
{ service: 'valhalla', inUse: false, quota: null },
|
||||
],
|
||||
lastSeenAt: '2026-10-29T12:00:00.000Z',
|
||||
};
|
||||
|
||||
@@ -31,7 +36,7 @@ describe('ServiceIdentityPanel', () => {
|
||||
render(<ServiceIdentityPanel />);
|
||||
expect(await screen.findByText('admin.trekApi.quota.state.connected')).toBeInTheDocument();
|
||||
expect(screen.getByText('admin.trekApi.quota.tier.std')).toBeInTheDocument();
|
||||
const bar = screen.getByRole('progressbar', { name: 'admin.trekApi.quota.title' });
|
||||
const bar = screen.getByRole('progressbar', { name: 'admin.trekApi.quota.service.places' });
|
||||
expect(bar).toHaveAttribute('aria-valuenow', '2500');
|
||||
expect(bar).toHaveAttribute('aria-valuemax', '10000');
|
||||
expect(screen.getByText('2,500 / 10,000')).toBeInTheDocument();
|
||||
@@ -41,7 +46,7 @@ describe('ServiceIdentityPanel', () => {
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-002: an installation without a key says so and offers a new identity', async () => {
|
||||
serve({ state: 'legacy', tier: 'legacy', keySource: null, quota: null, lastSeenAt: null });
|
||||
serve({ state: 'legacy', tier: 'legacy', keySource: null, services: [], lastSeenAt: null });
|
||||
render(<ServiceIdentityPanel />);
|
||||
expect(await screen.findByText('admin.trekApi.quota.hint.legacy')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('progressbar')).toBeNull();
|
||||
@@ -49,7 +54,7 @@ describe('ServiceIdentityPanel', () => {
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-003: switched off, it shows why and offers nothing', async () => {
|
||||
serve({ state: 'off', tier: null, keySource: null, quota: null, lastSeenAt: null });
|
||||
serve({ state: 'off', tier: null, keySource: null, services: [], lastSeenAt: null });
|
||||
render(<ServiceIdentityPanel />);
|
||||
expect(await screen.findByText('admin.trekApi.quota.hint.off')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: 'admin.trekApi.quota.regenerate' })).toBeNull();
|
||||
@@ -62,10 +67,45 @@ describe('ServiceIdentityPanel', () => {
|
||||
expect(screen.queryByRole('button', { name: 'admin.trekApi.quota.regenerate' })).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-005: a connected key without an allowance to show says it cannot be read', async () => {
|
||||
serve({ ...CONNECTED, quota: null });
|
||||
it('FE-ADMIN-SVCID-005: a service in use without an allowance to show says it cannot be read', async () => {
|
||||
serve({ ...CONNECTED, services: [{ service: 'places', inUse: true, quota: null }] });
|
||||
render(<ServiceIdentityPanel />);
|
||||
expect(await screen.findByText('admin.trekApi.quota.unavailable')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('progressbar')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-009: lists place search, address search and routing, each with its own allowance', async () => {
|
||||
serve({
|
||||
...CONNECTED,
|
||||
services: [
|
||||
{ service: 'places', inUse: true, quota: QUOTA },
|
||||
{ service: 'photon', inUse: true, quota: { ...QUOTA, used: 40, remaining: 9_960 } },
|
||||
{ service: 'valhalla', inUse: true, quota: { ...QUOTA, used: 9_500, remaining: 500 } },
|
||||
],
|
||||
});
|
||||
render(<ServiceIdentityPanel />);
|
||||
const list = await screen.findByRole('list', { name: 'admin.trekApi.quota.title' });
|
||||
expect(list.querySelectorAll('li')).toHaveLength(3);
|
||||
const value = (name: string) => screen.getByRole('progressbar', { name }).getAttribute('aria-valuenow');
|
||||
expect(value('admin.trekApi.quota.service.places')).toBe('2500');
|
||||
expect(value('admin.trekApi.quota.service.photon')).toBe('40');
|
||||
expect(value('admin.trekApi.quota.service.valhalla')).toBe('9500');
|
||||
expect(screen.queryByText('admin.trekApi.quota.notInUse')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-010: a service this instance does not ask is named as not in use, its allowance still shown', async () => {
|
||||
serve({
|
||||
...CONNECTED,
|
||||
services: [
|
||||
{ service: 'places', inUse: true, quota: QUOTA },
|
||||
{ service: 'photon', inUse: false, quota: null },
|
||||
{ service: 'valhalla', inUse: false, quota: QUOTA },
|
||||
],
|
||||
});
|
||||
render(<ServiceIdentityPanel />);
|
||||
expect(await screen.findAllByText('admin.trekApi.quota.notInUse')).toHaveLength(2);
|
||||
expect(screen.getAllByRole('progressbar')).toHaveLength(2);
|
||||
expect(screen.queryByText('admin.trekApi.quota.unavailable')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-006: a failed read says so instead of showing nothing', async () => {
|
||||
@@ -76,7 +116,7 @@ describe('ServiceIdentityPanel', () => {
|
||||
|
||||
it('FE-ADMIN-SVCID-007: a revoked key asks before it starts over, then shows the new state', async () => {
|
||||
let regenerated = 0;
|
||||
serve({ state: 'revoked', tier: 'std', keySource: 'registered', quota: null, lastSeenAt: null });
|
||||
serve({ state: 'revoked', tier: 'std', keySource: 'registered', services: [], lastSeenAt: null });
|
||||
server.use(
|
||||
http.post('/api/admin/service-identity/regenerate', () => {
|
||||
regenerated += 1;
|
||||
@@ -84,7 +124,7 @@ describe('ServiceIdentityPanel', () => {
|
||||
state: 'registering',
|
||||
tier: 'legacy',
|
||||
keySource: null,
|
||||
quota: null,
|
||||
services: [],
|
||||
lastSeenAt: null,
|
||||
});
|
||||
})
|
||||
@@ -106,7 +146,7 @@ describe('ServiceIdentityPanel', () => {
|
||||
});
|
||||
|
||||
it('FE-ADMIN-SVCID-008: a managed install leaves the identity to its operator', async () => {
|
||||
serve({ state: 'revoked', tier: 'std', keySource: 'registered', quota: null, lastSeenAt: null });
|
||||
serve({ state: 'revoked', tier: 'std', keySource: 'registered', services: [], lastSeenAt: null });
|
||||
render(<ServiceIdentityPanel managed />);
|
||||
expect(await screen.findByText('admin.trekApi.quota.hint.revoked')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: 'admin.trekApi.quota.regenerate' })).toBeNull();
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import type { ServiceIdentityState, ServiceIdentityStatus, ServiceQuotaView } from '@trek/shared';
|
||||
import type {
|
||||
ServiceAllowance,
|
||||
ServiceIdentityState,
|
||||
ServiceIdentityStatus,
|
||||
ServiceName,
|
||||
ServiceQuotaView,
|
||||
} from '@trek/shared';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
import { useRef, useState } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
@@ -18,6 +24,12 @@ const STATE_DOT: Record<ServiceIdentityState, string> = {
|
||||
revoked: 'bg-danger',
|
||||
};
|
||||
|
||||
const SERVICE_LABEL: Record<ServiceName, string> = {
|
||||
places: 'admin.trekApi.quota.service.places',
|
||||
photon: 'admin.trekApi.quota.service.photon',
|
||||
valhalla: 'admin.trekApi.quota.service.valhalla',
|
||||
};
|
||||
|
||||
/** Below a tenth of the day left the bar turns amber, at nothing left red. */
|
||||
function barTone(quota: ServiceQuotaView): string {
|
||||
if (quota.remaining === 0) return 'bg-danger';
|
||||
@@ -33,7 +45,7 @@ function Stat({ label, value }: { label: string; value: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
function QuotaMeter({ quota }: { quota: ServiceQuotaView }) {
|
||||
function QuotaMeter({ quota, label }: { quota: ServiceQuotaView; label: string }) {
|
||||
const { t, locale } = useTranslation();
|
||||
const is12h = useSettingsStore((s) => s.settings.time_format === '12h');
|
||||
const number = new Intl.NumberFormat(locale);
|
||||
@@ -48,7 +60,7 @@ function QuotaMeter({ quota }: { quota: ServiceQuotaView }) {
|
||||
<div className="flex flex-col gap-2.5">
|
||||
<div
|
||||
role="progressbar"
|
||||
aria-label={t('admin.trekApi.quota.title')}
|
||||
aria-label={label}
|
||||
aria-valuemin={0}
|
||||
aria-valuemax={quota.limit}
|
||||
aria-valuenow={quota.used}
|
||||
@@ -71,15 +83,39 @@ function QuotaMeter({ quota }: { quota: ServiceQuotaView }) {
|
||||
);
|
||||
}
|
||||
|
||||
function rowNote(t: TranslationFn, allowance: ServiceAllowance): string | null {
|
||||
if (!allowance.inUse) return t('admin.trekApi.quota.notInUse');
|
||||
return allowance.quota ? null : t('admin.trekApi.quota.unavailable');
|
||||
}
|
||||
|
||||
/**
|
||||
* One service under the key: its name, today's allowance where its host told
|
||||
* it, and whether this instance asks it at all. A service not in use is
|
||||
* dimmed rather than hidden, so the list reads the same on every install.
|
||||
*/
|
||||
function ServiceRow({ allowance }: { allowance: ServiceAllowance }) {
|
||||
const { t } = useTranslation();
|
||||
const name = t(SERVICE_LABEL[allowance.service]);
|
||||
const note = rowNote(t, allowance);
|
||||
|
||||
return (
|
||||
<li className={`flex flex-col gap-2 py-2.5 first:pt-0 last:pb-0 ${allowance.inUse ? '' : 'opacity-60'}`}>
|
||||
<p className="m-0 text-caption font-medium text-content-secondary">{name}</p>
|
||||
{allowance.quota && <QuotaMeter quota={allowance.quota} label={name} />}
|
||||
{note && <p className="m-0 text-caption leading-snug text-content-muted">{note}</p>}
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
function stateHint(t: TranslationFn, status: ServiceIdentityStatus | null, failed: boolean): string | null {
|
||||
if (!status) return failed ? t('admin.trekApi.quota.unavailable') : null;
|
||||
if (status.state !== 'connected') return t(`admin.trekApi.quota.hint.${status.state}`);
|
||||
return status.quota ? null : t('admin.trekApi.quota.unavailable');
|
||||
return status.state === 'connected' ? null : t(`admin.trekApi.quota.hint.${status.state}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* How this installation stands with the TREK services: whether it has its key,
|
||||
* today's allowance against what is used, and when the count starts again.
|
||||
* and for place search, address search and routing today's allowance against
|
||||
* what is used, and when the count starts again.
|
||||
* Read when the card is in view and refreshed every 30 seconds while it stays.
|
||||
* A managed install leaves the identity to its operator, so it offers no new one.
|
||||
*/
|
||||
@@ -121,7 +157,13 @@ export default function ServiceIdentityPanel({ managed = false }: { managed?: bo
|
||||
)}
|
||||
</div>
|
||||
|
||||
{status?.quota && status.state !== 'off' && <QuotaMeter quota={status.quota} />}
|
||||
{status && status.services.length > 0 && (
|
||||
<ul aria-label={t('admin.trekApi.quota.title')} className="m-0 list-none divide-y divide-edge-faint p-0">
|
||||
{status.services.map((allowance) => (
|
||||
<ServiceRow key={allowance.service} allowance={allowance} />
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{hint && <p className="m-0 text-caption leading-snug text-content-muted">{hint}</p>}
|
||||
|
||||
{status?.keySource === 'env' && (
|
||||
|
||||
@@ -17,7 +17,7 @@ const LEGACY: ServiceIdentityStatus = {
|
||||
state: 'legacy',
|
||||
tier: 'legacy',
|
||||
keySource: null,
|
||||
quota: null,
|
||||
services: [],
|
||||
lastSeenAt: null,
|
||||
};
|
||||
const card = { current: null };
|
||||
|
||||
Reference in New Issue
Block a user