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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | 1x 25x 25x 12x 23x 25x 25x 25x 25x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 125x 125x 25x 25x 25x 25x 25x 25x 75x 36x 158x 158x 158x 3x 3x 158x 40x 31x 28x 3x 22x 3x 158x 39x 5x 3x 3x 3x 8x 8x 8x 8x 8x 8x 9x 158x 100x 25x 25x 25x 25x 25x 25x 125x 25x 25x 25x 39x 139x 139x 4x 135x 7x 7x 7x 3x 4x 2x 2x 128x 4x 124x 7x 7x 7x 117x 7x 7x 7x 110x 110x 110x 110x 110x 110x 1x 1x 5x 3x 3x 2x 5x 3x 1x 23x 8x 8x 8x 8x 3x 5x 25x 20x 13x 8x 12x 12x 20x 12x 12x 12x 63x 63x 63x 63x 63x 147x 147x 147x 147x 63x | import { Inject, Injectable } from '@angular/core'; import { BehaviorSubject, combineLatest, interval, Observable, of, Subject, zip } from 'rxjs'; import { Sort } from '@angular/material/sort'; import { delay, filter, flatMap, map, startWith, switchMap, tap } from 'rxjs/operators'; import { BackendService } from '../backend.service'; import { BookletService } from '../booklet/booklet.service'; import { TestSessionUtil } from '../test-session/test-session.util'; import { isBooklet, Selected, CheckingOptions, TestSession, TestSessionFilter, TestSessionSetStats, TestSessionsSuperStates, CommandResponse, GotoCommandData, GroupMonitorConfig, TestSessionFilterList } from '../group-monitor.interfaces'; import { BookletUtil } from '../booklet/booklet.util'; import { GROUP_MONITOR_CONFIG } from '../group-monitor.config'; @Injectable() export class TestSessionManager { sortBy$: Subject<Sort>; filters$: Subject<TestSessionFilter[]>; checkingOptions: CheckingOptions = { enableAutoCheckAll: false, autoCheckAll: false }; private groupName: string = ''; get sessions$(): Observable<TestSession[]> { return this._sessions$.asObservable(); } get sessions(): TestSession[] { return this._sessions$.getValue(); } get checked(): TestSession[] { // this is intentionally not an observable return Object.values(this._checked); } get sessionsStats$(): Observable<TestSessionSetStats> { return this._sessionsStats$.asObservable(); } get checkedStats$(): Observable<TestSessionSetStats> { return this._checkedStats$.asObservable(); } get commandResponses$(): Observable<CommandResponse> { return this._commandResponses$ .pipe( filter(c => !!c) ); } private monitor$: Observable<TestSession[]> = new Observable<TestSession[]>(); private _sessions$: BehaviorSubject<TestSession[]> = new BehaviorSubject<TestSession[]>([]); private _checked: { [sessionTestSessionId: number]: TestSession } = {}; private _checkedStats$: BehaviorSubject<TestSessionSetStats>; private _sessionsStats$: BehaviorSubject<TestSessionSetStats>; private _commandResponses$: Subject<CommandResponse> = new Subject<CommandResponse>(); private _clock$: Observable<number>; static readonly basicFilters : TestSessionFilterList = { locked: { selected: false, source: 'base', filter: { id: 'locked', label: 'gm_filter_locked', target: 'testState', value: 'status', subValue: 'locked', type: 'equal', not: false } }, pending: { selected: false, source: 'base', filter: { id: 'pending', label: 'gm_filter_pending', target: 'testState', value: 'status', subValue: 'pending', type: 'equal', not: false } }, quick: { selected: false, source: 'quick', filter: { id: 'quickFilter', label: 'gm_filter_quick', target: 'personLabel', value: '', subValue: '', type: 'substring', not: true } } }; filterOptions: TestSessionFilterList = {}; constructor( private bs: BackendService, private bookletService: BookletService, @Inject(GROUP_MONITOR_CONFIG) private readonly groupMonitorConfig: GroupMonitorConfig ) { this._checkedStats$ = new BehaviorSubject<TestSessionSetStats>(TestSessionManager.getEmptyStats()); this._sessionsStats$ = new BehaviorSubject<TestSessionSetStats>(TestSessionManager.getEmptyStats()); this.sortBy$ = new BehaviorSubject<Sort>({ direction: 'asc', active: 'personLabel' }); this.filters$ = new BehaviorSubject<TestSessionFilter[]>([]); this._clock$ = this.groupMonitorConfig.checkForIdleInterval ? interval(this.groupMonitorConfig.checkForIdleInterval).pipe(startWith(0)) : of(0); this.resetFilters(); } connect(groupName: string): void { this.groupName = groupName; this.sortBy$.next({ direction: 'asc', active: 'personLabel' }); this.filters$.next([]); this.checkingOptions = { enableAutoCheckAll: true, autoCheckAll: true }; this._checkedStats$.next(TestSessionManager.getEmptyStats()); this._sessionsStats$.next(TestSessionManager.getEmptyStats()); // this._commandResponses$.next(); this.monitor$ = this.bs.observeSessionsMonitor(groupName) .pipe( switchMap(sessions => zip( ...sessions .map(session => combineLatest([this.bookletService.getBooklet(session.bookletName), this._clock$]) .pipe( map(([booklet]) => TestSessionUtil.analyzeTestSession(session, booklet)) ) ) )) ); this._sessions$.next([]); combineLatest([this.sortBy$, this.filters$, this.monitor$]) .pipe( // eslint-disable-next-line max-len map(([sortBy, filters, sessions]) => this.sortSessions(sortBy, TestSessionManager.filterSessions(sessions, filters))), tap(sessions => this.synchronizeChecked(sessions)) ) .subscribe(this._sessions$); } disconnect(): void { this.groupName = ''; this.bs.cutConnection(); } switchFilter(filterId: string): void { this.filterOptions[filterId].selected = !this.filterOptions[filterId].selected; this.refreshFilters(); } refreshFilters(): void { this.filters$.next(Object.values(this.filterOptions) .filter(filterOption => filterOption.selected) .map(filterOption => filterOption.filter) ); } resetFilters(): void { this.filterOptions = {}; Object.keys(TestSessionManager.basicFilters) .forEach(key => { this.filterOptions[key] = { selected: TestSessionManager.basicFilters[key].selected, source: TestSessionManager.basicFilters[key].source, filter: { ...TestSessionManager.basicFilters[key].filter } }; }); } private static filterSessions(sessions: TestSession[], filters: TestSessionFilter[]): TestSession[] { return sessions .filter(session => session.data.testId && session.data.testId > -1) // testsession without testId is deprecated .filter(session => !TestSessionManager.applyFilters(session, filters)); } private static applyFilters(session: TestSession, filters: TestSessionFilter[]): boolean { const regexTest = (regex: string, value: string): boolean => { try { return new RegExp(regex).test(value); } catch (e) { return false; } }; // eslint-disable-next-line @typescript-eslint/no-shadow const apply = (subject: string, filter: TestSessionFilter, inverted: boolean = false): boolean => { if (filter.not && !inverted) return !apply(subject, filter, true); if (Array.isArray(filter.value)) return filter.value.includes(subject); switch (filter.type) { case 'substring': return subject.includes(filter.value); case 'equal': return subject === filter.value; case 'regex': return regexTest(filter.value, subject); default: return false; } }; const filterOut: TestSessionFilter | undefined = filters .find((nextFilter: TestSessionFilter) => { switch (nextFilter.target) { case 'groupName': case 'personLabel': case 'mode': return apply(session.data[nextFilter.target] || '', nextFilter); case 'bookletId': return apply(session.data.bookletName || '', nextFilter); // bookletId is clearer for XML-authors case 'unitId': return apply(session.data.unitName || '', nextFilter); // unitId is clearer for XML-authors case 'unitLabel': return apply(session.current?.unit?.label || '', nextFilter); case 'bookletLabel': return apply('metadata' in session.booklet ? session.booklet.metadata.label : '', nextFilter); case 'blockId': return apply(session.current?.ancestor?.blockId || '', nextFilter); case 'blockLabel': return apply(session.current?.ancestor?.label || '', nextFilter); case 'testState': { Iif (Array.isArray(nextFilter.value)) return filterOut; const keyExists = (typeof session.data.testState[nextFilter.value] !== 'undefined'); const valueMatches = keyExists && (session.data.testState[nextFilter.value] === nextFilter.subValue); const testStateMatching = (typeof nextFilter.subValue !== 'undefined') ? valueMatches : keyExists; return (nextFilter.not ? !testStateMatching : testStateMatching); } case 'state': { return apply(session.state, nextFilter); } case 'bookletSpecies': { return apply(session.booklet.species || '', nextFilter); } default: return false; } }); return typeof filterOut !== 'undefined'; } private static getEmptyStats(): TestSessionSetStats { return { ...{ all: false, number: 0, differentBookletSpecies: 0, differentBooklets: 0, paused: 0, locked: 0 } }; } private synchronizeChecked(sessions: TestSession[]): void { const sessionsStats = TestSessionManager.getSessionSetStats(sessions); this.checkingOptions.enableAutoCheckAll = (sessionsStats.differentBookletSpecies < 2); if (!this.checkingOptions.enableAutoCheckAll) { this.checkingOptions.autoCheckAll = false; } const newCheckedSessions: { [sessionFullId: number]: TestSession } = {}; sessions .forEach(session => { Iif (this.checkingOptions.autoCheckAll || (typeof this._checked[session.data.testId] !== 'undefined')) { newCheckedSessions[session.data.testId] = session; } }); this._checked = newCheckedSessions; this._checkedStats$.next(TestSessionManager.getSessionSetStats(Object.values(this._checked), sessions.length)); this._sessionsStats$.next(sessionsStats); } sortSessions(sort: Sort, sessions: TestSession[]): TestSession[] { return sessions .sort((session1, session2) => { const sortDirectionFactor = (sort.direction === 'asc' ? 1 : -1); if (sort.active === 'timestamp') { return (session1.data.timestamp - session2.data.timestamp) * sortDirectionFactor; } if (sort.active === '_checked') { const session1isChecked = this.isChecked(session1); const session2isChecked = this.isChecked(session2); if (!session1isChecked && session2isChecked) { return 1 * sortDirectionFactor; } if (session1isChecked && !session2isChecked) { return -1 * sortDirectionFactor; } return 0; } if (sort.active === '_superState') { return (TestSessionsSuperStates.indexOf(session1.state) - TestSessionsSuperStates.indexOf(session2.state)) * sortDirectionFactor; } if (sort.active === '_currentBlock') { const s1curBlock = session1.current?.ancestor?.blockId ?? 'zzzzzzzzzz'; const s2curBlock = session2.current?.ancestor?.blockId ?? 'zzzzzzzzzz'; return s1curBlock.localeCompare(s2curBlock) * sortDirectionFactor; } if (sort.active === '_currentUnit') { const s1currentUnit = session1.current?.unit?.label ?? 'zzzzzzzzzz'; const s2currentUnit = session2.current?.unit?.label ?? 'zzzzzzzzzz'; return s1currentUnit.localeCompare(s2currentUnit) * sortDirectionFactor; } let valA = session1.data[sort.active as keyof typeof session1.data] ?? 'zzzzzzzzzz'; let valB = session2.data[sort.active as keyof typeof session2.data] ?? 'zzzzzzzzzz'; Iif (typeof valA === 'number') { valA = valA.toString(10); } Iif (typeof valB === 'number') { valB = valB.toString(10); } Iif ((typeof valA === 'object') || (typeof valB === 'object')) { return 0; } return valA.localeCompare(valB) * sortDirectionFactor; }); } testCommandPause(): void { const testIds = this.checked .filter(session => !TestSessionUtil.isPaused(session)) .filter(session => !['pending', 'locked'].includes(session.state)) .map(session => session.data.testId); Iif (!testIds.length) { this._commandResponses$.next({ commandType: 'pause', testIds }); return; } this.bs.command('pause', [], testIds).subscribe( response => this._commandResponses$.next(response) ); } testCommandResume(): void { const testIds = this.checked .filter(session => !['pending', 'locked'].includes(session.state)) .map(session => session.data.testId); Iif (!testIds.length) { this._commandResponses$.next({ commandType: 'resume', testIds }); return; } this.bs.command('resume', [], testIds).subscribe( response => this._commandResponses$.next(response) ); } testCommandGoto(selection: Selected): Observable<true> { const gfd = TestSessionManager.groupForGoto(this.checked, selection); const allTestIds = this.checked.map(s => s.data.testId); return zip( ...Object.keys(gfd).map(key => this.bs.command('goto', ['id', gfd[key].firstUnitId], gfd[key].testIds)) ).pipe( tap(() => { this._commandResponses$.next({ commandType: 'goto', testIds: allTestIds }); }), map(() => true) ); } private static groupForGoto(sessionsSet: TestSession[], selection: Selected): GotoCommandData { const groupedByBooklet: GotoCommandData = {}; sessionsSet.forEach(session => { if ( session.data.bookletName && !Object.keys(groupedByBooklet).includes(session.data.bookletName) && isBooklet(session.booklet) ) { const firstUnit = selection.element?.blockId ? BookletUtil.getFirstUnitOfBlock(selection.element.blockId, session.booklet) : null; if (firstUnit) { groupedByBooklet[session.data.bookletName] = { testIds: [], firstUnitId: firstUnit.id }; } } if (session.data.bookletName && Object.keys(groupedByBooklet).includes(session.data.bookletName)) { groupedByBooklet[session.data.bookletName].testIds.push(session.data.testId); } }); return groupedByBooklet; } testCommandUnlock(): void { const testIds = this.checked .filter(TestSessionUtil.isLocked) .map(session => session.data.testId); Iif (!testIds.length) { this._commandResponses$.next({ commandType: 'unlock', testIds }); return; } this.bs.unlock(this.groupName, testIds).subscribe( response => this._commandResponses$.next(response) ); } // todo unit test commandFinishEverything(): Observable<CommandResponse> { const getUnlockedConnectedTestIds = () => Object.values(this._sessions$.getValue()) .filter(session => !['pending', 'locked'].includes(session.state) && !TestSessionUtil.hasState(session.data.testState, 'CONTROLLER', 'TERMINATED') && (TestSessionUtil.hasState(session.data.testState, 'CONNECTION', 'POLLING') || TestSessionUtil.hasState(session.data.testState, 'CONNECTION', 'WEBSOCKET'))) .map(session => session.data.testId); const getUnlockedTestIds = () => Object.values(this._sessions$.getValue()) .filter(session => session.data.testId > 0) .filter(session => !['pending', 'locked'].includes(session.state)) .map(session => session.data.testId); this.filters$.next([]); return this.bs.command('terminate', ['lock'], getUnlockedConnectedTestIds()) .pipe( delay(1500), flatMap(() => this.bs.lock(this.groupName, getUnlockedTestIds())) ); } isChecked(session: TestSession): boolean { return (typeof this._checked[session.data.testId] !== 'undefined'); } checkSessionsBySelection(selected: Selected): void { Iif (this.checkingOptions.autoCheckAll) { return; } let toCheck: TestSession[] = []; if (selected.element) { if (!selected.spreading) { toCheck = [...this.checked, selected.originSession]; } else { toCheck = this._sessions$.getValue() .filter(session => (!['pending', 'locked'].includes(session.state))) .filter(session => (session.booklet.species === selected.originSession.booklet.species)) .filter(session => (selected.inversion ? !this.isChecked(session) : true)); } } this.replaceCheckedSessions(toCheck); } invertChecked(): void { Iif (this.checkingOptions.autoCheckAll) { return; } Iif (!this._sessions$) { return; } const unChecked = this._sessions$.getValue() .filter(session => session.data.testId && session.data.testId > -1) .filter(session => (!['pending', 'locked'].includes(session.state))) .filter(session => !this.isChecked(session)); this.replaceCheckedSessions(unChecked); } checkSession(session: TestSession): void { Iif (this.checkingOptions.autoCheckAll) { return; } this._checked[session.data.testId] = session; this.onCheckedChanged(); } uncheckSession(session: TestSession): void { Iif (this.checkingOptions.autoCheckAll) { return; } Iif (this.isChecked(session)) { delete this._checked[session.data.testId]; } this.onCheckedChanged(); } checkAll(): void { Iif (this.checkingOptions.autoCheckAll) { return; } const allSelectable = this._sessions$.getValue() .filter(session => (!['pending', 'locked'].includes(session.state))); this.replaceCheckedSessions(allSelectable); } checkNone(): void { this.replaceCheckedSessions([]); } private replaceCheckedSessions(sessionsToCheck: TestSession[]): void { const newCheckedSessions: { [testId: string]: TestSession } = {}; sessionsToCheck .forEach(session => { newCheckedSessions[session.data.testId] = session; }); this._checked = newCheckedSessions; this.onCheckedChanged(); } private onCheckedChanged(): void { this._checkedStats$.next(TestSessionManager.getSessionSetStats(this.checked, this.sessions.length)); } private static getSessionSetStats(sessionSet: TestSession[], all: number = sessionSet.length): TestSessionSetStats { const booklets = new Set(); const bookletSpecies = new Set(); let paused = 0; let locked = 0; sessionSet .forEach(session => { booklets.add(session.data.bookletName); bookletSpecies.add(session.booklet.species); if (TestSessionUtil.isPaused(session)) paused += 1; Iif (TestSessionUtil.isLocked(session)) locked += 1; }); return { number: sessionSet.length, differentBooklets: booklets.size, differentBookletSpecies: bookletSpecies.size, all: (all === sessionSet.length), paused, locked }; } } |