Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Feature/historical data preselect #63

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ const routes: Routes = [
{
path: "",
component: ScenarioHistoricaldataComponent
}];
},
{
path: ":tmid",
component: ScenarioHistoricaldataComponent
}
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ScenarioHistoricaldataRoutingModule { }
export class ScenarioHistoricaldataRoutingModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
Clicking on a data source that can be visualized will show the visualization diagram.
Since this API provides data from the past, it is also possible to retrieve data from machines which are currently offline.">
<div>
<app-select-machine (change)="selectedMachineChanged($event)"></app-select-machine>
<app-select-machine (change)="selectedMachineChanged($event)" [initialMachineId]="selectedMachineId">
</app-select-machine>
<app-date-range (change)="dateRangeChanged($event)"></app-date-range>

<ng-select class="source-key-select" *ngIf="(sourceKeys$ | async) as sourceKeys" [items]="sourceKeys.keys"
placeholder="Please select a source key" [clearable]="false" [searchable]="false"
[dropdownPosition]="'bottom'" (change)="sourceKeySelectChanged($event)">
[dropdownPosition]="'bottom'" [(ngModel)]="selectedSourceKey" (change)="sourceKeySelectChanged($event)">
</ng-select>

<div *ngIf="(sourceKeys$ | async) as sourceKeys; else loadingOrError">
<div class="columns">
<div class="column">
<div class="sourceKeyData full-height" *ngIf="lineSeriesData; else loadingOrError">
<ngx-charts-line-chart class="historicalDataChart" [xAxis]="true" [yAxis]="true"
[showGridLines]="true" [roundDomains]="true" [autoScale]="true" [results]="lineSeriesData"
[scheme]="lineChartScheme">
</ngx-charts-line-chart>
</div>
<div class="columns">
<div class="column">
<div class="sourceKeyData full-height" *ngIf="lineSeriesData; else loadingOrError">
<ngx-charts-line-chart class="historicalDataChart" [xAxis]="true" [yAxis]="true"
[showGridLines]="true" [roundDomains]="true" [autoScale]="true" [results]="lineSeriesData"
[scheme]="lineChartScheme">
</ngx-charts-line-chart>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import { DebugElement } from "@angular/core";
import { of, throwError } from "rxjs";
import { SourceKeys } from "./source-keys.model";
import { RouterTestingModule } from "@angular/router/testing";
import { Router } from "@angular/router";

const sourceKeysMock: SourceKeys = {
tmid: "1",
keys: ["Energy!AirPressure", "Energy!ElectricPower"]
};

describe("ScenarioHistoricaldataComponent", () => {
fdescribe("ScenarioHistoricaldataComponent", () => {
let component: ScenarioHistoricaldataComponent;
let fixture: ComponentFixture<ScenarioHistoricaldataComponent>;
let historicalDataService: HistoricalDataService;
let router: Router;
let element: DebugElement;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ScenarioHistoricaldataComponent],
imports: [SharedModule, HttpClientTestingModule, RouterTestingModule],
imports: [SharedModule, HttpClientTestingModule, RouterTestingModule.withRoutes([])],
providers: [HistoricalDataService]
}).compileComponents();
}));
Expand All @@ -33,20 +35,23 @@ describe("ScenarioHistoricaldataComponent", () => {
component = fixture.componentInstance;
element = fixture.debugElement;
historicalDataService = element.injector.get(HistoricalDataService);
router = element.injector.get(Router);
});

it("should create", () => {
expect(component).toBeTruthy();
});

