From ecf822be72bdb3145a548c4a78321b9415673ba8 Mon Sep 17 00:00:00 2001 From: Rickard Dybeck Date: Thu, 25 May 2023 14:36:14 -0400 Subject: [PATCH] rename files Signed-off-by: Rickard Dybeck --- .../src/UnprocessedEntitiesModule.ts | 132 +++++++++++++++++ .../src/index.ts | 2 +- .../src/module.ts | 135 ++++-------------- .../src/plugin.ts | 51 ------- 4 files changed, 160 insertions(+), 160 deletions(-) create mode 100644 plugins/catalog-backend-module-unprocessed/src/UnprocessedEntitiesModule.ts delete mode 100644 plugins/catalog-backend-module-unprocessed/src/plugin.ts diff --git a/plugins/catalog-backend-module-unprocessed/src/UnprocessedEntitiesModule.ts b/plugins/catalog-backend-module-unprocessed/src/UnprocessedEntitiesModule.ts new file mode 100644 index 0000000000..6ea184ce83 --- /dev/null +++ b/plugins/catalog-backend-module-unprocessed/src/UnprocessedEntitiesModule.ts @@ -0,0 +1,132 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + HydratedRefreshState, + RefreshState, + UnprocessedEntitiesRequest, + UnprocessedEntitiesResponse, +} from './types'; +import { Knex } from 'knex'; +import { HttpRouterService } from '@backstage/backend-plugin-api'; +import Router from 'express-promise-router'; +import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node'; + +/** + * Module providing Unprocessed Entities API endpoints + * + * @public + */ +export class UnprocessedEntitesModule { + private readonly moduleRouter; + + constructor( + private readonly database: Knex, + private readonly router: HttpRouterService, + ) { + this.moduleRouter = Router(); + this.router.use(this.moduleRouter); + } + + private async unprocessed( + request: UnprocessedEntitiesRequest, + ): Promise { + if (request.reason === 'pending') { + return { + type: 'pending', + entities: await this.pending(request.owner), + }; + } + return { + type: 'failed', + entities: await this.failed(request.owner), + }; + } + + private hydrateRefreshState(r: RefreshState): HydratedRefreshState { + return { + ...r, + unprocessed_entity: JSON.parse(r.unprocessed_entity), + ...(r.processed_entity && { + processed_entity: JSON.parse(r.processed_entity), + }), + ...(r.errors && { errors: JSON.parse(r.errors) }), + ...(r.cache && { cache: JSON.parse(r.cache) }), + }; + } + + private async pending(owner?: string): Promise { + const res = ( + await this.database('refresh_state.*') + .from('refresh_state') + .leftJoin( + 'final_entities', + 'final_entities.entity_id', + 'refresh_state.entity_id', + ) + .whereNull('final_entities.entity_id') + ).map(this.hydrateRefreshState); + if (owner) { + return res.filter(r => r.unprocessed_entity.spec?.owner === owner); + } + + return res; + } + + private async failed(owner?: string): Promise { + const res = ( + await this.database('refresh_state.*') + .from('refresh_state') + .rightJoin( + 'final_entities', + 'final_entities.entity_id', + 'refresh_state.entity_id', + ) + .whereNull('final_entities.final_entity') + ).map(this.hydrateRefreshState); + if (owner) { + return res.filter(r => r.unprocessed_entity.spec?.owner === owner); + } + + return res; + } + + registerRoutes() { + this.moduleRouter + .get('/entities/unprocessed/failed', async (req, res) => { + return res.json( + await this.unprocessed({ + reason: 'failed', + owner: req.query.owner as string, + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), + }), + ); + }) + .get('/entities/unprocessed/pending', async (req, res) => { + return res.json( + await this.unprocessed({ + reason: 'pending', + owner: req.query.owner as string, + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), + }), + ); + }); + } +} diff --git a/plugins/catalog-backend-module-unprocessed/src/index.ts b/plugins/catalog-backend-module-unprocessed/src/index.ts index c826e8593d..21cb2dbcd6 100644 --- a/plugins/catalog-backend-module-unprocessed/src/index.ts +++ b/plugins/catalog-backend-module-unprocessed/src/index.ts @@ -21,4 +21,4 @@ */ export * from './module'; -export * from './plugin'; +export * from './UnprocessedEntitiesModule'; diff --git a/plugins/catalog-backend-module-unprocessed/src/module.ts b/plugins/catalog-backend-module-unprocessed/src/module.ts index 6ea184ce83..1c4b6c658f 100644 --- a/plugins/catalog-backend-module-unprocessed/src/module.ts +++ b/plugins/catalog-backend-module-unprocessed/src/module.ts @@ -15,118 +15,37 @@ */ import { - HydratedRefreshState, - RefreshState, - UnprocessedEntitiesRequest, - UnprocessedEntitiesResponse, -} from './types'; -import { Knex } from 'knex'; -import { HttpRouterService } from '@backstage/backend-plugin-api'; -import Router from 'express-promise-router'; -import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node'; + coreServices, + createBackendModule, +} from '@backstage/backend-plugin-api'; +import { UnprocessedEntitesModule } from './UnprocessedEntitiesModule'; /** - * Module providing Unprocessed Entities API endpoints + * Catalog Module for Unprocessed Entities * * @public */ -export class UnprocessedEntitesModule { - private readonly moduleRouter; - - constructor( - private readonly database: Knex, - private readonly router: HttpRouterService, - ) { - this.moduleRouter = Router(); - this.router.use(this.moduleRouter); - } - - private async unprocessed( - request: UnprocessedEntitiesRequest, - ): Promise { - if (request.reason === 'pending') { - return { - type: 'pending', - entities: await this.pending(request.owner), - }; - } - return { - type: 'failed', - entities: await this.failed(request.owner), - }; - } - - private hydrateRefreshState(r: RefreshState): HydratedRefreshState { - return { - ...r, - unprocessed_entity: JSON.parse(r.unprocessed_entity), - ...(r.processed_entity && { - processed_entity: JSON.parse(r.processed_entity), - }), - ...(r.errors && { errors: JSON.parse(r.errors) }), - ...(r.cache && { cache: JSON.parse(r.cache) }), - }; - } - - private async pending(owner?: string): Promise { - const res = ( - await this.database('refresh_state.*') - .from('refresh_state') - .leftJoin( - 'final_entities', - 'final_entities.entity_id', - 'refresh_state.entity_id', - ) - .whereNull('final_entities.entity_id') - ).map(this.hydrateRefreshState); - if (owner) { - return res.filter(r => r.unprocessed_entity.spec?.owner === owner); - } - - return res; - } - - private async failed(owner?: string): Promise { - const res = ( - await this.database('refresh_state.*') - .from('refresh_state') - .rightJoin( - 'final_entities', - 'final_entities.entity_id', - 'refresh_state.entity_id', - ) - .whereNull('final_entities.final_entity') - ).map(this.hydrateRefreshState); - if (owner) { - return res.filter(r => r.unprocessed_entity.spec?.owner === owner); - } - - return res; - } - - registerRoutes() { - this.moduleRouter - .get('/entities/unprocessed/failed', async (req, res) => { - return res.json( - await this.unprocessed({ - reason: 'failed', - owner: req.query.owner as string, - authorizationToken: getBearerTokenFromAuthorizationHeader( - req.header('authorization'), - ), - }), +export const catalogModuleUnprocessedEntities = createBackendModule({ + pluginId: 'catalog', + moduleId: 'catalogModuleUnprocessedEntities', + register(env) { + env.registerInit({ + deps: { + database: coreServices.database, + router: coreServices.httpRouter, + logger: coreServices.logger, + }, + async init({ database, router, logger }) { + const module = new UnprocessedEntitesModule( + await database.getClient(), + router, ); - }) - .get('/entities/unprocessed/pending', async (req, res) => { - return res.json( - await this.unprocessed({ - reason: 'pending', - owner: req.query.owner as string, - authorizationToken: getBearerTokenFromAuthorizationHeader( - req.header('authorization'), - ), - }), + + module.registerRoutes(); + logger.info( + 'registered additional routes for catalogModuleUnprocessedEntities', ); - }); - } -} + }, + }); + }, +}); diff --git a/plugins/catalog-backend-module-unprocessed/src/plugin.ts b/plugins/catalog-backend-module-unprocessed/src/plugin.ts deleted file mode 100644 index 4f02f1efa0..0000000000 --- a/plugins/catalog-backend-module-unprocessed/src/plugin.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2023 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { - coreServices, - createBackendModule, -} from '@backstage/backend-plugin-api'; -import { UnprocessedEntitesModule } from './module'; - -/** - * Catalog Module for Unprocessed Entities - * - * @public - */ -export const catalogModuleUnprocessedEntities = createBackendModule({ - pluginId: 'catalog', - moduleId: 'catalogModuleUnprocessedEntities', - register(env) { - env.registerInit({ - deps: { - database: coreServices.database, - router: coreServices.httpRouter, - logger: coreServices.logger, - }, - async init({ database, router, logger }) { - const module = new UnprocessedEntitesModule( - await database.getClient(), - router, - ); - - module.registerRoutes(); - logger.info( - 'registered additional routes for catalogModuleUnprocessedEntities', - ); - }, - }); - }, -});