Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
code climate #2
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-cox committed Mar 8, 2018
1 parent 3d42118 commit d8d4476
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
spaceSchemaKey,
stackSchemaKey,
} from '../../store/helpers/entity-factory';
import { createEntityRelationKey } from '../../store/helpers/entity-relation.types';
import { ActionState, rootUpdatingKey } from '../../store/reducers/api-request-reducer/types';
import { selectEntity, selectUpdateInfo } from '../../store/selectors/api.selectors';
import { endpointEntitiesSelector } from '../../store/selectors/endpoint.selectors';
Expand All @@ -48,6 +47,7 @@ import {
EnvVarStratosProject,
} from './application/application-tabs-base/tabs/build-tab/application-env-vars.service';
import { getRoute, isTCPRoute } from './routes/routes.helper';
import { createEntityRelationKey } from '../../store/helpers/entity-relations.types';

export function createGetApplicationAction(guid: string, endpointGuid: string) {
return new GetApplication(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { selectRequestInfo } from '../../../../store/selectors/api.selectors';
import { APIResource } from '../../../../store/types/api.types';
import { CreateNewApplicationState } from '../../../../store/types/create-application.types';
import { createGetApplicationAction } from '../../application.service';
import { createEntityRelationKey } from '../../../../store/helpers/entity-relation.types';
import { createEntityRelationKey } from '../../../../store/helpers/entity-relations.types';

@Component({
selector: 'app-create-application-step3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Domain } from '../../../../store/types/domain.types';
import { Route, RouteMode } from '../../../../store/types/route.types';
import { ApplicationService } from '../../application.service';
import { routeSchemaKey, domainSchemaKey, applicationSchemaKey } from '../../../../store/helpers/entity-factory';
import { createEntityRelationKey } from '../../../../store/helpers/entity-relation.types';
import { createEntityRelationKey } from '../../../../store/helpers/entity-relations.types';

@Component({
selector: 'app-add-routes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { TableCellRadioComponent } from './table-cell-radio/table-cell-radio.com
import { TableCellRouteComponent } from './table-cell-route/table-cell-route.component';
import { TableCellTCPRouteComponent } from './table-cell-tcproute/table-cell-tcproute.component';
import { spaceSchemaKey, routeSchemaKey, domainSchemaKey, applicationSchemaKey } from '../../../../../store/helpers/entity-factory';
import { createEntityRelationPaginationKey, createEntityRelationKey } from '../../../../../store/helpers/entity-relation.types';
import { createEntityRelationPaginationKey, createEntityRelationKey } from '../../../../../store/helpers/entity-relations.types';

@Injectable()
export class CfAppMapRoutesListConfigService implements IListConfig<APIResource> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DeleteRoute, UnmapRoute } from '../../../../../store/actions/route.acti
import { RouterNav } from '../../../../../store/actions/router.actions';
import { AppState } from '../../../../../store/app-state';
import { applicationSchemaKey } from '../../../../../store/helpers/entity-factory';
import { createEntityRelationPaginationKey } from '../../../../../store/helpers/entity-relation.types';
import { createEntityRelationPaginationKey } from '../../../../../store/helpers/entity-relations.types';
import { selectEntity } from '../../../../../store/selectors/api.selectors';
import { APIResource, EntityInfo } from '../../../../../store/types/api.types';
import { ConfirmationDialog, ConfirmationDialogService } from '../../../confirmation-dialog.service';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { APIResource } from '../../../../../store/types/api.types';
import { PaginationEntityState } from '../../../../../store/types/pagination.types';
import { ListDataSource } from '../../data-sources-controllers/list-data-source';
import { IListConfig } from '../../list.component.types';
import { createEntityRelationKey } from '../../../../../store/helpers/entity-relation.types';
import { createEntityRelationKey } from '../../../../../store/helpers/entity-relations.types';



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { organisationWithSpaceKey } from '../../../../../store/helpers/entity-fa
import { APIResource } from '../../../../../store/types/api.types';
import { ListDataSource } from '../../data-sources-controllers/list-data-source';
import { IListConfig } from '../../list.component.types';
import { createEntityRelationKey } from '../../../../../store/helpers/entity-relation.types';
import { createEntityRelationKey } from '../../../../../store/helpers/entity-relations.types';

const orgWithSpaceSchema = entityFactory(organisationWithSpaceKey);
const spaceSchema = entityFactory(spaceSchemaKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { map } from 'rxjs/operators';
import { DeleteOrganisation, GetAllOrganisations } from '../../store/actions/organisation.actions';
import { AppState } from '../../store/app-state';
import { entityFactory, organisationWithSpaceKey, spaceSchemaKey } from '../../store/helpers/entity-factory';
import { createEntityRelationKey } from '../../store/helpers/entity-relation.types';
import { createEntityRelationKey } from '../../store/helpers/entity-relations.types';
import {
getCurrentPageRequestInfo,
getPaginationObservables,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/store/actions/application.actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Headers, RequestOptions, URLSearchParams } from '@angular/http';

import { applicationSchemaKey, appStatsSchemaKey, entityFactory, routeSchemaKey } from '../helpers/entity-factory';
import { EntityInlineChildAction, EntityInlineParentAction } from '../helpers/entity-relation.types';
import { EntityInlineChildAction, EntityInlineParentAction } from '../helpers/entity-relations.types';
import { pick } from '../helpers/reducer.helper';
import { ActionMergeFunction } from '../types/api.types';
import { CfApplication } from '../types/application.types';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/store/actions/organisation.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
spaceSchemaKey,
spacesKey,
} from '../helpers/entity-factory';
import { EntityInlineChildAction, EntityInlineParentAction } from '../helpers/entity-relation.types';
import { EntityInlineChildAction, EntityInlineParentAction } from '../helpers/entity-relations.types';
import { PaginatedAction } from '../types/pagination.types';
import { CFStartAction, ICFAction } from '../types/request.types';
import { getActions } from './action.helper';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/store/actions/relation.actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestOptions, URLSearchParams } from '@angular/http';

import { EntityInlineChildAction, EntityInlineParentAction, EntityTreeRelation } from '../helpers/entity-relation.types';
import { EntityInlineChildAction, EntityInlineParentAction, EntityTreeRelation } from '../helpers/entity-relations.types';
import { PaginatedAction } from '../types/pagination.types';
import { CFStartAction, IRequestActionEntity, RequestEntityLocation } from '../types/request.types';

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/store/actions/request.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Action } from '@ngrx/store';
import { NormalizedResponse } from '../types/api.types';
import { PaginatedAction } from '../types/pagination.types';
import { ICFAction } from '../types/request.types';
import { ValidationResult } from '../helpers/entity-relation.types';
import { ValidationResult } from '../helpers/entity-relations.types';

export const ApiActionTypes = {
API_REQUEST_START: 'API_REQUEST_START'
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/store/actions/space.actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RequestOptions } from '@angular/http';

import { entityFactory, routeSchemaKey, spaceSchemaKey, spaceWithOrgKey } from '../helpers/entity-factory';
import { EntityInlineChildAction, EntityInlineParentAction } from '../helpers/entity-relation.types';
import { EntityInlineChildAction, EntityInlineParentAction } from '../helpers/entity-relations.types';
import { PaginatedAction } from '../types/pagination.types';
import { CFStartAction, ICFAction } from '../types/request.types';
import { RouteEvents } from './route.actions';
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/app/store/effects/api.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import { ApiActionTypes, ValidateEntitiesStart } from './../actions/request.acti
import { AppState, IRequestEntityTypeState } from './../app-state';
import { APIResource, NormalizedResponse } from './../types/api.types';
import { StartRequestAction, WrapperRequestActionFailed } from './../types/request.types';
import { isEntityInlineParentAction, EntityInlineParentAction } from '../helpers/entity-relation.types';
import { listEntityRelations } from '../helpers/entity-relations.helpers';
import { isEntityInlineParentAction, EntityInlineParentAction } from '../helpers/entity-relations.types';
import { listEntityRelations } from '../helpers/entity-relations';

const { proxyAPIVersion, cfAPIVersion } = environment;

Expand Down
4 changes: 2 additions & 2 deletions src/frontend/app/store/effects/request.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
ValidateEntitiesStart,
} from '../actions/request.actions';
import { AppState } from '../app-state';
import { validateEntityRelations } from '../helpers/entity-relations.helpers';
import { getRequestTypeFromMethod } from '../reducers/api-request-reducer/request-helpers';
import { rootUpdatingKey } from '../reducers/api-request-reducer/types';
import { getAPIRequestDataState } from '../selectors/api.selectors';
import { UpdateCfAction, WrapperRequestActionFailed, WrapperRequestActionSuccess } from '../types/request.types';
import { validateEntityRelations } from '../helpers/entity-relations';

@Injectable()
export class RequestEffect {
Expand Down Expand Up @@ -54,7 +54,7 @@ export class RequestEffect {
* - exist - if a list, dispatch an action to store them in pagination.
* For example
* 1) a space may require routes
* 2) space is fetched as part of a different call that has not requested a space's routes be fetched line
* 2) space is fetched as part of a different call that has not requested a space's routes be fetched inline
* 3) this validation process will check each space, and if routes are missing fetch them
* 4) routes list is then stored in original space and as a pagination section
* 5) alternatively... if we've reached here for the same space but from an api request for that space.. ensure that the routes have not
Expand Down
101 changes: 101 additions & 0 deletions src/frontend/app/store/helpers/entity-relations.tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { schema } from 'normalizr';

