Implemented interface for API
Signed-off-by: Andre Wanlin <andrewanlin@gmail.com>
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
|
||||
```ts
|
||||
import { CatalogApi } from '@backstage/catalog-client';
|
||||
import { EntitiesOverview } from '@backstage/plugin-linguist-common';
|
||||
import { EntityResults } from '@backstage/plugin-linguist-common';
|
||||
import express from 'express';
|
||||
import { HumanDuration } from '@backstage/types';
|
||||
@@ -15,7 +14,6 @@ import { PluginDatabaseManager } from '@backstage/backend-common';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
import { PluginTaskScheduler } from '@backstage/backend-tasks';
|
||||
import { ProcessedEntity } from '@backstage/plugin-linguist-common';
|
||||
import { Results } from 'linguist-js/dist/types';
|
||||
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
|
||||
import { TokenManager } from '@backstage/backend-common';
|
||||
import { UrlReader } from '@backstage/backend-common';
|
||||
@@ -27,7 +25,15 @@ export function createRouter(
|
||||
): Promise<express.Router>;
|
||||
|
||||
// @public (undocumented)
|
||||
export class LinguistBackendApi {
|
||||
export interface LinguistBackendApi {
|
||||
// (undocumented)
|
||||
getEntityLanguages(entityRef: string): Promise<Languages>;
|
||||
// (undocumented)
|
||||
processEntities(): Promise<void>;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export class LinguistBackendClient implements LinguistBackendApi {
|
||||
constructor(
|
||||
logger: Logger,
|
||||
store: LinguistBackendStore,
|
||||
@@ -41,18 +47,8 @@ export class LinguistBackendApi {
|
||||
linguistJsOptions?: Record<string, unknown>,
|
||||
);
|
||||
// (undocumented)
|
||||
addNewEntities(): Promise<void>;
|
||||
// (undocumented)
|
||||
generateEntitiesLanguages(): Promise<void>;
|
||||
// (undocumented)
|
||||
generateEntityLanguages(entityRef: string, url: string): Promise<string>;
|
||||
// (undocumented)
|
||||
getEntitiesOverview(): Promise<EntitiesOverview>;
|
||||
// (undocumented)
|
||||
getEntityLanguages(entityRef: string): Promise<Languages>;
|
||||
// (undocumented)
|
||||
getLinguistResults(dir: string): Promise<Results>;
|
||||
// (undocumented)
|
||||
processEntities(): Promise<void>;
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -24,7 +24,7 @@ import { CatalogApi, GetEntitiesResponse } from '@backstage/catalog-client';
|
||||
import { Results } from 'linguist-js/dist/types';
|
||||
import { DateTime } from 'luxon';
|
||||
import { LinguistBackendStore } from '../db';
|
||||
import { kindOrDefault, LinguistBackendApi } from './LinguistBackendApi';
|
||||
import { kindOrDefault, LinguistBackendClient } from './LinguistBackendClient';
|
||||
import fs from 'fs-extra';
|
||||
import { LINGUIST_ANNOTATION } from '@backstage/plugin-linguist-common';
|
||||
|
||||
@@ -94,7 +94,7 @@ describe('Linguist backend API', () => {
|
||||
|
||||
const tokenManager = ServerTokenManager.noop();
|
||||
|
||||
const api = new LinguistBackendApi(
|
||||
const api = new LinguistBackendClient(
|
||||
logger,
|
||||
store,
|
||||
urlReader,
|
||||
@@ -203,7 +203,7 @@ describe('Linguist backend API', () => {
|
||||
});
|
||||
|
||||
it('should get entity overview with stale items', async () => {
|
||||
const apiWithAge = new LinguistBackendApi(
|
||||
const apiWithAge = new LinguistBackendClient(
|
||||
logger,
|
||||
store,
|
||||
urlReader,
|
||||
@@ -326,7 +326,7 @@ describe('Linguist backend API', () => {
|
||||
});
|
||||
|
||||
it('should generate languages for entities using defined batch size', async () => {
|
||||
const apiWithBatchSize = new LinguistBackendApi(
|
||||
const apiWithBatchSize = new LinguistBackendClient(
|
||||
logger,
|
||||
store,
|
||||
urlReader,
|
||||
+21
-9
@@ -39,9 +39,16 @@ import {
|
||||
} from '@backstage/catalog-model';
|
||||
import { assertError } from '@backstage/errors';
|
||||
import { HumanDuration } from '@backstage/types';
|
||||
import { Results } from 'linguist-js/dist/types';
|
||||
|
||||
/** @public */
|
||||
export class LinguistBackendApi {
|
||||
export interface LinguistBackendApi {
|
||||
getEntityLanguages(entityRef: string): Promise<Languages>;
|
||||
processEntities(): Promise<void>;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export class LinguistBackendClient implements LinguistBackendApi {
|
||||
private readonly logger: Logger;
|
||||
private readonly store: LinguistBackendStore;
|
||||
private readonly urlReader: UrlReader;
|
||||
@@ -77,13 +84,13 @@ export class LinguistBackendApi {
|
||||
this.linguistJsOptions = linguistJsOptions;
|
||||
}
|
||||
|
||||
public async getEntityLanguages(entityRef: string): Promise<Languages> {
|
||||
async getEntityLanguages(entityRef: string): Promise<Languages> {
|
||||
this.logger?.debug(`Getting languages for entity "${entityRef}"`);
|
||||
|
||||
return this.store.getEntityResults(entityRef);
|
||||
}
|
||||
|
||||
public async processEntities() {
|
||||
async processEntities(): Promise<void> {
|
||||
this.logger?.info('Updating list of entities');
|
||||
|
||||
await this.addNewEntities();
|
||||
@@ -93,7 +100,8 @@ export class LinguistBackendApi {
|
||||
await this.generateEntitiesLanguages();
|
||||
}
|
||||
|
||||
public async addNewEntities() {
|
||||
/** @internal */
|
||||
async addNewEntities(): Promise<void> {
|
||||
const annotationKey = this.useSourceLocation
|
||||
? ANNOTATION_SOURCE_LOCATION
|
||||
: LINGUIST_ANNOTATION;
|
||||
@@ -115,7 +123,8 @@ export class LinguistBackendApi {
|
||||
});
|
||||
}
|
||||
|
||||
public async generateEntitiesLanguages() {
|
||||
/** @internal */
|
||||
async generateEntitiesLanguages(): Promise<void> {
|
||||
const entitiesOverview = await this.getEntitiesOverview();
|
||||
this.logger?.info(
|
||||
`Entities overview: Entity: ${entitiesOverview.entityCount}, Processed: ${entitiesOverview.processedCount}, Pending: ${entitiesOverview.pendingCount}, Stale ${entitiesOverview.staleCount}`,
|
||||
@@ -152,7 +161,8 @@ export class LinguistBackendApi {
|
||||
}
|
||||
}
|
||||
|
||||
public async getEntitiesOverview(): Promise<EntitiesOverview> {
|
||||
/** @internal */
|
||||
async getEntitiesOverview(): Promise<EntitiesOverview> {
|
||||
this.logger?.debug('Getting pending entities');
|
||||
|
||||
const processedEntities = await this.store.getProcessedEntities();
|
||||
@@ -178,7 +188,8 @@ export class LinguistBackendApi {
|
||||
return entitiesOverview;
|
||||
}
|
||||
|
||||
public async generateEntityLanguages(
|
||||
/** @internal */
|
||||
async generateEntityLanguages(
|
||||
entityRef: string,
|
||||
url: string,
|
||||
): Promise<string> {
|
||||
@@ -230,13 +241,14 @@ export class LinguistBackendApi {
|
||||
}
|
||||
}
|
||||
|
||||
public async getLinguistResults(dir: string) {
|
||||
/** @internal */
|
||||
async getLinguistResults(dir: string): Promise<Results> {
|
||||
const results = await linguist(dir, this.linguistJsOptions);
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
export function kindOrDefault(kind?: string[]) {
|
||||
export function kindOrDefault(kind?: string[]): string[] {
|
||||
if (!kind || kind.length === 0) {
|
||||
return ['API', 'Component', 'Template'];
|
||||
}
|
||||
@@ -14,4 +14,5 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { LinguistBackendApi } from './LinguistBackendApi';
|
||||
export { LinguistBackendClient } from './LinguistBackendClient';
|
||||
export type { LinguistBackendApi } from './LinguistBackendClient';
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
export * from './service/router';
|
||||
export { LinguistBackendApi } from './api';
|
||||
export { LinguistBackendClient } from './api';
|
||||
export type { LinguistBackendApi } from './api';
|
||||
export { LinguistBackendDatabase } from './db';
|
||||
export type { LinguistBackendStore } from './db';
|
||||
|
||||
@@ -73,7 +73,7 @@ describe('createRouter', () => {
|
||||
const router = await createRouter(
|
||||
{ schedule: schedule, age: { days: 30 }, useSourceLocation: false },
|
||||
{
|
||||
linguistBackendApi,
|
||||
linguistBackendApi: linguistBackendApi,
|
||||
discovery: testDiscovery,
|
||||
database: createDatabase(),
|
||||
reader: mockUrlReader,
|
||||
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
import express from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import { Logger } from 'winston';
|
||||
import { LinguistBackendApi } from '../api';
|
||||
import { LinguistBackendApi, LinguistBackendClient } from '../api';
|
||||
import { LinguistBackendDatabase } from '../db';
|
||||
import {
|
||||
PluginTaskScheduler,
|
||||
@@ -77,9 +77,9 @@ export async function createRouter(
|
||||
|
||||
const catalogClient = new CatalogClient({ discoveryApi: discovery });
|
||||
|
||||
const linguistBackendApi =
|
||||
const linguistBackendClient =
|
||||
routerOptions.linguistBackendApi ||
|
||||
new LinguistBackendApi(
|
||||
new LinguistBackendClient(
|
||||
logger,
|
||||
linguistBackendStore,
|
||||
reader,
|
||||
@@ -103,7 +103,7 @@ export async function createRouter(
|
||||
initialDelay: schedule.initialDelay,
|
||||
scope: schedule.scope,
|
||||
fn: async () => {
|
||||
await linguistBackendApi.processEntities();
|
||||
await linguistBackendClient.processEntities();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -125,7 +125,7 @@ export async function createRouter(
|
||||
throw new Error('No entityRef was provided');
|
||||
}
|
||||
|
||||
const entityLanguages = await linguistBackendApi.getEntityLanguages(
|
||||
const entityLanguages = await linguistBackendClient.getEntityLanguages(
|
||||
entityRef as string,
|
||||
);
|
||||
res.status(200).json(entityLanguages);
|
||||
|
||||
Reference in New Issue
Block a user