All files / src/app/group-monitor group-monitor.component.ts

35.57% Statements 53/149
21.05% Branches 16/76
34.69% Functions 17/49
36.29% Lines 49/135

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393                                                                                        1x 1x   1x   1x 1x   1x                   1x 1x   1x 1x   1x   1x           1x 1x 1x 1x 1x 1x 1x 1x       1x   1x               1x     1x     1x     1x       1x 1x 1x       1x 1x 1x               1x                 1x 5x       1x       1x   1x 1x         1x           3x                                                   6x 8x 6x 6x                   6x   2x   2x           2x                                                                                                                                                                                                                                                                                                                                                                                  
import {
  Component, ElementRef, OnDestroy, OnInit, ViewChild
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Sort } from '@angular/material/sort';
import { MatSidenav } from '@angular/material/sidenav';
import {
  interval, Observable, Subscription
} from 'rxjs';
import { MatDialog } from '@angular/material/dialog';
import { MatSlideToggleChange } from '@angular/material/slide-toggle';
import { MatCheckboxChange } from '@angular/material/checkbox';
import { catchError, switchMap } from 'rxjs/operators';
import { KeyValue } from '@angular/common';
import {
  ConfirmDialogComponent, ConfirmDialogData, CustomtextService, ConnectionStatus,
  MainDataService
} from '../shared/shared.module';
import { BackendService } from './backend.service';
import {
  TestViewDisplayOptions,
  TestViewDisplayOptionKey,
  Selected,
  TestSession,
  TestSessionSetStats,
  CommandResponse,
  UIMessage,
  isBooklet,
  TestSessionFilter,
  TestSessionFilterListEntry,
  testSessionFilterListEntrySources, Profile, isColumnOption, isViewOption
} from './group-monitor.interfaces';
import { TestSessionManager } from './test-session-manager/test-session-manager.service';
import { BookletUtil } from './booklet/booklet.util';
import { AddFilterDialogComponent } from './components/add-filter-dialog/add-filter-dialog.component';
 
@Component({
  selector: 'tc-group-monitor',
  templateUrl: './group-monitor.component.html',
  styleUrls: [
    '../../monitor-layout.css',
    './group-monitor.component.css'
  ]
})
export class GroupMonitorComponent implements OnInit, OnDestroy {
  connectionStatus$: Observable<ConnectionStatus> | null = null;
 
  groupLabel = '';
 
  selectedElement: Selected | null = null;
  markedElement: Selected | null = null;
 
  displayOptions: TestViewDisplayOptions = {
    view: 'medium',
    groupColumn: 'hide',
    bookletColumn: 'show',
    blockColumn: 'show',
    unitColumn: 'hide',
    highlightSpecies: false,
    manualChecking: false
  };
 
  isScrollable = false;
  isClosing = false;
 
  quickFilter: string = '';
  quickFilterBoxOpen: boolean = false;
 
  messages: UIMessage[] = [];
 
  private subscriptions: Subscription[] = [];
 
  @ViewChild('adminbackground') mainElem!: ElementRef;
  @ViewChild('sidenav', { static: true }) sidenav!: MatSidenav;
 
  constructor(
    public dialog: MatDialog,
    private route: ActivatedRoute,
    private bs: BackendService,
    public tsm: TestSessionManager,
    private router: Router,
    private cts: CustomtextService,
    public mds: MainDataService,
    private addFilterDialog: MatDialog
  ) {}
 
  ngOnInit(): void {
    this.subscriptions = [
      this.route.params.subscribe(params => {
        this.groupLabel = this.mds.getAccessObject('testGroupMonitor', params['group-name']).label;
        const profileId = params['profile-id'];
        Iif (profileId) {
          this.bs.getProfile(profileId).subscribe(profile => this.applyProfile(profile));
        }
        this.tsm.connect(params['group-name']);
      }),
      this.tsm.sessionsStats$.subscribe(stats => {
        this.onSessionsUpdate(stats);
      }),
      this.tsm.checkedStats$.subscribe(stats => {
        this.onCheckedChange(stats);
      }),
      this.tsm.commandResponses$.subscribe(commandResponse => {
        this.messages.push(this.commandResponseToMessage(commandResponse));
      }),
      this.tsm.commandResponses$
        .pipe(switchMap(() => interval(7000)))
        .subscribe(() => { this.messages.shift(); })
    ];
 
    this.connectionStatus$ = this.bs.connectionStatus$;
    this.mds.appSubTitle$.next(this.cts.getCustomText('gm_headline') ?? '');
    this.tsm.resetFilters();
  }
 
  private commandResponseToMessage(commandResponse: CommandResponse): UIMessage {
    const command = this.cts.getCustomText(`gm_control_${commandResponse.commandType}`) || commandResponse.commandType;
    const successWarning = this.cts.getCustomText(`gm_control_${commandResponse.commandType}_success_warning`) || '';
    Iif (!commandResponse.testIds.length) {
      return {
        level: 'warning',
        text: 'Keine Tests Betroffen von: `%s`',
        customtext: 'gm_message_no_session_affected_by_command',
        replacements: [command, commandResponse.testIds.length.toString(10)]
      };
    }
    return {
      level: successWarning ? 'warning' : 'info',
      text: '`%s` an `%s` tests gesendet! %s',
      customtext: 'gm_message_command_sent_n_sessions',
      replacements: [command, commandResponse.testIds.length.toString(10), successWarning]
    };
  }
 
