All files / tools/ReFlex.TrackingServer/ClientApp/src/app/processing processing.component.ts

67.24% Statements 78/116
57.69% Branches 15/26
54.54% Functions 24/44
67.25% Lines 76/113

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 3671x                                                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                       1x               3x 3x   3x 3x   3x   3x   3x       3x 3x   3x         3x 3x 3x 3x 3x 3x                             3x   1x 1x   1x     1x           3x         3x               3x         2x               3x         2x               3x 3x       3x 3x     3x       2x       2x             3x         2x               3x         2x                   3x 3x 3x 3x 3x 3x 3x                                                                                                                                                       2x         2x                           1x       1x                   3x           2x 2x 2x         2x 2x   2x       2x   2x 2x           2x         2x 2x 2x      
import { CommonModule } from '@angular/common';
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  OnDestroy,
  OnInit,
  ViewChild,
  inject
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import {
  OptionCheckboxComponent,
  PanelHeaderComponent,
  ValueSelectionComponent,
  ValueSliderComponent,
  ValueTextComponent
} from '@reflex/angular-components/dist';
import {
  CompleteInteractionData,
  DEFAULT_SETTINGS,
  Interaction,
  ObserverType,
  ProcessingSettings,
  RemoteProcessingServiceSettings,
  TrackingServerAppSettings
} from '@reflex/shared-types';
import { NEVER, Observable, Subscription } from 'rxjs';
import { concatMap, map, startWith, switchMap, tap } from 'rxjs/operators';
import { CalibrationService } from 'src/shared/services/calibration.service';
import { ProcessingService } from 'src/shared/services/processing.service';
import { SettingsService } from 'src/shared/services/settingsService';
import { BASE_URL } from 'src/shared/tokens/base-url.token';
import { markViewForCheck } from 'src/shared/util/mark-view-for-check';
import { LogService } from '../log/log.service';
import { HistoryComponent } from './history/history.component';
import { InteractionsVisualizationComponent } from './interactions-visualization/interactions-visualization.component';
import { InteractionsComponent } from './interactions/interactions.component';
 
@Component({
  selector: 'app-processing',
  templateUrl: './processing.component.html',
  styleUrls: ['./processing.component.scss'],
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [
    CommonModule,
    FormsModule,
    PanelHeaderComponent,
    ValueSelectionComponent,
    ValueSliderComponent,
    ValueTextComponent,
    OptionCheckboxComponent,
    InteractionsComponent,
    HistoryComponent,
    InteractionsVisualizationComponent
  ]
})
export class ProcessingComponent implements OnInit, OnDestroy {
 
  @ViewChild('visualizationContainer')
  public visualization?: InteractionsVisualizationComponent;
 
  @ViewChild('interactionsList')
  public interactionsList?: InteractionsComponent;
 
  public statusText = '';
  public eventId = 0;
 
  public selectedProcessorIdx = -1;
  public interval = 0;
 
  public processors: Array<string> = [];
 
  public isInteractionProcessingActive = false;
 
  public remoteSettings: RemoteProcessingServiceSettings = {
    ...DEFAULT_SETTINGS.remoteProcessingServiceSettingsValues
  };
 
  public remoteAddress = '';
  public remotePort = 0;
 
  public processingSettings: ProcessingSettings = {
    interactionType: ObserverType.None,
    intervalDuration: 0
  };
 
  private readonly changeDetector = inject(ChangeDetectorRef);
  private readonly baseUrl = inject(BASE_URL);
  private readonly settingsService = inject(SettingsService);
  private readonly processingService = inject(ProcessingService);
  private readonly calibrationService = inject(CalibrationService);
  private readonly logService = inject(LogService);
 
  private selectedProcessorSubscription?: Subscription;
  private intervalSubscription?: Subscription;
  private interactionsSubscription?: Subscription;
  private observerSubscription?: Subscription;
  private remoteSettingsSubscription?: Subscription;
  private saveSettingsSubscription?: Subscription;
 
  // private readonly settingsSubscription?: Subscription;
  private statusSubscription?: Subscription;
 
  private readonly saveSettings$: Observable<TrackingServerAppSettings>;
 
  public constructor() {
    this.saveSettings$ = this.settingsService.getSettings().pipe(
      map((result) => {
        const settings = result;
        result.remoteProcessingServiceSettingsValues = this.remoteSettings;
 
        return settings;
      }),
      tap((updatedSettings: TrackingServerAppSettings) => {
        this.settingsService.saveSettings(updatedSettings);
      })
    );
  }
 
