Skip to content

Commit

Permalink
Fix non jetstream requests and other, minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
KlapTrap committed Oct 2, 2019
1 parent 60f56f5 commit 815507e
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { AutoscalerTabExtensionComponent } from './features/autoscaler-tab-exten
import { generateASEntities } from './store/autoscaler-entity-generator';
import { AutoscalerEffects } from './store/autoscaler.effects';

// TODO Work out why we need this and remove it.
// FIXME Work out why we need this and remove it.
const customRoutes: Routes = [
{
path: 'autoscaler',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ entityCache[gitBranchesEntityType] = GithubBranchSchema;
const GithubRepoSchema = new CFEntitySchema(gitRepoEntityType);
entityCache[gitRepoEntityType] = GithubRepoSchema;

const GithubCommitSchema = new CFEntitySchema(gitCommitEntityType, {}, { idAttribute: commit => commit.sha });
const GithubCommitSchema = new CFEntitySchema(gitCommitEntityType, {}, { idAttribute: commit => commit.guid });
entityCache[gitCommitEntityType] = GithubCommitSchema;

const CFInfoSchema = new CFEntitySchema(cfInfoEntityType);
Expand Down
12 changes: 10 additions & 2 deletions src/frontend/packages/cloud-foundry/src/cf-entity-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,14 @@ function generateGitCommitEntity(endpointDefinition: StratosEndpointExtensionDef
schema: cfEntityFactory(gitCommitEntityType),
label: 'Git Commit',
labelPlural: 'Git Commits',
endpoint: endpointDefinition
endpoint: endpointDefinition,
nonJetstreamRequest: true,
successfulRequestDataMapper: (data, endpointGuid, guid, entityType, endpointType, action) => {
return {
...data,
guid: action.guid
};
},
};
return new StratosCatalogueEntity<IFavoriteMetadata, GitCommit, GitCommitActionBuildersConfig, GitCommitActionBuilders>(
definition,
Expand All @@ -664,7 +671,8 @@ function generateGitCommitEntity(endpointDefinition: StratosEndpointExtensionDef
actionBuilders: gitCommitActionBuilders,
entityBuilder: {
getMetadata: ent => ({
name: ent.commit ? ent.commit.message || ent.sha : ent.sha
name: ent.commit ? ent.commit.message || ent.sha : ent.sha,
guid: ent.guid
}),
getGuid: metadata => metadata.guid,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class GitSCMTabComponent implements OnInit, OnDestroy {
});

this.commit$ = this.gitCommitEntityService.waitForEntity$.pipe(
map(p => p.entity && p.entity)
map(p => p.entity)
);

this.isHead$ = this.gitBranchEntityService.waitForEntity$.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ import { CreateSpaceQuotaStepComponent } from '../../../../core/src/features/clo
],
providers: [
CFEndpointsListConfigService,
// TODO: check if belongs to here
EndpointListHelper,
EndpointsListConfigService,
{
provide: ActiveRouteCfOrgSpace,
useValue: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export interface GitCommit {
};
message: string;
};
guid?: string;
}

2 changes: 0 additions & 2 deletions src/frontend/packages/core/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { GeneralEntityAppState, GeneralRequestDataState } from '../../store/src/
import { endpointSchemaKey } from '../../store/src/helpers/entity-factory';
import { getAPIRequestDataState } from '../../store/src/selectors/api.selectors';
import { recentlyVisitedSelector } from '../../store/src/selectors/recently-visitied.selectors';
import { AppStoreExtensionsModule } from '../../store/src/store.extensions.module';
import { AppStoreModule } from '../../store/src/store.module';
import { IFavoriteMetadata, UserFavorite } from '../../store/src/types/user-favorites.types';
import { TabNavService } from '../tab-nav.service';
Expand Down Expand Up @@ -86,7 +85,6 @@ export class CustomRouterStateSerializer
],
imports: [
EntityCatalogueModule.forFeature(generateStratosEntities),
AppStoreExtensionsModule,
RouteModule,
CloudFoundryPackageModule,
AppStoreModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export interface IStratosEntityDefinition<
readonly preRequest?: PreApiRequest;
readonly prePaginationRequest?: PrePaginationApiRequest;
readonly errorMessageHandler?: ApiErrorMessageHandler;
// Should the request response object for this entity be parsed as if it's passed through the jetstream backend?
readonly nonJetstreamRequest?: boolean;
readonly nonJetstreamRequestHandler?: nonJetstreamRequestHandler;
}

export class nonJetstreamRequestHandler<T = any> {
isSuccess: (request: T) => boolean;
getErrorCode?: (request: T) => string;
}

export interface IStratosEntityActions extends Partial<IStratosEntityWithIcons> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}

&__option {
padding-bottom: 40px;
padding-bottom: 20px;
mat-radio-button {
&:first-of-type {
padding-right: 12px;
Expand All @@ -25,10 +25,9 @@
margin-bottom: 30px;
}
&__subtext {
font-size: 13px;
font-size: 15px;
margin: 2px 0 0 24px;
opacity: .5;
position: absolute;
}
&__checkbox {
position: relative;
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/packages/core/src/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ import { ValuesPipe } from './pipes/values.pipe';
import { CloudFoundryUserProvidedServicesService } from './services/cloud-foundry-user-provided-services.service';
import { MetricsRangeSelectorService } from './services/metrics-range-selector.service';
import { UserPermissionDirective } from './user-permission.directive';
import { EndpointListHelper } from './components/list/list-types/endpoint/endpoint-list.helpers';
import { EndpointsListConfigService } from './components/list/list-types/endpoint/endpoints-list-config.service';

/* tslint:disable:max-line-length */

Expand Down Expand Up @@ -309,6 +311,8 @@ import { UserPermissionDirective } from './user-permission.directive';
providers: [
ListConfig,
ApplicationStateService,
EndpointListHelper,
EndpointsListConfigService,
// CfUserService,
ConfirmationDialogService,
EntityMonitorFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { IStratosEntityDefinition } from '../../../core/src/core/entity-catalogu
import { AppState, InternalAppState } from '../app-state';
import { EntityRequestAction } from '../types/request.types';
import { buildRequestEntityPipe } from './entity-request-base-handlers/build-entity-request.pipe';
import { handleMultiEndpointsPipeFactory } from './entity-request-base-handlers/handle-multi-endpoints.pipe';
import { handleJetstreamResponsePipeFactory, handleNonJetstreamResponsePipeFactory } from './entity-request-base-handlers/handle-multi-endpoints.pipe';
import { makeRequestEntityPipe } from './entity-request-base-handlers/make-request-entity-request.pipe';
import { mapMultiEndpointResponses } from './entity-request-base-handlers/map-multi-endpoint.pipes';
import { BasePipelineConfig, EntityRequestPipeline, PipelineResult } from './entity-request-pipeline.types';
import { singleRequestToPaged } from './pipeline-helpers';
import { singleRequestToPaged, isJetstreamRequest } from './pipeline-helpers';
import { PipelineHttpClient } from './pipline-http-client.service';

export interface SingleRequestPipelineConfig<T extends AppState = InternalAppState> extends BasePipelineConfig<T> {
Expand All @@ -33,15 +33,23 @@ export const baseRequestPipelineFactory: EntityRequestPipeline = (
const actionDispatcher = (actionToDispatch: Action) => store.dispatch(actionToDispatch);
const baseRequest = buildRequestEntityPipe(requestType, action.options);
const request = preRequest ? preRequest(baseRequest, action, catalogueEntity) : baseRequest;
const handleMultiEndpointsPipe = handleMultiEndpointsPipeFactory(action.options.url);
const definition = catalogueEntity.definition as IStratosEntityDefinition;
const isJetstreamEntityRequest = isJetstreamRequest(definition);
const handleMultiEndpointsPipe = isJetstreamEntityRequest ?
handleJetstreamResponsePipeFactory(
action.options.url
) : handleNonJetstreamResponsePipeFactory(
action.options.url,
definition.nonJetstreamRequestHandler
);
return makeRequestEntityPipe(
httpClient,
request,
entityCatalogue.getEndpoint(action.endpointType, action.subType),
action.endpointGuid,
action.externalRequest
).pipe(
map(response => singleRequestToPaged(response)),
map(response => isJetstreamEntityRequest ? singleRequestToPaged(response) : response),
// Convert { [endpointGuid]: <raw response> } to { { errors: [], successes: [] } }
map(handleMultiEndpointsPipe),
// Convert { { errors: [], successes: [] } } to { response: NoramlisedResponse, success: boolean }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { AppState, InternalAppState } from '../app-state';
import { PaginationFlattenerConfig } from '../helpers/paginated-request-helpers';
import { PaginatedAction } from '../types/pagination.types';
import { buildRequestEntityPipe } from './entity-request-base-handlers/build-entity-request.pipe';
import { handleMultiEndpointsPipeFactory } from './entity-request-base-handlers/handle-multi-endpoints.pipe';
import {
handleJetstreamResponsePipeFactory,
handleNonJetstreamResponsePipeFactory
} from './entity-request-base-handlers/handle-multi-endpoints.pipe';
import { makeRequestEntityPipe } from './entity-request-base-handlers/make-request-entity-request.pipe';
import { mapMultiEndpointResponses } from './entity-request-base-handlers/map-multi-endpoint.pipes';
import {
Expand All @@ -24,7 +27,7 @@ import {
} from './entity-request-pipeline.types';
import { getPaginationParamsPipe } from './pagination-request-base-handlers/get-params.pipe';
import { PaginationPageIterator } from './pagination-request-base-handlers/pagination-iterator.pipe';
import { mergeHttpParams, singleRequestToPaged } from './pipeline-helpers';
import { mergeHttpParams, singleRequestToPaged, isJetstreamRequest } from './pipeline-helpers';
import { PipelineHttpClient } from './pipline-http-client.service';

function getRequestObjectObservable(request: HttpRequest<any> | Observable<HttpRequest<any>>): Observable<HttpRequest<any>> {
Expand Down Expand Up @@ -82,10 +85,15 @@ export const basePaginatedRequestPipeline: EntityRequestPipeline = (
prePaginatedRequestFunction(requestFromStore, action, catalogueEntity, appState) :
requestFromStore;

const handleMultiEndpointsPipe = handleMultiEndpointsPipeFactory(
action.options.url,
flattenerConfig
);
const handleMultiEndpointsPipe = isJetstreamRequest(entity.definition) ?
handleJetstreamResponsePipeFactory(
action.options.url,
flattenerConfig
) : handleNonJetstreamResponsePipeFactory(
action.options.url,
entity.definition.nonJetstreamRequestHandler,
flattenerConfig
);

// Keep, helpful for debugging below chain via tap
// const debug = (val, location) => console.log(`${entity.endpointType}:${entity.entityKey}:${location}: `, val);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleMultiEndpointsPipeFactory, JetstreamError } from './handle-multi-endpoints.pipe';
import { handleJetstreamResponsePipeFactory, JetstreamError } from './handle-multi-endpoints.pipe';
import { JetstreamResponse } from '../entity-request-pipeline.types';
import { JetStreamErrorResponse } from '../../../../core/src/jetstream.helpers';

Expand Down Expand Up @@ -33,7 +33,7 @@ describe('handle-multi-endpoint-pipe', () => {
[endpoint2Guid]: [endpoint2Res],
[endpoint4Guid]: [endpoint4Res, endpoint4Res]
} as JetstreamResponse;
const handled = handleMultiEndpointsPipeFactory(url)(resData);
const handled = handleJetstreamResponsePipeFactory(url)(resData);
expect(handled.errors.length).toBe(2);
expect(handled.errors[0] instanceof JetstreamError).toBe(true);
expect(handled.errors[0].errorCode).toBe(1 + '');
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('handle-multi-endpoint-pipe', () => {
[endpoint2Guid]: [endpoint2Res],
[endpoint4Guid]: [endpoint4Res, endpoint4Res]
} as JetstreamResponse;
const handled = handleMultiEndpointsPipeFactory(url, {
const handled = handleJetstreamResponsePipeFactory(url, {
getEntitiesFromResponse: (page) => {
return page.entities;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { hasJetStreamError, JetStreamErrorResponse } from '../../../../core/src/jetstream.helpers';
import { PagedJetstreamResponse } from '../entity-request-pipeline.types';
import { PaginationPageIteratorConfig } from '../pagination-request-base-handlers/pagination-iterator.pipe';
import { nonJetstreamRequestHandler } from '../../../../core/src/core/entity-catalogue/entity-catalogue.types';

/**
* Generic container for information about an errored request to a specific endpoint
Expand Down Expand Up @@ -28,7 +29,7 @@ export interface HandledMultiEndpointResponse<T = any> {
successes: MultiEndpointResponse<T>[];
}

function mapResponses(
function mapJetstreamResponses(
jetstreamResponse: PagedJetstreamResponse,
requestUrl: string,
flattenerConfig: PaginationPageIteratorConfig<any, any>
Expand Down Expand Up @@ -61,7 +62,7 @@ function mapResponses(
}, baseResponse);
}

function getAllEntitiesFromResponses(response: any[], getEntitiesFromResponse?: (response: any) => any) {
function getAllEntitiesFromResponses(response: any, getEntitiesFromResponse?: (response: any) => any) {
if (!Array.isArray(response)) {
return response;
}
Expand All @@ -84,11 +85,12 @@ function getAllEntitiesFromResponses(response: any[], getEntitiesFromResponse?:
}

function postProcessSuccessResponses(
response: any[],
response: any,
endpointGuid: string,
flattenerConfig: PaginationPageIteratorConfig<any, any>
): MultiEndpointResponse<any> {
const entities = getAllEntitiesFromResponses(response, flattenerConfig ? flattenerConfig.getEntitiesFromResponse : null);
console.log(entities);
const jetStreamResponse = {
[endpointGuid]: response
};
Expand Down Expand Up @@ -125,9 +127,31 @@ function buildJetstreamError(
jetstreamErrorResponse,
);
}
export const handleMultiEndpointsPipeFactory = (
export const handleJetstreamResponsePipeFactory = (
requestUrl: string,
flattenerConfig?: PaginationPageIteratorConfig<any, any>
) => (resData: PagedJetstreamResponse): HandledMultiEndpointResponse => {
return mapResponses(resData, requestUrl, flattenerConfig);
return mapJetstreamResponses(resData, requestUrl, flattenerConfig);
};

export const handleNonJetstreamResponsePipeFactory = (
requestUrl: string,
nonJetstreamRequestHandler?: nonJetstreamRequestHandler,
flattenerConfig?: PaginationPageIteratorConfig<any, any>
) => (resData: any): HandledMultiEndpointResponse => {
const isSuccess = nonJetstreamRequestHandler ? nonJetstreamRequestHandler.isSuccess(resData) : true;
console.log(resData);
const mappedRes = postProcessSuccessResponses(resData, null, flattenerConfig);
if (isSuccess) {
return {
successes: [mappedRes],
errors: []
};
}
const errorCode = nonJetstreamRequestHandler && nonJetstreamRequestHandler.getErrorCode ?
nonJetstreamRequestHandler.getErrorCode(resData) : '500';
return {
successes: [],
errors: [new JetstreamError(errorCode, null, requestUrl, null)]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { StratosBaseCatalogueEntity } from '../../../core/src/core/entity-catalo
import { IStratosEntityDefinition } from '../../../core/src/core/entity-catalogue/entity-catalogue.types';
import { JetstreamResponse, PagedJetstreamResponse } from './entity-request-pipeline.types';

export function isJetstreamRequest(definition: IStratosEntityDefinition): boolean {
return !definition.nonJetstreamRequest && !definition.nonJetstreamRequestHandler;
}


export function getSuccessMapper(catalogueEntity: StratosBaseCatalogueEntity) {
const definition = catalogueEntity.definition as IStratosEntityDefinition;
if (typeof definition.successfulRequestDataMapper === 'string') {
Expand Down

0 comments on commit 815507e

Please sign in to comment.