  ngOnDestroy(): void {
    this.tsm.disconnect();
    this.subscriptions.forEach(subscription => subscription.unsubscribe());
  }
 
  ngAfterViewChecked(): void {
    this.isScrollable = this.mainElem.nativeElement.clientHeight < this.mainElem.nativeElement.scrollHeight;
  }
 
  private onSessionsUpdate(stats: TestSessionSetStats): void {
    this.displayOptions.highlightSpecies = (stats.differentBookletSpecies > 1);
 
    if (!this.tsm.checkingOptions.enableAutoCheckAll) {
      this.displayOptions.manualChecking = true;
    }
  }
 
  private onCheckedChange(stats: TestSessionSetStats): void {
    Iif (stats.differentBookletSpecies > 1) {
      this.selectedElement = null;
    }
  }
 
  // eslint-disable-next-line class-methods-use-this
  trackSession = (index: number, session: TestSession): number => session.data.testId;
 
  setTableSorting(sort: Sort): void {
    Iif (!sort.active || sort.direction === '') {
      return;
    }
    this.tsm.sortBy$?.next(sort);
  }
 
  setDisplayOption(option: TestViewDisplayOptionKey, value: TestViewDisplayOptions[TestViewDisplayOptionKey]): void {
    Iif (Object.keys(this.displayOptions).includes(option)) {
      (this.displayOptions as { [option in TestViewDisplayOptionKey]: TestViewDisplayOptions[TestViewDisplayOptionKey] })[option] = value;
    }
  }
 
  scrollDown(): void {
    this.mainElem.nativeElement.scrollTo(0, this.mainElem.nativeElement.scrollHeight);
  }
 
  updateScrollHint(): void {
    const elem = this.mainElem.nativeElement;
    const reachedBottom = (elem.scrollTop + elem.clientHeight === elem.scrollHeight);
    elem.classList[reachedBottom ? 'add' : 'remove']('hide-scroll-hint');
  }
 
  getSessionColor(session: TestSession): string {
    const stripes = (c1: string, c2: string) => `repeating-linear-gradient(45deg, ${c1}, ${c1} 10px, ${c2} 10px, ${c2} 20px)`;
    const hsl = (h: number, s: number, l: number) => `hsl(${h}, ${s}%, ${l}%)`;
    const colorful = this.displayOptions.highlightSpecies && session.booklet.species;
    const h = colorful ? (
      session.booklet.species.length *
      session.booklet.species.charCodeAt(0) *
      session.booklet.species.charCodeAt(session.booklet.species.length / 4) *
      session.booklet.species.charCodeAt(session.booklet.species.length / 4) *
      session.booklet.species.charCodeAt(session.booklet.species.length / 2) *
      session.booklet.species.charCodeAt(3 * (session.booklet.species.length / 4)) *
      session.booklet.species.charCodeAt(session.booklet.species.length - 1)
    ) % 360 : 0;
 
    switch (session.state) {
      case 'paused':
        return hsl(h, colorful ? 45 : 0, 90);
      case 'pending':
        return stripes(hsl(h, colorful ? 75 : 0, 95), hsl(h, 0, 98));
      case 'locked':
        return stripes(hsl(h, colorful ? 75 : 0, 95), hsl(0, 0, 92));
      case 'error':
        return stripes(hsl(h, colorful ? 75 : 0, 95), hsl(0, 30, 95));
      default:
        return hsl(h, colorful ? 75 : 0, colorful ? 95 : 100);
    }
  }
 
  markElement(marking: Selected): void {
    this.markedElement = marking;
  }
 
  selectElement(selected: Selected): void {
    this.tsm.checkSessionsBySelection(selected);
    this.selectedElement = selected;
  }
 
  finishEverythingCommand(): void {
    const dialogRef = this.dialog.open(ConfirmDialogComponent, {
      width: 'auto',
      data: <ConfirmDialogData>{
        title: 'Testdurchführung Beenden',
        content: 'Achtung! Diese Aktion sperrt und beendet sämtliche Tests dieser Sitzung.',
        confirmbuttonlabel: 'Ja, ich möchte die Testdurchführung Beenden',
        showcancel: true
      }
    });
 
    dialogRef.afterClosed().subscribe((confirmed: boolean) => {
      Iif (confirmed) {
        this.isClosing = true;
        this.tsm.commandFinishEverything()
          .pipe(catchError(err => {
            this.isClosing = false;
            throw err;
          }))
          .subscribe(() => {
            setTimeout(() => { this.router.navigateByUrl('/r/login'); }, 5000); // go away
          });
      }
    });
  }
 