  public ngOnInit(): void {
    this.statusSubscription = this.processingService
      .getStatus()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          this.statusText = result;
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.observerSubscription = this.processingService
      .getObserverTypes()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          this.processors = result;
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.selectedProcessorSubscription = this.processingService
      .getSelectedObserverType()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          this.updateProcessor(result);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    const interactions$ = this.processingService.getInteractions();
    this.interactionsSubscription = this.processingService
      .getStatus()
      .pipe(
        tap((processing) => {
          this.isInteractionProcessingActive = processing === 'Active';
          this.updateStatusText();
        }),
        switchMap((processing) =>
          processing
            ? interactions$
            : NEVER.pipe<Array<Interaction>>(startWith([]))),
        concatMap((raw) =>
          this.calibrationService.computeCalibratedAbsolutePosition(raw))
      )
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => this.updateInteractions(result),
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.intervalSubscription = this.processingService
      .getInterval()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          this.updateInterval(result);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.remoteSettingsSubscription = this.processingService
      .getRemoteProcessorSettings()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          this.updateRemoteSettings(result);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public ngOnDestroy(): void {
    this.statusSubscription?.unsubscribe();
    this.selectedProcessorSubscription?.unsubscribe();
    this.intervalSubscription?.unsubscribe();
    this.interactionsSubscription?.unsubscribe();
    this.observerSubscription?.unsubscribe();
    this.remoteSettingsSubscription?.unsubscribe();
    this.saveSettingsSubscription?.unsubscribe();
  }
 
  public isInteractionProcessingActiveChanged(): void {
    this.processingService
      .toggleProcessing()
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          console.log(
            `Processing status toggle - result:  ${result.status} - ${result.body?.value}`
          );
          this.isInteractionProcessingActive = result.body?.value as boolean;
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public setProcessor(): void {
    this.processingService
      .setObserverType(this.processors[this.selectedProcessorIdx])
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          console.log(
            `response to change Observer Type: ${result.status} - ${result.body?.value}`
          );
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public setInterval(): void {
    this.processingService
      .setInterval(this.interval)
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          console.log(
            `Response to change interval request: ' + ${result.status} - Value: ${result.body?.value}`
          );
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public setAddress(): void {
    this.updateAddress();
  }
 
  public setPort(): void {
    this.updateAddress();
  }
 
  public updateAddress(): void {
    const completeAddress = `${this.remoteAddress}:${this.remotePort}`;
    if (!URL.canParse(completeAddress)) {
      console.warn(`Invalid URL: ${completeAddress}`);
 
      return;
    }
 
    this.remoteSettings.address = completeAddress;
    this.updateSettings();
  }
 
  public updateSettings(): void {
    this.processingService
      .setRemoteProcessorSettings(this.remoteSettings)
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          console.log(
            `Response to set remote processing settings request: ' + ${
              result.status
            } - Value: ${JSON.stringify(result.body)}`
          );
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public saveRemoteProcessingSettings(): void {
    this.saveSettingsSubscription = this.saveSettings$
      .pipe(markViewForCheck(this.changeDetector))
      .subscribe(
        (result) => {
          console.log(`saved settings:  ${JSON.stringify(result)}`);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  private updateStatusText(): void {
    this.statusText = this.isInteractionProcessingActive
      ? 'Active'
      : 'Inactive';
  }
 
  private updateProcessor(idx: number): void {
    this.selectedProcessorIdx = idx;
    Eif (this.processingSettings !== undefined && this.processingSettings !== null) {
      this.processingSettings.interactionType = idx;
    }
  }
 
  private updateInterval(interval: number): void {
    Eif (this.processingSettings !== undefined && this.processingSettings !== null) {
      this.processingSettings.intervalDuration = interval;
    }
    this.interval = interval;
  }
 
  private updateInteractions(interactions: CompleteInteractionData): void {
    this.eventId++;
 
    this.interactionsList?.updateInteractions(interactions);
    this.visualization?.updateCalibratedInteractions(interactions);
  }
 
  private updateRemoteSettings(
    settings: RemoteProcessingServiceSettings
  ): void {
    this.remoteSettings = {
      ...settings,
      useGzipCompression: settings.useGzipCompression ?? true
    };
 
    const url = new URL(this.remoteSettings.address);
    this.remoteAddress = `${url.protocol}//${url.hostname}`;
    this.remotePort = Number.parseFloat(url.port);
  }
}