import {
createEntityRelationPaginationKey,
EntityInlineParentAction,
EntityTreeRelation,
ValidationResult,
EntityTree,
createEntityRelationKey,
} from './entity-relations.types';

const entityTreeCache: {
[entityKey: string]: EntityTree
} = {};

function generateCacheKey(entityKey: string, action: EntityInlineParentAction): string {
let includeRelations = action.includeRelations || [];
includeRelations = includeRelations.sort((a, b) => {
return a.localeCompare(b);
});
return entityKey + '+' + includeRelations.join(',');
}

export function fetchEntityTree(action: EntityInlineParentAction): EntityTree {
let entity = action.entity;
const isArray = entity['length'] > 0;
entity = isArray ? entity[0] : entity;
const entityKey = entity['key'];
const cacheKey = generateCacheKey(entityKey, action);
let entityTree = entityTreeCache[cacheKey];
if (!entityTree) {
const rootEntityRelation = new EntityTreeRelation(
entityKey,
entity as schema.Entity,
isArray,
null,
'',
new Array<EntityTreeRelation>()
);
entityTree = {
rootRelation: rootEntityRelation,
requiredParamNames: new Array<string>(),
};
createEntityTree(entityTree, rootEntityRelation);
entityTreeCache[cacheKey] = entityTree;
// console.log('fetchEntity: Not Found');
} else {
// console.log('fetchEntity: Found');
}
// Calc max depth and exclude not needed
entityTree.rootRelation.childRelations = parseEntityTree(entityTree, entityTree.rootRelation, action.includeRelations);
return entityTree;
}

