Skip to content

Commit

Permalink
feat(geoDB): service to load data from config file into the indexed-db (
Browse files Browse the repository at this point in the history
#1115)

Co-authored-by: Pierre-Étienne Lord <[email protected]>
  • Loading branch information
pelord and Pierre-Étienne Lord authored Nov 11, 2022
1 parent 36fe2c3 commit a8f866a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
82 changes: 82 additions & 0 deletions packages/geo/src/lib/offline/geoDB/configFileToGeoDB.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Injectable } from '@angular/core';
import { MessageService } from '@igo2/core';
import { HttpClient } from '@angular/common/http';
import { catchError, concatMap } from 'rxjs/operators';
import { GeoDBService } from './geoDB.service';
import { of, zip } from 'rxjs';
import { DatasToIDB, GeoDBData } from './geoDB.interface';

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


constructor(
private http: HttpClient,
private geoDBService: GeoDBService,
private messageService: MessageService
) { }


load(url: string) {
let downloadMessage;
this.http.get(url).pipe(

catchError((error: any): any => {
console.log(`GeoData file ${url} could not be read`);
error.error.caught = true;
throw error;
}),
concatMap((datasToIDB: DatasToIDB) => {
const datas$ = [];
let firstDownload = true;
if (datasToIDB?.geoDatas) {
const currentDate = new Date();
datasToIDB?.geoDatas.map((geoData) => {
if (typeof geoData.triggerDate === 'string') {
geoData.triggerDate = new Date(Date.parse(geoData.triggerDate.replace(/-/g, ' ')));
}
if (currentDate >= geoData.triggerDate) {
if (geoData.action === 'update') {
const insertEvent = `${geoData.source || 'automatedDataUpdate'} (${geoData.triggerDate})`;
geoData.urls.map((url) => {
datas$.push(
this.geoDBService.getByID(url).pipe(concatMap((res: GeoDBData) => {
if (res?.insertEvent !== insertEvent) {
if (firstDownload) {
downloadMessage = this.messageService
.info('igo.geo.indexedDb.data-download-start', undefined,
{ disableTimeOut: true, progressBar: false, closeButton: true, tapToDismiss: false });
firstDownload = false;
}
return this.http.get(url)
.pipe(concatMap(r => this.geoDBService.update(url, url as any, r, 'system' as any, insertEvent)));
} else {
return of(false);
}
}))
);
});
} else if (geoData.action === 'delete') {
geoData.urls.map((url) => {
datas$.push(this.geoDBService.deleteByKey(url));
});
}
}
});
}
return zip(...datas$);
})
).subscribe(() => {
if (downloadMessage) {
setTimeout(() => {
this.messageService.remove((downloadMessage as any).toastId);
this.messageService.success('igo.geo.indexedDb.data-download-completed', undefined, { timeOut: 40000 });
}, 2500);
}
});
}


}
13 changes: 12 additions & 1 deletion packages/geo/src/lib/offline/geoDB/geoDB.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ import { InsertSourceInsertDBEnum } from "./geoDB.enums";

export interface GeoDBData {
url: string;
regionID: number;
regionID: any;
object: any;
compressed: boolean;
insertSource: InsertSourceInsertDBEnum;
insertEvent: string;
}

export interface GeoDataToIDB {
triggerDate: Date | string;
action: "delete" | 'update';
urls: string[];
source?: string;
}

export interface DatasToIDB {
geoDatas: GeoDataToIDB[]
}
10 changes: 9 additions & 1 deletion packages/geo/src/lib/offline/geoDB/geoDB.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class GeoDBService {
* @param insertEvent Name of the event where the insert has been triggered
* @returns
*/
update(url: string, regionID: number, object: any, insertSource: InsertSourceInsertDBEnum, insertEvent: string): Observable<any> {
update(url: string, regionID: any, object: any, insertSource: InsertSourceInsertDBEnum, insertEvent: string): Observable<any> {
if (!object) {
return;
}
Expand Down Expand Up @@ -108,6 +108,14 @@ export class GeoDBService {
);
}

getByID(url: string): Observable<any> {
return this.ngxIndexedDBService.getByID(this.dbName, url);
}

deleteByKey(url: string): Observable<any> {
return this.ngxIndexedDBService.deleteByKey(this.dbName, url);
}

getRegionTileCountByID(id: number): Observable<number> {
const subject: Subject<number> = new Subject();
const dbRequest = this.getRegionByID(id)
Expand Down
1 change: 1 addition & 0 deletions packages/geo/src/lib/offline/geoDB/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './geoDB.enums';
export * from './geoDB.interface';
export * from './geoDB.service';
export * from './configFileToGeoDB.service';
6 changes: 5 additions & 1 deletion packages/geo/src/locale/en.geo.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"unreadable": {
"text": "The file '{{value}}' is unreadable",
"title": "Unreadable file"
"title": "Unreadable file"
},
"tooLarge": {
"text": "The file '{{value}}' is too large (Max: {{size}} MB)",
Expand Down Expand Up @@ -746,6 +746,10 @@
"link": {
"message": "Hyperlink copied to clipboard"
}
},
"indexedDb": {
"data-download-start": "Downloading data. Please don't leave the current app.",
"data-download-completed": "Data download completed"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/geo/src/locale/fr.geo.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@
"link": {
"message": "Le lien a été copié dans le presse-papier"
}
},
"indexedDb": {
"data-download-start": "Téléchargement des données. Veuillez ne pas quitter.",
"data-download-completed": "Téléchargement des données terminée."
}
}
}
Expand Down

0 comments on commit a8f866a

Please sign in to comment.