Refactor age to use HumanDuration

Signed-off-by: Andre Wanlin <andrewanlin@gmail.com>
This commit is contained in:
Andre Wanlin
2023-02-08 15:55:50 -06:00
parent 75cfee5688
commit bb1cfedc33
8 changed files with 38 additions and 39 deletions
+1
View File
@@ -36,5 +36,6 @@ export default async function createPlugin(
database: env.database,
scheduler: env.scheduler,
schedule: schedule,
age: { days: 30 },
});
}
+13 -5
View File
@@ -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
+4
View File
@@ -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<EntitiesOverview>;
@@ -64,6 +66,8 @@ export interface LinguistBackendStore {
// @public (undocumented)
export interface RouterOptions {
// (undocumented)
age?: HumanDuration;
// (undocumented)
config: Config;
// (undocumented)
-27
View File
@@ -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;
};
}
+2 -3
View File
@@ -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"
]
}
@@ -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<Languages> {
@@ -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;
});
+13 -2
View File
@@ -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<express.Router> {
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) {
+1
View File
@@ -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