@backstage-community/plugin-tech-insights-backend 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +93 -259
  3. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @backstage-community/plugin-tech-insights-backend
2
2
 
3
+ ## 0.6.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [1d33996]
8
+ - @backstage-community/plugin-tech-insights-common@0.2.18
9
+ - @backstage-community/plugin-tech-insights-node@0.6.7
10
+
11
+ ## 0.6.2
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [a84eb44]
16
+ - @backstage-community/plugin-tech-insights-common@0.2.17
17
+ - @backstage-community/plugin-tech-insights-node@0.6.6
18
+
3
19
  ## 0.6.1
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Tech Insights Backend
2
2
 
3
- This is the backend for the default Backstage Tech Insights feature.
4
- This provides the API for the frontend tech insights, scorecards and fact visualization functionality,
5
- as well as a framework to run fact retrievers and store fact values in to a data store.
3
+ The backend plugin for Tech Insights.
4
+
5
+ It provides the API for the frontend tech insights, scorecards and fact visualization functionality, as well as a framework to run fact retrievers and store fact values in to a data store.
6
+
7
+ Looking for the old backend installation docs? Visit [here](./docs/old-backend-system.md).
6
8
 
7
9
  ## Installation
8
10
 
@@ -13,133 +15,104 @@ as well as a framework to run fact retrievers and store fact values in to a data
13
15
  yarn --cwd packages/backend add @backstage-community/plugin-tech-insights-backend
14
16
  ```
15
17
 
16
- ### Adding the plugin to your `packages/backend`
18
+ ### Add the plugin to your backend
17
19
 
18
20
  ```ts title="packages/backend/src/index.ts"
21
+ // Add the following to `packages/backend/src/index.ts`
19
22
  backend.add(import('@backstage-community/plugin-tech-insights-backend'));
20
23
  ```
21
24
 
22
- You can use the extension points [@backstage-community/plugin-tech-insights-node](../tech-insights-node)
23
- to add your `FactRetriever` or set a `FactCheckerFactory`.
25
+ ## Built-in Defaults
26
+
27
+ ### Included FactRetrievers
24
28
 
25
- The built-in `FactRetrievers`:
29
+ `FactRetrievers` are only registered if configured in the `app-config.yaml`. Meaning that while you have the tech insights backend installed, you will not have any fact retrievers present in your application.
26
30
 
27
- - `entityMetadataFactRetriever`
28
- - `entityOwnershipFactRetriever`
29
- - `techdocsFactRetriever`
31
+ There are three built-in FactRetrievers that come with Tech Insights:
30
32
 
31
- `FactRetrievers` only get registered if they get configured:
33
+ - `entityMetadataFactRetriever`: Generates facts which indicate the completeness of entity metadata
34
+ - `entityOwnershipFactRetriever`: Generates facts which indicate the quality of data in the `spec.owner` field
35
+ - `techdocsFactRetriever`: Generates facts related to the completeness of techdocs configuration for entities
36
+
37
+ #### Registering the built-in FactRetrievers
38
+
39
+ The following example registers the built-in fact retrievers to run every 6 hours, while retaining the latest two weeks of fact data.
32
40
 
33
41
  ```yaml title="app-config.yaml"
34
42
  techInsights:
35
43
  factRetrievers:
44
+ entityMetadataFactRetriever:
45
+ # How often the fact retriever should run
46
+ cadence: '*/15 * * * *'
47
+ # How long to keep the fact data
48
+ lifecycle: { timeToLive: { weeks: 2 } }
36
49
  entityOwnershipFactRetriever:
37
50
  cadence: '*/15 * * * *'
38
51
  lifecycle: { timeToLive: { weeks: 2 } }
