<commit_before>import {Component} from '@angular/core';
import {JobGroupDTO} from "../shared/model/job-group.model";
import {ActivatedRoute, Router} from '@angular/router';
import {JobGroupService} from "../shared/service/rest/job-group.service";
import {combineLatest, Observable} from 'rxjs';
import {filter, map} from 'rxjs/operators';
import {ApplicationDashboardService} from "./service/application-dashboard.service";

@Component({
  selector: 'osm-application-dashboard',
  templateUrl: './application-dashboard.component.html',
  styleUrls: ['./application-dashboard.component.css']
})
export class ApplicationDashboardComponent {
  jobGroups$: Observable<JobGroupDTO[]>;
  selectedApplication$: Observable<JobGroupDTO>;

  constructor(private jobGroupService: JobGroupService, private route: ActivatedRoute, private router: Router, private dashboardService: ApplicationDashboardService) {
    this.jobGroups$ = jobGroupService.activeOrRecentlyMeasured$.pipe(
      map((jobGroups: JobGroupDTO[]) => this.sortJobGroupsByName(jobGroups))
    );

    this.selectedApplication$ = combineLatest(this.jobGroups$, this.route.paramMap).pipe(
      map(([jobGroups, params]) => this.lookForJobGroupWithId(jobGroups, params.get('jobGroupId'))),
      filter(jobGroup => !!jobGroup)
    );

    this.handleInvalidNavigation();
  }

  private handleInvalidNavigation() {
    combineLatest(this.jobGroups$, this.route.paramMap).subscribe(([jobGroups, params]) => {
      if (this.lookForJobGroupWithId(jobGroups, params.get('jobGroupId'))) {
        return;
      }
      if (!params.get('jobGroupId')) {
        this.updateApplication(jobGroups[0])
      }
    });
  }

  updateApplication(jobGroup: JobGroupDTO) {
    this.router.navigate(['/application-dashboard', jobGroup.id]);
    this.dashboardService.updateMetricsForApplication(jobGroup.id);
  }

  private lookForJobGroupWithId(jobGroups: JobGroupDTO[], jobGroupId: string) {
    return jobGroups.find(jobGroup => jobGroup.id == Number(jobGroupId));
  }

  private sortJobGroupsByName(jobGroups: JobGroupDTO[]) {
    return jobGroups.sort((a, b) => a.name.localeCompare(b.name, [], {sensitivity: "base"}))
  }
}<commit_msg>[IT-2320] work-around for getting pagemetrics on navigate<commit_after>import {Component} from '@angular/core';
import {JobGroupDTO} from "../shared/model/job-group.model";
import {ActivatedRoute, Router} from '@angular/router';
import {JobGroupService} from "../shared/service/rest/job-group.service";
import {combineLatest, Observable} from 'rxjs';
import {filter, map} from 'rxjs/operators';
import {ApplicationDashboardService} from "./service/application-dashboard.service";

@Component({
  selector: 'osm-application-dashboard',
  templateUrl: './application-dashboard.component.html',
  styleUrls: ['./application-dashboard.component.css']
})
export class ApplicationDashboardComponent {
  jobGroups$: Observable<JobGroupDTO[]>;
  selectedApplication$: Observable<JobGroupDTO>;

  constructor(private jobGroupService: JobGroupService, private route: ActivatedRoute, private router: Router, private dashboardService: ApplicationDashboardService) {
    this.jobGroups$ = jobGroupService.activeOrRecentlyMeasured$.pipe(
      map((jobGroups: JobGroupDTO[]) => this.sortJobGroupsByName(jobGroups))
    );

    this.handleValidNavigation();
    this.handleInvalidNavigation();
  }

  private handleValidNavigation() {
    this.selectedApplication$ = combineLatest(this.route.paramMap, this.jobGroups$).pipe(
      map(([params, jobGroups]) => this.lookForJobGroupWithId(jobGroups, params.get('jobGroupId'))),
      filter(jobGroup => !!jobGroup)
    ).pipe(map(jobGroup => {
      this.dashboardService.updateMetricsForApplication(jobGroup.id);
      return jobGroup
    }))

  }

  private handleInvalidNavigation() {
    combineLatest(this.jobGroups$, this.route.paramMap).subscribe(([jobGroups, params]) => {
      if (this.lookForJobGroupWithId(jobGroups, params.get('jobGroupId'))) {
        return;
      }
      if (!params.get('jobGroupId')) {
        this.updateApplication(jobGroups[0])
      }
    });
  }

  updateApplication(jobGroup: JobGroupDTO) {
    this.dashboardService.updateMetricsForApplication(jobGroup.id);
    this.router.navigate(['/application-dashboard', jobGroup.id]);
  }

  private lookForJobGroupWithId(jobGroups: JobGroupDTO[], jobGroupId: string) {
    let selectedApplication = jobGroups.find(jobGroup => jobGroup.id == Number(jobGroupId));
    return selectedApplication
  }

  private sortJobGroupsByName(jobGroups: JobGroupDTO[]) {
    return jobGroups.sort((a, b) => a.name.localeCompare(b.name, [], {sensitivity: "base"}))
  }
}
{'fraction_non_alphanumeric': 0.13959991395999138, 'fraction_numerical': 0.001290600129060013, 'mean_word_length': 5.2, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1696189', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1365, 'n_words': 230}