All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.app.core.services.campaign.service.ts Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-FileCopyrightText: 2017-2024 Enedis
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 */

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from '@env/environment';
import {
    Campaign,
    CampaignExecutionFullReport,
    CampaignExecutionReport,
    Dataset,
    Execution,
    ScenarioIndex
} from '@model';
import { HttpClient } from '@angular/common/http';
import { distinct } from '@shared/tools';

@Injectable({
    providedIn: 'root'
})
export class CampaignService {

    private resourceUrl = '/api/ui/campaign/v1';
    private ressourceUrlExecution = '/api/ui/campaign/execution/v1';

    constructor(private http: HttpClient) { }

    public static existRunningCampaignReport(lastCampaignReports: Array): boolean {
        return lastCampaignReports && distinct(lastCampaignReports.map(report => report.status)).includes('RUNNING');
    }

    findAllCampaigns(): Observable> {
        return this.http.get>(environment.backend + this.resourceUrl).pipe(map((res: Array) => {
            res.sort((a, b) => a.title.localeCompare(b.title));
            return res;
        }));
    }

    findLastCampaignReports(nbReports: number = 10): Observable> {
        return this.http.get>(environment.backend + this.resourceUrl + `/lastexecutions/${nbReports}`);
    }

    findAllScenarios(id: number): Observable> {
        return this.http.get>(environment.backend + this.resourceUrl + `/${id}/scenarios`)
            .pipe(map((res: Array) => {
                return res.map(s => new ScenarioIndex(
                    s.metadata.id,
                    s.metadata.title,
                    s.metadata.description,
                    s.metadata.repositorySource,
                    s.metadata.creationDate,
                    s.metadata.tags,
                    s.metadata.executions
                ));
            }));
    }

    findAllCampaignsForScenario(id: number): Observable> {
        return this.http.get>(environment.backend + this.resourceUrl + `/scenario/${id}`)
            .pipe(map((res: Array) => {
                return res;
            }));
    }

    find(id: number): Observable {
        return this.http.get(environment.backend + `${this.resourceUrl}/${id}`);
    }

    findExecution(id: number): Observable {
        return this.http.get(`${environment.backend}${this.resourceUrl}/execution/${id}`)
            .pipe(
                map(campaignExecutionFullReport => ({
                    ...campaignExecutionFullReport,
                    scenarioExecutionReports: Execution.deserializeExecutions(campaignExecutionFullReport.scenarioExecutionReports)
                }))
            );
    }

    create(campaign: Campaign): Observable {
        const copy = this.convert(campaign);
        return this.http.post(environment.backend + this.resourceUrl, copy);
    }

    delete(id: number): Observable {
        return this.http.delete(environment.backend + `${this.resourceUrl}/${id}`);
    }

    update(campaign: Campaign): Observable {
        const copy = this.convert(campaign);
        return this.http.put(environment.backend + this.resourceUrl, copy);
    }

    executeCampaign(campaignId: number, env: string, dataset?: Dataset): Observable {
        return this.http.post(environment.backend + `${this.ressourceUrlExecution}/byID/${campaignId}/${env}`, dataset != null ? dataset : {});
    }

    stopExecution(campaignId: number, executionId: number): Observable {
        return this.http.post(environment.backend +
            `${this.ressourceUrlExecution}/${executionId}/stop`, {}).pipe(map((res: Response) => {
            }));
    }

    replayFailedScenario(executionId: number): Observable {
        return this.http.post(environment.backend + `${this.ressourceUrlExecution}/replay/${executionId}`, null);
    }

    private convert(campaign: Campaign): Campaign {
        delete campaign.campaignExecutionReports;
        return Object.assign({}, campaign);
    }

}