52
+ techdocsFactRetriever:
53
+ cadence: '*/15 * * * *'
54
+ lifecycle: { timeToLive: { weeks: 2 } }
39
55
  ```
40
56
 
41
- ### Adding the plugin to your `packages/backend` (old)
42
-
43
- You'll need to add the plugin to the router in your `backend` package. You can
44
- do this by creating a file called `packages/backend/src/plugins/techInsights.ts`. An example content for `techInsights.ts` could be something like this.
45
-
46
- ```ts
47
- import {
48
- createRouter,
49
- buildTechInsightsContext,
50
- } from '@backstage-community/plugin-tech-insights-backend';
51
- import { Router } from 'express';
52
- import { PluginEnvironment } from '../types';
53
-
54
- export default async function createPlugin(
55
- env: PluginEnvironment,
56
- ): Promise<Router> {
57
- const builder = buildTechInsightsContext({
58
- logger: env.logger,
59
- config: env.config,
60
- database: env.database,
61
- discovery: env.discovery,
62
- scheduler: env.scheduler,
63
- tokenManager: env.tokenManager,
64
- factRetrievers: [], // Fact retrievers registrations you want tech insights to use
65
- });
66
-
67
- return await createRouter({
68
- ...(await builder),
69
- logger: env.logger,
70
- config: env.config,
71
- });
72
- }
73
- ```
74
-
75
- With the `techInsights.ts` router setup in place, add the router to
76
- `packages/backend/src/index.ts`:
57
+ The optional `lifecycle` configuration value limits the lifetime of fact data. The value can be either `maxItems` or TTL (time to live). Valid options are either a number for `maxItems` or a Luxon duration-like object for TTL.
77
58
 
78
- ```diff
79
- +import techInsights from './plugins/techInsights';
59
+ ```yaml
60
+ # Lifecycle configuration examples
61
+ lifecycle: { timeToLive: { weeks: 2 } }
80
62
 
81
- async function main() {
82
- ...
83
- const createEnv = makeCreateEnv(config);
63
+ # Deletes all but 7 latest facts for each id/entity pair
64
+ lifecycle: { maxItems: 7 };
84
65
 
85
- const catalogEnv = useHotMemoize(module, () => createEnv('catalog'));
86
- + const techInsightsEnv = useHotMemoize(module, () => createEnv('tech_insights'));
87
-
88
- const apiRouter = Router();
89
- + apiRouter.use('/tech-insights', await techInsights(techInsightsEnv));
90
- ...
91
- apiRouter.use(notFoundHandler());
92
- }
66
+ # Deletes all facts older than 2 weeks (TTL)
67
+ lifecycle: { timeToLive: 1209600000 } # Luxon duration like object
68
+ lifecycle: { timeToLive: { weeks: 2 } }; # Human readable value
93
69
  ```
94
70
 
95
- ### Adding fact retrievers
71
+ #### Running fact retrievers in a multi-instance installation
96
72
 