  testCommandGoto(): void {
    if (!this.selectedElement?.element?.blockId) {
      this.messages.push({
        level: 'warning',
        customtext: 'gm_test_command_no_selected_block',
        text: 'Kein Zielblock ausgewählt'
      });
    } else {
      this.tsm.testCommandGoto(this.selectedElement)
        .subscribe(() => this.selectNextBlock());
    }
  }
 
  private selectNextBlock(): void {
    Iif (!this.selectedElement) {
      return;
    }
    Iif (!isBooklet(this.selectedElement.originSession.booklet)) {
      return;
    }
    this.selectedElement = {
      element: this.selectedElement.element?.nextBlockId ?
        BookletUtil.getBlockById(
          this.selectedElement.element.nextBlockId,
          this.selectedElement.originSession.booklet
        ) : null,
      inversion: false,
      originSession: this.selectedElement.originSession,
      spreading: this.selectedElement.spreading
    };
  }
 
  unlockCommand(): void {
    this.tsm.testCommandUnlock();
  }
 
  toggleChecked(checked: boolean, session: TestSession): void {
    if (!this.tsm.isChecked(session)) {
      this.tsm.checkSession(session);
    } else {
      this.tsm.uncheckSession(session);
    }
  }
 
  invertChecked(event: Event): boolean {
    event.preventDefault();
    this.tsm.invertChecked();
    return false;
  }
 
  toggleAlwaysCheckAll(event: MatSlideToggleChange): void {
    if (this.tsm.checkingOptions.enableAutoCheckAll && event.checked) {
      this.tsm.checkAll();
      this.displayOptions.manualChecking = false;
      this.tsm.checkingOptions.autoCheckAll = true;
    } else {
      this.tsm.checkNone();
      this.displayOptions.manualChecking = true;
      this.tsm.checkingOptions.autoCheckAll = false;
    }
  }
 
  toggleCheckAll(event: MatCheckboxChange): void {
    if (event.checked) {
      this.tsm.checkAll();
    } else {
      this.tsm.checkNone();
    }
  }
 
  addFilter(): void {
    this.openFilterDialog();
  }
 
  editFilter(key: string): void {
    this.openFilterDialog(this.tsm.filterOptions[key]);
  }
 
  private openFilterDialog(filterEntry: TestSessionFilterListEntry | undefined = undefined) {
    const data = filterEntry ? filterEntry.filter : {};
    const dialogRef = this.addFilterDialog.open(AddFilterDialogComponent, { width: 'auto', data });
 
    dialogRef.afterClosed().subscribe((newFilter: TestSessionFilter) => {
      Iif (!newFilter) return;
      this.tsm.filterOptions[newFilter.id] = {
        selected: true,
        filter: newFilter,
        source: filterEntry?.source || 'custom'
      };
      this.tsm.refreshFilters();
    });
  }
 
  // eslint-disable-next-line class-methods-use-this
  sortFilterMenuEntries(
    a: KeyValue<string, TestSessionFilterListEntry>,
    b: KeyValue<string, TestSessionFilterListEntry>
  ): number {
    const aLevel = testSessionFilterListEntrySources.indexOf(a.value.source);
    const bLevel = testSessionFilterListEntrySources.indexOf(b.value.source);
    Iif (aLevel !== bLevel) return aLevel - bLevel;
    return a.value.filter.label > b.value.filter.label ? 1 : -1;
  }
 
  quickFilterOnUpdateModel() {
    if (!this.quickFilter) {
      this.tsm.filterOptions.quick.selected = false;
    } else {
      this.tsm.filterOptions.quick.selected = true;
      this.tsm.filterOptions.quick.filter.value = this.quickFilter;
    }
 
    this.tsm.refreshFilters();
  }
 
  toggleQuickFilterBox(): void {
    this.quickFilterBoxOpen = !this.quickFilterBoxOpen;
  }
 
  quickFilterOnFocusOut(): void {
    Iif (!this.quickFilter) this.quickFilterBoxOpen = false;
  }
 
  private applyProfile(p: Profile): void {
    Iif (isColumnOption(p.settings.blockColumn)) this.displayOptions.blockColumn = p.settings.blockColumn;
    Iif (isColumnOption(p.settings.unitColumn)) this.displayOptions.unitColumn = p.settings.unitColumn;
    Iif (isColumnOption(p.settings.groupColumn)) this.displayOptions.groupColumn = p.settings.groupColumn;
    Iif (isColumnOption(p.settings.bookletColumn)) this.displayOptions.bookletColumn = p.settings.bookletColumn;
    Iif (isViewOption(p.settings.view)) this.displayOptions.view = p.settings.view;
 
    (p.filters || [])
      .forEach((filter: TestSessionFilter, index: number) => {
        filter.id = `profile_filter:${index}`;
        this.tsm.filterOptions[filter.id] = { selected: true, filter, source: 'profile' };
      });
 
    Object.entries(p.filtersEnabled || [])
      .forEach(([f, onOff]) => {
        Iif (this.tsm.filterOptions[f]) {
          this.tsm.filterOptions[f].selected = ['1', 'true', 'on', 'yes'].includes(onOff);
        }
      });
 
    this.tsm.refreshFilters();
  }
}