From bb1cfedc3399b4c7ab4ef23cd3f51b8a18ce0084 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Wed, 8 Feb 2023 15:55:50 -0600 Subject: [PATCH] Refactor age to use HumanDuration Signed-off-by: Andre Wanlin --- packages/backend/src/plugins/linguist.ts | 1 + plugins/linguist-backend/README.md | 18 +++++++++---- plugins/linguist-backend/api-report.md | 4 +++ plugins/linguist-backend/config.d.ts | 27 ------------------- plugins/linguist-backend/package.json | 5 ++-- .../src/api/LinguistBackendApi.ts | 6 +++-- .../linguist-backend/src/service/router.ts | 15 +++++++++-- yarn.lock | 1 + 8 files changed, 38 insertions(+), 39 deletions(-) delete mode 100644 plugins/linguist-backend/config.d.ts diff --git a/packages/backend/src/plugins/linguist.ts b/packages/backend/src/plugins/linguist.ts index d8e56f658b..85bf3a72ec 100644 --- a/packages/backend/src/plugins/linguist.ts +++ b/packages/backend/src/plugins/linguist.ts @@ -36,5 +36,6 @@ export default async function createPlugin( database: env.database, scheduler: env.scheduler, schedule: schedule, + age: { days: 30 }, }); } diff --git a/plugins/linguist-backend/README.md b/plugins/linguist-backend/README.md index d3c39a5fe7..0ad585dedd 100644 --- a/plugins/linguist-backend/README.md +++ b/plugins/linguist-backend/README.md @@ -79,14 +79,22 @@ const schedule: TaskScheduleDefinition = { }; ``` -The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the following configuration to your `app-config.yaml`: +The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the `age` in your `packages/backend/src/plugins/linguist.ts`: -```yaml -linguist: - age: 15 # Days +```diff + return createRouter({ + logger: env.logger, + config: env.config, + reader: env.reader, + discovery: env.discovery, + database: env.database, + scheduler: env.scheduler, + schedule: schedule, ++ age: { days: 15 }, + }); ``` -With the configuration setup like this if the language breakdown is older than 15 days it will get regenerated. It's recommended that if you choose to use this configuration to set it to a large value - 30, 90, or 180 - as this data generally does not change drastically. +With the `age` setup like this if the language breakdown is older than 15 days it will get regenerated. It's recommended that if you choose to use this configuration to set it to a large value - 30, 90, or 180 - as this data generally does not change drastically. ## Links diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index 26656416df..3524c02611 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -7,6 +7,7 @@ import { Config } from '@backstage/config'; import { EntitiesOverview } from '@backstage/plugin-linguist-common'; import { EntityResults } from '@backstage/plugin-linguist-common'; import express from 'express'; +import { HumanDuration } from '@backstage/types'; import { Knex } from 'knex'; import { Languages } from '@backstage/plugin-linguist-common'; import { Logger } from 'winston'; @@ -28,6 +29,7 @@ export class LinguistBackendApi { store: LinguistBackendStore, urlReader: UrlReader, discovery: PluginEndpointDiscovery, + age?: HumanDuration | undefined, ); // (undocumented) getEntitiesOverview(): Promise; @@ -64,6 +66,8 @@ export interface LinguistBackendStore { // @public (undocumented) export interface RouterOptions { + // (undocumented) + age?: HumanDuration; // (undocumented) config: Config; // (undocumented) diff --git a/plugins/linguist-backend/config.d.ts b/plugins/linguist-backend/config.d.ts deleted file mode 100644 index 8e0133c90b..0000000000 --- a/plugins/linguist-backend/config.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2022 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. - */ - -export interface Config { - /** Configuration options for the linguist-backend plugin */ - linguist?: { - /** - * The age of an entity's languages before a refresh occurs, - * an age of 30 would mean the languages would be regenerated after 30 days. - * The default is 0 meaning languages won't be refreshed once generated. - */ - age: number; - }; -} diff --git a/plugins/linguist-backend/package.json b/plugins/linguist-backend/package.json index b79a97decd..b1bf7be071 100644 --- a/plugins/linguist-backend/package.json +++ b/plugins/linguist-backend/package.json @@ -30,6 +30,7 @@ "@backstage/errors": "workspace:^", "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-linguist-common": "workspace:^", + "@backstage/types": "workspace:^", "@types/express": "*", "express": "^4.18.1", "express-promise-router": "^4.1.0", @@ -50,8 +51,6 @@ }, "files": [ "dist", - "config.d.ts", "migrations/**/*.{js,d.ts}" - ], - "configSchema": "config.d.ts" + ] } diff --git a/plugins/linguist-backend/src/api/LinguistBackendApi.ts b/plugins/linguist-backend/src/api/LinguistBackendApi.ts index fdabea6d6c..816d731d7f 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendApi.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendApi.ts @@ -36,6 +36,7 @@ import fs from 'fs-extra'; import linguist from 'linguist-js'; import { stringifyEntityRef } from '@backstage/catalog-model'; import { assertError } from '@backstage/errors'; +import { HumanDuration } from '@backstage/types'; /** @public */ export class LinguistBackendApi { @@ -45,6 +46,7 @@ export class LinguistBackendApi { private readonly store: LinguistBackendStore, private readonly urlReader: UrlReader, private readonly discovery: PluginEndpointDiscovery, + private readonly age?: HumanDuration, ) {} public async getEntityLanguages(entityRef: string): Promise { @@ -148,8 +150,8 @@ export class LinguistBackendApi { const processedEntities = await this.store.getProcessedEntities(); const staleEntities = processedEntities.filter(pe => { - if (age === 0) return false; - const staleDate = DateTime.now().minus({ days: age }); + if (age === undefined) return false; + const staleDate = DateTime.now().minus(this.age as HumanDuration); return DateTime.fromJSDate(pe.processed_date) >= staleDate; }); diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index 091b7f408d..122cb939e3 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -30,6 +30,7 @@ import { PluginTaskScheduler, TaskScheduleDefinition, } from '@backstage/backend-tasks'; +import { HumanDuration } from '@backstage/types'; /** @public */ export interface RouterOptions { @@ -41,14 +42,23 @@ export interface RouterOptions { discovery: PluginEndpointDiscovery; scheduler?: PluginTaskScheduler; schedule?: TaskScheduleDefinition; + age?: HumanDuration; } /** @public */ export async function createRouter( options: RouterOptions, ): Promise { - const { config, logger, reader, database, discovery, scheduler, schedule } = - options; + const { + config, + logger, + reader, + database, + discovery, + scheduler, + schedule, + age, + } = options; const linguistBackendStore = await LinguistBackendDatabase.create( await database.getClient(), @@ -62,6 +72,7 @@ export async function createRouter( linguistBackendStore, reader, discovery, + age, ); if (scheduler && schedule) { diff --git a/yarn.lock b/yarn.lock index 9697e098ce..9dd67caa8b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6908,6 +6908,7 @@ __metadata: "@backstage/errors": "workspace:^" "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-linguist-common": "workspace:^" + "@backstage/types": "workspace:^" "@types/express": "*" "@types/supertest": ^2.0.8 express: ^4.18.1