Commit 4d2987ed authored by Lukas Burgey's avatar Lukas Burgey

Change the way updates are received

parent f4622c00
...@@ -114,6 +114,7 @@ export interface State { ...@@ -114,6 +114,7 @@ export interface State {
export interface Update { export interface Update {
deployment?: Deployment; deployment?: Deployment;
deployment_state?: DeploymentState;
error?: string; error?: string;
} }
......
...@@ -11,7 +11,7 @@ import { SnackBarService } from './snackbar.service'; ...@@ -11,7 +11,7 @@ import { SnackBarService } from './snackbar.service';
import { IdpService } from './idp.service'; import { IdpService } from './idp.service';
import { import {
VO, User, Update, State, Deployment, DeploymentState, SSHKey, NewSSHKey, IdP, Service VO, User, Update, State, Deployment, DeploymentState, SSHKey, NewSSHKey, IdP, Service, Site
} from './types/types.module'; } from './types/types.module';
...@@ -26,6 +26,9 @@ export class UserService { ...@@ -26,6 +26,9 @@ export class UserService {
private sshKeys: SSHKey[] = new Array<SSHKey>(); private sshKeys: SSHKey[] = new Array<SSHKey>();
private sshKeys$ = new BehaviorSubject<SSHKey[]>([]); private sshKeys$ = new BehaviorSubject<SSHKey[]>([]);
private deploymentStates: Map<number, DeploymentState> = new Map([]);
private deploymentStates$ = new BehaviorSubject<DeploymentState[]>([]);
private deployments: Map<number, Deployment> = new Map([]); private deployments: Map<number, Deployment> = new Map([]);
private deployments$ = new BehaviorSubject<Deployment[]>([]); private deployments$ = new BehaviorSubject<Deployment[]>([]);
...@@ -75,7 +78,6 @@ export class UserService { ...@@ -75,7 +78,6 @@ export class UserService {
this.snackBar.open('Logged in'); this.snackBar.open('Logged in');
} }
if (newUser.id) { if (newUser.id) {
this.connectLiveUpdates(newUser.id); this.connectLiveUpdates(newUser.id);
} }
...@@ -92,6 +94,13 @@ export class UserService { ...@@ -92,6 +94,13 @@ export class UserService {
this.deployments$.next(Array.from(this.deployments.values())); this.deployments$.next(Array.from(this.deployments.values()));
} }
if (newUser.states) {
newUser.states.forEach((state: DeploymentState) => {
this.deploymentStates.set(state.id, state)
});
this.deploymentStates$.next(Array.from(this.deploymentStates.values()));
}
this.loggedIn = true; this.loggedIn = true;
this.initialized = true; this.initialized = true;
} }
...@@ -103,9 +112,8 @@ export class UserService { ...@@ -103,9 +112,8 @@ export class UserService {
private connectLiveUpdates(userID: number): void { private connectLiveUpdates(userID: number): void {
// handle with care // handle with care
let login = userID const login = userID
let passcode = this.cookieService.get('sessionid'); const passcode = this.cookieService.get('sessionid');
const stompConfig: StompConfig = { const stompConfig: StompConfig = {
// Which server? // Which server?
url: 'wss://'+window.location.host+'/ws', url: 'wss://'+window.location.host+'/ws',
...@@ -129,10 +137,11 @@ export class UserService { ...@@ -129,10 +137,11 @@ export class UserService {
// Will log diagnostics on console // Will log diagnostics on console
debug: false, debug: false,
}; };
this.stompService.config = stompConfig; this.stompService.config = stompConfig;
this.stompService.initAndConnect(); this.stompService.initAndConnect();
let subscription = this.stompService.subscribe( const subscription = this.stompService.subscribe(
'/exchange/users/' + userID.toString() '/exchange/users/' + userID.toString()
); );
...@@ -145,6 +154,10 @@ export class UserService { ...@@ -145,6 +154,10 @@ export class UserService {
this.snackBar.open(update.error); this.snackBar.open(update.error);
} }
if (update.deployment_state) {
this.updateDeploymentState(update.deployment_state);
}
if (update.deployment) { if (update.deployment) {
this.updateDeployment(update.deployment); this.updateDeployment(update.deployment);
} }
...@@ -154,12 +167,21 @@ export class UserService { ...@@ -154,12 +167,21 @@ export class UserService {
} }
private updateDeployment(dep: Deployment): void { private updateDeployment(dep: Deployment): void {
if (dep) { if (dep != undefined && dep != null) {
dep.states.forEach((state: DeploymentState) => this.updateDeploymentState(state),
);
this.deployments.set(dep.id, dep); this.deployments.set(dep.id, dep);
this.deployments$.next(Array.from(this.deployments.values())); this.deployments$.next(Array.from(this.deployments.values()));
} }
} }
private updateDeploymentState(ds: DeploymentState): void {
if (ds != undefined && ds != null) {
this.deploymentStates.set(ds.id, ds);
this.deploymentStates$.next(Array.from(this.deploymentStates.values()));
}
}
private updateState(update: State): void { private updateState(update: State): void {
if (update) { if (update) {
// report an occured error // report an occured error
...@@ -289,13 +311,15 @@ export class UserService { ...@@ -289,13 +311,15 @@ export class UserService {
); );
} }
public changeDeployment(action: string, vo: VO): Observable<Deployment> { public changeDeployment(action: string, vo: VO): void {
const body = { const body = {
'type': action, 'type': action,
'vo': vo.id, 'vo': vo.id,
}; };
return this.http.post<Deployment>('/backend/api/deployments', body).pipe( this.http.post<Deployment>('/backend/api/deployments', body).pipe(
catchError(this.catchError(true, "Error changing deployment")), catchError(this.catchError(true, "Error changing deployment")),
).subscribe(
(dep: Deployment) => this.updateDeployment(dep),
); );
} }
...@@ -317,6 +341,18 @@ export class UserService { ...@@ -317,6 +341,18 @@ export class UserService {
return this.deployments$.asObservable(); return this.deployments$.asObservable();
} }
public subscribeDeploymentStates(): Observable<DeploymentState[]> {
return this.deploymentStates$.asObservable();
}
public subscribeStateFor(site: Site, service: Service): Observable<DeploymentState> {
return this.deploymentStates$.asObservable().pipe(
map((states: DeploymentState[]) => states.find(
(dsi: DeploymentState) => dsi.site.id == site.id && dsi.service.id == service.id,
)),
);
}
public subscribeVOs(): Observable<VO[]> { public subscribeVOs(): Observable<VO[]> {
let voSelector = (user: User) => user ? user.vos : []; let voSelector = (user: User) => user ? user.vos : [];
return this.subscribeSpecific<VO[]>(voSelector); return this.subscribeSpecific<VO[]>(voSelector);
...@@ -343,9 +379,4 @@ export class UserService { ...@@ -343,9 +379,4 @@ export class UserService {
), ),
); );
} }
public subscribeDeploymentStates(): Observable<DeploymentState[]> {
let statesSelector = (user: User) => user ? user.states : [];
return this.subscribeSpecific<DeploymentState[]>(statesSelector);
}
} }
...@@ -25,7 +25,6 @@ export class VoDataComponent implements OnInit { ...@@ -25,7 +25,6 @@ export class VoDataComponent implements OnInit {
private sites$ = new BehaviorSubject<Site[]>([]); private sites$ = new BehaviorSubject<Site[]>([]);
public sites$$: Observable<Site[]>; public sites$$: Observable<Site[]>;
private deployment$ = <BehaviorSubject<Deployment>> new BehaviorSubject(undefined);
public deployment$$: Observable<Deployment>; public deployment$$: Observable<Deployment>;
constructor( constructor(
...@@ -58,23 +57,15 @@ export class VoDataComponent implements OnInit { ...@@ -58,23 +57,15 @@ export class VoDataComponent implements OnInit {
this.sites$.next(uniqueSites); this.sites$.next(uniqueSites);
} }
); );
this.userService.subscribeVODeployment(this.vo).subscribe(
(dep: Deployment) => {
if (dep){
this.deployment$.next(dep);
}
}
);
this.services$$ = this.services$.asObservable(); this.services$$ = this.services$.asObservable();
this.sites$$ = this.sites$.asObservable(); this.sites$$ = this.sites$.asObservable();
this.deployment$$ = this.deployment$.asObservable(); this.deployment$$ = this.userService.subscribeVODeployment(this.vo);
} }
ngOnDestroy(): void { ngOnDestroy(): void {
this.services$.complete(); this.services$.complete();
this.sites$.complete(); this.sites$.complete();
this.deployment$.complete();
} }
public servicesAtSite(site: Site): Observable<Service[]> { public servicesAtSite(site: Site): Observable<Service[]> {
...@@ -88,18 +79,10 @@ export class VoDataComponent implements OnInit { ...@@ -88,18 +79,10 @@ export class VoDataComponent implements OnInit {
} }
public subscribeStateItem(site: Site, service: Service): Observable<DeploymentState> { public subscribeStateItem(site: Site, service: Service): Observable<DeploymentState> {
return this.userService.subscribeDeploymentStates().pipe( return this.userService.subscribeStateFor(site, service);
map((states: DeploymentState[]) => states.find(
(dsi: DeploymentState) => dsi.site.id == site.id && dsi.service.id == service.id,
)),
);
} }
public changeDeployment(action: string): void { public changeDeployment(action: string): void {
this.userService.changeDeployment(action, this.vo).subscribe( this.userService.changeDeployment(action, this.vo);
(dep: Deployment) => {
this.deployment$.next(dep);
}
);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment