Correction sur la mise en cache des

données référentiel et leurs utilisation
sur la liste des sauts.
This commit is contained in:
Sébastien André
2021-05-28 17:19:12 +02:00
parent dba69b938e
commit ba99a0f047
14 changed files with 143 additions and 79 deletions

View File

@@ -1,6 +1,18 @@
<mat-toolbar *ngIf="this.show()"> <mat-toolbar *ngIf="this.show()">
<button mat-icon-button (click)="snav.toggle()"><mat-icon>menu</mat-icon></button> <button mat-icon-button (click)="snav.toggle()"><mat-icon>menu</mat-icon></button>
<h2>{{ title }}</h2> <h2>{{ title }}</h2>
<mat-select (selectionChange)="switchLang($event)" [(value)]="selectedLanguageFlag"
style="width: 40px;" >
<mat-select-trigger>
<img src="{{ 'assets/img/' + selectedLanguageFlag + '.svg' }}" style="width: 15px;">
</mat-select-trigger>
<mat-option value="fr" style="width: 50px;">
<img src="assets/img/fr.svg" style="width: 15px;">
</mat-option>
<mat-option value="en" style="width: 50px;">
<img src="assets/img/en.svg" style="width: 15px;">
</mat-option>
</mat-select>
</mat-toolbar> </mat-toolbar>
<mat-sidenav-container> <mat-sidenav-container>
@@ -44,7 +56,7 @@
</mat-nav-list> </mat-nav-list>
<mat-nav-list *ngIf="currentUser"> <mat-nav-list *ngIf="currentUser">
<mat-icon aria-hidden="false" aria-label="To logout">logout</mat-icon> <mat-icon aria-hidden="false" aria-label="To logout">logout</mat-icon>
<span (click)="snav.toggle(); logout()" style="cursor: pointer;">sdfsdfsf {{ 'App_Nav_Logout' | translate }}</span> <span (click)="snav.toggle(); logout()" style="cursor: pointer;">{{ 'App_Nav_Logout' | translate }}</span>
</mat-nav-list> </mat-nav-list>
</mat-sidenav> </mat-sidenav>

View File

