From 2ec904756522a935f95de370ff97a4ff166cc67e Mon Sep 17 00:00:00 2001 From: genja Date: Wed, 1 Jul 2026 10:45:31 +0200 Subject: [PATCH] added websocket implementation to frontend --- .../keywords-search.component.html | 246 ++++++++---------- .../keywords-search.component.scss | 17 ++ .../keywords-search.component.ts | 208 ++++++++++----- 3 files changed, 272 insertions(+), 199 deletions(-) diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.html b/webapp/src/app/modules/keywords-search/keywords-search.component.html index 8f4e5e51..02aab57c 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.html +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.html @@ -59,17 +59,7 @@ (click)="extractKeywords()" [disabled]="isExtracting()"> troubleshoot - @switch (loadingState()) { - @case ('extracting-url') { - URL wird abgerufen... - } - @case ('extracting-text') { - Text wird analysiert... - } - @default { - Keywords extrahieren - } - } + Keywords extrahieren } @@ -112,137 +102,121 @@ } - @if (!isExtracting()) { -
- Extrahierte Keywords - @if (extractedKeywordsExist()) { -
- - @if (broadKeywords().length > 0) { -
-

Breite Keywords

- - @for (keyword of broadKeywords(); track keyword) { - {{ keyword }} - } - -
- } - - @if (coarseKeywords().length > 0) { -
-

Coarse Keywords

- - @for (keyword of coarseKeywords(); track keyword) { - {{ keyword }} - } - -
- } - - @if (fineKeywords().length > 0) { -
-

Fine Keywords

- - @for (keyword of fineKeywords(); track keyword) { - {{ keyword }} - } - -
- } -
+
+ Extrahierte Keywords + @if (extractedKeywordsExist()) { +
+ + @if (broadKeywords().length > 0) { +
+

Breite Keywords

+ + @for (keyword of broadKeywords(); track keyword) { + {{ keyword }} + } + +
+ } + + @if (coarseKeywords().length > 0) { +
+

Coarse Keywords

+ + @for (keyword of coarseKeywords(); track keyword) { + {{ keyword }} + } + +
+ } + + @if (fineKeywords().length > 0) { +
+

Fine Keywords

+ + @for (keyword of fineKeywords(); track keyword) { + {{ keyword }} + } + +
+ } +
- -
- - @if (areAllExtractedSelected()) { - - } @else { - - } - - + +
+ + @if (areAllExtractedSelected()) { -
- } @else { -

- Keine extrahierten Keywords vorhanden. Extrahiere Keywords, um - sie hier zu sehen oder füge sie manuell hinzu. -

- } + } @else { + + } + + + +
+ } @else { +

+ Keine extrahierten Keywords vorhanden. Extrahiere Keywords, um sie + hier zu sehen oder füge sie manuell hinzu. +

+ } +
+ + @if (isExtracting()) { +
+ + +
{{ statusMessage() }}
- } @else { - - @switch (loadingState()) { - @case ('extracting-url') { - - } - @case ('extracting-text') { - - } - @default { - - } - } - } diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.scss b/webapp/src/app/modules/keywords-search/keywords-search.component.scss index 8545aeca..daf9bb39 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.scss +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.scss @@ -242,3 +242,20 @@ background: rgba(0, 0, 0, 0.03); } } +.progress-container { + margin-top: 1rem; + padding: 0.5rem 0; + + mat-progress-bar { + height: 8px; + border-radius: 4px; + } + + .status-message { + margin-top: 0.5rem; + font-size: 0.9rem; + color: var(--sg-text-secondary); + text-align: center; + font-weight: 500; + } +} diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.ts b/webapp/src/app/modules/keywords-search/keywords-search.component.ts index 3e144b4c..0af2ae1d 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.ts +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.ts @@ -11,11 +11,16 @@ import { MatInputModule } from '@angular/material/input'; import { MatTabsModule } from '@angular/material/tabs'; import { ActivatedRoute, Router } from '@angular/router'; import { debounceTime, distinctUntilChanged } from 'rxjs'; -import { KeywordsService } from '../../api/api/keywords.service'; import { KeywordsRequest } from '../../api/model/keywordsRequest'; import { MatExpansionModule } from '@angular/material/expansion'; +import { ExtractedKeywords } from '../../api/model/extractedKeywords'; +import { ExtractedKeywordsResponse } from '../../api/model/extractedKeywordsResponse'; import { DestroyRef } from '@angular/core'; -import { LoadingComponent } from '../../shared/ui/loading/loading.component'; +import { MatProgressBarModule } from '@angular/material/progress-bar'; +import { + KeywordWebSocketService, + WsEvent, +} from '../../shared/services/websocket.service'; type InputMode = 'url' | 'text' | 'keywords'; @Component({ @@ -31,7 +36,7 @@ type InputMode = 'url' | 'text' | 'keywords'; MatDividerModule, MatCardModule, MatExpansionModule, - LoadingComponent, + MatProgressBarModule, ], templateUrl: './keywords-search.component.html', styleUrl: './keywords-search.component.scss', @@ -80,22 +85,17 @@ export class KeywordsSearchComponent { private isRestoringState = false; - readonly loadingState = computed(() => { - if (!this.isExtracting()) return 'idle' as const; - - const mode = this.form.controls.selectedInputType.value; - if (mode === 'url') return 'extracting-url' as const; - if (mode === 'text') return 'extracting-text' as const; - return 'extracting-keywords' as const; - }); + readonly progress = signal(0); + readonly statusMessage = signal(''); constructor( private readonly formBuilder: FormBuilder, private readonly route: ActivatedRoute, private readonly router: Router, - private readonly keywordsService: KeywordsService, private readonly destroyRef: DestroyRef, + private readonly websocketService: KeywordWebSocketService, ) { - this.route.queryParamMap.pipe(takeUntilDestroyed()).subscribe(() => { + this.route.queryParamMap.pipe(takeUntilDestroyed()).subscribe((event) => { + console.log('Component received event:', event); this.restoreStateFromQueryParams(); }); @@ -117,6 +117,64 @@ export class KeywordsSearchComponent { this.updateQueryParams(); }); + //get streamed data + this.websocketService.getMessages().subscribe((event: WsEvent) => { + switch (event.type) { + case 'status': + console.log('STATUS:', event.step, event.message); + this.statusMessage.set(event.message || ''); + this.progress.set(this.getProgressForStep(event.step || '')); + break; + case 'keywords': + console.log('KEYWORDS:', event.keywords); + if (event.keywords) this.addExtractedKeyword(event.keywords); + break; + + // case 'result': + // console.log('RESULT:', event.result); + // this.processExtractedKeywords( + // event.result as ExtractedKeywordsResponse, + // ); + // this.isExtracting.set(false); + // break; + case 'completed': + console.log('COMPLETED:', event.result); + this.processExtractedKeywords( + event.result as ExtractedKeywordsResponse, + ); + this.isExtracting.set(false); + this.progress.set(100); + this.statusMessage.set('Fertig!'); + this.websocketService.close(); + this.isExtracting.set(false); + break; + + case 'error': + console.error('ERROR:', event.message); + this.progress.set(0); + this.statusMessage.set('Fehler: ' + event.message); + this.broadKeywords.set([]); + this.coarseKeywords.set([]); + this.fineKeywords.set([]); + this.websocketService.close(); + this.isExtracting.set(false); + // Keep manual, but remove selected that are not in manual + this.selectedKeywords.set( + this.selectedKeywords().filter((k) => + this.manualKeywords().includes(k), + ), + ); + // extractionSource stays as it was (or reset? we keep it) + this.updateQueryParams(); + break; + default: + console.warn('Unknown event type:', event); + } + this.destroyRef.onDestroy(() => { + this.websocketService.close(); + }); + }); + // Clear session storage on component destroy this.destroyRef.onDestroy(() => { const nav = this.router.lastSuccessfulNavigation; @@ -251,12 +309,62 @@ export class KeywordsSearchComponent { isSearchDisabled(): boolean { return this.selectedKeywords().length === 0 || this.isExtracting(); } + addExtractedKeyword(keywords: ExtractedKeywords): void { + const broad = this.extractKeywordStrings(keywords.broad_keywords); + const coarse = this.extractKeywordStrings(keywords.coarse_grained); + const fine = this.extractKeywordStrings(keywords.fine_grained); - extractKeywords(): void { + this.broadKeywords.set(broad); + this.coarseKeywords.set(coarse); + this.fineKeywords.set(fine); + } + processExtractedKeywords(result: ExtractedKeywordsResponse): void { + if (result === undefined) { + return; + } + console.log('Processing extracted keywords:', result); + const { results, source, source_content, source_url } = result; + + const broad = this.extractKeywordStrings(results.broad_keywords); + const coarse = this.extractKeywordStrings(results.coarse_grained); + const fine = this.extractKeywordStrings(results.fine_grained); + + this.broadKeywords.set(broad); + this.coarseKeywords.set(coarse); + this.fineKeywords.set(fine); + this.sourceContent.set(source_content); + this.extractionSource.set(source as InputMode); + this.sourceUrl.set(source_url ?? ''); + this.saveSourceToSession(); + + console.log('Extracted keywords:', { + broad, + coarse, + fine, + manual: this.manualKeywords(), + }); + // Keep only selected keywords that still exist in any category (including manual) + const allExtracted = [ + ...broad, + ...coarse, + ...fine, + ...this.manualKeywords(), + ]; + this.selectedKeywords.set( + this.selectedKeywords().filter((k) => allExtracted.includes(k)), + ); + + this.updateQueryParams(); + } + async extractKeywords(): Promise { this.clearExtractedKeywords(); const query = this.getCurrentExtractionQuery(); if (!query) return; + // Reset progress and status + this.progress.set(0); + this.statusMessage.set('Starte Extraktion...'); + const currentMode = this.form.controls.selectedInputType.value; const keywordsRequest: KeywordsRequest = { query: query, @@ -265,56 +373,15 @@ export class KeywordsSearchComponent { }; this.isExtracting.set(true); - this.keywordsService.extractKeywords(keywordsRequest).subscribe({ - next: ({ results, source, source_content, source_url }) => { - const broad = this.extractKeywordStrings(results.broad_keywords); - const coarse = this.extractKeywordStrings(results.coarse_grained); - const fine = this.extractKeywordStrings(results.fine_grained); - this.broadKeywords.set(broad); - this.coarseKeywords.set(coarse); - this.fineKeywords.set(fine); - this.sourceContent.set(source_content); - this.extractionSource.set(source as InputMode); - this.sourceUrl.set(source_url ?? ''); - this.saveSourceToSession(); - - console.log('Extracted keywords:', { - broad, - coarse, - fine, - manual: this.manualKeywords(), - }); - // Keep only selected keywords that still exist in any category (including manual) - const allExtracted = [ - ...broad, - ...coarse, - ...fine, - ...this.manualKeywords(), - ]; - this.selectedKeywords.set( - this.selectedKeywords().filter((k) => allExtracted.includes(k)), - ); - - this.updateQueryParams(); - }, - error: () => { - this.broadKeywords.set([]); - this.coarseKeywords.set([]); - this.fineKeywords.set([]); - // Keep manual, but remove selected that are not in manual - this.selectedKeywords.set( - this.selectedKeywords().filter((k) => - this.manualKeywords().includes(k), - ), - ); - // extractionSource stays as it was (or reset? we keep it) - this.updateQueryParams(); - }, - complete: () => { - this.isExtracting.set(false); - }, - }); + try { + // sendRequest now waits internally for the connection to open + await this.websocketService.sendRequest(keywordsRequest); + } catch (error) { + console.error('WebSocket send error:', error); + this.isExtracting.set(false); + this.statusMessage.set('Fehler beim Senden der Anfrage'); + } } search(): void { @@ -390,6 +457,7 @@ export class KeywordsSearchComponent { sourceContent: this.sourceContent(), sourceUrl: this.sourceUrl(), }; + console.log(data); sessionStorage.setItem(this.SESSION_KEY, JSON.stringify(data)); } @@ -446,4 +514,18 @@ export class KeywordsSearchComponent { const normalized = value.trim(); return normalized.length ? normalized : null; } + private getProgressForStep(step: string): number { + switch (step) { + case 'started': + return 10; + case 'fetching_url': + return 30; + case 'content_extracted': + return 60; + case 'calling_llm': + return 80; + default: + return 50; // fallback for unknown steps + } + } }