ghostfolio/apps/client/src/app/pages/tools/analysis/analysis-page.component.ts

205 lines
6.0 KiB
TypeScript
Raw Normal View History

2021-04-13 21:53:58 +02:00
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { ToggleOption } from '@ghostfolio/client/components/toggle/interfaces/toggle-option.type';
import { DataService } from '@ghostfolio/client/services/data.service';
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service';
import { UNKNOWN_KEY } from '@ghostfolio/common/config';
import {
PortfolioItem,
PortfolioPosition,
User
} from '@ghostfolio/common/interfaces';
2021-04-13 21:53:58 +02:00
import { DeviceDetectorService } from 'ngx-device-detector';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({
selector: 'gf-analysis-page',
templateUrl: './analysis-page.html',
styleUrls: ['./analysis-page.scss']
})
export class AnalysisPageComponent implements OnDestroy, OnInit {
public accounts: {
[symbol: string]: Pick<PortfolioPosition, 'name'> & { value: number };
};
public continents: {
[code: string]: { name: string; value: number };
};
public countries: {
[code: string]: { name: string; value: number };
};
2021-04-13 21:53:58 +02:00
public deviceType: string;
public period = 'current';
public periodOptions: ToggleOption[] = [
{ label: 'Initial', value: 'original' },
{ label: 'Current', value: 'current' }
];
public hasImpersonationId: boolean;
public portfolioItems: PortfolioItem[];
public portfolioPositions: { [symbol: string]: PortfolioPosition };
public positions: { [symbol: string]: any };
public positionsArray: PortfolioPosition[];
public user: User;
private unsubscribeSubject = new Subject<void>();
/**
* @constructor
*/
public constructor(
private changeDetectorRef: ChangeDetectorRef,
2021-04-13 21:53:58 +02:00
private dataService: DataService,
private deviceService: DeviceDetectorService,
private impersonationStorageService: ImpersonationStorageService,
private userService: UserService
2021-04-13 21:53:58 +02:00
) {}
/**
* Initializes the controller
*/
public ngOnInit() {
this.deviceType = this.deviceService.getDeviceInfo().deviceType;
this.impersonationStorageService
.onChangeHasImpersonation()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((aId) => {
this.hasImpersonationId = !!aId;
});
this.dataService
.fetchPortfolio()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((response) => {
this.portfolioItems = response;
this.changeDetectorRef.markForCheck();
2021-04-13 21:53:58 +02:00
});
this.dataService
.fetchPortfolioPositions({})
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((response = {}) => {
this.portfolioPositions = response;
this.initializeAnalysisData(this.portfolioPositions, this.period);
this.changeDetectorRef.markForCheck();
2021-04-13 21:53:58 +02:00
});
this.userService.stateChanged
2021-04-13 21:53:58 +02:00
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((state) => {
if (state?.user) {
this.user = state.user;
2021-04-13 21:53:58 +02:00
this.changeDetectorRef.markForCheck();
}
2021-04-13 21:53:58 +02:00
});
}
public initializeAnalysisData(
aPortfolioPositions: {
[symbol: string]: PortfolioPosition;
},
aPeriod: string
) {
this.accounts = {};
this.continents = {
[UNKNOWN_KEY]: {
name: UNKNOWN_KEY,
value: 0
}
};
this.countries = {
[UNKNOWN_KEY]: {
name: UNKNOWN_KEY,
value: 0
}
};
2021-04-13 21:53:58 +02:00
this.positions = {};
this.positionsArray = [];
for (const [symbol, position] of Object.entries(aPortfolioPositions)) {
this.positions[symbol] = {
currency: position.currency,
exchange: position.exchange,
industry: position.industry,
sector: position.sector,
type: position.type,
value:
aPeriod === 'original'
? position.allocationInvestment
: position.allocationCurrent
2021-04-13 21:53:58 +02:00
};
this.positionsArray.push(position);
for (const [account, { current, original }] of Object.entries(
position.accounts
2021-04-13 21:53:58 +02:00
)) {
if (this.accounts[account]?.value) {
this.accounts[account].value +=
2021-04-13 21:53:58 +02:00
aPeriod === 'original' ? original : current;
} else {
this.accounts[account] = {
name: account,
value: aPeriod === 'original' ? original : current
2021-04-13 21:53:58 +02:00
};
}
}
if (position.countries.length > 0) {
for (const country of position.countries) {
const { code, continent, name, weight } = country;
if (this.continents[continent]?.value) {
this.continents[continent].value += weight * position.value;
} else {
this.continents[continent] = {
name: continent,
value:
weight *
(aPeriod === 'original'
? this.portfolioPositions[symbol].investment
: this.portfolioPositions[symbol].value)
};
}
if (this.countries[code]?.value) {
this.countries[code].value += weight * position.value;
} else {
this.countries[code] = {
name,
value:
weight *
(aPeriod === 'original'
? this.portfolioPositions[symbol].investment
: this.portfolioPositions[symbol].value)
};
}
}
} else {
this.continents[UNKNOWN_KEY].value +=
aPeriod === 'original'
? this.portfolioPositions[symbol].investment
: this.portfolioPositions[symbol].value;
this.countries[UNKNOWN_KEY].value +=
aPeriod === 'original'
? this.portfolioPositions[symbol].investment
: this.portfolioPositions[symbol].value;
}
2021-04-13 21:53:58 +02:00
}
}
public onChangePeriod(aValue: string) {
this.period = aValue;
this.initializeAnalysisData(this.portfolioPositions, this.period);
}
public ngOnDestroy() {
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
}
}