@@ -1,6 +1,7 @@
import { Component, OnInit } from "@angular/core"; import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router"; import { Router } from "@angular/router";
import { TranslateService } from "@ngx-translate/core"; import { TranslateService } from "@ngx-translate/core";
import { forkJoin, Observable } from 'rxjs';
import { User } from "../models/user"; import { User } from "../models/user";
import { CacheApiKey } from "../models/cache-api-key.enum"; import { CacheApiKey } from "../models/cache-api-key.enum";
@@ -9,6 +10,10 @@ import { AuthenticationService } from "../services/authentication.service";
import { ServiceComm } from "../services/service-comm.service"; import { ServiceComm } from "../services/service-comm.service";
import { ConfigurationHelper } from "../services/configuration-helper"; import { ConfigurationHelper } from "../services/configuration-helper";
import { ServiceCacheApi } from "../services/service-cache-api.service"; import { ServiceCacheApi } from "../services/service-cache-api.service";
import { AircraftService } from "../services/aircraft.service";
import { GearService } from "../services/gear.service";
import { JumpTypeService } from "../services/jump-type.service";
import { DropzoneService } from "../services/dropzone.service";
@Component({ @Component({
selector: "app-root", selector: "app-root",
@@ -19,37 +24,66 @@ export class AppComponent implements OnInit {
public title = "app"; public title = "app";
public currentUser: User; public currentUser: User;
public version: string; public version: string;
public selectedLanguageFlag: string;
constructor(private router: Router, constructor(private router: Router,
private authenticationService: AuthenticationService, private authenticationService: AuthenticationService,
private serviceComm: ServiceComm, private serviceComm: ServiceComm,
private serviceCacheApi : ServiceCacheApi, private serviceCacheApi : ServiceCacheApi,
private translateService: TranslateService) private translateService: TranslateService,
private serviceApiAircraft : AircraftService,
private serviceApiJumpType : JumpTypeService,
private serviceApiDropzone : DropzoneService,
private serviceApiGear : GearService)
{ {
this.authenticationService.alwaysLogin();
this.authenticationService.currentUser.subscribe(user => { this.authenticationService.currentUser.subscribe(user => {
this.currentUser = user; if (user) {
this.translateService.use(user.language); this.currentUser = user;
this.translateService.addLangs(['en', 'fr']);
this.translateService.use(user.language);
this.selectedLanguageFlag = user.language;
this.putToCacheRefDatas().subscribe(() => { console.log("Push to cache the referentiel datas"); });
}
}); });
ConfigurationHelper.settings.subscribe(settings => ConfigurationHelper.settings.subscribe(settings =>
{ {
if (settings != null) { if (settings != null) {
this.version = settings.version; this.version = settings.version;
} }
}); });
} }
ngOnInit() { ngOnInit() {
this.serviceComm.componentTitle.subscribe(title => (this.title = title)); this.serviceComm.componentTitle.subscribe(title => (this.title = title));
} }
show() { public show() {
return this.authenticationService.currentUserValue != undefined; return this.authenticationService.currentUserValue != undefined;
} }
logout() { public logout() {
this.serviceCacheApi.delete(CacheApiKey.Dropzone); this.serviceCacheApi.delete(CacheApiKey.Dropzone);
this.authenticationService.logout(); this.authenticationService.logout();
this.router.navigate(["/login"], { skipLocationChange: true }); this.router.navigate(["/login"], { skipLocationChange: true });
} }
public switchLang(event: any) {
this.translateService.use(event.value);
this.currentUser.language = event.value;
this.authenticationService.currentUserValue = this.currentUser;
this.selectedLanguageFlag = event.value;
}
private putToCacheRefDatas(): Observable<any[]> {
var aircraftResp = this.serviceApiAircraft.getListOfAircrafts(false);
var jumpTypeResp = this.serviceApiJumpType.getListOfJumpTypes();
var dzResp = this.serviceApiDropzone.getListOfDropZones(false);
var gearResp = this.serviceApiGear.getListOfGears();
return forkJoin([aircraftResp, jumpTypeResp, dzResp, gearResp]);
}
} }

View File

@@ -1,6 +1,5 @@
import { Component, OnInit } from "@angular/core"; import { Component, OnInit } from "@angular/core";
import { TranslateService } from "@ngx-translate/core"; import { TranslateService } from "@ngx-translate/core";
import { AuthenticationService } from "../../services/authentication.service";
import { ServiceComm } from "../../services/service-comm.service"; import { ServiceComm } from "../../services/service-comm.service";
@Component({ @Component({
@@ -10,13 +9,11 @@ import { ServiceComm } from "../../services/service-comm.service";
}) })
export class DefaultComponent implements OnInit { export class DefaultComponent implements OnInit {
constructor(private serviceComm: ServiceComm, constructor(private serviceComm: ServiceComm,
private authenticationService: AuthenticationService,
private translateService: TranslateService) {} private translateService: TranslateService) {}
ngOnInit() { ngOnInit() {
this.translateService.get("Home").subscribe( this.translateService.get("Home").subscribe(
data => { this.serviceComm.UpdatedComponentTitle(data); } data => { this.serviceComm.UpdatedComponentTitle(data); }
) );
this.authenticationService.alwaysLogin();
} }
} }

View File

@@ -16,7 +16,6 @@ import { JumpInfosComponent } from "../jump-infos/jump-infos.component";
styleUrls: ['./list-of-jumps.component.css'] styleUrls: ['./list-of-jumps.component.css']
}) })
export class ListOfJumpsComponent implements OnInit { export class ListOfJumpsComponent implements OnInit {
public listOfJumps: Observable<Array<Jump>>;
public displayedColumns: Array<string> = [ public displayedColumns: Array<string> = [
'infos', 'infos',
'id', 'id',
@@ -46,8 +45,7 @@ export class ListOfJumpsComponent implements OnInit {
} }
getListOfJumps() { getListOfJumps() {
this.listOfJumps = this.serviceApi.GetListOfJumps(); this.serviceApi.GetListOfJumps().subscribe(data => {
this.listOfJumps.subscribe(data => {
setTimeout(() => { setTimeout(() => {
data.sort((a, b) => { data.sort((a, b) => {
if (a.jumpDate.getTime() === b.jumpDate.getTime()) { if (a.jumpDate.getTime() === b.jumpDate.getTime()) {

View File

@@ -2,15 +2,10 @@ import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router'; import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatInput } from '@angular/material/input'; import { MatInput } from '@angular/material/input';
import { TranslateService } from '@ngx-translate/core';
import { first } from 'rxjs/operators'; import { first } from 'rxjs/operators';
import { AuthenticationService } from '../../services/authentication.service'; import { AuthenticationService } from '../../services/authentication.service';
import { AircraftService } from "../../services/aircraft.service";
import { GearService } from "../../services/gear.service";
import { JumpTypeService } from "../../services/jump-type.service";
import { DropzoneService } from "../../services/dropzone.service";
@Component({ @Component({
selector: 'app-login-user', selector: 'app-login-user',
@@ -28,12 +23,7 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
constructor(private formBuilder: FormBuilder, constructor(private formBuilder: FormBuilder,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private authenticationService: AuthenticationService, private authenticationService: AuthenticationService) {
private serviceApiAircraft : AircraftService,
private serviceApiJumpType : JumpTypeService,
private serviceApiDropzone : DropzoneService,
private serviceApiGear : GearService,
private translateService: TranslateService) {
if (this.authenticationService.currentUserValue) { if (this.authenticationService.currentUserValue) {
this.router.navigate(['/']); this.router.navigate(['/']);
} }
@@ -64,7 +54,6 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
onLoginSubmit() { onLoginSubmit() {
this.submitted = true; this.submitted = true;
// stop here if form is invalid
if (this.loginForm.invalid) { if (this.loginForm.invalid) {
return; return;
} }
@@ -74,11 +63,6 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
.pipe(first()) .pipe(first())
.subscribe( .subscribe(
data => { data => {
this.serviceApiAircraft.getListOfAircrafts(false).subscribe();
this.serviceApiJumpType.getListOfJumpTypes().subscribe();
this.serviceApiDropzone.getListOfDropZones(false).subscribe();
this.serviceApiGear.getListOfGears().subscribe();
this.router.navigate([this.returnUrl]); this.router.navigate([this.returnUrl]);
}, },
error => { error => {

View File

@@ -8,7 +8,6 @@ import { TranslateService } from "@ngx-translate/core";
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
public selectedLanguageFlag: string; public selectedLanguageFlag: string;
private flag: Map<string, string>;
constructor(private translate: TranslateService) { constructor(private translate: TranslateService) {
translate.addLangs(['en', 'fr']); translate.addLangs(['en', 'fr']);

View File

@@ -45,10 +45,10 @@ export class SummaryComponent implements OnInit {
this.serviceApi.getStatsOfLastMonth() this.serviceApi.getStatsOfLastMonth()
.subscribe(data => { .subscribe(data => {
data.byDz.sort((a, b) => b.nb - a.nb );
this.dsJumpForLastMonthByDz = new MatTableDataSource(data.byDz); this.dsJumpForLastMonthByDz = new MatTableDataSource(data.byDz);
this.dsJumpForLastMonthByJumpType = new MatTableDataSource( data.byJumpType.sort((a, b) => b.nb - a.nb );
data.byJumpType this.dsJumpForLastMonthByJumpType = new MatTableDataSource(data.byJumpType);
);
}); });
} }
@@ -94,7 +94,7 @@ export class SummaryComponent implements OnInit {
case 4: case 4:
this.serviceApi.getStatsByGear() this.serviceApi.getStatsByGear()
.subscribe(data => { .subscribe(data => {
data.sort((a, b) => b.nb - a.nb ); data.sort((a, b) => b.label.localeCompare(a.label) );
this.dsNbJumpByGear = new MatTableDataSource(data); this.dsNbJumpByGear = new MatTableDataSource(data);
}); });
break; break;

View File

@@ -40,8 +40,12 @@ export class AircraftService extends BaseService {
public getById(id: number) : Observable<AircraftResp> { public getById(id: number) : Observable<AircraftResp> {
return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft) return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft)
.pipe(map(data => { .pipe(map(data => {
return data.find(f => f.id === id); return data.find(f => f.id === id);
})); }));
}
public getFromCache(): Observable<Array<AircraftResp>> {
return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft);
} }
} }

View File

@@ -22,10 +22,6 @@ export class AuthenticationService extends BaseService {
this.currentUser = this.currentUserSubject.asObservable(); this.currentUser = this.currentUserSubject.asObservable();
} }
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
public login(username: string, password: string) { public login(username: string, password: string) {
const bodyLogin = { const bodyLogin = {
login: username, login: username,
@@ -61,6 +57,24 @@ export class AuthenticationService extends BaseService {
})); }));
} }
public alwaysLogin() {
this.http.get(`${this.apiUrl}/User/AlwaysLogin`,
{ headers: this.headers })
.subscribe();
}
public logout() {
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
public set currentUserValue(value: User) {
this.currentUserSubject.next(value);
}
private pushToken(login: string, password: string, user: User){ private pushToken(login: string, password: string, user: User){
if (user && user.token) { if (user && user.token) {
user.authdata = window.btoa(login + ":" + password); user.authdata = window.btoa(login + ":" + password);
@@ -72,15 +86,4 @@ export class AuthenticationService extends BaseService {
this.currentUserSubject.next(user); this.currentUserSubject.next(user);
} }
} }
public alwaysLogin() {
this.http.get(`${this.apiUrl}/User/AlwaysLogin`,
{ headers: this.headers })
.subscribe();
}
public logout() {
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
} }

View File

@@ -80,8 +80,12 @@ export class DropzoneService extends BaseService {
public getById(id: number) : Observable<DropZoneResp> { public getById(id: number) : Observable<DropZoneResp> {
return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone) return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone)
.pipe(map(data => { .pipe(map(data => {
return data.find(f => f.id === id); return data.find(f => f.id === id);
})); }));
}
public getFromCache(): Observable<Array<DropZoneResp>> {
return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone);
} }
} }

View File

@@ -44,8 +44,12 @@ export class GearService extends BaseService {
public getById(id: number) : Observable<GearResp> { public getById(id: number) : Observable<GearResp> {
return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear) return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear)
.pipe(map(data => { .pipe(map(data => {
return data.find(f => f.id === id); return data.find(f => f.id === id);
})); }));
}
public getFromCache(): Observable<Array<GearResp>> {
return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear);
} }
} }

View File

@@ -32,8 +32,12 @@ export class JumpTypeService extends BaseService {
public getById(id: number) : Observable<JumpTypeResp> { public getById(id: number) : Observable<JumpTypeResp> {
return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType) return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType)
.pipe(map(data => { .pipe(map(data => {
return data.find(f => f.id === id); return data.find(f => f.id === id);
})); }));
}
public getFromCache(): Observable<Array<JumpTypeResp>> {
return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType);
} }
} }

View File

@@ -5,6 +5,10 @@ import { Observable } from "rxjs";
import { map } from "rxjs/operators"; import { map } from "rxjs/operators";
import { JumpResp, JumpReq, Jump } from "../models/jump"; import { JumpResp, JumpReq, Jump } from "../models/jump";
import { GearResp } from "../models/gear";
import { DropZoneResp } from "../models/dropzone";
import { AircraftResp } from "../models/aircraft";
import { JumpTypeResp } from "../models/jumpType";
import { DateService } from "./date.service"; import { DateService } from "./date.service";
import { BaseService } from "./base.service"; import { BaseService } from "./base.service";
@@ -29,16 +33,7 @@ export class JumpService extends BaseService {
return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`, return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`,
{ headers: this.headers }) { headers: this.headers })
.pipe(map((response) => { .pipe(map((response) => {
let details = response.map((data) => return this.MapWithDataInCache(response);
{
let tmp = new Jump(data);
this.dropzoneService.getById(data.dropZoneId).subscribe((d)=> tmp.dropZone = d );
this.aircraftService.getById(data.aircraftId).subscribe((d)=> tmp.aircraft = d );
this.jumpTypeService.getById(data.jumpTypeId).subscribe((d)=> tmp.jumpType = d );
this.gearService.getById(data.gearId).subscribe((d)=> tmp.gear = d );
return tmp;
});
return details;
})); }));
} }
@@ -145,4 +140,27 @@ export class JumpService extends BaseService {
.subscribe(); .subscribe();
} }
} }
private MapWithDataInCache(apiResp: Array<JumpResp>) : Array<Jump> {
let allDropzones: Array<DropZoneResp>;
this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; });
let allAircrafts: Array<AircraftResp>;
this.aircraftService.getFromCache().subscribe(data => { allAircrafts = data; });
let allJumpType: Array<JumpTypeResp>;
this.jumpTypeService.getFromCache().subscribe(data => { allJumpType = data; });
let allGears: Array<GearResp>;
this.gearService.getFromCache().subscribe(data => { allGears = data; });
return apiResp.map((data) =>
{
let tmp = new Jump(data);
tmp.dropZone = allDropzones.find(d => d.id == data.dropZoneId);
tmp.aircraft = allAircrafts.find(d => d.id == data.aircraftId);
tmp.jumpType = allJumpType.find(d => d.id == data.jumpTypeId);
tmp.gear = allGears.find(d => d.id == data.gearId);
return tmp;
});
}
} }

View File

@@ -15,6 +15,7 @@ export class ServiceCacheApi {
} }
public get<T>(key: CacheApiKey, callToApi: Observable<T>) : Observable<T> { public get<T>(key: CacheApiKey, callToApi: Observable<T>) : Observable<T> {
console.log(`Get cache : ${CacheApiKey[key]}`);
const cached = this.cache.get(key); const cached = this.cache.get(key);
if (cached) { if (cached) {
@@ -28,10 +29,12 @@ export class ServiceCacheApi {
} }
public delete(key: CacheApiKey) { public delete(key: CacheApiKey) {
console.log(`Delete cache : ${CacheApiKey[key]}`);
this.cache.delete(key); this.cache.delete(key);
} }
public getByKey<T>(key: CacheApiKey) : Observable<T> { public getByKey<T>(key: CacheApiKey) : Observable<T> {
console.log(`Get cache by key : ${CacheApiKey[key]}`);
return this.cache.get(key); return this.cache.get(key);
} }
} }