Skip to content

Commit

Permalink
Merge pull request #2511 from cloudfoundry-incubator/fix-app-count-stuck
Browse files Browse the repository at this point in the history
Fix for app count being stuck after add in org card
  • Loading branch information
nwmac authored Jun 26, 2018
2 parents 7d73e4a + 22731fd commit 67158d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { first, map, publishReplay, refCount } from 'rxjs/operators';
import { Observable, combineLatest } from 'rxjs';
import { first, map, publishReplay, refCount, switchMap } from 'rxjs/operators';

import { IApp, ICfV2Info, IOrganization, ISpace } from '../../../core/cf-api.types';
import { EntityService } from '../../../core/entity-service';
Expand Down Expand Up @@ -32,6 +32,7 @@ import { CfApplicationState } from '../../../store/types/application.types';
import { EndpointModel, EndpointUser } from '../../../store/types/endpoint.types';
import { CfUser } from '../../../store/types/user.types';
import { ActiveRouteCfOrgSpace } from '../cf-page.types';
import { selectEntity } from '../../../store/selectors/api.selectors';

export function appDataSort(app1: APIResource<IApp>, app2: APIResource<IApp>): number {
const app1Date = new Date(app1.metadata.updated_at);
Expand Down Expand Up @@ -138,20 +139,8 @@ export class CloudFoundryEndpointService {
this.info$ = this.cfInfoEntityService.waitForEntity$;

this.allApps$ = this.orgs$.pipe(
map(p => {
return p
.filter(o => !!o.entity.spaces)
.map(o => {
return o.entity.spaces.map(space => space.entity.apps || []);
});
}),
map(a => {
let flatArray = [];
a.forEach(
appsInSpace => (flatArray = flatArray.concat(...appsInSpace))
);
return flatArray;
})
map(orgs => [].concat(...orgs.map(org => org.entity.spaces))),
map((spaces: APIResource<ISpace>[]) => [].concat(...spaces.map(space => space.entity.apps)))
);

this.fetchDomains();
Expand Down Expand Up @@ -209,7 +198,7 @@ export class CloudFoundryEndpointService {
statMetric: string
): number {
return apps ? apps
.filter(a => a.entity.state !== CfApplicationState.STOPPED)
.filter(a => a.entity && a.entity.state !== CfApplicationState.STOPPED)
.map(a => a.entity[statMetric] * a.entity.instances)
.reduce((a, t) => a + t, 0) : 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { routeReducer } from './routes.reducer';
import { serviceInstanceReducer } from './service-instance.reducer';
import { systemEndpointsReducer } from './system-endpoints.reducer';
import { userReducer, userSpaceOrgReducer, endpointDisconnectUserReducer } from './users.reducer';
import { applicationAddRemoveReducer } from './application-add-remove-reducer';

/**
* This module uses the request data reducer and request reducer factories to create
Expand Down Expand Up @@ -134,6 +135,7 @@ export function requestDataReducer(state, action) {
],
[spaceSchemaKey]: [
endpointDisconnectApplicationReducer('space'),
applicationAddRemoveReducer('space'),
userSpaceOrgReducer(true)
],
[organizationSchemaKey]: [
Expand Down
71 changes: 71 additions & 0 deletions src/frontend/app/store/reducers/application-add-remove-reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { APIResource } from '../types/api.types';
import { APISuccessOrFailedAction } from '../types/request.types';
import { CREATE_SUCCESS, DELETE_SUCCESS } from '../actions/application.actions';
import { IApp, ISpace } from '../../core/cf-api.types';
import { deepMergeState } from '../helpers/reducer.helper';


export function applicationAddRemoveReducer(name) {
return function (state: APIResource, action: APISuccessOrFailedAction) {
switch (action.type) {
case CREATE_SUCCESS:
return addApplicationToSpace(state, action);
case DELETE_SUCCESS:
return deleteApplicationFromSpace(state, action);
}
return state;
};
}

function addApplicationToSpace(state: APIResource, action: APISuccessOrFailedAction) {
if (action.response && action.response.entities && action.response.entities.application) {
const apps = action.response.entities.application;
const updatedSpaces = {};
Object.keys(apps).forEach(appGuid => {
const app = apps[appGuid] as APIResource<IApp>;
const spaceGuid = app.entity.space_guid;
const space = state[spaceGuid] as APIResource<ISpace>;
if (space.entity.apps) {
const newSpaceEntity = {
entity: {
apps: [ ...space.entity.apps, app.metadata.guid ]
}
};
updatedSpaces[spaceGuid] = newSpaceEntity;
}
});
if (Object.keys(updatedSpaces).length) {
return deepMergeState(state, updatedSpaces);
}
}

return state;
}

interface SpaceAsAppRefs {
apps: string[];
}

function deleteApplicationFromSpace(state: APIResource, action: APISuccessOrFailedAction) {
// GUID of the application that was deleted
const appGuid = action.apiAction.guid;
// We don't know ths space GUID, but app guids are unique, so look across all spaces
const updatedSpaces = {};
Object.keys(state).forEach(spaceGuid => {
const space = state[spaceGuid] as APIResource<SpaceAsAppRefs>;
const apps = <string[]> space.entity.apps;
if (apps && apps.findIndex((value) => value === appGuid) >= 0) {
const newSpaceEntity = {
entity: {
apps: apps.filter((guid) => guid !== appGuid)
}
};
updatedSpaces[spaceGuid] = newSpaceEntity;
}
});
if (Object.keys(updatedSpaces).length) {
return deepMergeState(state, updatedSpaces);
}

return state;
}

0 comments on commit 67158d9

Please sign in to comment.