97
- At this point the Tech Insights backend is installed in your backend package, but
98
- you will not have any fact retrievers present in your application. To have the implemented FactRetrieverEngine within this package to be able to retrieve and store fact data into the database, you need to add these.
73
+ The Tech Insights plugin utilizes `PluginTaskScheduler` to schedule and coordinate task invocation across instances. See [the PluginTaskScheduler documentation](https://backstage.io/docs/reference/backend-tasks.plugintaskscheduler) for more information.
99
74
 
100
- To create factRetrieverRegistration you need to implement `FactRetriever` interface defined in `@backstage-community/plugin-tech-insights-node` package (see [Creating fact retrievers](#creating-fact-retrievers) for details). After you have implemented this interface you can wrap that into a registration object like follows:
75
+ ### Included FactChecker
101
76
 
102
- ```ts
103
- import { createFactRetrieverRegistration } from '@backstage-community/plugin-tech-insights-backend';
77
+ **NOTE**: You need a Fact Checker configured to get access to the backend routes that will allow the facts to be checked. If you don't have one configured, you will see 404s and potentially other errors.
104
78
 
105
- const myFactRetriever = {
106
- /**
107
- * snip
108
- */
109
- };
79
+ There is a default FactChecker implementation provided in the [@backstage-community/plugin-tech-insights-backend-module-jsonfc](../tech-insights-backend-module-jsonfc/README.md) module. This implementation uses `json-rules-engine` as the underlying functionality to run checks.
110
80
 
111
- const myFactRetrieverRegistration = createFactRetrieverRegistration({
112
- cadence: '1 * 2 * * ', // On the first minute of the second day of the month
113
- factRetriever: myFactRetriever,
114
- });
115
- ```
81
+ You must install the module into your backend system to use the fact checker.
116
82
 
117
- FactRetrieverRegistration also accepts an optional `lifecycle` configuration value. This can be either MaxItems or TTL (time to live). Valid options for this value are either a number for MaxItems or a Luxon duration like object for TTL. For example:
83
+ ```bash
84
+ # From your Backstage root directory
85
+ yarn --cwd packages/backend add @backstage-community/plugin-tech-insights-backend-module-jsonfc
86
+ ```
118
87
 
119
- ```ts
120
- const maxItems = { maxItems: 7 }; // Deletes all but 7 latest facts for each id/entity pair
121
- const ttl = { timeToLive: 1209600000 }; // (2 weeks) Deletes items older than 2 weeks
122
- const ttlWithAHumanReadableValue = { timeToLive: { weeks: 2 } }; // Deletes items older than 2 weeks
88
+ ```diff title="packages/backend/src/index.ts"
89
+ // Add the following to `packages/backend/src/index.ts`
90
+ backend.add(import('@backstage-community/plugin-tech-insights-backend'));
91
+ +backend.add(import('@backstage-community/plugin-tech-insights-backend-module-jsonfc'));
123
92
  ```
124
93
 
125
- To register these fact retrievers to your application you can modify the example `techInsights.ts` file shown above like this:
94
+ Then, configure the checks in the `app-config.yaml` file. The following example configures a _check_ to verify a group has been set as the `spec.owner` for an entity. The check uses the `entityOwnershipFactRetriever` fact retriever to get the data.
126
95
 
127
- ```diff
128
- const builder = buildTechInsightsContext({
129
- logger: env.logger,
130
- config: env.config,
131
- database: env.database,
132
- discovery: env.discovery,
133
- tokenManager: env.tokenManager,
134
- scheduler: env.scheduler,
135
- - factRetrievers: [],
136
- + factRetrievers: [myFactRetrieverRegistration],
137
- });
96
+ ```yaml title="app-config.yaml"
97
+ techInsights:
98
+ factRetrievers: ...
99
+ factChecker:
100
+ checks:
101
+ groupOwnerCheck:
102
+ type: json-rules-engine
103
+ name: Group Owner Check
104
+ description: Verifies that a group has been set as the spec.owner for this entity
105
+ factIds:
106
+ - entityOwnershipFactRetriever
107
+ rule:
108
+ conditions:
109
+ all:
110
+ - fact: hasGroupOwner
111
+ operator: equal
112
+ value: true
138
113
  ```
139
114
 
140
- #### Running fact retrievers in a multi-instance installation
141
-
142
- The Tech Insights plugin utilizes the `PluginTaskScheduler` for scheduling tasks and coordinating the task invocation across instances. See [the PluginTaskScheduler documentation](https://backstage.io/docs/reference/backend-tasks.plugintaskscheduler) for more information.
115
+ ## Creating custom implementations of FactRetrievers
143
116
 
144
117
  ### Creating Fact Retrievers
145
118
 
@@ -156,7 +129,7 @@ An example implementation of a FactRetriever could for example be as follows:
156
129
  ```ts
157
130
  import { FactRetriever } from '@backstage-community/plugin-tech-insights-node';
158
131
 
159
- const myFactRetriever: FactRetriever = {
132
+ export const myFactRetriever: FactRetriever = {
160
133
  id: 'documentation-number-factretriever', // unique identifier of the fact retriever
161
134
  version: '0.1.1', // SemVer version number of this fact retriever schema. This should be incremented if the implementation changes
162
135
  entityFilter: [{ kind: 'component' }], // EntityFilter to be used in the future (creating checks, graphs etc.) to figure out which entities this fact retrieves data for.
@@ -195,7 +168,7 @@ const myFactRetriever: FactRetriever = {
195
168
 
196
169
  // All facts that this retriever returns
197
170
  facts: {
198
- examplenumberfact: 2, //
171
+ examplenumberfact: 2,
199
172
  },
200
173
  // (optional) timestamp to use as a Luxon DateTime object
201
174
  };
@@ -204,43 +177,33 @@ const myFactRetriever: FactRetriever = {
204
177
  };
205
178
  ```
206
179
 
207
- ### Adding a fact checker
208
-
209
- This module comes with a possibility to additionally add a fact checker and expose fact checking endpoints from the API. To be able to enable this feature you need to add a FactCheckerFactory implementation to be part of the `DefaultTechInsightsBuilder` constructor call.
210
-
211
- There is a default FactChecker implementation provided in module `@backstage-community/plugin-tech-insights-backend-module-jsonfc`. This implementation uses `json-rules-engine` as the underlying functionality to run checks. If you want to implement your own FactChecker, for example to be able to handle other than `boolean` result types, you can do so by implementing `FactCheckerFactory` and `FactChecker` interfaces from `@backstage-community/plugin-tech-insights-common` package.
212
-
213
- To add the default FactChecker into your Tech Insights you need to install the module into your backend application:
214
-
215
- ```bash
216
- # From your Backstage root directory
217
- yarn --cwd packages/backend add @backstage-community/plugin-tech-insights-backend-module-jsonfc
218
- ```
180
+ Additional FactRetrievers are added to the Tech Insights backend by creating a new backend module and registering the FactRetriever with the `techInsightsFactRetrieversExtensionPoint` extension point. This, along with other extension points, are available in the [@backstage-community/plugin-tech-insights-node](../tech-insights-node/README.md) library.
219
181
 
220
- and modify the `techInsights.ts` file to contain a reference to the FactChecker implementation.
182
+ ```ts title="plugins/tech-insights-backend-module-my-fact-retriever/src/module.ts"
183
+ import { techInsightsFactRetrieversExtensionPoint } from '@backstage-community/plugin-tech-insights-node';
184
+ import { myFactRetriever } from './myFactRetriever';
221
185
 
222
- ```diff
223
- +import { JsonRulesEngineFactCheckerFactory } from '@backstage-community/plugin-tech-insights-backend-module-jsonfc';
224
-
225
- +const myFactCheckerFactory = new JsonRulesEngineFactCheckerFactory({
226
- + checks: [],
227
- + logger: env.logger,
228
- +}),
229
-
230
- const builder = new DefaultTechInsightsBuilder({
231
- logger: env.logger,
232
- config: env.config,
233
- database: env.database,
234
- discovery: env.discovery,
235
- tokenManager: env.tokenManager,
236
- factRetrievers: [myFactRetrieverRegistration],
237
- + factCheckerFactory: myFactCheckerFactory
238
- });
186
+ export const techInsightsModuleMyFactRetriever = createBackendModule({
187
+ pluginId: 'tech-insights',
188
+ moduleId: 'my-fact-retriever',
189
+ register(reg) {
190
+ reg.registerInit({
191
+ deps: {
192
+ providers: techInsightsFactRetrieversExtensionPoint,
193
+ },
194
+ async init({ providers }) {
195
+ providers.addFactRetrievers({
196
+ myFactRetriever,
197
+ });
198
+ },
199
+ });
200
+ },
201
+ });
239
202
  ```
240
203
 
241
- NOTE: You need a Fact Checker Factory to get access to the backend routes that will allow the facts to be checked. If you don't have a Fact Checker Factory you will see 404s and potentially other errors.
204
+ ### Adding a fact checker
242
205
 
243
- To be able to run checks, you need to additionally add individual checks into your FactChecker implementation. For examples how to add these, you can check the documentation of the individual implementation of the FactChecker
206
+ If you want to implement your own FactChecker, for example to be able to handle other than `boolean` result types, you can do so by implementing `FactCheckerFactory` and `FactChecker` interfaces from `@backstage-community/plugin-tech-insights-common` package.
244
207
 
245
208
  #### Modifying check persistence
246
209
 
@@ -255,132 +218,3 @@ const myFactCheckerFactory = new JsonRulesEngineFactCheckerFactory({
255
218
  }),
256
219
 
257
220
  ```
258
-
259
- ## Included FactRetrievers
260
-
261
- There are three FactRetrievers that come out of the box with Tech Insights:
262
-
263
- - `entityMetadataFactRetriever`: Generates facts which indicate the completeness of entity metadata
264
- - `entityOwnershipFactRetriever`: Generates facts which indicate the quality of data in the spec.owner field
265
- - `techdocsFactRetriever`: Generates facts related to the completeness of techdocs configuration for entities
266
-
267
- ## Backend Example
268
-
269
- Here's an example backend setup that will use the three included fact retrievers so you can get an idea of how this all works. This will be the entire contents of your `techInsights.ts` file found at `\packages\backend\src\plugins` as per [Adding the plugin to your `packages/backend`](#adding-the-plugin-to-your-packagesbackend)
270
-
271
- ```ts
272
- import {
273
- createRouter,
274
- buildTechInsightsContext,
275
- createFactRetrieverRegistration,
276
- entityOwnershipFactRetriever,
277
- entityMetadataFactRetriever,
278
- techdocsFactRetriever,
279
- } from '@backstage-community/plugin-tech-insights-backend';
280
- import { Router } from 'express';
281
- import { PluginEnvironment } from '../types';
282
- import {
283
- JsonRulesEngineFactCheckerFactory,
284
- JSON_RULE_ENGINE_CHECK_TYPE,
285
- } from '@backstage-community/plugin-tech-insights-backend-module-jsonfc';
286
-
287
- const ttlTwoWeeks = { timeToLive: { weeks: 2 } };
288
-
289
- export default async function createPlugin(
290
- env: PluginEnvironment,
291
- ): Promise<Router> {
292
- const techInsightsContext = await buildTechInsightsContext({
293
- logger: env.logger,
294
- config: env.config,
295
- database: env.database,
296
- discovery: env.discovery,
297
- tokenManager: env.tokenManager,
298
- scheduler: env.scheduler,
299
- factRetrievers: [
300
- createFactRetrieverRegistration({
301
- cadence: '0 */6 * * *', // Run every 6 hours - https://crontab.guru/#0_*/6_*_*_*
302
- factRetriever: entityOwnershipFactRetriever,
303
- lifecycle: ttlTwoWeeks,
304
- }),
305
- createFactRetrieverRegistration({
306
- cadence: '0 */6 * * *',
307
- factRetriever: entityMetadataFactRetriever,
308
- lifecycle: ttlTwoWeeks,
309
- }),
310
- createFactRetrieverRegistration({
311
- cadence: '0 */6 * * *',
312
- factRetriever: techdocsFactRetriever,
313
- lifecycle: ttlTwoWeeks,
314
- }),
315
- ],
316
- factCheckerFactory: new JsonRulesEngineFactCheckerFactory({
317
- logger: env.logger,
318
- checks: [
319
- {
320
- id: 'groupOwnerCheck',
321
- type: JSON_RULE_ENGINE_CHECK_TYPE,
322
- name: 'Group Owner Check',
323
- description:
324
- 'Verifies that a Group has been set as the owner for this entity',
325
- factIds: ['entityOwnershipFactRetriever'],
326
- rule: {
327
- conditions: {
328
- all: [
329
- {
330
- fact: 'hasGroupOwner',
331
- operator: 'equal',
332
- value: true,
333
- },
334
- ],
335
- },
336
- },
337
- },
338
- {
339
- id: 'titleCheck',
340
- type: JSON_RULE_ENGINE_CHECK_TYPE,
341
- name: 'Title Check',
342
- description:
343
- 'Verifies that a Title, used to improve readability, has been set for this entity',
344
- factIds: ['entityMetadataFactRetriever'],
345
- rule: {
346
- conditions: {
347
- all: [
348
- {
349
- fact: 'hasTitle',
350
- operator: 'equal',
351
- value: true,
352
- },
353
- ],
354
- },
355
- },
356
- },
357
- {
358
- id: 'techDocsCheck',
359
- type: JSON_RULE_ENGINE_CHECK_TYPE,
360
- name: 'TechDocs Check',
361
- description:
362
- 'Verifies that TechDocs has been enabled for this entity',
363
- factIds: ['techdocsFactRetriever'],
364
- rule: {
365
- conditions: {
366
- all: [
367
- {
368
- fact: 'hasAnnotationBackstageIoTechdocsRef',
369
- operator: 'equal',
370
- value: true,
371
- },
372
- ],
373
- },
374
- },
375
- },
376
- ],
377
- }),
378
- });
379
-
380
- return await createRouter({
381
- ...techInsightsContext,
382
- logger: env.logger,
383
- config: env.config,
384
- });
385
- }
386
- ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage-community/plugin-tech-insights-backend",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "backstage": {
5
5
  "role": "backend-plugin",
6
6
  "pluginId": "tech-insights",
@@ -45,8 +45,8 @@
45
45
  "test": "backstage-cli package test"
46
46
  },
47
47
  "dependencies": {
48
- "@backstage-community/plugin-tech-insights-common": "^0.2.16",
49
- "@backstage-community/plugin-tech-insights-node": "^0.6.5",
48
+ "@backstage-community/plugin-tech-insights-common": "^0.2.18",
49
+ "@backstage-community/plugin-tech-insights-node": "^0.6.7",
50
50
  "@backstage/backend-common": "^0.24.0",
51
51
  "@backstage/backend-defaults": "^0.4.3",
52
52
  "@backstage/backend-plugin-api": "^0.8.0",