Make order search, device results and decisions visible in the WebUI
This commit is contained in:
+90
-24
@@ -1,13 +1,13 @@
|
||||
import { actionTitle, deviceName, display, Fact, identityName, OrderRows, orderTitle, PageButtons, Requester, Status, statusOf, targets, when } from './order-presentation';
|
||||
import { actionTitle, ApprovalDecision, deviceName, display, Fact, identityName, OrderRows, orderTitle, PageButtons, Requester, Status, statusOf, statusText, targets, when } from './order-presentation';
|
||||
import { Devices } from './devices';
|
||||
import { CustomerManagement } from './customer-management';
|
||||
import { ErrorBox, useResource, errorText } from './resource';
|
||||
import { NavIcon, type NavKey } from './navigation';
|
||||
import { NavIcon, orderDetailRoute, type NavKey } from './navigation';
|
||||
import { OrderComposer } from './order-composer';
|
||||
import { ApprovalInbox, usePendingApprovals } from './approval-inbox';
|
||||
import { ScriptLibrary } from './script-library';
|
||||
import { McpTokenManagement } from './mcp-token-management';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { actions, Action, ApiError, authConfig, approvalBlockers, expectedStdout, objectId, page, PlanInput, pretty, record, RecordValue, request, session, Session, stringField, tenant, tenantPath, validateInput, windowsPowerShellRemoteSignedProfile, fullMaintenanceScriptProfile } from './api';
|
||||
import './style.css';
|
||||
@@ -357,6 +357,9 @@ function PlanDetail({ tenantId, id, auth, onOperation, customerName, mode = 'rev
|
||||
<Requester id={saved.principal} ownId={auth.principal.id}/>
|
||||
</Fact>}
|
||||
<Fact label="Status">{data.approval_state ? <Status value={data.approval_state}/> : 'Vorbereitet'}</Fact>
|
||||
{typeof data.approval_state === 'string' && data.approval_state !== 'pending_approval' && <Fact label="Entscheidung">
|
||||
<ApprovalDecision order={data} ownId={auth.principal.id}/>
|
||||
</Fact>}
|
||||
<Fact label="Anlass">{display(saved.reason, 'Kein Anlass angegeben')}</Fact>
|
||||
</dl>
|
||||
<h3>Zielgeräte und Zeitfenster</h3>
|
||||
@@ -393,18 +396,70 @@ function PlanDetail({ tenantId, id, auth, onOperation, customerName, mode = 'rev
|
||||
</section>;
|
||||
}
|
||||
|
||||
function Operations({ tenantId, open, customerName }: { tenantId: string; open: (id: string) => void; customerName?: string }) {
|
||||
const emptyOrderFilters = { q: '', device: '', status: '', from: '', to: '' };
|
||||
type OrderFilters = typeof emptyOrderFilters;
|
||||
const orderStatuses = ['pending_approval', 'approved', 'queued', 'running', 'succeeded', 'partially_succeeded',
|
||||
'failed', 'needs_attention', 'rejected', 'expired', 'revoked', 'cancelled'];
|
||||
function orderDay(value: string, next = false) {
|
||||
const date = new Date(value + 'T00:00:00');
|
||||
if (next) date.setDate(date.getDate() + 1);
|
||||
return date.toISOString();
|
||||
}
|
||||
function Operations({ tenantId, open, customerName, filters, apply }: {
|
||||
tenantId: string; open: (id: string) => void; customerName?: string;
|
||||
filters: OrderFilters; apply: (filters: OrderFilters) => void;
|
||||
}) {
|
||||
const [pages, setPages] = useState<string[]>([]);
|
||||
const [draft, setDraft] = useState(filters);
|
||||
const [filterError, setFilterError] = useState<string | null>(null);
|
||||
const cursor = pages[pages.length - 1];
|
||||
const list = useResource(async signal => page(await request(tenantPath(tenantId,
|
||||
'/operations?sort=recent' + (cursor ? '&cursor=' + encodeURIComponent(cursor) : '')), { signal })), [tenantId, cursor], true);
|
||||
if (list.error) return <ErrorBox error={list.error}/>;
|
||||
const params = new URLSearchParams({sort:'recent', limit:'25'});
|
||||
if (cursor) params.set('cursor', cursor);
|
||||
if (filters.q) params.set('q', filters.q);
|
||||
if (filters.device) params.set('device', filters.device);
|
||||
if (filters.status) params.set('status', filters.status);
|
||||
if (filters.from) params.set('from', orderDay(filters.from));
|
||||
if (filters.to) params.set('until', orderDay(filters.to, true));
|
||||
const query = params.toString();
|
||||
const list = useResource(async signal => page(await request(tenantPath(tenantId, '/operations?' + query), { signal })), [tenantId, query], true);
|
||||
const filtering = Object.values(filters).some(Boolean);
|
||||
function reset() {
|
||||
setDraft(emptyOrderFilters);
|
||||
setFilterError(null);
|
||||
setPages([]);
|
||||
apply(emptyOrderFilters);
|
||||
}
|
||||
return <section aria-label="Auftragsliste" className="fleet">
|
||||
<form className="order-filters" aria-label="Aufträge filtern" onSubmit={event => {
|
||||
event.preventDefault();
|
||||
if (draft.from && draft.to && draft.from > draft.to) {
|
||||
setFilterError('Das Enddatum darf nicht vor dem Startdatum liegen.');
|
||||
return;
|
||||
}
|
||||
setPages([]);
|
||||
setFilterError(null);
|
||||
apply({ ...draft, q: draft.q.trim(), device: draft.device.trim() });
|
||||
}}>
|
||||
<label>Auftrag<input type="search" maxLength={120} value={draft.q} onChange={e => setDraft({ ...draft, q:e.target.value })} placeholder="Name oder Anlass"/></label>
|
||||
<label>Gerät<input type="search" maxLength={120} value={draft.device} onChange={e => setDraft({ ...draft, device:e.target.value })} placeholder="Hostname oder Geräte-ID"/></label>
|
||||
<label>Status<select value={draft.status} onChange={e => setDraft({ ...draft, status:e.target.value })}>
|
||||
<option value="">Alle Status</option>
|
||||
{orderStatuses.map(status => <option key={status} value={status}>{statusText(status)}</option>)}
|
||||
</select></label>
|
||||
<label>Von<input type="date" value={draft.from} onChange={e => setDraft({ ...draft, from:e.target.value })}/></label>
|
||||
<label>Bis einschließlich<input type="date" value={draft.to} onChange={e => setDraft({ ...draft, to:e.target.value })}/></label>
|
||||
<div className="order-filter-actions">
|
||||
<button type="submit">Filtern</button>
|
||||
{(filtering || Object.values(draft).some(Boolean)) && <button type="button" className="quiet" onClick={reset}>Filter zurücksetzen</button>}
|
||||
</div>
|
||||
<ErrorBox error={filterError}/>
|
||||
</form>
|
||||
<ErrorBox error={list.error}/>
|
||||
{!list.data && !list.error && <p className="list-message">Aufträge werden geladen …</p>}
|
||||
{list.data && (list.data.items.length
|
||||
{list.data && !list.error && (list.data.items.length
|
||||
? <OrderRows items={list.data.items} customerName={customerName} open={row => open(objectId(row,'operation'))}/>
|
||||
: <p className="list-message">Für diesen Kunden gibt es noch keine Aufträge.</p>)}
|
||||
{list.data && <PageButtons pages={pages} next={list.data.next_cursor} change={setPages}/>}
|
||||
: <p className="list-message">{filtering ? 'Keine Aufträge passen zu den Filtern.' : 'Für diesen Kunden gibt es noch keine Aufträge.'}</p>)}
|
||||
{list.data && !list.error && <PageButtons pages={pages} next={list.data.next_cursor} change={setPages}/>}
|
||||
</section>;
|
||||
}
|
||||
function OrderScript({ tenantId, operation, names }: { tenantId: string; operation: RecordValue; names: RecordValue[] }) {
|
||||
@@ -449,16 +504,17 @@ function OrderScript({ tenantId, operation, names }: { tenantId: string; operati
|
||||
})}
|
||||
</article>;
|
||||
}
|
||||
function OperationDetail({ tenantId, id, auth, openPlan, customerName }: {
|
||||
tenantId: string; id: string; auth: Session; openPlan: (id: string) => void; customerName?: string
|
||||
function OperationDetail({ tenantId, id, resultId: linkedResult, auth, openPlan, customerName }: {
|
||||
tenantId: string; id: string; resultId?: string; auth: Session; openPlan: (id: string) => void; customerName?: string
|
||||
}) {
|
||||
const op = useResource(async signal => record(await request(tenantPath(tenantId, '/operations/' + encodeURIComponent(id)), { signal })), [tenantId, id], true);
|
||||
const [jobId, setJobId] = useState<string | null>(null);
|
||||
const [jobId, setJobId] = useState<string | null>(linkedResult ?? null);
|
||||
const data = op.error ? null : op.data;
|
||||
if (!data) return <section data-operation-id={id}><ErrorBox error={op.error}/>{!op.error && <p>Auftrag wird geladen …</p>}</section>;
|
||||
const names = targets(data);
|
||||
const jobs = Array.isArray(data.jobs) ? data.jobs.map(j => record(j)) : [];
|
||||
const singleJob = jobs.length === 1 ? jobs[0] : null;
|
||||
const resultMissing = !!linkedResult && !jobs.some(job => job.job_id === linkedResult);
|
||||
const singleJob = jobs.length === 1 && !resultMissing ? jobs[0] : null;
|
||||
const name = (job: RecordValue) => {
|
||||
const target = names.find(t => t.asset_id === job.asset_id && t.agent_id === job.agent_id);
|
||||
return target ? deviceName(target) : display(job.asset_id, 'Gerät unbekannt');
|
||||
@@ -470,9 +526,10 @@ function OperationDetail({ tenantId, id, auth, openPlan, customerName }: {
|
||||
<Fact label="Anlass">{display(data.reason, 'Kein Anlass angegeben')}</Fact></dl>
|
||||
<div className="order-execution">
|
||||
{singleJob ? <JobDetail key={objectId(singleJob, 'job')} tenantId={tenantId}
|
||||
id={objectId(singleJob, 'job')} hostname={name(singleJob)} auth={auth}/>
|
||||
id={objectId(singleJob, 'job')} hostname={name(singleJob)} auth={auth} scrollToResult={!!linkedResult}/>
|
||||
: <div className="order-results">
|
||||
<h3>Geräte und Ergebnisse</h3>
|
||||
{resultMissing && <p role="alert">Das verlinkte Geräteergebnis gehört nicht zu diesem Auftrag oder ist nicht mehr verfügbar.</p>}
|
||||
{jobs.length ? <ul className="job-list">{jobs.map(job => {
|
||||
const resultId = objectId(job, 'job');
|
||||
const hostname = name(job);
|
||||
@@ -486,7 +543,8 @@ function OperationDetail({ tenantId, id, auth, openPlan, customerName }: {
|
||||
aria-label={action + ' für ' + hostname}
|
||||
onClick={() => setJobId(expanded ? null : resultId)}>{action}</button>
|
||||
</div>
|
||||
{expanded && <JobDetail tenantId={tenantId} id={resultId} hostname={hostname} auth={auth}/>}
|
||||
{expanded && <JobDetail tenantId={tenantId} id={resultId} hostname={hostname} auth={auth}
|
||||
scrollToResult={!!jobId && jobId === linkedResult}/>}
|
||||
</li>;
|
||||
})}</ul>
|
||||
: <><p>{names.map(deviceName).join(', ')}</p><p className="muted">{data.approval_state === 'pending_approval'
|
||||
@@ -500,13 +558,19 @@ function OperationDetail({ tenantId, id, auth, openPlan, customerName }: {
|
||||
<JsonView value={data} label="Technische Auftragsdaten"/>
|
||||
</section>;
|
||||
}
|
||||
function JobDetail({ tenantId, id, auth, hostname }: { tenantId: string; id: string; auth: Session; hostname: string }) {
|
||||
function JobDetail({ tenantId, id, auth, hostname, scrollToResult }: {
|
||||
tenantId: string; id: string; auth: Session; hostname: string; scrollToResult?: boolean
|
||||
}) {
|
||||
const job = useResource(async signal => record(await request(tenantPath(tenantId, '/jobs/'+encodeURIComponent(id)), { signal })), [tenantId, id], true);
|
||||
const result = useRef<HTMLElement>(null);
|
||||
useEffect(() => {
|
||||
if (scrollToResult) result.current?.scrollIntoView({ block:'start' });
|
||||
}, [id, scrollToResult]);
|
||||
const [error, setError] = useState<string | null>(null), [busy, setBusy] = useState(false);
|
||||
const data = job.error ? null : job.data;
|
||||
const state = data?.state;
|
||||
const terminal = ['succeeded','failed','cancelled','expired','unknown','closed_unknown'];
|
||||
return <article className="job-result" data-job-id={id}><h3>Ergebnis für {hostname}</h3><ErrorBox error={job.error ?? error}/>
|
||||
return <article ref={result} className="job-result" data-job-id={id}><h3>Ergebnis für {hostname}</h3><ErrorBox error={job.error ?? error}/>
|
||||
{!data && !job.error && <p>Ergebnis wird geladen …</p>}
|
||||
{data && <><ConsoleOutput job={data}/><JsonView value={data} label="Technische Ausführungsdaten"/></>}
|
||||
{data && allowed(auth,'jobs:cancel') && !terminal.includes(String(state)) && !data.cancel_requested && <details><summary>Abbruch anfordern</summary>
|
||||
@@ -567,7 +631,8 @@ function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
|
||||
);
|
||||
const customers = tenants.data?.items ?? auth.tenants;
|
||||
const [selectedCustomer, setCustomer] = useState(auth.tenants[0]?.tenant_id ?? '');
|
||||
const [detailKind, detailTenant, detailId] = route.tab.split('/');
|
||||
const [detailKind, detailTenant, detailId, detailJob] = route.tab.split('/');
|
||||
const [orderFilters, setOrderFilters] = useState(emptyOrderFilters);
|
||||
const detailRoute = (route.page === 'genehmigungen' && detailKind === 'pruefen')
|
||||
|| (route.page === 'auftraege' && (detailKind === 'ansehen' || detailKind === 'entwurf'));
|
||||
const customer = detailRoute && detailTenant ? detailTenant : selectedCustomer;
|
||||
@@ -601,9 +666,9 @@ function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
|
||||
// Gate rendering during a tenant change; a previously loaded session is scoped to its own customer.
|
||||
const scoped = current.data?.id === customer ? current.data.auth : null;
|
||||
const customerName = customers.find((c) => c.tenant_id === customer)?.name ?? '';
|
||||
function openOperation(tenantId: string, id: string) {
|
||||
function openOperation(tenantId: string, id: string, jobId?: string) {
|
||||
switchCustomer(tenantId);
|
||||
go('auftraege', `ansehen/${tenantId}/${id}`);
|
||||
go('auftraege', orderDetailRoute(tenantId, id, jobId));
|
||||
}
|
||||
function openReview(tenantId: string, id: string) {
|
||||
switchCustomer(tenantId);
|
||||
@@ -741,7 +806,7 @@ function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
|
||||
<a href="#genehmigungen" className="back-link">← Zu den Genehmigungen</a>
|
||||
<PlanDetail key={detailId} tenantId={customer} id={detailId} auth={scoped}
|
||||
customerName={customerName} mode="review" onOperation={afterSubmission} />
|
||||
</> : <><div className="page-title"><h1>Genehmigungen</h1></div><ApprovalInbox customers={customers} open={openReview} /></>
|
||||
</> : <><div className="page-title"><h1>Genehmigungen</h1></div><ApprovalInbox ownId={auth.principal.id} open={openReview} /></>
|
||||
)}
|
||||
{route.page === 'auftraege' && (
|
||||
route.tab === 'neu' ? <>
|
||||
@@ -759,13 +824,14 @@ function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
|
||||
{detailKind === 'entwurf' ?
|
||||
<PlanDetail key={detailId} tenantId={customer} id={detailId} auth={scoped}
|
||||
customerName={customerName} mode="submit" onOperation={afterSubmission} />
|
||||
: <OperationDetail key={detailId} tenantId={customer} id={detailId} auth={scoped}
|
||||
: <OperationDetail key={detailId+'/'+(detailJob ?? '')} tenantId={customer} id={detailId} resultId={detailJob} auth={scoped}
|
||||
customerName={customerName} openPlan={id => openReview(customer,id)} />}
|
||||
</> : <>
|
||||
<div className="page-title"><h1>Aufträge</h1>
|
||||
{allowed(scoped, 'operations:plan') && <a className="primary-action" href="#auftraege/neu"><NavIcon name="plus" /> Neuer Auftrag</a>}
|
||||
</div>
|
||||
<Operations tenantId={customer} customerName={customerName} open={id => openOperation(customer,id)} />
|
||||
<Operations tenantId={customer} customerName={customerName} open={id => openOperation(customer,id)}
|
||||
filters={orderFilters} apply={setOrderFilters}/>
|
||||
</>
|
||||
)}
|
||||
{route.page === 'verwaltung' && (
|
||||
|
||||
Reference in New Issue
Block a user