feat: browser notifications for scenario runs
- add RunEventsService and emit run.started / run.finished from the scheduler - stream run events over SSE at GET /api/v1/scenarios/runs/events with a heartbeat - add useRunNotifications hook and NotificationToggle (opt-in bell in the side panel footer) - refresh runs lists when a run finishes - document the events stream and notifications Closes #12
This commit is contained in:
18 files changed
+503
-6
No files matched your search
@@ -11,12 +11,14 @@ import {
|
||||
Post,
|
||||
Query,
|
||||
Res,
|
||||
Sse,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
} from "@nestjs/common";
|
||||
import { FileInterceptor } from "@nestjs/platform-express";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import { Response } from "express";
|
||||
import { interval, map, merge, type Observable } from "rxjs";
|
||||
import type { File as MulterFile } from "multer";
|
||||
import { stringify as yamlStringify } from "yaml";
|
||||
import { AddScenarioCredentialDto } from "./dto/add-scenario-credential.dto";
|
||||
@@ -28,15 +30,24 @@ import { RunsQueryDto } from "./dto/runs-query.dto";
|
||||
import { ExportEntity, ScenarioExportDto } from "./dto/scenario-export.dto";
|
||||
import { UpdateScenarioStepDto } from "./dto/update-scenario-step.dto";
|
||||
import { UpdateScenarioDto } from "./dto/update-scenario.dto";
|
||||
import { RunEventsService } from "./run-events.service";
|
||||
import { ScenarioService } from "./scenario.service";
|
||||
import { E2eExportService } from "./e2e-export.service";
|
||||
|
||||
interface SseMessage {
|
||||
type: string;
|
||||
data: object;
|
||||
}
|
||||
|
||||
@ApiTags("scenarios")
|
||||
@Controller("scenarios")
|
||||
export class ScenarioController {
|
||||
private static readonly HEARTBEAT_MS = 25_000;
|
||||
|
||||
constructor(
|
||||
private readonly scenarioService: ScenarioService,
|
||||
private readonly e2eExportService: E2eExportService,
|
||||
private readonly runEvents: RunEventsService,
|
||||
) {}
|
||||
|
||||
// ── Scenarios ─────────────────────────────────────────────────────────────
|
||||
@@ -73,6 +84,23 @@ export class ScenarioController {
|
||||
return this.scenarioService.findAllRuns(query);
|
||||
}
|
||||
|
||||
@Sse("runs/events")
|
||||
@ApiOperation({
|
||||
summary:
|
||||
"Server-sent events stream of run lifecycle events (run.started, run.finished) for all scenarios",
|
||||
})
|
||||
@ApiResponse({ status: 200, description: "text/event-stream" })
|
||||
streamRunEvents(): Observable<SseMessage> {
|
||||
const events = this.runEvents
|
||||
.stream()
|
||||
.pipe(map((event): SseMessage => ({ type: event.type, data: event })));
|
||||
// Periodic named event so proxies don't drop an idle connection
|
||||
const heartbeat = interval(ScenarioController.HEARTBEAT_MS).pipe(
|
||||
map((): SseMessage => ({ type: "ping", data: {} })),
|
||||
);
|
||||
return merge(events, heartbeat);
|
||||
}
|
||||
|
||||
@Get("run-defaults")
|
||||
@ApiOperation({ summary: "Get server-configured defaults for new runs" })
|
||||
@ApiResponse({ status: 200 })
|
||||
|
||||
Reference in new issue
Block a user