-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from NREL/issue-265
Issue 265: Move KPI/KPM selection to facility level
- Loading branch information
Showing
82 changed files
with
436 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 2 additions & 13 deletions
15
src/app/core-components/import-backup-modal/import-backup-modal.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { UpdateDbEntriesService } from './update-db-entries.service'; | ||
import { stubServiceProviders } from '../spec-helpers/spec-test-service-stub'; | ||
|
||
describe('UpdateDbEntriesService', () => { | ||
let service: UpdateDbEntriesService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: stubServiceProviders | ||
}); | ||
service = TestBed.inject(UpdateDbEntriesService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { KeyPerformanceIndicatorsIdbService } from './key-performance-indicators-idb.service'; | ||
import { KeyPerformanceMetricImpactsIdbService } from './key-performance-metric-impacts-idb.service'; | ||
import { CompanyIdbService } from './company-idb.service'; | ||
import { FacilityIdbService } from './facility-idb.service'; | ||
import { IdbCompany } from '../models/company'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { IdbFacility } from '../models/facility'; | ||
import { IdbKeyPerformanceIndicator } from '../models/keyPerformanceIndicator'; | ||
import { IdbUser } from '../models/user'; | ||
import { UserIdbService } from './user-idb.service'; | ||
import { IdbKeyPerformanceMetricImpact } from '../models/keyPerformanceMetricImpact'; | ||
import { getGUID } from '../shared/helpFunctions'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class UpdateDbEntriesService { | ||
|
||
constructor( | ||
private keyPerformanceIndicatorsIdbService: KeyPerformanceIndicatorsIdbService, | ||
private keyPerformanceMetricImpactsIdbService: KeyPerformanceMetricImpactsIdbService, | ||
private facilityIdbService: FacilityIdbService, | ||
private userIdbService: UserIdbService | ||
) { } | ||
|
||
async updateDbEntries(user: IdbUser): Promise<IdbUser> { | ||
let userNeedsUpdate: boolean = false; | ||
if (!user.kpiFacilityMigrationDone) { | ||
await this.updateToFacilityKPI(); | ||
user.kpiFacilityMigrationDone = true; | ||
userNeedsUpdate = true; | ||
} | ||
|
||
if (userNeedsUpdate) { | ||
user = await firstValueFrom(this.userIdbService.updateWithObservable(user)); | ||
this.userIdbService.user.next(user); | ||
} | ||
return user; | ||
} | ||
|
||
//migration of KPIs to facility level | ||
async updateToFacilityKPI() { | ||
let keyPerformanceIndicators: Array<IdbKeyPerformanceIndicator> = await firstValueFrom(this.keyPerformanceIndicatorsIdbService.getAll()); | ||
//get kpis without facility ids | ||
let noFacilityKeyPerformanceIndicators: Array<IdbKeyPerformanceIndicator> = keyPerformanceIndicators.filter(indicator => { | ||
return indicator.facilityId == undefined; | ||
}); | ||
if (noFacilityKeyPerformanceIndicators.length > 0) { | ||
let facilities: Array<IdbFacility> = await firstValueFrom(this.facilityIdbService.getAll()); | ||
let keyPerformanceMetricImpacts: Array<IdbKeyPerformanceMetricImpact> = await firstValueFrom(this.keyPerformanceMetricImpactsIdbService.getAll()); | ||
|
||
for (let i = 0; i < noFacilityKeyPerformanceIndicators.length; i++) { | ||
let kpi: IdbKeyPerformanceIndicator = noFacilityKeyPerformanceIndicators[i]; | ||
// move kpi to all facilities in company | ||
let kpiFacilities: Array<IdbFacility> = facilities.filter(facility => { return facility.companyId == kpi.companyId }); | ||
//if multiple facilities in a company | ||
//need to create copies of kpis with uniq guids | ||
|
||
for (let f = 0; f < kpiFacilities.length; f++) { | ||
let facility: IdbFacility = kpiFacilities[f]; | ||
let facilityMetricImpacts: Array<IdbKeyPerformanceMetricImpact> = keyPerformanceMetricImpacts.filter(impact => { return impact.facilityId == facility.guid }); | ||
kpi.facilityId = facility.guid; | ||
if (f == 0) { | ||
await firstValueFrom(this.keyPerformanceIndicatorsIdbService.updateWithObservable(kpi)); | ||
} else { | ||
//create new kpis when multiple facilities; | ||
let originalKpiGuid: string = kpi.guid; | ||
let originalKpmGuids: Array<{ originalGuid: string, newGuid: string }> = kpi.performanceMetrics.map(metric => { return { originalGuid: metric.guid, newGuid: undefined } }); | ||
|
||
let newKpiGuid: string = getGUID(); | ||
kpi.guid = newKpiGuid; | ||
let associatedImpacts: Array<IdbKeyPerformanceMetricImpact> = facilityMetricImpacts.filter(impact => { return impact.kpiGuid == originalKpiGuid }); | ||
//add new kpi with updated guids | ||
for (let m = 0; m < kpi.performanceMetrics.length; m++) { | ||
let newGuid: string = getGUID(); | ||
let kpmGuidIndex: number = originalKpmGuids.findIndex(kpmGuid => { return kpmGuid.originalGuid == kpi.performanceMetrics[m].guid }); | ||
originalKpmGuids[kpmGuidIndex].newGuid = newGuid; | ||
kpi.performanceMetrics[m].guid = newGuid; | ||
}; | ||
delete kpi.id; | ||
await firstValueFrom(this.keyPerformanceIndicatorsIdbService.addWithObservable(kpi)); | ||
//updates guid for associated impacts | ||
for (let a = 0; a < associatedImpacts.length; a++) { | ||
let impact: IdbKeyPerformanceMetricImpact = associatedImpacts[a]; | ||
impact.kpiGuid = newKpiGuid; | ||
let newKpmGuid: string = originalKpmGuids.find(ogGuid => { return ogGuid.originalGuid == impact.kpmGuid }).newGuid; | ||
impact.kpmGuid = newKpmGuid; | ||
await firstValueFrom(this.keyPerformanceMetricImpactsIdbService.updateWithObservable(impact)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { IdbEntry, getNewIdbEntry } from "./idbEntry"; | ||
|
||
export interface IdbUser extends IdbEntry { | ||
skipSplashScreen: boolean | ||
skipSplashScreen: boolean, | ||
kpiFacilityMigrationDone: boolean | ||
} | ||
|
||
export function getNewIdbUser(): IdbUser { | ||
let idbEntry: IdbEntry = getNewIdbEntry(); | ||
return { | ||
...idbEntry, | ||
kpiFacilityMigrationDone: true, | ||
skipSplashScreen: false | ||
} | ||
} |
Oops, something went wrong.