Update on the tunnel flight

This commit is contained in:
Sébastien ANDRE
2023-08-16 17:39:24 +02:00
parent dc06f256b4
commit fe7ffa9016
6 changed files with 108 additions and 45 deletions

View File

@@ -11,16 +11,20 @@
<mat-radio-button value="12Months">{{ 'ListTunnelFlight_12Months' | translate }}</mat-radio-button>
</mat-radio-group>
<canvas baseChart style="width: 50%;"
[data]="barChartData"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[type]="barChartType">
</canvas>
<div style="display: block; width: 55%;">
<canvas baseChart
[data]="barChartData"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[type]="barChartType">
</canvas>
</div>
<div>
<table mat-table [dataSource]="dataSourceTable">
<button mat-raised-button color="accent" (click)="onLoadTable()" >{{ 'ListTunnelFlight_LoadTable' | translate }}</button>
<table mat-table [dataSource]="dataSourceTable" *ngIf="dataSourceTable?.data.length">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef>ID</th>
<td mat-cell *matCellDef="let element">

View File

@@ -7,7 +7,7 @@ import { ChartConfiguration, ChartData } from 'chart.js';
import { ServiceComm } from '../../services/service-comm.service';
import { TunnelFlightService } from "../../services/tunnel-flight.service";
import { DateService } from '../../services/date.service';
import { TunnelFlight } from '../../models/tunnel-flight';
import { TunnelFlight, TunnelFlightByMonth } from '../../models/tunnel-flight';
@Component({
selector: 'app-list-of-tunnel-flights',
@@ -32,9 +32,9 @@ export class ListOfTunnelFlightsComponent implements OnInit {
];
constructor(private serviceComm: ServiceComm,
private serviceTunnelFlight: TunnelFlightService,
private translateService: TranslateService,
private dateService: DateService) { }
private serviceTunnelFlight: TunnelFlightService,
private translateService: TranslateService,
private dateService: DateService) { }
ngOnInit() {
this.serviceComm.forceTranslateTitle.subscribe((data) => {
@@ -46,14 +46,25 @@ export class ListOfTunnelFlightsComponent implements OnInit {
this.chartConfig();
this.selectedPeriod = "currentYear"
this.getData();
this.getDataForGraph();
}
public onPeriodChange() {
this.getDataForGraph();
if (this.dataSourceTable?.data.length > 0){
this.getDataForTable();
}
}
public onLoadTable() {
this.getDataForTable();
}
private chartConfig() {
this.barChartType = "bar";
this.barChartOptions = {
responsive: false,
responsive: true,
plugins: {
legend: {
display: true
@@ -86,7 +97,7 @@ export class ListOfTunnelFlightsComponent implements OnInit {
);
}
private getData(): void {
private getDataForTable(): void {
this.isLoading = true;
// Get data to show in a table
@@ -103,15 +114,34 @@ export class ListOfTunnelFlightsComponent implements OnInit {
break;
}
let tunnelFlights: Array<TunnelFlight> = [];
this.serviceTunnelFlight.getTunnelFlights(beginDate, endDate)
.subscribe((data) => {
tunnelFlights = data;
this.dataSourceTable.data = data;
this.isLoading = false;
});
}
// Format data to show a chart
private getDataForGraph(): void {
this.isLoading = true;
// Get data to show in a table
let endDate = new Date();
let beginDate = new Date();
endDate.setHours(0, 0, 0, 0);
switch (this.selectedPeriod) {
case "currentYear":
beginDate = new Date(endDate.getFullYear(), 0, 1);
break;
case "12Months":
beginDate = this.dateService.addMonths(endDate, -12);
beginDate.setDate(1);
break;
}
this.serviceTunnelFlight.getTunnelFlightsByMonth(beginDate, endDate)
.subscribe((data) => {
const allMonths = this.getMontsBetweenDates(beginDate, endDate)
const cumulatedTime = this.getCumulatedTimeByMonth(tunnelFlights, allMonths);
const cumulatedTime = this.getCumulatedTimeByMonth(data, allMonths);
this.barChartData = {
labels: allMonths,
datasets: [
@@ -123,17 +153,30 @@ export class ListOfTunnelFlightsComponent implements OnInit {
});
}
private getCumulatedTimeByMonth(tunnelFlights: TunnelFlight[], allMonths: string[]): Array<number> {
private getMontsBetweenDates(beginDate: Date, endDate: Date): Array<string> {
let results: Array<string> = [];
let tmpBeginDate = new Date(beginDate.getTime());
const tmpEndDate = new Date(endDate.getTime());
while (tmpBeginDate < tmpEndDate) {
results.push(formatDate(tmpBeginDate, "yy-MM", "en"));
tmpBeginDate.setMonth(tmpBeginDate.getMonth() + 1);
}
return results;
}
private getCumulatedTimeByMonth(stats: TunnelFlightByMonth[], allMonths: string[]): Array<number> {
let results: Array<number> = [];
for (let i = 0; i < allMonths.length; i++) {
const month = allMonths[i];
let tmpTunnelFlights = tunnelFlights.filter((d) => d.flightMonth == month);
let tmp = stats.filter((d) => d.month == month);
let sum = 0;
tmpTunnelFlights.map((data) => {
sum += data.nbMinutes;
});
if (tmp.length == 1){
sum = tmp[0].nb;
}
results.push(sum);
}
@@ -150,22 +193,4 @@ export class ListOfTunnelFlightsComponent implements OnInit {
return 'Sum: ' + sum;
};
public onPeriodChange() {
this.getData();
}
private getMontsBetweenDates(beginDate: Date, endDate: Date): Array<string> {
let results: Array<string> = [];
let tmpBeginDate = new Date(beginDate.getTime());
const tmpEndDate = new Date(endDate.getTime());
while (tmpBeginDate < tmpEndDate) {
results.push(formatDate(tmpBeginDate, "yy-MM", "en"));
tmpBeginDate.setMonth(tmpBeginDate.getMonth() + 1);
}
return results;
}
}