All files / src/app/services init.service.ts

85.48% Statements 53/62
68.75% Branches 11/16
100% Functions 14/14
83.63% Lines 46/55

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                220x   110x 110x   110x   110x 110x 110x     110x 110x 110x 110x 110x     110x     110x   110x 110x   110x 109x   109x 109x 109x 109x                 110x 110x 110x 109x               110x 110x 110x         110x 107x         107x     110x 110x 110x 110x         110x 110x 220x 217x       107x     220x 220x 220x       217x 217x          
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { MatomoInitializerService } from 'ngx-matomo-client';
import { firstValueFrom, Subject } from 'rxjs';
 
@Injectable({
  providedIn: 'root'
})
export class InitService {
 
  apiUrl = "";
  isUrlFound$: Subject<boolean> = new Subject();
 
  constructor(private http: HttpClient) { }
 
  async initUrls() {
    this.createMatomoConfig();
    return this.findApiUrl();
  }
 
  async findApiUrl() {
    const protocol = window.location.protocol;
    const hostname = window.location.hostname.toLowerCase();
    const apiUrls = [];
    Iif (hostname !== "localhost" && hostname.includes('.')) {
      apiUrls.push(protocol + "//api." + hostname);
    }
    Iif (hostname !== "frontend-service") {
      apiUrls.push(protocol + "//" + hostname + ':5001');
    }
    apiUrls.push(protocol + "//backend-service"); // For ci
 
    const apiUrl: string = "";
    for (const url of apiUrls) {
 
      const isUrlValid = await this.checkApiUrl(url);
      Eif (isUrlValid) {
        // this.apiUrl$.next(url);
        this.apiUrl = url;
        console.log('found api url', url);
        this.isUrlFound$.next(true);
        return url;
      }
 
    }
    this.isUrlFound$.next(false);
    return apiUrl;
 
  }
 
  async checkApiUrl(url: string): Promise<boolean> {
    try {
      const response = await firstValueFrom(this.http.get<any>(url)); // eslint-disable-line  @typescript-eslint/no-explicit-any
      return typeof response === 'object' && 'twinpad_version' in response;
    }
    catch (e) {
      console.error('catched', e);
      return false;
    }
  }
 
  async createMatomoConfig() {
    const matomoInitializer = inject(MatomoInitializerService);
    const config = {
      siteId: 1,
      trackerUrl: "",
    };
 
    const foundUrl = await this.findAnalyticsUrl();
    Iif (foundUrl) {
      config.trackerUrl = foundUrl;
    }
 
    // if no matomo found, the initialization will fail gracefully
    matomoInitializer.initializeTracker(config);
  }
 
  async findAnalyticsUrl() {
    const protocol = window.location.protocol;
    const hostname = window.location.hostname.toLowerCase();
    const analyticsUrls = [
      protocol + "//analytics." + hostname,
      protocol + "//" + hostname + ":22604"
    ];
 
    const analyticsUrl: string = "";
    for (const url of analyticsUrls) {
      const isUrlValid = await this.checkAnalyticsUrl(url + "/matomo.js");
      Iif (isUrlValid) {
        return url;
      }
    }
    return analyticsUrl;
  }
 
  async checkAnalyticsUrl(url: string): Promise<boolean> {
    try {
      const response = await firstValueFrom(this.http.get(url, { responseType: "text" }));
      return response.includes("tracking client");
    }
    catch (e) {
      console.warn('Error fetching analytics url:', e);
      return false;
    }
  }
 
}