export function createEntityTree(tree: EntityTree, entityRelation: EntityTreeRelation, schemaObj?, path: string = '') {
const rootEntitySchema = schemaObj || entityRelation.entity['schema'];
Object.keys(rootEntitySchema).forEach(key => {
let value = rootEntitySchema[key];
const isArray = value['length'] > 0;
value = isArray ? value[0] : value;

const newPath = path ? path + '.' + key : key;
if (value instanceof schema.Entity) {
const newEntityRelation = new EntityTreeRelation(
value.key,
value,
isArray,
key,
newPath,
new Array<EntityTreeRelation>()
);
entityRelation.childRelations.push(newEntityRelation);
createEntityTree(tree, newEntityRelation, null, '');
} else if (value instanceof Object) {
createEntityTree(tree, entityRelation, value, newPath);
}
});
}

export function parseEntityTree(tree: EntityTree, entityRelation: EntityTreeRelation, includeRelations: string[] = [])
: EntityTreeRelation[] {
const newChildRelations = new Array<EntityTreeRelation>();
entityRelation.childRelations.forEach((relation, index, array) => {
const parentChildKey = createEntityRelationKey(entityRelation.entityKey, relation.entityKey);
if (includeRelations.indexOf(parentChildKey) >= 0) {
const clone = { ...relation };
newChildRelations.push(clone);
if (tree.requiredParamNames.indexOf(relation.paramName) < 0) {
tree.requiredParamNames.push(relation.paramName);
}
clone.childRelations = parseEntityTree(tree, relation, includeRelations);
}
});
entityRelation.childRelations = newChildRelations;
if (entityRelation.childRelations.length) {
tree.maxDepth = tree.maxDepth || 0;
tree.maxDepth++;
}
return newChildRelations;
}

Loading

0 comments on commit d8d4476

Please sign in to comment.