it("should fetch data and display it", fakeAsync(async () => {
fit("should fetch data and display it", fakeAsync(async () => {
const getSourceKeysSpy = spyOn(historicalDataService, "getSourceKeys").and.returnValue(of(sourceKeysMock));
const routerNavigateSpy = spyOn(router, "navigate").and.returnValue(null);
fixture.detectChanges();
expect(getSourceKeysSpy).toHaveBeenCalledTimes(0);
const machineId = "1";
component.selectedMachineChanged(machineId);
fixture.detectChanges();

expect(routerNavigateSpy).toHaveBeenCalled();
expect(getSourceKeysSpy).toHaveBeenCalledTimes(1);
expect(getSourceKeysSpy).toHaveBeenCalledWith(machineId);
expect((await component.sourceKeys$.toPromise()).keys.length).toEqual(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HistoricalDataService } from "./scenario-historicaldata.service";
import { HistoricalDataResponseElement, HistoricItemData } from "./historical-data.model";
import { filter, concatMap, tap, map, catchError } from "rxjs/operators";
import * as moment from "moment";
import { Router, ActivatedRoute } from "@angular/router";

@Component({
selector: "app-scenario-historicaldata",
Expand All @@ -18,21 +19,33 @@ export class ScenarioHistoricaldataComponent implements OnInit {
limit: 1000
}
});
public loadingCriterias: any;
sourceKeys$: Observable<SourceKeys>;
error$ = new Subject<boolean>();
loading$ = new Subject<boolean>();
lineSeriesData: LineSeriesData[];
lineChartScheme = {
domain: ["#e3000b", "#0092b4", "#303741"]
};
public selectedMachineId: string;
public selectedSourceKey: string;

constructor(private readonly historicalDataService: HistoricalDataService) {
constructor(
private readonly historicalDataService: HistoricalDataService,
private readonly router: Router,
private readonly route: ActivatedRoute
) {
this.error$.next(false);
this.loading$.next(false);
}

ngOnInit() {
this.searchCriteria$
this.selectedMachineId = this.route.snapshot.params.tmid;
if (this.route.snapshot.queryParams.sourceKey) {
this.selectedSourceKey = this.route.snapshot.queryParams.sourceKey;
}

this.loadingCriterias = this.searchCriteria$
.pipe(
filter(this.filterIncompleteSearchCriteria()),
tap(this.setLoadingFlags()),
Expand Down Expand Up @@ -89,6 +102,10 @@ export class ScenarioHistoricaldataComponent implements OnInit {
}

public selectedMachineChanged(tmid: string) {
this.selectedMachineId = tmid;
this.lineSeriesData = null;
this.createRoute();

this.error$.next(false);
this.loading$.next(true);
this.sourceKeys$ = null;
Expand All @@ -107,6 +124,14 @@ export class ScenarioHistoricaldataComponent implements OnInit {
sourceKeys => {
this.sourceKeys$ = of(sourceKeys);
this.loading$.next(false);
if (this.selectedSourceKey) {
if (sourceKeys.keys.some(el => el === this.selectedSourceKey)) {
this.sourceKeySelectChanged(this.selectedSourceKey);
} else {
this.selectedSourceKey = null;
this.createRoute();
}
}
},
error => {
console.error("could not load sourceKeys", error);
Expand All @@ -132,6 +157,8 @@ export class ScenarioHistoricaldataComponent implements OnInit {
if (!key) {
return;
}
this.selectedSourceKey = key;
this.createRoute();
this.sourceKeySelected(key);
}

Expand Down Expand Up @@ -174,6 +201,14 @@ export class ScenarioHistoricaldataComponent implements OnInit {
);
};
}

private createRoute(): void {
this.router.navigate(["scenario-historicaldata", this.selectedMachineId], {
queryParams: {
sourceKey: this.selectedSourceKey
}
});
}
}

interface ScenarionHistoricaldataSearchCriteria {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ScenarioMachinestateComponent } from './scenario-machinestate.component';
import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { ScenarioMachinestateComponent } from "./scenario-machinestate.component";

const routes: Routes = [
{
path: '',
path: "",
component: ScenarioMachinestateComponent
},
{
path: ':tmid',
path: ":tmid",
component: ScenarioMachinestateComponent
}
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ScenarioMachinestateRoutingModule { }
export class ScenarioMachinestateRoutingModule {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="control">
<ng-select *ngIf="items$ | async; else loadingOrError" class="select-machine" [items]="items$ | async"
<ng-select *ngIf="(items$ | async) as items; else loadingOrError" class="select-machine" [items]="items"
groupBy="assignedMachines" bindLabel="displayName" placeholder="Please select a machine" [clearable]="false"
[searchable]="false" [dropdownPosition]="'bottom'" (change)="selectedMachineChanged($event)"
[(ngModel)]="selectedMachine">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ export class SelectMachineComponent implements OnInit {

ngOnInit() {
this.items$ = this.machineOverviewService.getSubscriptions().pipe(
tap(subscriptions => {
for (const subscription of subscriptions) {
for (const machine of subscription.assignedMachines) {
if (machine.tmid === this.initialMachineId) {
this.selectedMachine = machine;
tap(subscriptions => {
for (const subscription of subscriptions) {
for (const machine of subscription.assignedMachines) {
if (machine.tmid === this.initialMachineId) {
this.selectedMachine = machine;
this.selectedMachineChanged(this.selectedMachine);
}
}
}
}
}
}),
}),
catchError(err => {
this.error$.next(true);
this.loading$.next(false);
Expand Down