@backstage/plugin-catalog-backend 3.7.0-next.2 → 3.7.0
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.
- package/CHANGELOG.md +80 -0
- package/config.d.ts +5 -1
- package/dist/actions/createQueryCatalogEntitiesAction.cjs.js +57 -49
- package/dist/actions/createQueryCatalogEntitiesAction.cjs.js.map +1 -1
- package/dist/actions/index.cjs.js.map +1 -1
- package/dist/database/operations/stitcher/performStitching.cjs.js +10 -2
- package/dist/database/operations/stitcher/performStitching.cjs.js.map +1 -1
- package/dist/database/operations/stitcher/util.cjs.js +0 -11
- package/dist/database/operations/stitcher/util.cjs.js.map +1 -1
- package/dist/processing/DefaultCatalogProcessingEngine.cjs.js +3 -1
- package/dist/processing/DefaultCatalogProcessingEngine.cjs.js.map +1 -1
- package/dist/service/CatalogPlugin.cjs.js +4 -1
- package/dist/service/CatalogPlugin.cjs.js.map +1 -1
- package/dist/stitching/DefaultStitcher.cjs.js +3 -1
- package/dist/stitching/DefaultStitcher.cjs.js.map +1 -1
- package/dist/stitching/types.cjs.js +8 -1
- package/dist/stitching/types.cjs.js.map +1 -1
- package/migrations/20260516000000_relations_target_index.js +92 -0
- package/package.json +21 -21
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,85 @@
|
|
|
1
1
|
# @backstage/plugin-catalog-backend
|
|
2
2
|
|
|
3
|
+
## 3.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- c2de113: **BREAKING**: When paginating entities with an order field via `/entities/by-query`, entities that lack the order field are now excluded from both the result set and the `totalItems` count. Previously these entities appeared at the end of the sorted result via `NULLS LAST`, but cursor-based pagination could not actually reach them past the first page — the count over-reported the number of navigable entities. The new behavior aligns the count with what is actually returned.
|
|
8
|
+
|
|
9
|
+
This also removes the `DISTINCT` deduplication from the sort-field CTE, which is a prerequisite for the planner to use the `(key, value, entity_id)` index in sort order and short-circuit on `LIMIT`. Installations with duplicate search rows should land the search-table deduplication migration before adopting this change.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 3f5e7ec: Added `catalog.actions.experimentalCatalogLayersDescriptions.enabled` config option. When enabled, the `query-catalog-entities` action description references `get-catalog-model-description` for field information instead of embedding a static model description.
|
|
14
|
+
- ccbad9d: Improved the performance of the `catalog_entities_count` metric.
|
|
15
|
+
|
|
16
|
+
The legacy Prometheus and OpenTelemetry observable gauges previously each ran their own copy of the per-kind count query against the `search` table on every metrics scrape. On large catalogs this could pile up faster than the queries completed, contending for buffers and stalling the database.
|
|
17
|
+
|
|
18
|
+
The two callbacks now share a single query result with a short in-process TTL cache, and the underlying query reads from `final_entities` instead of `search`, avoiding the bitmap heap scans that dominated the previous form. The emitted labels and values are unchanged.
|
|
19
|
+
|
|
20
|
+
- 17a9550: Deprecated immediate mode stitching (`catalog.stitchingStrategy.mode: 'immediate'`). A warning is now logged on startup when immediate mode is configured. Immediate mode will be removed in the next Backstage release. Migrate to deferred mode (the default) by removing the `stitchingStrategy` configuration or setting `mode: 'deferred'`.
|
|
21
|
+
- add5d1a: Restructured the entity listing endpoint so that, when a sort field is specified, the search-by-key index drives the query rather than being side-joined onto `final_entities`. This lets PostgreSQL walk the `(key, value, entity_id)` index in already-sorted order and short-circuit on `LIMIT`, reducing typical broad-filter paginated list times from seconds to milliseconds. Entities that lack the sort field still appear at the end of sorted results (NULLS LAST semantics preserved), ordered by `entity_id`.
|
|
22
|
+
- 387ea7d: Simplified the entity facets aggregation from `COUNT(DISTINCT entity_id)` to `COUNT(*)`. The unique constraint on `(entity_id, key, value)` guarantees each entity appears at most once per search row group, making the `DISTINCT` unnecessary. This allows the database to use a simpler aggregation plan.
|
|
23
|
+
- 3f55b73: Improved the performance of the entity facets endpoint when filters are applied. The filtered entity set is now combined with the search table through an inner join rather than a `WHERE entity_id IN (subquery)`. Results are unchanged; on large catalogs the query planner is able to choose dramatically cheaper plans, with measured improvements ranging from roughly 1.2× on already-fast cases to 7× or more on high-cardinality facets.
|
|
24
|
+
- b33f845: Fixed several database migration `down` functions that were not properly reversible, causing the SQL report to show warnings:
|
|
25
|
+
|
|
26
|
+
- `20241003170511_alter_target_in_locations.js`: both `up` and `down` now include `.notNullable()` when altering the `locations.target` column, preventing the `NOT NULL` constraint from being accidentally dropped when widening the column type from `varchar(255)` to `text`.
|
|
27
|
+
- `20220116144621_remove_legacy.js`: the `down` function now properly recreates the three dropped legacy tables (`entities`, `entities_search`, `entities_relations`) with correct columns and indices.
|
|
28
|
+
- `20210302150147_refresh_state.js`: the `down` function now drops dependent tables in the correct order (avoiding a FK constraint violation) and fixes a typo where the table was referred to as `references` instead of `refresh_state_references`.
|
|
29
|
+
- `20201005122705_add_entity_full_name.js`: the `down` function now drops the `full_name` column from `entities` (not `entities_search`), and restores the `entities_unique_name` index with the correct column order `(kind, name, namespace)`.
|
|
30
|
+
- `20200702153613_entities.js`: the `down` function now uses `table.integer('generation')` instead of `table.string('generation')`, restoring the correct column type.
|
|
31
|
+
|
|
32
|
+
- cde3643: Added missing description to the `type` parameter on the `unregister-entity` MCP action.
|
|
33
|
+
- cf195de: Fixed a performance regression in the `/entity-facets` endpoint when filters or permission conditions are applied, by routing the EXISTS-based filter through `final_entities` instead of correlating against the much larger `search` table.
|
|
34
|
+
- 07ec25d: Moved `generateStableHash` out of shared utility file to avoid pulling `node:crypto` into browser bundles
|
|
35
|
+
- bc32c13: Added a missing index on `relations.target_entity_ref`. Several query paths (orphan deletion, entity ancestry, eager pruning) join or filter on this column, but no index existed — causing full sequential scans of the relations table on every invocation. On a production catalog with ~3.5M relation rows, individual lookups were taking ~122ms (full table scan) instead of <1ms (index scan).
|
|
36
|
+
- 744fa1f: Removed duplicated entries that appeared in both `dependencies` and `devDependencies`.
|
|
37
|
+
- e9b78e9: Removed the `uuid` dependency and replaced usage with the built-in `crypto.randomUUID()`.
|
|
38
|
+
- 7445f0f: Added a migration that removes duplicate rows from the `search` table, creates covering indices for improved query performance, and adds a `UNIQUE` constraint on `(entity_id, key, value)`.
|
|
39
|
+
|
|
40
|
+
This is a long-running migration on large catalogs. On PostgreSQL with millions of search rows, the index creation may take 5-15 minutes per index. During this time, other pods running the previous version will continue to serve traffic normally — the index creation does not block reads or writes. However, if a Kubernetes liveness probe kills the pod before the index build completes, the build is lost and the next startup will start over. On large tables this can repeat indefinitely.
|
|
41
|
+
|
|
42
|
+
**For large installations**, it is recommended to run the following SQL commands against your PostgreSQL database **before deploying** this version. Each index build takes a few minutes but does not block reads or writes. If these have already completed, the migration will detect the existing indices and skip all work — startup will be instant.
|
|
43
|
+
|
|
44
|
+
```sql
|
|
45
|
+
-- Step 1: Remove duplicate search rows
|
|
46
|
+
WITH cte AS (
|
|
47
|
+
SELECT ctid, row_number() OVER (PARTITION BY entity_id, key, value) AS rn
|
|
48
|
+
FROM search
|
|
49
|
+
)
|
|
50
|
+
DELETE FROM search USING cte WHERE search.ctid = cte.ctid AND cte.rn > 1;
|
|
51
|
+
|
|
52
|
+
-- Step 2: Create new indices (run each separately)
|
|
53
|
+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS
|
|
54
|
+
search_entity_key_value_idx ON search (entity_id, key, value);
|
|
55
|
+
CREATE INDEX CONCURRENTLY IF NOT EXISTS
|
|
56
|
+
search_key_value_entity_idx ON search (key, value, entity_id);
|
|
57
|
+
CREATE INDEX CONCURRENTLY IF NOT EXISTS
|
|
58
|
+
search_facets_covering_idx ON search (key, original_value, entity_id)
|
|
59
|
+
WHERE original_value IS NOT NULL;
|
|
60
|
+
|
|
61
|
+
-- Step 3: Drop old indices that are no longer needed
|
|
62
|
+
DROP INDEX CONCURRENTLY IF EXISTS search_key_value_idx;
|
|
63
|
+
DROP INDEX CONCURRENTLY IF EXISTS search_key_original_value_idx;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Also fixed `buildEntitySearch` to remove duplicate output for entities with duplicate array values, and added `ON CONFLICT DO UPDATE` to `syncSearchRows` so that concurrent stitching races are handled gracefully.
|
|
67
|
+
|
|
68
|
+
- Updated dependencies
|
|
69
|
+
- @backstage/catalog-model@1.9.0
|
|
70
|
+
- @backstage/errors@1.3.1
|
|
71
|
+
- @backstage/backend-plugin-api@1.9.1
|
|
72
|
+
- @backstage/plugin-catalog-node@2.2.1
|
|
73
|
+
- @backstage/filter-predicates@0.1.3
|
|
74
|
+
- @backstage/integration@2.0.2
|
|
75
|
+
- @backstage/plugin-permission-node@0.11.0
|
|
76
|
+
- @backstage/plugin-permission-common@0.9.9
|
|
77
|
+
- @backstage/backend-openapi-utils@0.6.9
|
|
78
|
+
- @backstage/catalog-client@1.15.1
|
|
79
|
+
- @backstage/config@1.3.8
|
|
80
|
+
- @backstage/plugin-catalog-common@1.1.10
|
|
81
|
+
- @backstage/plugin-events-node@0.4.22
|
|
82
|
+
|
|
3
83
|
## 3.7.0-next.2
|
|
4
84
|
|
|
5
85
|
### Minor Changes
|
package/config.d.ts
CHANGED
|
@@ -179,7 +179,11 @@ export interface Config {
|
|
|
179
179
|
*/
|
|
180
180
|
stitchingStrategy?:
|
|
181
181
|
| {
|
|
182
|
-
/**
|
|
182
|
+
/**
|
|
183
|
+
* Perform stitching in-band immediately when needed.
|
|
184
|
+
*
|
|
185
|
+
* @deprecated Immediate mode stitching has been deprecated and will be removed in a future release. Migrate to deferred mode (the default).
|
|
186
|
+
*/
|
|
183
187
|
mode: 'immediate';
|
|
184
188
|
}
|
|
185
189
|
| {
|
|
@@ -2,19 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
var filterPredicates = require('@backstage/filter-predicates');
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
const QUERY_SYNTAX = `
|
|
6
|
+
## Query Syntax
|
|
7
|
+
|
|
8
|
+
The query uses predicate expressions with dot-notation field paths.
|
|
9
|
+
|
|
10
|
+
Simple matching:
|
|
11
|
+
{ query: { kind: "Component" } }
|
|
12
|
+
{ query: { kind: "Component", "spec.type": "service" } }
|
|
13
|
+
|
|
14
|
+
Value operators:
|
|
15
|
+
{ query: { kind: { "$in": ["API", "Component"] } } }
|
|
16
|
+
{ query: { "metadata.annotations.backstage.io/techdocs-ref": { "$exists": true } } }
|
|
17
|
+
{ query: { "metadata.tags": { "$contains": "java" } } }
|
|
18
|
+
{ query: { "metadata.name": { "$hasPrefix": "team-" } } }
|
|
19
|
+
|
|
20
|
+
Logical operators:
|
|
21
|
+
{ query: { "$all": [{ kind: "Component" }, { "spec.lifecycle": "production" }] } }
|
|
22
|
+
{ query: { "$any": [{ "spec.type": "service" }, { "spec.type": "website" }] } }
|
|
23
|
+
{ query: { "$not": { kind: "Group" } } }
|
|
24
|
+
|
|
25
|
+
Querying relations - find all entities owned by a specific group:
|
|
26
|
+
{ query: { "relations.ownedby": "group:default/team-alpha" } }
|
|
27
|
+
|
|
28
|
+
Combined example - find production services or websites with TechDocs:
|
|
29
|
+
{ query: { "$all": [
|
|
30
|
+
{ kind: "Component", "spec.lifecycle": "production" },
|
|
31
|
+
{ "$any": [{ "spec.type": "service" }, { "spec.type": "website" }] },
|
|
32
|
+
{ "metadata.annotations.backstage.io/techdocs-ref": { "$exists": true } }
|
|
33
|
+
] } }
|
|
34
|
+
|
|
35
|
+
## Other Options
|
|
36
|
+
|
|
37
|
+
Limit returned fields: { fields: ["kind", "metadata.name", "metadata.namespace"] }
|
|
38
|
+
Sort results: { orderFields: { field: "metadata.name", order: "asc" } }
|
|
39
|
+
Full text search: { fullTextFilter: { term: "auth", fields: ["metadata.name", "metadata.title"] } }
|
|
40
|
+
Pagination: Use limit (e.g. 20) and the returned nextPageCursor for subsequent requests via cursor.
|
|
41
|
+
`;
|
|
42
|
+
const INLINE_MODEL_DESCRIPTION = `
|
|
18
43
|
Query entities from the Backstage Software Catalog using predicate filters.
|
|
19
44
|
|
|
20
45
|
## Catalog Model
|
|
@@ -58,43 +83,26 @@ Entities have bidirectional relations stored in the "relations" array. Common re
|
|
|
58
83
|
Relations can be queried via "relations.<type>" e.g. "relations.ownedby: user:default/jane-doe". The value there must always be a valid entity reference.
|
|
59
84
|
|
|
60
85
|
When querying for entity relationships, prefer using relations over spec fields. For example, use "relations.ownedby" instead of "spec.owner" to find entities owned by a particular group or user.
|
|
86
|
+
${QUERY_SYNTAX}`;
|
|
87
|
+
const MODEL_REFERENCE_DESCRIPTION = `
|
|
88
|
+
Query entities from the Backstage Software Catalog using predicate filters.
|
|
61
89
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
{ query: { "$any": [{ "spec.type": "service" }, { "spec.type": "website" }] } }
|
|
79
|
-
{ query: { "$not": { kind: "Group" } } }
|
|
80
|
-
|
|
81
|
-
Querying relations - find all entities owned by a specific group:
|
|
82
|
-
{ query: { "relations.ownedby": "group:default/team-alpha" } }
|
|
83
|
-
|
|
84
|
-
Combined example - find production services or websites with TechDocs:
|
|
85
|
-
{ query: { "$all": [
|
|
86
|
-
{ kind: "Component", "spec.lifecycle": "production" },
|
|
87
|
-
{ "$any": [{ "spec.type": "service" }, { "spec.type": "website" }] },
|
|
88
|
-
{ "metadata.annotations.backstage.io/techdocs-ref": { "$exists": true } }
|
|
89
|
-
] } }
|
|
90
|
-
|
|
91
|
-
## Other Options
|
|
92
|
-
|
|
93
|
-
Limit returned fields: { fields: ["kind", "metadata.name", "metadata.namespace"] }
|
|
94
|
-
Sort results: { orderFields: { field: "metadata.name", order: "asc" } }
|
|
95
|
-
Full text search: { fullTextFilter: { term: "auth", fields: ["metadata.name", "metadata.title"] } }
|
|
96
|
-
Pagination: Use limit (e.g. 20) and the returned nextPageCursor for subsequent requests via cursor.
|
|
97
|
-
`,
|
|
90
|
+
For a complete list of entity kinds, fields, relations, and other queryable attributes available in the catalog, use \`get-catalog-model-description\`.
|
|
91
|
+
${QUERY_SYNTAX}`;
|
|
92
|
+
const createQueryCatalogEntitiesAction = ({
|
|
93
|
+
catalog,
|
|
94
|
+
actionsRegistry,
|
|
95
|
+
useExperimentalCatalogLayersDescriptions
|
|
96
|
+
}) => {
|
|
97
|
+
actionsRegistry.register({
|
|
98
|
+
name: "query-catalog-entities",
|
|
99
|
+
title: "Query Catalog Entities",
|
|
100
|
+
attributes: {
|
|
101
|
+
destructive: false,
|
|
102
|
+
readOnly: true,
|
|
103
|
+
idempotent: true
|
|
104
|
+
},
|
|
105
|
+
description: useExperimentalCatalogLayersDescriptions ? MODEL_REFERENCE_DESCRIPTION : INLINE_MODEL_DESCRIPTION,
|
|
98
106
|
schema: {
|
|
99
107
|
input: (z) => z.object({
|
|
100
108
|
query: filterPredicates.createZodV3FilterPredicateSchema(z).optional().describe(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueryCatalogEntitiesAction.cjs.js","sources":["../../src/actions/createQueryCatalogEntitiesAction.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { ActionsRegistryService } from '@backstage/backend-plugin-api/alpha';\nimport { CatalogService } from '@backstage/plugin-catalog-node';\nimport { createZodV3FilterPredicateSchema } from '@backstage/filter-predicates';\n\nexport const createQueryCatalogEntitiesAction = ({\n catalog,\n actionsRegistry,\n}: {\n catalog: CatalogService;\n actionsRegistry: ActionsRegistryService;\n}) => {\n actionsRegistry.register({\n name: 'query-catalog-entities',\n title: 'Query Catalog Entities',\n attributes: {\n destructive: false,\n readOnly: true,\n idempotent: true,\n },\n description: `\nQuery entities from the Backstage Software Catalog using predicate filters.\n\n## Catalog Model\n\nThe catalog contains entities of different kinds. Every entity has \"kind\", \"apiVersion\", \"metadata\", and optionally \"spec\" and \"relations\". Fields use dot notation for querying.\n\nCommon metadata fields on all entities: name, namespace (default: \"default\"), title, description, labels, annotations, tags (string array), links.\n\nEntity references use the format \"kind:namespace/name\", e.g. \"component:default/my-service\" or \"user:default/jane.doe\".\n\n### Entity Kinds\n\n**Component** - A piece of software such as a service, website, or library.\n spec fields: type (e.g. \"service\", \"website\", \"library\"), lifecycle (e.g. \"production\", \"experimental\", \"deprecated\"), owner (entity ref), system, subcomponentOf, providesApis, consumesApis, dependsOn, dependencyOf.\n\n**API** - An interface that components expose, such as REST APIs or event streams.\n spec fields: type (e.g. \"openapi\", \"asyncapi\", \"graphql\", \"grpc\"), lifecycle, owner (entity ref), definition (the API spec content), system.\n\n**System** - A collection of components, APIs, and resources that together expose some functionality.\n spec fields: owner (entity ref), domain, type.\n\n**Domain** - A grouping of systems that share terminology, domain models, and business purpose.\n spec fields: owner (entity ref), subdomainOf, type.\n\n**Resource** - Infrastructure required to operate a component, such as databases or storage buckets.\n spec fields: type, owner (entity ref), system, dependsOn, dependencyOf.\n\n**Group** - An organizational entity such as a team or business unit.\n spec fields: type (e.g. \"team\", \"business-unit\"), children (entity refs), parent (entity ref), members (entity refs), profile (displayName, email, picture).\n\n**User** - A person, such as an employee or contractor.\n spec fields: memberOf (entity refs), profile (displayName, email, picture).\n\n**Location** - A marker that references other catalog descriptor files to be ingested.\n spec fields: type, target, targets, presence.\n\n### Relations\n\nEntities have bidirectional relations stored in the \"relations\" array. Common relation types: ownedBy/ownerOf, dependsOn/dependencyOf, providesApi/apiProvidedBy, consumesApi/apiConsumedBy, parentOf/childOf, memberOf/hasMember, partOf/hasPart.\n\nRelations can be queried via \"relations.<type>\" e.g. \"relations.ownedby: user:default/jane-doe\". The value there must always be a valid entity reference.\n\nWhen querying for entity relationships, prefer using relations over spec fields. For example, use \"relations.ownedby\" instead of \"spec.owner\" to find entities owned by a particular group or user.\n\n## Query Syntax\n\nThe query uses predicate expressions with dot-notation field paths.\n\nSimple matching:\n { query: { kind: \"Component\" } }\n { query: { kind: \"Component\", \"spec.type\": \"service\" } }\n\nValue operators:\n { query: { kind: { \"$in\": [\"API\", \"Component\"] } } }\n { query: { \"metadata.annotations.backstage.io/techdocs-ref\": { \"$exists\": true } } }\n { query: { \"metadata.tags\": { \"$contains\": \"java\" } } }\n { query: { \"metadata.name\": { \"$hasPrefix\": \"team-\" } } }\n\nLogical operators:\n { query: { \"$all\": [{ kind: \"Component\" }, { \"spec.lifecycle\": \"production\" }] } }\n { query: { \"$any\": [{ \"spec.type\": \"service\" }, { \"spec.type\": \"website\" }] } }\n { query: { \"$not\": { kind: \"Group\" } } }\n\nQuerying relations - find all entities owned by a specific group:\n { query: { \"relations.ownedby\": \"group:default/team-alpha\" } }\n\nCombined example - find production services or websites with TechDocs:\n { query: { \"$all\": [\n { kind: \"Component\", \"spec.lifecycle\": \"production\" },\n { \"$any\": [{ \"spec.type\": \"service\" }, { \"spec.type\": \"website\" }] },\n { \"metadata.annotations.backstage.io/techdocs-ref\": { \"$exists\": true } }\n ] } }\n\n## Other Options\n\nLimit returned fields: { fields: [\"kind\", \"metadata.name\", \"metadata.namespace\"] }\nSort results: { orderFields: { field: \"metadata.name\", order: \"asc\" } }\nFull text search: { fullTextFilter: { term: \"auth\", fields: [\"metadata.name\", \"metadata.title\"] } }\nPagination: Use limit (e.g. 20) and the returned nextPageCursor for subsequent requests via cursor.\n `,\n schema: {\n input: z =>\n z.object({\n query: createZodV3FilterPredicateSchema(z)\n .optional()\n .describe(\n 'Entity predicate query. Supports field matching, $all, $any, $not, $exists, $in, $contains, and $hasPrefix operators.',\n ),\n fields: z\n .array(z.string())\n .optional()\n .describe(\n 'Specific fields to include in the response. If not provided, all fields are returned. Each entry is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n limit: z\n .number()\n .int()\n .positive()\n .optional()\n .describe('Maximum number of entities to return at a time.'),\n offset: z\n .number()\n .int()\n .min(0)\n .optional()\n .describe('Number of entities to skip before returning results.'),\n orderFields: z\n .union([\n z.object({\n field: z\n .string()\n .describe(\n 'Field to order by. The format is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n order: z.enum(['asc', 'desc']).describe('Sort order'),\n }),\n z.array(\n z.object({\n field: z\n .string()\n .describe(\n 'Field to order by. The format is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n order: z.enum(['asc', 'desc']).describe('Sort order'),\n }),\n ),\n ])\n .optional()\n .describe(\n 'Ordering criteria for the results. Can be a single order directive or an array for multi-field sorting.',\n ),\n fullTextFilter: z\n .object({\n term: z.string().describe('Full text search term'),\n fields: z\n .array(z.string())\n .optional()\n .describe(\n 'Fields to search within. Each entry is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n })\n .optional()\n .describe('Full text search criteria'),\n cursor: z\n .string()\n .optional()\n .describe(\n 'Cursor for pagination. This can be used only after the first request with a response containing a cursor. If a cursor is given it takes precedence over `offset`.',\n ),\n }),\n output: z =>\n z.object({\n items: z\n .array(z.object({}).passthrough())\n .describe('List of entities'),\n totalItems: z.number().describe('Total number of entities'),\n hasMoreEntities: z\n .boolean()\n .describe('Whether more entities are available'),\n nextPageCursor: z\n .string()\n .optional()\n .describe('Next page cursor used to fetch next page of entities'),\n }),\n },\n action: async ({ input, credentials }) => {\n const response = await catalog.queryEntities(\n {\n ...input,\n query: input.query,\n },\n { credentials },\n );\n\n return {\n output: {\n items: response.items,\n totalItems: response.totalItems,\n hasMoreEntities: !!response.pageInfo.nextCursor,\n nextPageCursor: response.pageInfo.nextCursor,\n },\n };\n },\n });\n};\n"],"names":["createZodV3FilterPredicateSchema"],"mapp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EAAO,CAAE,SAAS,0BAA0B,CAAA;AAAA,QAC1D,eAAA,EAAiB,CAAA,CACd,OAAA,EAAQ,CACR,SAAS,qCAAqC,CAAA;AAAA,QACjD,gBAAgB,CAAA,CACb,MAAA,GACA,QAAA,EAAS,CACT,SAAS,sDAAsD;AAAA,OACnE;AAAA,KACL;AAAA,IACA,MAAA,EAAQ,OAAO,EAAE,KAAA,EAAO,aAAY,KAAM;AACxC,MAAA,MAAM,QAAA,GAAW,MAAM,OAAA,CAAQ,aAAA;AAAA,QAC7B;AAAA,UACE,GAAG,KAAA;AAAA,UACH,OAAO,KAAA,CAAM;AAAA,SACf;AAAA,QACA,EAAE,WAAA;AAAY,OAChB;AAEA,MAAA,OAAO;AAAA,QACL,MAAA,EAAQ;AAAA,UACN,OAAO,QAAA,CAAS,KAAA;AAAA,UAChB,YAAY,QAAA,CAAS,UAAA;AAAA,UACrB,eAAA,EAAiB,CAAC,CAAC,QAAA,CAAS,QAAA,CAAS,UAAA;AAAA,UACrC,cAAA,EAAgB,SAAS,QAAA,CAAS;AAAA;AACpC,OACF;AAAA,IACF;AAAA,GACD,CAAA;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"createQueryCatalogEntitiesAction.cjs.js","sources":["../../src/actions/createQueryCatalogEntitiesAction.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { ActionsRegistryService } from '@backstage/backend-plugin-api/alpha';\nimport { CatalogService } from '@backstage/plugin-catalog-node';\nimport { createZodV3FilterPredicateSchema } from '@backstage/filter-predicates';\n\nconst QUERY_SYNTAX = `\n## Query Syntax\n\nThe query uses predicate expressions with dot-notation field paths.\n\nSimple matching:\n { query: { kind: \"Component\" } }\n { query: { kind: \"Component\", \"spec.type\": \"service\" } }\n\nValue operators:\n { query: { kind: { \"$in\": [\"API\", \"Component\"] } } }\n { query: { \"metadata.annotations.backstage.io/techdocs-ref\": { \"$exists\": true } } }\n { query: { \"metadata.tags\": { \"$contains\": \"java\" } } }\n { query: { \"metadata.name\": { \"$hasPrefix\": \"team-\" } } }\n\nLogical operators:\n { query: { \"$all\": [{ kind: \"Component\" }, { \"spec.lifecycle\": \"production\" }] } }\n { query: { \"$any\": [{ \"spec.type\": \"service\" }, { \"spec.type\": \"website\" }] } }\n { query: { \"$not\": { kind: \"Group\" } } }\n\nQuerying relations - find all entities owned by a specific group:\n { query: { \"relations.ownedby\": \"group:default/team-alpha\" } }\n\nCombined example - find production services or websites with TechDocs:\n { query: { \"$all\": [\n { kind: \"Component\", \"spec.lifecycle\": \"production\" },\n { \"$any\": [{ \"spec.type\": \"service\" }, { \"spec.type\": \"website\" }] },\n { \"metadata.annotations.backstage.io/techdocs-ref\": { \"$exists\": true } }\n ] } }\n\n## Other Options\n\nLimit returned fields: { fields: [\"kind\", \"metadata.name\", \"metadata.namespace\"] }\nSort results: { orderFields: { field: \"metadata.name\", order: \"asc\" } }\nFull text search: { fullTextFilter: { term: \"auth\", fields: [\"metadata.name\", \"metadata.title\"] } }\nPagination: Use limit (e.g. 20) and the returned nextPageCursor for subsequent requests via cursor.\n`;\n\nconst INLINE_MODEL_DESCRIPTION = `\nQuery entities from the Backstage Software Catalog using predicate filters.\n\n## Catalog Model\n\nThe catalog contains entities of different kinds. Every entity has \"kind\", \"apiVersion\", \"metadata\", and optionally \"spec\" and \"relations\". Fields use dot notation for querying.\n\nCommon metadata fields on all entities: name, namespace (default: \"default\"), title, description, labels, annotations, tags (string array), links.\n\nEntity references use the format \"kind:namespace/name\", e.g. \"component:default/my-service\" or \"user:default/jane.doe\".\n\n### Entity Kinds\n\n**Component** - A piece of software such as a service, website, or library.\n spec fields: type (e.g. \"service\", \"website\", \"library\"), lifecycle (e.g. \"production\", \"experimental\", \"deprecated\"), owner (entity ref), system, subcomponentOf, providesApis, consumesApis, dependsOn, dependencyOf.\n\n**API** - An interface that components expose, such as REST APIs or event streams.\n spec fields: type (e.g. \"openapi\", \"asyncapi\", \"graphql\", \"grpc\"), lifecycle, owner (entity ref), definition (the API spec content), system.\n\n**System** - A collection of components, APIs, and resources that together expose some functionality.\n spec fields: owner (entity ref), domain, type.\n\n**Domain** - A grouping of systems that share terminology, domain models, and business purpose.\n spec fields: owner (entity ref), subdomainOf, type.\n\n**Resource** - Infrastructure required to operate a component, such as databases or storage buckets.\n spec fields: type, owner (entity ref), system, dependsOn, dependencyOf.\n\n**Group** - An organizational entity such as a team or business unit.\n spec fields: type (e.g. \"team\", \"business-unit\"), children (entity refs), parent (entity ref), members (entity refs), profile (displayName, email, picture).\n\n**User** - A person, such as an employee or contractor.\n spec fields: memberOf (entity refs), profile (displayName, email, picture).\n\n**Location** - A marker that references other catalog descriptor files to be ingested.\n spec fields: type, target, targets, presence.\n\n### Relations\n\nEntities have bidirectional relations stored in the \"relations\" array. Common relation types: ownedBy/ownerOf, dependsOn/dependencyOf, providesApi/apiProvidedBy, consumesApi/apiConsumedBy, parentOf/childOf, memberOf/hasMember, partOf/hasPart.\n\nRelations can be queried via \"relations.<type>\" e.g. \"relations.ownedby: user:default/jane-doe\". The value there must always be a valid entity reference.\n\nWhen querying for entity relationships, prefer using relations over spec fields. For example, use \"relations.ownedby\" instead of \"spec.owner\" to find entities owned by a particular group or user.\n${QUERY_SYNTAX}`;\n\nconst MODEL_REFERENCE_DESCRIPTION = `\nQuery entities from the Backstage Software Catalog using predicate filters.\n\nFor a complete list of entity kinds, fields, relations, and other queryable attributes available in the catalog, use \\`get-catalog-model-description\\`.\n${QUERY_SYNTAX}`;\n\nexport const createQueryCatalogEntitiesAction = ({\n catalog,\n actionsRegistry,\n useExperimentalCatalogLayersDescriptions,\n}: {\n catalog: CatalogService;\n actionsRegistry: ActionsRegistryService;\n useExperimentalCatalogLayersDescriptions?: boolean;\n}) => {\n actionsRegistry.register({\n name: 'query-catalog-entities',\n title: 'Query Catalog Entities',\n attributes: {\n destructive: false,\n readOnly: true,\n idempotent: true,\n },\n description: useExperimentalCatalogLayersDescriptions\n ? MODEL_REFERENCE_DESCRIPTION\n : INLINE_MODEL_DESCRIPTION,\n schema: {\n input: z =>\n z.object({\n query: createZodV3FilterPredicateSchema(z)\n .optional()\n .describe(\n 'Entity predicate query. Supports field matching, $all, $any, $not, $exists, $in, $contains, and $hasPrefix operators.',\n ),\n fields: z\n .array(z.string())\n .optional()\n .describe(\n 'Specific fields to include in the response. If not provided, all fields are returned. Each entry is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n limit: z\n .number()\n .int()\n .positive()\n .optional()\n .describe('Maximum number of entities to return at a time.'),\n offset: z\n .number()\n .int()\n .min(0)\n .optional()\n .describe('Number of entities to skip before returning results.'),\n orderFields: z\n .union([\n z.object({\n field: z\n .string()\n .describe(\n 'Field to order by. The format is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n order: z.enum(['asc', 'desc']).describe('Sort order'),\n }),\n z.array(\n z.object({\n field: z\n .string()\n .describe(\n 'Field to order by. The format is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n order: z.enum(['asc', 'desc']).describe('Sort order'),\n }),\n ),\n ])\n .optional()\n .describe(\n 'Ordering criteria for the results. Can be a single order directive or an array for multi-field sorting.',\n ),\n fullTextFilter: z\n .object({\n term: z.string().describe('Full text search term'),\n fields: z\n .array(z.string())\n .optional()\n .describe(\n 'Fields to search within. Each entry is a dot separated path into an entity, e.g. `spec.type`.',\n ),\n })\n .optional()\n .describe('Full text search criteria'),\n cursor: z\n .string()\n .optional()\n .describe(\n 'Cursor for pagination. This can be used only after the first request with a response containing a cursor. If a cursor is given it takes precedence over `offset`.',\n ),\n }),\n output: z =>\n z.object({\n items: z\n .array(z.object({}).passthrough())\n .describe('List of entities'),\n totalItems: z.number().describe('Total number of entities'),\n hasMoreEntities: z\n .boolean()\n .describe('Whether more entities are available'),\n nextPageCursor: z\n .string()\n .optional()\n .describe('Next page cursor used to fetch next page of entities'),\n }),\n },\n action: async ({ input, credentials }) => {\n const response = await catalog.queryEntities(\n {\n ...input,\n query: input.query,\n },\n { credentials },\n );\n\n return {\n output: {\n items: response.items,\n totalItems: response.totalItems,\n hasMoreEntities: !!response.pageInfo.nextCursor,\n nextPageCursor: response.pageInfo.nextCursor,\n },\n };\n },\n });\n};\n"],"names":["createZodV3FilterPredicateSchema"],"mappings":";;;;AAmBA,MAAM,YAAA,GAAe;AAAA;;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AAsCrB,MAAM,wBAAA,GAA2B;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;;AAAA;;AAAA;;AAAA;AAAA,EA4C/B,YAAY,CAAA,CAAA;AAEd,MAAM,2BAAA,GAA8B;AAAA;;AAAA;AAAA,EAIlC,YAAY,CAAA,CAAA;AAEP,MAAM,mCAAmC,CAAC;AAAA,EAC/C,OAAA;AAAA,EACA,eAAA;AAAA,EACA;AACF,CAAA,KAIM;AACJ,EAAA,eAAA,CAAgB,QAAA,CAAS;AAAA,IACvB,IAAA,EAAM,wBAAA;AAAA,IACN,KAAA,EAAO,wBAAA;AAAA,IACP,UAAA,EAAY;AAAA,MACV,WAAA,EAAa,KAAA;AAAA,MACb,QAAA,EAAU,IAAA;AAAA,MACV,UAAA,EAAY;AAAA,KACd;AAAA,IACA,WAAA,EAA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;AACpC,OACF;AAAA,IACF;AAAA,GACD,CAAA;AACH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../../src/actions/index.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ActionsRegistryService } from '@backstage/backend-plugin-api/alpha';\nimport { CatalogService } from '@backstage/plugin-catalog-node';\nimport { ModelHolder } from '../model/ModelHolder';\nimport { createGetCatalogModelDescriptionAction } from './createGetCatalogModelDescriptionAction.ts';\nimport { createGetCatalogEntityAction } from './createGetCatalogEntityAction.ts';\nimport { createValidateEntityAction } from './createValidateEntityAction.ts';\nimport { createRegisterCatalogEntitiesAction } from './createRegisterCatalogEntitiesAction.ts';\nimport { createUnregisterCatalogEntitiesAction } from './createUnregisterCatalogEntitiesAction.ts';\nimport { createQueryCatalogEntitiesAction } from './createQueryCatalogEntitiesAction.ts';\n\nexport const createCatalogActions = (options: {\n actionsRegistry: ActionsRegistryService;\n catalog: CatalogService;\n modelHolder: ModelHolder | undefined;\n}) => {\n createGetCatalogModelDescriptionAction(options);\n createGetCatalogEntityAction(options);\n createValidateEntityAction(options);\n createRegisterCatalogEntitiesAction(options);\n createUnregisterCatalogEntitiesAction(options);\n createQueryCatalogEntitiesAction(options);\n};\n"],"names":["createGetCatalogModelDescriptionAction","createGetCatalogEntityAction","createValidateEntityAction","createRegisterCatalogEntitiesAction","createUnregisterCatalogEntitiesAction","createQueryCatalogEntitiesAction"],"mappings":";;;;;;;;;AA0BO,MAAM,oBAAA,GAAuB,CAAC,OAAA,
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../../src/actions/index.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ActionsRegistryService } from '@backstage/backend-plugin-api/alpha';\nimport { CatalogService } from '@backstage/plugin-catalog-node';\nimport { ModelHolder } from '../model/ModelHolder';\nimport { createGetCatalogModelDescriptionAction } from './createGetCatalogModelDescriptionAction.ts';\nimport { createGetCatalogEntityAction } from './createGetCatalogEntityAction.ts';\nimport { createValidateEntityAction } from './createValidateEntityAction.ts';\nimport { createRegisterCatalogEntitiesAction } from './createRegisterCatalogEntitiesAction.ts';\nimport { createUnregisterCatalogEntitiesAction } from './createUnregisterCatalogEntitiesAction.ts';\nimport { createQueryCatalogEntitiesAction } from './createQueryCatalogEntitiesAction.ts';\n\nexport const createCatalogActions = (options: {\n actionsRegistry: ActionsRegistryService;\n catalog: CatalogService;\n modelHolder: ModelHolder | undefined;\n useExperimentalCatalogLayersDescriptions?: boolean;\n}) => {\n createGetCatalogModelDescriptionAction(options);\n createGetCatalogEntityAction(options);\n createValidateEntityAction(options);\n createRegisterCatalogEntitiesAction(options);\n createUnregisterCatalogEntitiesAction(options);\n createQueryCatalogEntitiesAction(options);\n};\n"],"names":["createGetCatalogModelDescriptionAction","createGetCatalogEntityAction","createValidateEntityAction","createRegisterCatalogEntitiesAction","createUnregisterCatalogEntitiesAction","createQueryCatalogEntitiesAction"],"mappings":";;;;;;;;;AA0BO,MAAM,oBAAA,GAAuB,CAAC,OAAA,KAK/B;AACJ,EAAAA,6EAAA,CAAuC,OAAO,CAAA;AAC9C,EAAAC,yDAAA,CAA6B,OAAO,CAAA;AACpC,EAAAC,qDAAA,CAA2B,OAAO,CAAA;AAClC,EAAAC,uEAAA,CAAoC,OAAO,CAAA;AAC3C,EAAAC,2EAAA,CAAsC,OAAO,CAAA;AAC7C,EAAAC,iEAAA,CAAiC,OAAO,CAAA;AAC1C;;;;"}
|
|
@@ -2,12 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
var catalogClient = require('@backstage/catalog-client');
|
|
4
4
|
var catalogModel = require('@backstage/catalog-model');
|
|
5
|
+
var node_crypto = require('node:crypto');
|
|
6
|
+
var stableStringify = require('fast-json-stable-stringify');
|
|
5
7
|
var buildEntitySearch = require('./buildEntitySearch.cjs.js');
|
|
6
8
|
var markDeferredStitchCompleted = require('./markDeferredStitchCompleted.cjs.js');
|
|
7
9
|
var syncSearchRows = require('./syncSearchRows.cjs.js');
|
|
8
|
-
var util = require('./util.cjs.js');
|
|
9
10
|
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
10
11
|
|
|
12
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
13
|
+
|
|
14
|
+
var stableStringify__default = /*#__PURE__*/_interopDefaultCompat(stableStringify);
|
|
15
|
+
|
|
16
|
+
function generateStableHash(entity) {
|
|
17
|
+
return node_crypto.createHash("sha1").update(stableStringify__default.default({ ...entity })).digest("hex");
|
|
18
|
+
}
|
|
11
19
|
const scriptProtocolPattern = (
|
|
12
20
|
// eslint-disable-next-line no-control-regex
|
|
13
21
|
/^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*\:/i
|
|
@@ -110,7 +118,7 @@ async function performStitching(options) {
|
|
|
110
118
|
items: [...entity.status?.items ?? [], ...statusItems]
|
|
111
119
|
};
|
|
112
120
|
}
|
|
113
|
-
const hash =
|
|
121
|
+
const hash = generateStableHash(entity);
|
|
114
122
|
if (hash === previousHash) {
|
|
115
123
|
logger.debug(`Skipped stitching of ${entityRef}, no changes`);
|
|
116
124
|
return "unchanged";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"performStitching.cjs.js","sources":["../../../../src/database/operations/stitcher/performStitching.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ENTITY_STATUS_CATALOG_PROCESSING_TYPE } from '@backstage/catalog-client';\nimport {\n ANNOTATION_EDIT_URL,\n ANNOTATION_VIEW_URL,\n EntityRelation,\n} from '@backstage/catalog-model';\nimport { AlphaEntity, EntityStatusItem } from '@backstage/catalog-model/alpha';\nimport { SerializedError } from '@backstage/errors';\nimport { Knex } from 'knex';\nimport { StitchingStrategy } from '../../../stitching/types';\nimport {\n DbFinalEntitiesRow,\n DbRefreshStateRow,\n DbStitchQueueRow,\n} from '../../tables';\nimport { buildEntitySearch } from './buildEntitySearch';\nimport { markDeferredStitchCompleted } from './markDeferredStitchCompleted';\nimport { syncSearchRows } from './syncSearchRows';\nimport { generateStableHash } from './util';\nimport {\n LoggerService,\n isDatabaseConflictError,\n} from '@backstage/backend-plugin-api';\n\n// See https://github.com/facebook/react/blob/f0cf832e1d0c8544c36aa8b310960885a11a847c/packages/react-dom-bindings/src/shared/sanitizeURL.js\nconst scriptProtocolPattern =\n // eslint-disable-next-line no-control-regex\n /^[\\u0000-\\u001F ]*j[\\r\\n\\t]*a[\\r\\n\\t]*v[\\r\\n\\t]*a[\\r\\n\\t]*s[\\r\\n\\t]*c[\\r\\n\\t]*r[\\r\\n\\t]*i[\\r\\n\\t]*p[\\r\\n\\t]*t[\\r\\n\\t]*\\:/i;\n\n/**\n * Performs the act of stitching - to take all of the various outputs from the\n * ingestion process, and stitching them together into the final entity JSON\n * shape.\n */\nexport async function performStitching(options: {\n knex: Knex | Knex.Transaction;\n logger: LoggerService;\n strategy: StitchingStrategy;\n entityRef: string;\n stitchTicket?: string;\n}): Promise<'changed' | 'unchanged' | 'abandoned'> {\n const { knex, logger, entityRef } = options;\n const stitchTicket = options.stitchTicket;\n\n // In deferred mode, the entity is removed from the stitch queue on ANY\n // completion, except when an exception is thrown. In the latter case, the\n // entity will be retried at a later time.\n let removeFromStitchQueueOnCompletion = options.strategy.mode === 'deferred';\n\n try {\n const entityResult = await knex<DbRefreshStateRow>('refresh_state')\n .where({ entity_ref: entityRef })\n .limit(1)\n .select('entity_id');\n if (!entityResult.length) {\n // Entity does no exist in refresh state table, no stitching required.\n return 'abandoned';\n }\n\n // Ensure that a final_entities row exists for this entity.\n try {\n await knex<DbFinalEntitiesRow>('final_entities')\n .insert({\n entity_id: entityResult[0].entity_id,\n hash: '',\n entity_ref: entityRef,\n })\n .onConflict('entity_id')\n .ignore();\n } catch (error) {\n // It's possible to hit a race where a refresh_state table delete + insert\n // is done just after we read the entity_id from it. This conflict is safe\n // to ignore because the current stitching operation will be triggered by\n // the old entry, and the new entry will trigger it's own stitching that\n // will update the entity.\n if (isDatabaseConflictError(error)) {\n logger.debug(`Skipping stitching of ${entityRef}, conflict`, error);\n return 'abandoned';\n }\n\n throw error;\n }\n\n // Selecting from refresh_state and final_entities should yield exactly\n // one row (except in abnormal cases where the stitch was invoked for\n // something that didn't exist at all, in which case it's zero rows).\n // The join with the temporary incoming_references still gives one row.\n const [processedResult, relationsResult] = await Promise.all([\n knex\n .with('incoming_references', function incomingReferences(builder) {\n return builder\n .from('refresh_state_references')\n .where({ target_entity_ref: entityRef })\n .count({ count: '*' });\n })\n .select({\n entityId: 'refresh_state.entity_id',\n processedEntity: 'refresh_state.processed_entity',\n errors: 'refresh_state.errors',\n incomingReferenceCount: 'incoming_references.count',\n previousHash: 'final_entities.hash',\n })\n .from('refresh_state')\n .where({ 'refresh_state.entity_ref': entityRef })\n .crossJoin(knex.raw('incoming_references'))\n .leftOuterJoin('final_entities', {\n 'final_entities.entity_id': 'refresh_state.entity_id',\n }),\n knex\n .distinct({\n relationType: 'type',\n relationTarget: 'target_entity_ref',\n })\n .from('relations')\n .where({ source_entity_ref: entityRef })\n .orderBy('relationType', 'asc')\n .orderBy('relationTarget', 'asc'),\n ]);\n\n // If there were no rows returned, it would mean that there was no\n // matching row even in the refresh_state. This can happen for example\n // if we emit a relation to something that hasn't been ingested yet.\n // It's safe to ignore this stitch attempt in that case.\n if (!processedResult.length) {\n logger.debug(\n `Unable to stitch ${entityRef}, item does not exist in refresh state table`,\n );\n return 'abandoned';\n }\n\n const {\n entityId,\n processedEntity,\n errors,\n incomingReferenceCount,\n previousHash,\n } = processedResult[0];\n\n // If there was no processed entity in place, the target hasn't been\n // through the processing steps yet. It's safe to ignore this stitch\n // attempt in that case, since another stitch will be triggered when\n // that processing has finished.\n if (!processedEntity) {\n logger.debug(\n `Unable to stitch ${entityRef}, the entity has not yet been processed`,\n );\n return 'abandoned';\n }\n\n // Grab the processed entity and stitch all of the relevant data into\n // it\n const entity = JSON.parse(processedEntity) as AlphaEntity;\n const isOrphan = Number(incomingReferenceCount) === 0;\n let statusItems: EntityStatusItem[] = [];\n\n if (isOrphan) {\n logger.debug(`${entityRef} is an orphan`);\n entity.metadata.annotations = {\n ...entity.metadata.annotations,\n ['backstage.io/orphan']: 'true',\n };\n }\n if (errors) {\n const parsedErrors = JSON.parse(errors) as SerializedError[];\n if (Array.isArray(parsedErrors) && parsedErrors.length) {\n statusItems = parsedErrors.map(e => ({\n type: ENTITY_STATUS_CATALOG_PROCESSING_TYPE,\n level: 'error',\n message: `${e.name}: ${e.message}`,\n error: e,\n }));\n }\n }\n // We opt to do this check here as we otherwise can't guarantee that it will be run after all processors\n for (const annotation of [ANNOTATION_VIEW_URL, ANNOTATION_EDIT_URL]) {\n const value = entity.metadata.annotations?.[annotation];\n if (typeof value === 'string' && scriptProtocolPattern.test(value)) {\n entity.metadata.annotations![annotation] =\n 'https://backstage.io/annotation-rejected-for-security-reasons';\n }\n }\n\n // TODO: entityRef is lower case and should be uppercase in the final\n // result\n entity.relations = relationsResult\n .filter(row => row.relationType /* exclude null row, if relevant */)\n .map<EntityRelation>(row => ({\n type: row.relationType!,\n targetRef: row.relationTarget!,\n }));\n if (statusItems.length) {\n entity.status = {\n ...entity.status,\n items: [...(entity.status?.items ?? []), ...statusItems],\n };\n }\n\n // If the output entity was actually not changed, just abort\n const hash = generateStableHash(entity);\n if (hash === previousHash) {\n logger.debug(`Skipped stitching of ${entityRef}, no changes`);\n return 'unchanged';\n }\n\n entity.metadata.uid = entityId;\n if (!entity.metadata.etag) {\n // If the original data source did not have its own etag handling,\n // use the hash as a good-quality etag\n entity.metadata.etag = hash;\n }\n\n // This may throw if the entity is invalid, so we call it before\n // the final_entities write, even though we may end up not needing\n // to write the search index.\n const searchEntries = buildEntitySearch(entityId, entity);\n\n let updateQuery = knex<DbFinalEntitiesRow>('final_entities')\n .update({\n final_entity: JSON.stringify(entity),\n hash,\n last_updated_at: knex.fn.now(),\n })\n .where('entity_id', entityId);\n\n // In deferred mode, guard against concurrent stitchers by checking that\n // the stitch_ticket in stitch_queue still matches what we were given.\n if (options.strategy.mode === 'deferred' && stitchTicket) {\n updateQuery = updateQuery.whereExists(\n knex<DbStitchQueueRow>('stitch_queue')\n .where('stitch_queue.entity_ref', entityRef)\n .where('stitch_queue.stitch_ticket', stitchTicket)\n .select(knex.raw('1')),\n );\n }\n\n const amountOfRowsChanged = await updateQuery;\n\n if (amountOfRowsChanged === 0) {\n logger.debug(`Entity ${entityRef} is already stitched, skipping write.`);\n return 'abandoned';\n }\n\n await syncSearchRows(knex, entityId, searchEntries);\n\n return 'changed';\n } catch (error) {\n removeFromStitchQueueOnCompletion = false;\n throw error;\n } finally {\n if (removeFromStitchQueueOnCompletion && stitchTicket) {\n await markDeferredStitchCompleted({\n knex: knex,\n entityRef,\n stitchTicket,\n });\n }\n }\n}\n"],"names":["isDatabaseConflictError","ENTITY_STATUS_CATALOG_PROCESSING_TYPE","ANNOTATION_VIEW_URL","ANNOTATION_EDIT_URL","generateStableHash","buildEntitySearch","syncSearchRows","markDeferredStitchCompleted"],"mappings":";;;;;;;;;;AAyCA,MAAM,qBAAA;AAAA;AAAA,EAEJ;AAAA,CAAA;AAOF,eAAsB,iBAAiB,OAAA,EAMY;AACjD,EAAA,MAAM,EAAE,IAAA,EAAM,MAAA,EAAQ,SAAA,EAAU,GAAI,OAAA;AACpC,EAAA,MAAM,eAAe,OAAA,CAAQ,YAAA;AAK7B,EAAA,IAAI,iCAAA,GAAoC,OAAA,CAAQ,QAAA,CAAS,IAAA,KAAS,UAAA;AAElE,EAAA,IAAI;AACF,IAAA,MAAM,YAAA,GAAe,MAAM,IAAA,CAAwB,eAAe,EAC/D,KAAA,CAAM,EAAE,UAAA,EAAY,SAAA,EAAW,CAAA,CAC/B,KAAA,CAAM,CAAC,CAAA,CACP,OAAO,WAAW,CAAA;AACrB,IAAA,IAAI,CAAC,aAAa,MAAA,EAAQ;AAExB,MAAA,OAAO,WAAA;AAAA,IACT;AAGA,IAAA,IAAI;AACF,MAAA,MAAM,IAAA,CAAyB,gBAAgB,CAAA,CAC5C,MAAA,CAAO;AAAA,QACN,SAAA,EAAW,YAAA,CAAa,CAAC,CAAA,CAAE,SAAA;AAAA,QAC3B,IAAA,EAAM,EAAA;AAAA,QACN,UAAA,EAAY;AAAA,OACb,CAAA,CACA,UAAA,CAAW,WAAW,EACtB,MAAA,EAAO;AAAA,IACZ,SAAS,KAAA,EAAO;AAMd,MAAA,IAAIA,wCAAA,CAAwB,KAAK,CAAA,EAAG;AAClC,QAAA,MAAA,CAAO,KAAA,CAAM,CAAA,sBAAA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AA,IAAI,WAAA,GAAc,IAAA,CAAyB,gBAAgB,CAAA,CACxD,MAAA,CAAO;AAAA,MACN,YAAA,EAAc,IAAA,CAAK,SAAA,CAAU,MAAM,CAAA;AAAA,MACnC,IAAA;AAAA,MACA,eAAA,EAAiB,IAAA,CAAK,EAAA,CAAG,GAAA;AAAI,KAC9B,CAAA,CACA,KAAA,CAAM,WAAA,EAAa,QAAQ,CAAA;AAI9B,IAAA,IAAI,OAAA,CAAQ,QAAA,CAAS,IAAA,KAAS,UAAA,IAAc,YAAA,EAAc;AACxD,MAAA,WAAA,GAAc,WAAA,CAAY,WAAA;AAAA,QACxB,IAAA,CAAuB,cAAc,CAAA,CAClC,KAAA,CAAM,2BAA2B,SAAS,CAAA,CAC1C,KAAA,CAAM,4BAAA,EAA8B,YAAY,CAAA,CAChD,MAAA,CAAO,IAAA,CAAK,GAAA,CAAI,GAAG,CAAC;AAAA,OACzB;AAAA,IACF;AAEA,IAAA,MAAM,sBAAsB,MAAM,WAAA;AAElC,IAAA,IAAI,wBAAwB,CAAA,EAAG;AAC7B,MAAA,MAAA,CAAO,KAAA,CAAM,CAAA,OAAA,EAAU,SAAS,CAAA,qCAAA,CAAuC,CAAA;AACvE,MAAA,OAAO,WAAA;AAAA,IACT;AAEA,IAAA,MAAMC,6BAAA,CAAe,IAAA,EAAM,QAAA,EAAU,aAAa,CAAA;AAElD,IAAA,OAAO,SAAA;AAAA,EACT,SAAS,KAAA,EAAO;AACd,IAAA,iCAAA,GAAoC,KAAA;AACpC,IAAA,MAAM,KAAA;AAAA,EACR,CAAA,SAAE;AACA,IAAA,IAAI,qCAAqC,YAAA,EAAc;AACrD,MAAA,MAAMC,uDAAA,CAA4B;AAAA,QAChC,IAAA;AAAA,QACA,SAAA;AAAA,QACA;AAAA,OACD,CAAA;AAAA,IACH;AAAA,EACF;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"performStitching.cjs.js","sources":["../../../../src/database/operations/stitcher/performStitching.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ENTITY_STATUS_CATALOG_PROCESSING_TYPE } from '@backstage/catalog-client';\nimport {\n ANNOTATION_EDIT_URL,\n ANNOTATION_VIEW_URL,\n Entity,\n EntityRelation,\n} from '@backstage/catalog-model';\nimport { AlphaEntity, EntityStatusItem } from '@backstage/catalog-model/alpha';\nimport { SerializedError } from '@backstage/errors';\nimport { Knex } from 'knex';\nimport { createHash } from 'node:crypto';\nimport stableStringify from 'fast-json-stable-stringify';\nimport { StitchingStrategy } from '../../../stitching/types';\nimport {\n DbFinalEntitiesRow,\n DbRefreshStateRow,\n DbStitchQueueRow,\n} from '../../tables';\nimport { buildEntitySearch } from './buildEntitySearch';\nimport { markDeferredStitchCompleted } from './markDeferredStitchCompleted';\nimport { syncSearchRows } from './syncSearchRows';\nimport {\n LoggerService,\n isDatabaseConflictError,\n} from '@backstage/backend-plugin-api';\n\nfunction generateStableHash(entity: Entity) {\n return createHash('sha1')\n .update(stableStringify({ ...entity }))\n .digest('hex');\n}\n\n// See https://github.com/facebook/react/blob/f0cf832e1d0c8544c36aa8b310960885a11a847c/packages/react-dom-bindings/src/shared/sanitizeURL.js\nconst scriptProtocolPattern =\n // eslint-disable-next-line no-control-regex\n /^[\\u0000-\\u001F ]*j[\\r\\n\\t]*a[\\r\\n\\t]*v[\\r\\n\\t]*a[\\r\\n\\t]*s[\\r\\n\\t]*c[\\r\\n\\t]*r[\\r\\n\\t]*i[\\r\\n\\t]*p[\\r\\n\\t]*t[\\r\\n\\t]*\\:/i;\n\n/**\n * Performs the act of stitching - to take all of the various outputs from the\n * ingestion process, and stitching them together into the final entity JSON\n * shape.\n */\nexport async function performStitching(options: {\n knex: Knex | Knex.Transaction;\n logger: LoggerService;\n strategy: StitchingStrategy;\n entityRef: string;\n stitchTicket?: string;\n}): Promise<'changed' | 'unchanged' | 'abandoned'> {\n const { knex, logger, entityRef } = options;\n const stitchTicket = options.stitchTicket;\n\n // In deferred mode, the entity is removed from the stitch queue on ANY\n // completion, except when an exception is thrown. In the latter case, the\n // entity will be retried at a later time.\n let removeFromStitchQueueOnCompletion = options.strategy.mode === 'deferred';\n\n try {\n const entityResult = await knex<DbRefreshStateRow>('refresh_state')\n .where({ entity_ref: entityRef })\n .limit(1)\n .select('entity_id');\n if (!entityResult.length) {\n // Entity does no exist in refresh state table, no stitching required.\n return 'abandoned';\n }\n\n // Ensure that a final_entities row exists for this entity.\n try {\n await knex<DbFinalEntitiesRow>('final_entities')\n .insert({\n entity_id: entityResult[0].entity_id,\n hash: '',\n entity_ref: entityRef,\n })\n .onConflict('entity_id')\n .ignore();\n } catch (error) {\n // It's possible to hit a race where a refresh_state table delete + insert\n // is done just after we read the entity_id from it. This conflict is safe\n // to ignore because the current stitching operation will be triggered by\n // the old entry, and the new entry will trigger it's own stitching that\n // will update the entity.\n if (isDatabaseConflictError(error)) {\n logger.debug(`Skipping stitching of ${entityRef}, conflict`, error);\n return 'abandoned';\n }\n\n throw error;\n }\n\n // Selecting from refresh_state and final_entities should yield exactly\n // one row (except in abnormal cases where the stitch was invoked for\n // something that didn't exist at all, in which case it's zero rows).\n // The join with the temporary incoming_references still gives one row.\n const [processedResult, relationsResult] = await Promise.all([\n knex\n .with('incoming_references', function incomingReferences(builder) {\n return builder\n .from('refresh_state_references')\n .where({ target_entity_ref: entityRef })\n .count({ count: '*' });\n })\n .select({\n entityId: 'refresh_state.entity_id',\n processedEntity: 'refresh_state.processed_entity',\n errors: 'refresh_state.errors',\n incomingReferenceCount: 'incoming_references.count',\n previousHash: 'final_entities.hash',\n })\n .from('refresh_state')\n .where({ 'refresh_state.entity_ref': entityRef })\n .crossJoin(knex.raw('incoming_references'))\n .leftOuterJoin('final_entities', {\n 'final_entities.entity_id': 'refresh_state.entity_id',\n }),\n knex\n .distinct({\n relationType: 'type',\n relationTarget: 'target_entity_ref',\n })\n .from('relations')\n .where({ source_entity_ref: entityRef })\n .orderBy('relationType', 'asc')\n .orderBy('relationTarget', 'asc'),\n ]);\n\n // If there were no rows returned, it would mean that there was no\n // matching row even in the refresh_state. This can happen for example\n // if we emit a relation to something that hasn't been ingested yet.\n // It's safe to ignore this stitch attempt in that case.\n if (!processedResult.length) {\n logger.debug(\n `Unable to stitch ${entityRef}, item does not exist in refresh state table`,\n );\n return 'abandoned';\n }\n\n const {\n entityId,\n processedEntity,\n errors,\n incomingReferenceCount,\n previousHash,\n } = processedResult[0];\n\n // If there was no processed entity in place, the target hasn't been\n // through the processing steps yet. It's safe to ignore this stitch\n // attempt in that case, since another stitch will be triggered when\n // that processing has finished.\n if (!processedEntity) {\n logger.debug(\n `Unable to stitch ${entityRef}, the entity has not yet been processed`,\n );\n return 'abandoned';\n }\n\n // Grab the processed entity and stitch all of the relevant data into\n // it\n const entity = JSON.parse(processedEntity) as AlphaEntity;\n const isOrphan = Number(incomingReferenceCount) === 0;\n let statusItems: EntityStatusItem[] = [];\n\n if (isOrphan) {\n logger.debug(`${entityRef} is an orphan`);\n entity.metadata.annotations = {\n ...entity.metadata.annotations,\n ['backstage.io/orphan']: 'true',\n };\n }\n if (errors) {\n const parsedErrors = JSON.parse(errors) as SerializedError[];\n if (Array.isArray(parsedErrors) && parsedErrors.length) {\n statusItems = parsedErrors.map(e => ({\n type: ENTITY_STATUS_CATALOG_PROCESSING_TYPE,\n level: 'error',\n message: `${e.name}: ${e.message}`,\n error: e,\n }));\n }\n }\n // We opt to do this check here as we otherwise can't guarantee that it will be run after all processors\n for (const annotation of [ANNOTATION_VIEW_URL, ANNOTATION_EDIT_URL]) {\n const value = entity.metadata.annotations?.[annotation];\n if (typeof value === 'string' && scriptProtocolPattern.test(value)) {\n entity.metadata.annotations![annotation] =\n 'https://backstage.io/annotation-rejected-for-security-reasons';\n }\n }\n\n // TODO: entityRef is lower case and should be uppercase in the final\n // result\n entity.relations = relationsResult\n .filter(row => row.relationType /* exclude null row, if relevant */)\n .map<EntityRelation>(row => ({\n type: row.relationType!,\n targetRef: row.relationTarget!,\n }));\n if (statusItems.length) {\n entity.status = {\n ...entity.status,\n items: [...(entity.status?.items ?? []), ...statusItems],\n };\n }\n\n // If the output entity was actually not changed, just abort\n const hash = generateStableHash(entity);\n if (hash === previousHash) {\n logger.debug(`Skipped stitching of ${entityRef}, no changes`);\n return 'unchanged';\n }\n\n entity.metadata.uid = entityId;\n if (!entity.metadata.etag) {\n // If the original data source did not have its own etag handling,\n // use the hash as a good-quality etag\n entity.metadata.etag = hash;\n }\n\n // This may throw if the entity is invalid, so we call it before\n // the final_entities write, even though we may end up not needing\n // to write the search index.\n const searchEntries = buildEntitySearch(entityId, entity);\n\n let updateQuery = knex<DbFinalEntitiesRow>('final_entities')\n .update({\n final_entity: JSON.stringify(entity),\n hash,\n last_updated_at: knex.fn.now(),\n })\n .where('entity_id', entityId);\n\n // In deferred mode, guard against concurrent stitchers by checking that\n // the stitch_ticket in stitch_queue still matches what we were given.\n if (options.strategy.mode === 'deferred' && stitchTicket) {\n updateQuery = updateQuery.whereExists(\n knex<DbStitchQueueRow>('stitch_queue')\n .where('stitch_queue.entity_ref', entityRef)\n .where('stitch_queue.stitch_ticket', stitchTicket)\n .select(knex.raw('1')),\n );\n }\n\n const amountOfRowsChanged = await updateQuery;\n\n if (amountOfRowsChanged === 0) {\n logger.debug(`Entity ${entityRef} is already stitched, skipping write.`);\n return 'abandoned';\n }\n\n await syncSearchRows(knex, entityId, searchEntries);\n\n return 'changed';\n } catch (error) {\n removeFromStitchQueueOnCompletion = false;\n throw error;\n } finally {\n if (removeFromStitchQueueOnCompletion && stitchTicket) {\n await markDeferredStitchCompleted({\n knex: knex,\n entityRef,\n stitchTicket,\n });\n }\n }\n}\n"],"names":["createHash","stableStringify","isDatabaseConflictError","ENTITY_STATUS_CATALOG_PROCESSING_TYPE","ANNOTATION_VIEW_URL","ANNOTATION_EDIT_URL","buildEntitySearch","syncSearchRows","markDeferredStitchCompleted"],"mappings":";;;;;;;;;;;;;;;AA0CA,SAAS,mBAAmB,MAAA,EAAgB;AAC1C,EAAA,OAAOA,sBAAA,CAAW,MAAM,CAAA,CACrB,MAAA,CAAOC,gCAAA,CAAgB,EAAE,GAAG,MAAA,EAAQ,CAAC,CAAA,CACrC,MAAA,CAAO,KAAK,CAAA;AACjB;AAGA,MAAM,qBAAA;AAAA;AAAA,EAEJ;AAAA,CAAA;AAOF,eAAsB,iBAAiB,OAAA,EAMY;AACjD,EAAA,MAAM,EAAE,IAAA,EAAM,MAAA,EAAQ,SAAA,EAAU,GAAI,OAAA;AACpC,EAAA,MAAM,eAAe,OAAA,CAAQ,YAAA;AAK7B,EAAA,IAAI,iCAAA,GAAoC,OAAA,CAAQ,QAAA,CAAS,IAAA,KAAS,UAAA;AAElE,EAAA,IAAI;AACF,IAAA,MAAM,YAAA,GAAe,MAAM,IAAA,CAAwB,eAAe,EAC/D,KAAA,CAAM,EAAE,UAAA,EAAY,SAAA,EAAW,CAAA,CAC/B,KAAA,CAAM,CAAC,CAAA,CACP,OAAO,WAAW,CAAA;AACrB,IAAA,IAAI,CAAC,aAAa,MAAA,EAAQ;AAExB,MAAA,OAAO,WAAA;AAAA,IACT;AAGA,IAAA,IAAI;AACF,MAAA,MAAM,IAAA,CAAyB,gBAAgB,CAAA,CAC5C,MAAA,CAAO;AAAA,QACN,SAAA,EAAW,YAAA,CAAa,CAAC,CAAA,CAAE,SAAA;AAAA,QAC3B,IAAA,EAAM,EAAA;AAAA,QACN,UAAA,EAAY;AAAA,OACb,CAAA,CACA,UAAA,CAAW,WAAW,EACtB,MAAA,EAAO;AAAA,IACZ,SAAS,KAAA,EAAO;AAMd,MAAA,IAAIC,wCA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-1,19 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var node_crypto = require('node:crypto');
|
|
4
|
-
var stableStringify = require('fast-json-stable-stringify');
|
|
5
|
-
|
|
6
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
7
|
-
|
|
8
|
-
var stableStringify__default = /*#__PURE__*/_interopDefaultCompat(stableStringify);
|
|
9
|
-
|
|
10
3
|
const BATCH_SIZE = 50;
|
|
11
4
|
const NULL_SENTINEL = "";
|
|
12
|
-
function generateStableHash(entity) {
|
|
13
|
-
return node_crypto.createHash("sha1").update(stableStringify__default.default({ ...entity })).digest("hex");
|
|
14
|
-
}
|
|
15
5
|
|
|
16
6
|
exports.BATCH_SIZE = BATCH_SIZE;
|
|
17
7
|
exports.NULL_SENTINEL = NULL_SENTINEL;
|
|
18
|
-
exports.generateStableHash = generateStableHash;
|
|
19
8
|
//# sourceMappingURL=util.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.cjs.js","sources":["../../../../src/database/operations/stitcher/util.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\
|
|
1
|
+
{"version":3,"file":"util.cjs.js","sources":["../../../../src/database/operations/stitcher/util.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// The number of items that are sent per batch to the database layer, when\n// doing .batchInsert calls to knex. This needs to be low enough to not cause\n// errors in the underlying engine due to exceeding query limits, but large\n// enough to get the speed benefits.\nexport const BATCH_SIZE = 50;\n\n// The SOH (Start of Heading) control character, used as a stand-in for NULL\n// in contexts where NULL cannot participate in equality comparisons (SQL\n// COALESCE, JS dedup keys). It cannot appear in real entity metadata values\n// since they are human-readable strings.\nexport const NULL_SENTINEL = '\\x01';\n"],"names":[],"mappings":";;AAoBO,MAAM,UAAA,GAAa;AAMnB,MAAM,aAAA,GAAgB;;;;;"}
|
|
@@ -249,7 +249,9 @@ class DefaultCatalogProcessingEngine {
|
|
|
249
249
|
return () => {
|
|
250
250
|
};
|
|
251
251
|
}
|
|
252
|
-
const stitchingStrategy = types.stitchingStrategyFromConfig(this.config
|
|
252
|
+
const stitchingStrategy = types.stitchingStrategyFromConfig(this.config, {
|
|
253
|
+
logger: this.logger
|
|
254
|
+
});
|
|
253
255
|
const runOnce = async () => {
|
|
254
256
|
try {
|
|
255
257
|
const n = await deleteOrphanedEntities.deleteOrphanedEntities({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultCatalogProcessingEngine.cjs.js","sources":["../../src/processing/DefaultCatalogProcessingEngine.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ANNOTATION_LOCATION,\n Entity,\n stringifyEntityRef,\n} from '@backstage/catalog-model';\nimport { serializeError, stringifyError, toError } from '@backstage/errors';\nimport { Hash } from 'node:crypto';\nimport stableStringify from 'fast-json-stable-stringify';\nimport { Knex } from 'knex';\nimport { trace } from '@opentelemetry/api';\nimport { ProcessingDatabase, RefreshStateItem } from '../database/types';\nimport { createCounterMetric, createSummaryMetric } from '../util/metrics';\nimport { CatalogProcessingOrchestrator, EntityProcessingResult } from './types';\nimport { Stitcher, stitchingStrategyFromConfig } from '../stitching/types';\nimport { startTaskPipeline } from './TaskPipeline';\nimport { Config } from '@backstage/config';\nimport {\n addEntityAttributes,\n TRACER_ID,\n withActiveSpan,\n} from '../util/opentelemetry';\nimport { deleteOrphanedEntities } from '../database/operations/util/deleteOrphanedEntities';\nimport { EventsService } from '@backstage/plugin-events-node';\nimport { CATALOG_ERRORS_TOPIC } from '../constants';\nimport { LoggerService, SchedulerService } from '@backstage/backend-plugin-api';\nimport { MetricsService } from '@backstage/backend-plugin-api/alpha';\n\nconst CACHE_TTL = 5;\n\nconst tracer = trace.getTracer(TRACER_ID);\n\nexport type ProgressTracker = ReturnType<typeof progressTracker>;\n\nconst stableStringifyArray = (arr: any[]) => {\n const sorted = arr.map(stableStringify).sort();\n return `[${sorted.join(',')}]`;\n};\n\n// NOTE(freben): Perhaps surprisingly, this class does not implement the\n// CatalogProcessingEngine type. That type is externally visible and its name is\n// the way it is for historic reasons. This class has no particular reason to\n// implement that precise interface; nowadays there are several different\n// engines \"hiding\" behind the CatalogProcessingEngine interface, of which this\n// is just one.\nexport class DefaultCatalogProcessingEngine {\n private readonly config: Config;\n private readonly scheduler: SchedulerService;\n private readonly logger: LoggerService;\n private readonly knex: Knex;\n private readonly processingDatabase: ProcessingDatabase;\n private readonly orchestrator: CatalogProcessingOrchestrator;\n private readonly stitcher: Stitcher;\n private readonly createHash: () => Hash;\n private readonly pollingIntervalMs: number;\n private readonly orphanCleanupIntervalMs: number;\n private readonly onProcessingError?: (event: {\n unprocessedEntity: Entity;\n errors: Error[];\n }) => Promise<void> | void;\n private readonly tracker: ProgressTracker;\n private readonly events: EventsService;\n\n private stopFunc?: () => void;\n\n constructor(options: {\n config: Config;\n scheduler: SchedulerService;\n logger: LoggerService;\n knex: Knex;\n processingDatabase: ProcessingDatabase;\n orchestrator: CatalogProcessingOrchestrator;\n stitcher: Stitcher;\n createHash: () => Hash;\n pollingIntervalMs?: number;\n orphanCleanupIntervalMs?: number;\n onProcessingError?: (event: {\n unprocessedEntity: Entity;\n errors: Error[];\n }) => Promise<void> | void;\n tracker?: ProgressTracker;\n events: EventsService;\n metrics: MetricsService;\n }) {\n this.config = options.config;\n this.scheduler = options.scheduler;\n this.logger = options.logger;\n this.knex = options.knex;\n this.processingDatabase = options.processingDatabase;\n this.orchestrator = options.orchestrator;\n this.stitcher = options.stitcher;\n this.createHash = options.createHash;\n this.pollingIntervalMs = options.pollingIntervalMs ?? 1_000;\n this.orphanCleanupIntervalMs = options.orphanCleanupIntervalMs ?? 30_000;\n this.onProcessingError = options.onProcessingError;\n this.tracker = options.tracker ?? progressTracker(options.metrics);\n this.events = options.events;\n\n this.stopFunc = undefined;\n }\n\n async start() {\n if (this.stopFunc) {\n throw new Error('Processing engine is already started');\n }\n\n const stopPipeline = this.startPipeline();\n const stopCleanup = this.startOrphanCleanup();\n\n this.stopFunc = () => {\n stopPipeline();\n stopCleanup();\n };\n }\n\n async stop() {\n if (this.stopFunc) {\n this.stopFunc();\n this.stopFunc = undefined;\n }\n }\n\n private startPipeline(): () => void {\n return startTaskPipeline<RefreshStateItem>({\n lowWatermark: 5,\n highWatermark: 10,\n pollingIntervalMs: this.pollingIntervalMs,\n loadTasks: async count => {\n try {\n const { items } = await this.processingDatabase.transaction(\n async tx => {\n return this.processingDatabase.getProcessableEntities(tx, {\n processBatchSize: count,\n });\n },\n );\n return items;\n } catch (error) {\n this.logger.warn('Failed to load processing items', error);\n return [];\n }\n },\n processTask: async item => {\n await withActiveSpan(tracer, 'ProcessingRun', async span => {\n const track = this.tracker.processStart(item, this.logger);\n addEntityAttributes(span, item.unprocessedEntity);\n\n try {\n const {\n id,\n state,\n unprocessedEntity,\n entityRef,\n locationKey,\n resultHash: previousResultHash,\n } = item;\n const result = await this.orchestrator.process({\n entity: unprocessedEntity,\n state,\n });\n\n track.markProcessorsCompleted(result);\n\n if (result.ok) {\n const { ttl: _, ...stateWithoutTtl } = state ?? {};\n if (\n stableStringify(stateWithoutTtl) !==\n stableStringify(result.state)\n ) {\n await this.processingDatabase.transaction(async tx => {\n await this.processingDatabase.updateEntityCache(tx, {\n id,\n state: {\n ttl: CACHE_TTL,\n ...result.state,\n },\n });\n });\n }\n } else {\n const maybeTtl = state?.ttl;\n const ttl = Number.isInteger(maybeTtl) ? (maybeTtl as number) : 0;\n await this.processingDatabase.transaction(async tx => {\n await this.processingDatabase.updateEntityCache(tx, {\n id,\n state: ttl > 0 ? { ...state, ttl: ttl - 1 } : {},\n });\n });\n }\n\n const location =\n unprocessedEntity?.metadata?.annotations?.[ANNOTATION_LOCATION];\n if (result.errors.length) {\n this.events.publish({\n topic: CATALOG_ERRORS_TOPIC,\n eventPayload: {\n entity: entityRef,\n location,\n errors: result.errors,\n },\n });\n }\n const errorsString = JSON.stringify(\n result.errors.map(e => serializeError(e)),\n );\n\n let hashBuilder = this.createHash().update(errorsString);\n\n if (result.ok) {\n const { entityRefs: parents } =\n await this.processingDatabase.transaction(tx =>\n this.processingDatabase.listParents(tx, {\n entityRefs: [\n entityRef,\n ...result.deferredEntities.map(e =>\n stringifyEntityRef(e.entity),\n ),\n ],\n }),\n );\n\n hashBuilder = hashBuilder\n .update(stableStringify({ ...result.completedEntity }))\n .update(stableStringifyArray([...result.deferredEntities]))\n .update(stableStringifyArray([...result.relations]))\n .update(stableStringifyArray([...result.refreshKeys]))\n .update(stableStringifyArray([...parents]));\n }\n\n const resultHash = hashBuilder.digest('hex');\n if (resultHash === previousResultHash) {\n // If nothing changed in our produced outputs, we cannot have any\n // significant effect on our surroundings; therefore, we just abort\n // without any updates / stitching.\n track.markSuccessfulWithNoChanges();\n return;\n }\n\n // If the result was marked as not OK, it signals that some part of the\n // processing pipeline threw an exception. This can happen both as part of\n // non-catastrophic things such as due to validation errors, as well as if\n // something fatal happens inside the processing for other reasons. In any\n // case, this means we can't trust that anything in the output is okay. So\n // just store the errors and trigger a stitch so that they become visible to\n // the outside.\n if (!result.ok) {\n // notify the error listener if the entity can not be processed.\n Promise.resolve(undefined)\n .then(() =>\n this.onProcessingError?.({\n unprocessedEntity,\n errors: result.errors,\n }),\n )\n .catch(error => {\n this.logger.debug(\n `Processing error listener threw an exception, ${stringifyError(\n error,\n )}`,\n );\n });\n\n await this.processingDatabase.transaction(async tx => {\n await this.processingDatabase.updateProcessedEntityErrors(tx, {\n id,\n errors: errorsString,\n resultHash,\n });\n });\n\n await this.stitcher.stitch({\n entityRefs: [stringifyEntityRef(unprocessedEntity)],\n });\n\n track.markSuccessfulWithErrors();\n return;\n }\n\n result.completedEntity.metadata.uid = id;\n let oldRelationSources: Map<string, string>;\n await this.processingDatabase.transaction(async tx => {\n const { previous } =\n await this.processingDatabase.updateProcessedEntity(tx, {\n id,\n processedEntity: result.completedEntity,\n resultHash,\n errors: errorsString,\n relations: result.relations,\n deferredEntities: result.deferredEntities,\n locationKey,\n refreshKeys: result.refreshKeys,\n });\n oldRelationSources = new Map(\n previous.relations.map(r => [\n `${r.source_entity_ref}:${r.type}->${r.target_entity_ref}`,\n r.source_entity_ref,\n ]),\n );\n });\n\n const newRelationSources = new Map<string, string>(\n result.relations.map(relation => {\n const sourceEntityRef = stringifyEntityRef(relation.source);\n const targetEntityRef = stringifyEntityRef(relation.target);\n return [\n `${sourceEntityRef}:${relation.type}->${targetEntityRef}`,\n sourceEntityRef,\n ];\n }),\n );\n\n const setOfThingsToStitch = new Set<string>([\n stringifyEntityRef(result.completedEntity),\n ]);\n newRelationSources.forEach((sourceEntityRef, uniqueKey) => {\n if (!oldRelationSources.has(uniqueKey)) {\n setOfThingsToStitch.add(sourceEntityRef);\n }\n });\n oldRelationSources!.forEach((sourceEntityRef, uniqueKey) => {\n if (!newRelationSources.has(uniqueKey)) {\n setOfThingsToStitch.add(sourceEntityRef);\n }\n });\n\n await this.stitcher.stitch({\n entityRefs: setOfThingsToStitch,\n });\n\n track.markSuccessfulWithChanges();\n } catch (error) {\n track.markFailed(toError(error));\n }\n });\n },\n });\n }\n\n private startOrphanCleanup(): () => void {\n const orphanStrategy =\n this.config.getOptionalString('catalog.orphanStrategy') ?? 'delete';\n if (orphanStrategy !== 'delete') {\n return () => {};\n }\n\n const stitchingStrategy = stitchingStrategyFromConfig(this.config);\n\n const runOnce = async () => {\n try {\n const n = await deleteOrphanedEntities({\n knex: this.knex,\n strategy: stitchingStrategy,\n });\n if (n > 0) {\n this.logger.info(`Deleted ${n} orphaned entities`);\n }\n } catch (error) {\n this.logger.warn(`Failed to delete orphaned entities`, error);\n }\n };\n\n const abortController = new AbortController();\n this.scheduler.scheduleTask({\n id: 'catalog_orphan_cleanup',\n frequency: { milliseconds: this.orphanCleanupIntervalMs },\n timeout: { milliseconds: this.orphanCleanupIntervalMs * 0.8 },\n fn: runOnce,\n signal: abortController.signal,\n });\n\n return () => {\n abortController.abort();\n };\n }\n}\n\n// Helps wrap the timing and logging behaviors\nfunction progressTracker(metrics: MetricsService) {\n // prom-client metrics are deprecated in favour of OpenTelemetry metrics.\n const promProcessedEntities = createCounterMetric({\n name: 'catalog_processed_entities_count',\n help: 'Amount of entities processed, DEPRECATED, use OpenTelemetry metrics instead',\n labelNames: ['result'],\n });\n const promProcessingDuration = createSummaryMetric({\n name: 'catalog_processing_duration_seconds',\n help: 'Time spent executing the full processing flow, DEPRECATED, use OpenTelemetry metrics instead',\n labelNames: ['result'],\n });\n const promProcessorsDuration = createSummaryMetric({\n name: 'catalog_processors_duration_seconds',\n help: 'Time spent executing catalog processors, DEPRECATED, use OpenTelemetry metrics instead',\n labelNames: ['result'],\n });\n const promProcessingQueueDelay = createSummaryMetric({\n name: 'catalog_processing_queue_delay_seconds',\n help: 'The amount of delay between being scheduled for processing, and the start of actually being processed, DEPRECATED, use OpenTelemetry metrics instead',\n });\n\n const processedEntities = metrics.createCounter(\n 'catalog.processed.entities.count',\n { description: 'Amount of entities processed' },\n );\n\n const processingDuration = metrics.createHistogram(\n 'catalog.processing.duration',\n {\n description: 'Time spent executing the full processing flow',\n unit: 'seconds',\n },\n );\n\n const processorsDuration = metrics.createHistogram(\n 'catalog.processors.duration',\n {\n description: 'Time spent executing catalog processors',\n unit: 'seconds',\n },\n );\n\n const processingQueueDelay = metrics.createHistogram(\n 'catalog.processing.queue.delay',\n {\n description:\n 'The amount of delay between being scheduled for processing, and the start of actually being processed',\n unit: 'seconds',\n },\n );\n\n function processStart(item: RefreshStateItem, logger: LoggerService) {\n const startTime = process.hrtime();\n const endOverallTimer = promProcessingDuration.startTimer();\n const endProcessorsTimer = promProcessorsDuration.startTimer();\n\n logger.debug(`Processing ${item.entityRef}`);\n\n if (item.nextUpdateAt) {\n const seconds = -item.nextUpdateAt.diffNow().as('seconds');\n promProcessingQueueDelay.observe(seconds);\n processingQueueDelay.record(seconds);\n }\n\n function endTime() {\n const delta = process.hrtime(startTime);\n return delta[0] + delta[1] / 1e9;\n }\n\n function markProcessorsCompleted(result: EntityProcessingResult) {\n endProcessorsTimer({ result: result.ok ? 'ok' : 'failed' });\n processorsDuration.record(endTime(), {\n result: result.ok ? 'ok' : 'failed',\n });\n }\n\n function markSuccessfulWithNoChanges() {\n endOverallTimer({ result: 'unchanged' });\n promProcessedEntities.inc({ result: 'unchanged' }, 1);\n\n processingDuration.record(endTime(), { result: 'unchanged' });\n processedEntities.add(1, { result: 'unchanged' });\n }\n\n function markSuccessfulWithErrors() {\n endOverallTimer({ result: 'errors' });\n promProcessedEntities.inc({ result: 'errors' }, 1);\n\n processingDuration.record(endTime(), { result: 'errors' });\n processedEntities.add(1, { result: 'errors' });\n }\n\n function markSuccessfulWithChanges() {\n endOverallTimer({ result: 'changed' });\n promProcessedEntities.inc({ result: 'changed' }, 1);\n\n processingDuration.record(endTime(), { result: 'changed' });\n processedEntities.add(1, { result: 'changed' });\n }\n\n function markFailed(error: Error) {\n promProcessedEntities.inc({ result: 'failed' }, 1);\n processedEntities.add(1, { result: 'failed' });\n logger.warn(`Processing of ${item.entityRef} failed`, error);\n }\n\n return {\n markProcessorsCompleted,\n markSuccessfulWithNoChanges,\n markSuccessfulWithErrors,\n markSuccessfulWithChanges,\n markFailed,\n };\n }\n\n return { processStart };\n}\n"],"names":["trace","TRACER_ID","stableStringify","startTaskPipeline","withActiveSpan","addEntityAttributes","ANNOTATION_LOCATION","CATALOG_ERRORS_TOPIC","serializeError","stringifyEntityRef","stringifyError","toError","stitchingStrategyFromConfig","deleteOrphanedEntities","metrics","createCounterMetric","createSummaryMetric"],"mappings":";;;;;;;;;;;;;;;;;AA2CA,MAAM,SAAA,GAAY,CAAA;AAElB,MAAM,MAAA,GAASA,SAAA,CAAM,SAAA,CAAUC,uBAAS,CAAA;AAIxC,MAAM,oBAAA,GAAuB,CAAC,GAAA,KAAe;AAC3C,EAAA,MAAM,MAAA,GAAS,GAAA,CAAI,GAAA,CAAIC,gCAAe,EAAE,IAAA,EAAK;AAC7C,EAAA,OAAO,CAAA,CAAA,EAAI,MAAA,CAAO,IAAA,CAAK,GAAG,CAAC,CAAA,CAAA,CAAA;AAC7B,CAAA;AAQO,MAAM,8BAAA,CAA+B;AAAA,EACzB,MAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA;AAAA,EACA,kBAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA,UAAA;AAAA,EACA,iBAAA;AAAA,EACA,uBAAA;AAAA,EACA,iBAAA;AAAA,EAIA,OAAA;AAAA,EACA,MAAA;AAAA,EAET,QAAA;AAAA,EAER,YAAY,OAAA,EAkBT;AACD,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,Y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A,IAAA,SAAS,WAAW,KAAA,EAAc;AAChC,MAAA,qBAAA,CAAsB,GAAA,CAAI,EAAE,MAAA,EAAQ,QAAA,IAAY,CAAC,CAAA;AACjD,MAAA,iBAAA,CAAkB,GAAA,CAAI,CAAA,EAAG,EAAE,MAAA,EAAQ,UAAU,CAAA;AAC7C,MAAA,MAAA,CAAO,IAAA,CAAK,CAAA,cAAA,EAAiB,IAAA,CAAK,SAAS,WAAW,KAAK,CAAA;AAAA,IAC7D;AAEA,IAAA,OAAO;AAAA,MACL,uBAAA;AAAA,MACA,2BAAA;AAAA,MACA,wBAAA;AAAA,MACA,yBAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO,EAAE,YAAA,EAAa;AACxB;;;;"}
|
|
1
|
+
{"version":3,"file":"DefaultCatalogProcessingEngine.cjs.js","sources":["../../src/processing/DefaultCatalogProcessingEngine.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n ANNOTATION_LOCATION,\n Entity,\n stringifyEntityRef,\n} from '@backstage/catalog-model';\nimport { serializeError, stringifyError, toError } from '@backstage/errors';\nimport { Hash } from 'node:crypto';\nimport stableStringify from 'fast-json-stable-stringify';\nimport { Knex } from 'knex';\nimport { trace } from '@opentelemetry/api';\nimport { ProcessingDatabase, RefreshStateItem } from '../database/types';\nimport { createCounterMetric, createSummaryMetric } from '../util/metrics';\nimport { CatalogProcessingOrchestrator, EntityProcessingResult } from './types';\nimport { Stitcher, stitchingStrategyFromConfig } from '../stitching/types';\nimport { startTaskPipeline } from './TaskPipeline';\nimport { Config } from '@backstage/config';\nimport {\n addEntityAttributes,\n TRACER_ID,\n withActiveSpan,\n} from '../util/opentelemetry';\nimport { deleteOrphanedEntities } from '../database/operations/util/deleteOrphanedEntities';\nimport { EventsService } from '@backstage/plugin-events-node';\nimport { CATALOG_ERRORS_TOPIC } from '../constants';\nimport { LoggerService, SchedulerService } from '@backstage/backend-plugin-api';\nimport { MetricsService } from '@backstage/backend-plugin-api/alpha';\n\nconst CACHE_TTL = 5;\n\nconst tracer = trace.getTracer(TRACER_ID);\n\nexport type ProgressTracker = ReturnType<typeof progressTracker>;\n\nconst stableStringifyArray = (arr: any[]) => {\n const sorted = arr.map(stableStringify).sort();\n return `[${sorted.join(',')}]`;\n};\n\n// NOTE(freben): Perhaps surprisingly, this class does not implement the\n// CatalogProcessingEngine type. That type is externally visible and its name is\n// the way it is for historic reasons. This class has no particular reason to\n// implement that precise interface; nowadays there are several different\n// engines \"hiding\" behind the CatalogProcessingEngine interface, of which this\n// is just one.\nexport class DefaultCatalogProcessingEngine {\n private readonly config: Config;\n private readonly scheduler: SchedulerService;\n private readonly logger: LoggerService;\n private readonly knex: Knex;\n private readonly processingDatabase: ProcessingDatabase;\n private readonly orchestrator: CatalogProcessingOrchestrator;\n private readonly stitcher: Stitcher;\n private readonly createHash: () => Hash;\n private readonly pollingIntervalMs: number;\n private readonly orphanCleanupIntervalMs: number;\n private readonly onProcessingError?: (event: {\n unprocessedEntity: Entity;\n errors: Error[];\n }) => Promise<void> | void;\n private readonly tracker: ProgressTracker;\n private readonly events: EventsService;\n\n private stopFunc?: () => void;\n\n constructor(options: {\n config: Config;\n scheduler: SchedulerService;\n logger: LoggerService;\n knex: Knex;\n processingDatabase: ProcessingDatabase;\n orchestrator: CatalogProcessingOrchestrator;\n stitcher: Stitcher;\n createHash: () => Hash;\n pollingIntervalMs?: number;\n orphanCleanupIntervalMs?: number;\n onProcessingError?: (event: {\n unprocessedEntity: Entity;\n errors: Error[];\n }) => Promise<void> | void;\n tracker?: ProgressTracker;\n events: EventsService;\n metrics: MetricsService;\n }) {\n this.config = options.config;\n this.scheduler = options.scheduler;\n this.logger = options.logger;\n this.knex = options.knex;\n this.processingDatabase = options.processingDatabase;\n this.orchestrator = options.orchestrator;\n this.stitcher = options.stitcher;\n this.createHash = options.createHash;\n this.pollingIntervalMs = options.pollingIntervalMs ?? 1_000;\n this.orphanCleanupIntervalMs = options.orphanCleanupIntervalMs ?? 30_000;\n this.onProcessingError = options.onProcessingError;\n this.tracker = options.tracker ?? progressTracker(options.metrics);\n this.events = options.events;\n\n this.stopFunc = undefined;\n }\n\n async start() {\n if (this.stopFunc) {\n throw new Error('Processing engine is already started');\n }\n\n const stopPipeline = this.startPipeline();\n const stopCleanup = this.startOrphanCleanup();\n\n this.stopFunc = () => {\n stopPipeline();\n stopCleanup();\n };\n }\n\n async stop() {\n if (this.stopFunc) {\n this.stopFunc();\n this.stopFunc = undefined;\n }\n }\n\n private startPipeline(): () => void {\n return startTaskPipeline<RefreshStateItem>({\n lowWatermark: 5,\n highWatermark: 10,\n pollingIntervalMs: this.pollingIntervalMs,\n loadTasks: async count => {\n try {\n const { items } = await this.processingDatabase.transaction(\n async tx => {\n return this.processingDatabase.getProcessableEntities(tx, {\n processBatchSize: count,\n });\n },\n );\n return items;\n } catch (error) {\n this.logger.warn('Failed to load processing items', error);\n return [];\n }\n },\n processTask: async item => {\n await withActiveSpan(tracer, 'ProcessingRun', async span => {\n const track = this.tracker.processStart(item, this.logger);\n addEntityAttributes(span, item.unprocessedEntity);\n\n try {\n const {\n id,\n state,\n unprocessedEntity,\n entityRef,\n locationKey,\n resultHash: previousResultHash,\n } = item;\n const result = await this.orchestrator.process({\n entity: unprocessedEntity,\n state,\n });\n\n track.markProcessorsCompleted(result);\n\n if (result.ok) {\n const { ttl: _, ...stateWithoutTtl } = state ?? {};\n if (\n stableStringify(stateWithoutTtl) !==\n stableStringify(result.state)\n ) {\n await this.processingDatabase.transaction(async tx => {\n await this.processingDatabase.updateEntityCache(tx, {\n id,\n state: {\n ttl: CACHE_TTL,\n ...result.state,\n },\n });\n });\n }\n } else {\n const maybeTtl = state?.ttl;\n const ttl = Number.isInteger(maybeTtl) ? (maybeTtl as number) : 0;\n await this.processingDatabase.transaction(async tx => {\n await this.processingDatabase.updateEntityCache(tx, {\n id,\n state: ttl > 0 ? { ...state, ttl: ttl - 1 } : {},\n });\n });\n }\n\n const location =\n unprocessedEntity?.metadata?.annotations?.[ANNOTATION_LOCATION];\n if (result.errors.length) {\n this.events.publish({\n topic: CATALOG_ERRORS_TOPIC,\n eventPayload: {\n entity: entityRef,\n location,\n errors: result.errors,\n },\n });\n }\n const errorsString = JSON.stringify(\n result.errors.map(e => serializeError(e)),\n );\n\n let hashBuilder = this.createHash().update(errorsString);\n\n if (result.ok) {\n const { entityRefs: parents } =\n await this.processingDatabase.transaction(tx =>\n this.processingDatabase.listParents(tx, {\n entityRefs: [\n entityRef,\n ...result.deferredEntities.map(e =>\n stringifyEntityRef(e.entity),\n ),\n ],\n }),\n );\n\n hashBuilder = hashBuilder\n .update(stableStringify({ ...result.completedEntity }))\n .update(stableStringifyArray([...result.deferredEntities]))\n .update(stableStringifyArray([...result.relations]))\n .update(stableStringifyArray([...result.refreshKeys]))\n .update(stableStringifyArray([...parents]));\n }\n\n const resultHash = hashBuilder.digest('hex');\n if (resultHash === previousResultHash) {\n // If nothing changed in our produced outputs, we cannot have any\n // significant effect on our surroundings; therefore, we just abort\n // without any updates / stitching.\n track.markSuccessfulWithNoChanges();\n return;\n }\n\n // If the result was marked as not OK, it signals that some part of the\n // processing pipeline threw an exception. This can happen both as part of\n // non-catastrophic things such as due to validation errors, as well as if\n // something fatal happens inside the processing for other reasons. In any\n // case, this means we can't trust that anything in the output is okay. So\n // just store the errors and trigger a stitch so that they become visible to\n // the outside.\n if (!result.ok) {\n // notify the error listener if the entity can not be processed.\n Promise.resolve(undefined)\n .then(() =>\n this.onProcessingError?.({\n unprocessedEntity,\n errors: result.errors,\n }),\n )\n .catch(error => {\n this.logger.debug(\n `Processing error listener threw an exception, ${stringifyError(\n error,\n )}`,\n );\n });\n\n await this.processingDatabase.transaction(async tx => {\n await this.processingDatabase.updateProcessedEntityErrors(tx, {\n id,\n errors: errorsString,\n resultHash,\n });\n });\n\n await this.stitcher.stitch({\n entityRefs: [stringifyEntityRef(unprocessedEntity)],\n });\n\n track.markSuccessfulWithErrors();\n return;\n }\n\n result.completedEntity.metadata.uid = id;\n let oldRelationSources: Map<string, string>;\n await this.processingDatabase.transaction(async tx => {\n const { previous } =\n await this.processingDatabase.updateProcessedEntity(tx, {\n id,\n processedEntity: result.completedEntity,\n resultHash,\n errors: errorsString,\n relations: result.relations,\n deferredEntities: result.deferredEntities,\n locationKey,\n refreshKeys: result.refreshKeys,\n });\n oldRelationSources = new Map(\n previous.relations.map(r => [\n `${r.source_entity_ref}:${r.type}->${r.target_entity_ref}`,\n r.source_entity_ref,\n ]),\n );\n });\n\n const newRelationSources = new Map<string, string>(\n result.relations.map(relation => {\n const sourceEntityRef = stringifyEntityRef(relation.source);\n const targetEntityRef = stringifyEntityRef(relation.target);\n return [\n `${sourceEntityRef}:${relation.type}->${targetEntityRef}`,\n sourceEntityRef,\n ];\n }),\n );\n\n const setOfThingsToStitch = new Set<string>([\n stringifyEntityRef(result.completedEntity),\n ]);\n newRelationSources.forEach((sourceEntityRef, uniqueKey) => {\n if (!oldRelationSources.has(uniqueKey)) {\n setOfThingsToStitch.add(sourceEntityRef);\n }\n });\n oldRelationSources!.forEach((sourceEntityRef, uniqueKey) => {\n if (!newRelationSources.has(uniqueKey)) {\n setOfThingsToStitch.add(sourceEntityRef);\n }\n });\n\n await this.stitcher.stitch({\n entityRefs: setOfThingsToStitch,\n });\n\n track.markSuccessfulWithChanges();\n } catch (error) {\n track.markFailed(toError(error));\n }\n });\n },\n });\n }\n\n private startOrphanCleanup(): () => void {\n const orphanStrategy =\n this.config.getOptionalString('catalog.orphanStrategy') ?? 'delete';\n if (orphanStrategy !== 'delete') {\n return () => {};\n }\n\n const stitchingStrategy = stitchingStrategyFromConfig(this.config, {\n logger: this.logger,\n });\n\n const runOnce = async () => {\n try {\n const n = await deleteOrphanedEntities({\n knex: this.knex,\n strategy: stitchingStrategy,\n });\n if (n > 0) {\n this.logger.info(`Deleted ${n} orphaned entities`);\n }\n } catch (error) {\n this.logger.warn(`Failed to delete orphaned entities`, error);\n }\n };\n\n const abortController = new AbortController();\n this.scheduler.scheduleTask({\n id: 'catalog_orphan_cleanup',\n frequency: { milliseconds: this.orphanCleanupIntervalMs },\n timeout: { milliseconds: this.orphanCleanupIntervalMs * 0.8 },\n fn: runOnce,\n signal: abortController.signal,\n });\n\n return () => {\n abortController.abort();\n };\n }\n}\n\n// Helps wrap the timing and logging behaviors\nfunction progressTracker(metrics: MetricsService) {\n // prom-client metrics are deprecated in favour of OpenTelemetry metrics.\n const promProcessedEntities = createCounterMetric({\n name: 'catalog_processed_entities_count',\n help: 'Amount of entities processed, DEPRECATED, use OpenTelemetry metrics instead',\n labelNames: ['result'],\n });\n const promProcessingDuration = createSummaryMetric({\n name: 'catalog_processing_duration_seconds',\n help: 'Time spent executing the full processing flow, DEPRECATED, use OpenTelemetry metrics instead',\n labelNames: ['result'],\n });\n const promProcessorsDuration = createSummaryMetric({\n name: 'catalog_processors_duration_seconds',\n help: 'Time spent executing catalog processors, DEPRECATED, use OpenTelemetry metrics instead',\n labelNames: ['result'],\n });\n const promProcessingQueueDelay = createSummaryMetric({\n name: 'catalog_processing_queue_delay_seconds',\n help: 'The amount of delay between being scheduled for processing, and the start of actually being processed, DEPRECATED, use OpenTelemetry metrics instead',\n });\n\n const processedEntities = metrics.createCounter(\n 'catalog.processed.entities.count',\n { description: 'Amount of entities processed' },\n );\n\n const processingDuration = metrics.createHistogram(\n 'catalog.processing.duration',\n {\n description: 'Time spent executing the full processing flow',\n unit: 'seconds',\n },\n );\n\n const processorsDuration = metrics.createHistogram(\n 'catalog.processors.duration',\n {\n description: 'Time spent executing catalog processors',\n unit: 'seconds',\n },\n );\n\n const processingQueueDelay = metrics.createHistogram(\n 'catalog.processing.queue.delay',\n {\n description:\n 'The amount of delay between being scheduled for processing, and the start of actually being processed',\n unit: 'seconds',\n },\n );\n\n function processStart(item: RefreshStateItem, logger: LoggerService) {\n const startTime = process.hrtime();\n const endOverallTimer = promProcessingDuration.startTimer();\n const endProcessorsTimer = promProcessorsDuration.startTimer();\n\n logger.debug(`Processing ${item.entityRef}`);\n\n if (item.nextUpdateAt) {\n const seconds = -item.nextUpdateAt.diffNow().as('seconds');\n promProcessingQueueDelay.observe(seconds);\n processingQueueDelay.record(seconds);\n }\n\n function endTime() {\n const delta = process.hrtime(startTime);\n return delta[0] + delta[1] / 1e9;\n }\n\n function markProcessorsCompleted(result: EntityProcessingResult) {\n endProcessorsTimer({ result: result.ok ? 'ok' : 'failed' });\n processorsDuration.record(endTime(), {\n result: result.ok ? 'ok' : 'failed',\n });\n }\n\n function markSuccessfulWithNoChanges() {\n endOverallTimer({ result: 'unchanged' });\n promProcessedEntities.inc({ result: 'unchanged' }, 1);\n\n processingDuration.record(endTime(), { result: 'unchanged' });\n processedEntities.add(1, { result: 'unchanged' });\n }\n\n function markSuccessfulWithErrors() {\n endOverallTimer({ result: 'errors' });\n promProcessedEntities.inc({ result: 'errors' }, 1);\n\n processingDuration.record(endTime(), { result: 'errors' });\n processedEntities.add(1, { result: 'errors' });\n }\n\n function markSuccessfulWithChanges() {\n endOverallTimer({ result: 'changed' });\n promProcessedEntities.inc({ result: 'changed' }, 1);\n\n processingDuration.record(endTime(), { result: 'changed' });\n processedEntities.add(1, { result: 'changed' });\n }\n\n function markFailed(error: Error) {\n promProcessedEntities.inc({ result: 'failed' }, 1);\n processedEntities.add(1, { result: 'failed' });\n logger.warn(`Processing of ${item.entityRef} failed`, error);\n }\n\n return {\n markProcessorsCompleted,\n markSuccessfulWithNoChanges,\n markSuccessfulWithErrors,\n markSuccessfulWithChanges,\n markFailed,\n };\n }\n\n return { processStart };\n}\n"],"names":["trace","TRACER_ID","stableStringify","startTaskPipeline","withActiveSpan","addEntityAttributes","ANNOTATION_LOCATION","CATALOG_ERRORS_TOPIC","serializeError","stringifyEntityRef","stringifyError","toError","stitchingStrategyFromConfig","deleteOrphanedEntities","metrics","createCounterMetric","createSummaryMetric"],"mappings":";;;;;;;;;;;;;;;;;AA2CA,MAAM,SAAA,GAAY,CAAA;AAElB,MAAM,MAAA,GAASA,SAAA,CAAM,SAAA,CAAUC,uBAAS,CAAA;AAIxC,MAAM,oBAAA,GAAuB,CAAC,GAAA,KAAe;AAC3C,EAAA,MAAM,MAAA,GAAS,GAAA,CAAI,GAAA,CAAIC,gCAAe,EAAE,IAAA,EAAK;AAC7C,EAAA,OAAO,CAAA,CAAA,EAAI,MAAA,CAAO,IAAA,CAAK,GAAG,CAAC,CAAA,CAAA,CAAA;AAC7B,CAAA;AAQO,MAAM,8BAAA,CAA+B;AAAA,EACzB,MAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA;AAAA,EACA,kBAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA,UAAA;AAAA,EACA,iBAAA;AAAA,EACA,uBAAA;AAAA,EACA,iBAAA;AAAA,EAIA,OAAA;AAAA,EACA,MAAA;AAAA,EAET,QAAA;AAAA,EAER,YAAY,OAAA,EAkBT;AACD,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,YAAY,OAAA,CAAQ,SAAA;AACzB,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,OAAO,OAAA,CAAQ,IAAA;AACpB,IAAA,IAAA,CAAK,qBAAqB,OAAA,CAAQ,kBAAA;AAClC,IAAA,IAAA,CAAK,eAAe,OAAA,CAAQ,YAAA;AAC5B,IAAA,IAAA,CAAK,WAAW,OAAA,CAAQ,QAAA;AACxB,IAAA,IAAA,CAAK,aAAa,OAAA,CAAQ,UAAA;AAC1B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AAA;AAAA,IAC7D;AAEA,IAAA,OAAO;AAAA,MACL,uBAAA;AAAA,MACA,2BAAA;AAAA,MACA,wBAAA;AAAA,MACA,yBAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAEA,EAAA,OAAO,EAAE,YAAA,EAAa;AACxB;;;;"}
|
|
@@ -206,7 +206,10 @@ const catalogPlugin = backendPluginApi.createBackendPlugin({
|
|
|
206
206
|
index.createCatalogActions({
|
|
207
207
|
catalog,
|
|
208
208
|
actionsRegistry,
|
|
209
|
-
modelHolder
|
|
209
|
+
modelHolder,
|
|
210
|
+
useExperimentalCatalogLayersDescriptions: config.getOptionalBoolean(
|
|
211
|
+
"catalog.actions.experimentalCatalogLayersDescriptions.enabled"
|
|
212
|
+
) ?? false
|
|
210
213
|
});
|
|
211
214
|
const scmEventsMessagesCounter = metrics.createCounter("catalog.events.scm.messages", {
|
|
212
215
|
description: "Number of SCM event messages received by the catalog backend",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CatalogPlugin.cjs.js","sources":["../../src/service/CatalogPlugin.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport {\n actionsRegistryServiceRef,\n metricsServiceRef,\n} from '@backstage/backend-plugin-api/alpha';\nimport { Entity, Validators } from '@backstage/catalog-model';\nimport { CatalogModelSource } from '@backstage/catalog-model/alpha';\nimport { ForwardedError } from '@backstage/errors';\nimport {\n catalogAnalysisExtensionPoint,\n CatalogLocationsExtensionPoint,\n catalogLocationsExtensionPoint,\n catalogProcessingExtensionPoint,\n CatalogProcessor,\n CatalogProcessorParser,\n catalogServiceRef,\n LocationAnalyzer,\n PlaceholderResolver,\n ScmLocationAnalyzer,\n} from '@backstage/plugin-catalog-node';\nimport {\n CatalogModelExtensionPoint,\n catalogModelExtensionPoint,\n catalogScmEventsServiceRef,\n} from '@backstage/plugin-catalog-node/alpha';\nimport { eventsServiceRef } from '@backstage/plugin-events-node';\nimport { merge } from 'lodash';\nimport { createCatalogActions } from '../actions';\nimport { ModelHolder } from '../model/ModelHolder';\nimport type { EntityProviderEntry } from '../processing/connectEntityProviders';\nimport { CatalogBuilder } from './CatalogBuilder';\n\nclass CatalogLocationsExtensionPointImpl\n implements CatalogLocationsExtensionPoint\n{\n #locationTypes: string[] | undefined;\n\n setAllowedLocationTypes(locationTypes: Array<string>) {\n this.#locationTypes = locationTypes;\n }\n\n get allowedLocationTypes() {\n return this.#locationTypes;\n }\n}\n\nclass CatalogModelExtensionPointImpl implements CatalogModelExtensionPoint {\n #fieldValidators: Partial<Validators> = {};\n\n setFieldValidators(validators: Partial<Validators>): void {\n merge(this.#fieldValidators, validators);\n }\n\n get fieldValidators() {\n return this.#fieldValidators;\n }\n\n #entityDataParser?: CatalogProcessorParser;\n\n setEntityDataParser(parser: CatalogProcessorParser): void {\n if (this.#entityDataParser) {\n throw new Error(\n 'Attempted to install second EntityDataParser. Only one can be set.',\n );\n }\n this.#entityDataParser = parser;\n }\n\n get entityDataParser() {\n return this.#entityDataParser;\n }\n\n #modelSources: CatalogModelSource[] = [];\n\n addModelSource(source: CatalogModelSource): void {\n this.#modelSources.push(source);\n }\n\n get modelSources() {\n return this.#modelSources;\n }\n}\n\n/**\n * Catalog plugin\n * @public\n */\nexport const catalogPlugin = createBackendPlugin({\n pluginId: 'catalog',\n register(env) {\n const processors = new Array<CatalogProcessor>();\n const entityProviders = new Array<EntityProviderEntry>();\n const placeholderResolvers: Record<string, PlaceholderResolver> = {};\n let onProcessingError:\n | ((event: {\n unprocessedEntity: Entity;\n errors: Error[];\n }) => Promise<void> | void)\n | undefined = undefined;\n\n env.registerExtensionPoint({\n extensionPoint: catalogProcessingExtensionPoint,\n factory: context => ({\n addProcessor: (...newProcessors) => {\n processors.push(...newProcessors.flat());\n },\n addEntityProvider: (...providers) => {\n entityProviders.push(\n ...providers.flat().map(provider => ({\n provider,\n context,\n })),\n );\n },\n addPlaceholderResolver: (key, resolver) => {\n if (key in placeholderResolvers) {\n throw new Error(\n `A placeholder resolver for '${key}' has already been set up, please check your config.`,\n );\n }\n placeholderResolvers[key] = resolver;\n },\n setOnProcessingErrorHandler: handler => {\n onProcessingError = handler;\n },\n }),\n });\n\n let locationAnalyzerFactory:\n | ((options: {\n scmLocationAnalyzers: ScmLocationAnalyzer[];\n }) => Promise<{ locationAnalyzer: LocationAnalyzer }>)\n | undefined = undefined;\n const scmLocationAnalyzers = new Array<ScmLocationAnalyzer>();\n env.registerExtensionPoint(catalogAnalysisExtensionPoint, {\n setLocationAnalyzer(analyzerOrFactory) {\n if (locationAnalyzerFactory) {\n throw new Error('LocationAnalyzer has already been set');\n }\n if (typeof analyzerOrFactory === 'function') {\n locationAnalyzerFactory = analyzerOrFactory;\n } else {\n locationAnalyzerFactory = async () => ({\n locationAnalyzer: analyzerOrFactory,\n });\n }\n },\n addScmLocationAnalyzer(analyzer: ScmLocationAnalyzer) {\n scmLocationAnalyzers.push(analyzer);\n },\n });\n\n const modelExtensions = new CatalogModelExtensionPointImpl();\n env.registerExtensionPoint(catalogModelExtensionPoint, modelExtensions);\n\n const locationTypeExtensions = new CatalogLocationsExtensionPointImpl();\n env.registerExtensionPoint(\n catalogLocationsExtensionPoint,\n locationTypeExtensions,\n );\n\n env.registerInit({\n deps: {\n logger: coreServices.logger,\n config: coreServices.rootConfig,\n reader: coreServices.urlReader,\n permissions: coreServices.permissions,\n permissionsRegistry: coreServices.permissionsRegistry,\n database: coreServices.database,\n httpRouter: coreServices.httpRouter,\n lifecycle: coreServices.rootLifecycle,\n scheduler: coreServices.scheduler,\n auth: coreServices.auth,\n httpAuth: coreServices.httpAuth,\n auditor: coreServices.auditor,\n events: eventsServiceRef,\n catalog: catalogServiceRef,\n actionsRegistry: actionsRegistryServiceRef,\n catalogScmEvents: catalogScmEventsServiceRef,\n metrics: metricsServiceRef,\n },\n async init({\n logger,\n config,\n reader,\n database,\n permissions,\n permissionsRegistry,\n httpRouter,\n lifecycle,\n scheduler,\n auth,\n httpAuth,\n catalog,\n actionsRegistry,\n auditor,\n events,\n catalogScmEvents,\n metrics,\n }) {\n const modelHolder = modelExtensions.modelSources.length\n ? await ModelHolder.create({\n sources: modelExtensions.modelSources,\n logger,\n lifecycle,\n })\n : undefined;\n\n const builder = await CatalogBuilder.create({\n config,\n modelHolder,\n reader,\n permissions,\n permissionsRegistry,\n database,\n scheduler,\n logger,\n auth,\n httpAuth,\n auditor,\n events,\n catalogScmEvents,\n metrics,\n });\n\n if (onProcessingError) {\n builder.subscribe({ onProcessingError });\n }\n builder.addProcessor(...processors);\n builder.addEntityProvider(...entityProviders);\n\n if (modelExtensions.entityDataParser) {\n builder.setEntityDataParser(modelExtensions.entityDataParser);\n }\n\n Object.entries(placeholderResolvers).forEach(([key, resolver]) =>\n builder.setPlaceholderResolver(key, resolver),\n );\n if (locationAnalyzerFactory) {\n const { locationAnalyzer } = await locationAnalyzerFactory({\n scmLocationAnalyzers,\n }).catch(e => {\n throw new ForwardedError('Failed to create LocationAnalyzer', e);\n });\n builder.setLocationAnalyzer(locationAnalyzer);\n } else {\n builder.addLocationAnalyzers(...scmLocationAnalyzers);\n }\n builder.setFieldFormatValidators(modelExtensions.fieldValidators);\n\n if (locationTypeExtensions.allowedLocationTypes) {\n builder.setAllowedLocationTypes(\n locationTypeExtensions.allowedLocationTypes,\n );\n }\n\n const { processingEngine, router } = await builder.build();\n\n if (config.getOptional('catalog.processingInterval') ?? true) {\n lifecycle.addStartupHook(async () => {\n await processingEngine.start();\n });\n lifecycle.addShutdownHook(() => processingEngine.stop());\n }\n\n httpRouter.use(router);\n\n createCatalogActions({\n catalog,\n actionsRegistry,\n modelHolder,\n });\n\n const scmEventsMessagesCounter = metrics.createCounter<{\n eventType: string;\n }>('catalog.events.scm.messages', {\n description:\n 'Number of SCM event messages received by the catalog backend',\n unit: 'short',\n });\n catalogScmEvents.subscribe({\n onEvents: async e => {\n for (const event of e) {\n const eventType = event.type.split('.')[0];\n scmEventsMessagesCounter.add(1, { eventType });\n }\n },\n });\n },\n });\n },\n});\n"],"names":["merge","createBackendPlugin","catalogProcessingExtensionPoint","catalogAnalysisExtensionPoint","catalogModelExtensionPoint","catalogLocationsExtensionPoint","coreServices","eventsServiceRef","catalogServiceRef","actionsRegistryServiceRef","catalogScmEventsServiceRef","metricsServiceRef","ModelHolder","CatalogBuilder","ForwardedError","createCatalogActions"],"mappings":";;;;;;;;;;;;;AAmDA,MAAM,kCAAA,CAEN;AAAA,EACE,cAAA;AAAA,EAEA,wBAAwB,aAAA,EAA8B;AACpD,IAAA,IAAA,CAAK,cAAA,GAAiB,aAAA;AAAA,EACxB;AAAA,EAEA,IAAI,oBAAA,GAAuB;AACzB,IAAA,OAAO,IAAA,CAAK,cAAA;AAAA,EACd;AACF;AAEA,MAAM,8BAAA,CAAqE;AAAA,EACzE,mBAAwC,EAAC;AAAA,EAEzC,mBAAmB,UAAA,EAAuC;AACxD,IAAAA,YAAA,CAAM,IAAA,CAAK,kBAAkB,UAAU,CAAA;AAAA,EACzC;AAAA,EAEA,IAAI,eAAA,GAAkB;AACpB,IAAA,OAAO,IAAA,CAAK,gBAAA;AAAA,EACd;AAAA,EAEA,iBAAA;AAAA,EAEA,oBAAoB,MAAA,EAAsC;AACxD,IAAA,IAAI,KAAK,iBAAA,EAAmB;AAC1B,MAAA,MAAM,IAAI,KAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AACA,IAAA,IAAA,CAAK,iBAAA,GAAoB,MAAA;AAAA,EAC3B;AAAA,EAEA,IAAI,gBAAA,GAAmB;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,UAAA,SAAA,CAAU,eAAe,YAAY;AACnC,YAAA,MAAM,iBAAiB,KAAA,EAAM;AAAA,UAC/B,CAAC,CAAA;AACD,UAAA,SAAA,CAAU,eAAA,CAAgB,MAAM,gBAAA,CAAiB,IAAA,EAAM,CAAA;AAAA,QACzD;AAEA,QAAA,UAAA,CAAW,IAAI,MAAM,CAAA;AAErB,QAAAC,0BAAA,CAAqB;AAAA,UACnB,OAAA;AAAA,UACA,eAAA;AAAA,UACA;AAAA,SACD,CAAA;AAED,QAAA,MAAM,wBAAA,GAA2B,OAAA,CAAQ,aAAA,CAEtC,6BAAA,EAA+B;AAAA,UAChC,WAAA,EACE,8DAAA;AAAA,UACF,IAAA,EAAM;AAAA,SACP,CAAA;AACD,QAAA,gBAAA,CAAiB,SAAA,CAAU;AAAA,UACzB,QAAA,EAAU,OAAM,CAAA,KAAK;AACnB,YAAA,KAAA,MAAW,SAAS,CAAA,EAAG;AACrB,cAAA,MAAM,YAAY,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM,GAAG,EAAE,CAAC,CAAA;AACzC,cAAA,wBAAA,CAAyB,GAAA,CAAI,CAAA,EAAG,EAAE,SAAA,EAAW,CAAA;AAAA,YAC/C;AAAA,UACF;AAAA,SACD,CAAA;AAAA,MACH;AAAA,KACD,CAAA;AAAA,EACH;AACF,CAAC;;;;"}
|
|
1
|
+
{"version":3,"file":"CatalogPlugin.cjs.js","sources":["../../src/service/CatalogPlugin.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createBackendPlugin,\n} from '@backstage/backend-plugin-api';\nimport {\n actionsRegistryServiceRef,\n metricsServiceRef,\n} from '@backstage/backend-plugin-api/alpha';\nimport { Entity, Validators } from '@backstage/catalog-model';\nimport { CatalogModelSource } from '@backstage/catalog-model/alpha';\nimport { ForwardedError } from '@backstage/errors';\nimport {\n catalogAnalysisExtensionPoint,\n CatalogLocationsExtensionPoint,\n catalogLocationsExtensionPoint,\n catalogProcessingExtensionPoint,\n CatalogProcessor,\n CatalogProcessorParser,\n catalogServiceRef,\n LocationAnalyzer,\n PlaceholderResolver,\n ScmLocationAnalyzer,\n} from '@backstage/plugin-catalog-node';\nimport {\n CatalogModelExtensionPoint,\n catalogModelExtensionPoint,\n catalogScmEventsServiceRef,\n} from '@backstage/plugin-catalog-node/alpha';\nimport { eventsServiceRef } from '@backstage/plugin-events-node';\nimport { merge } from 'lodash';\nimport { createCatalogActions } from '../actions';\nimport { ModelHolder } from '../model/ModelHolder';\nimport type { EntityProviderEntry } from '../processing/connectEntityProviders';\nimport { CatalogBuilder } from './CatalogBuilder';\n\nclass CatalogLocationsExtensionPointImpl\n implements CatalogLocationsExtensionPoint\n{\n #locationTypes: string[] | undefined;\n\n setAllowedLocationTypes(locationTypes: Array<string>) {\n this.#locationTypes = locationTypes;\n }\n\n get allowedLocationTypes() {\n return this.#locationTypes;\n }\n}\n\nclass CatalogModelExtensionPointImpl implements CatalogModelExtensionPoint {\n #fieldValidators: Partial<Validators> = {};\n\n setFieldValidators(validators: Partial<Validators>): void {\n merge(this.#fieldValidators, validators);\n }\n\n get fieldValidators() {\n return this.#fieldValidators;\n }\n\n #entityDataParser?: CatalogProcessorParser;\n\n setEntityDataParser(parser: CatalogProcessorParser): void {\n if (this.#entityDataParser) {\n throw new Error(\n 'Attempted to install second EntityDataParser. Only one can be set.',\n );\n }\n this.#entityDataParser = parser;\n }\n\n get entityDataParser() {\n return this.#entityDataParser;\n }\n\n #modelSources: CatalogModelSource[] = [];\n\n addModelSource(source: CatalogModelSource): void {\n this.#modelSources.push(source);\n }\n\n get modelSources() {\n return this.#modelSources;\n }\n}\n\n/**\n * Catalog plugin\n * @public\n */\nexport const catalogPlugin = createBackendPlugin({\n pluginId: 'catalog',\n register(env) {\n const processors = new Array<CatalogProcessor>();\n const entityProviders = new Array<EntityProviderEntry>();\n const placeholderResolvers: Record<string, PlaceholderResolver> = {};\n let onProcessingError:\n | ((event: {\n unprocessedEntity: Entity;\n errors: Error[];\n }) => Promise<void> | void)\n | undefined = undefined;\n\n env.registerExtensionPoint({\n extensionPoint: catalogProcessingExtensionPoint,\n factory: context => ({\n addProcessor: (...newProcessors) => {\n processors.push(...newProcessors.flat());\n },\n addEntityProvider: (...providers) => {\n entityProviders.push(\n ...providers.flat().map(provider => ({\n provider,\n context,\n })),\n );\n },\n addPlaceholderResolver: (key, resolver) => {\n if (key in placeholderResolvers) {\n throw new Error(\n `A placeholder resolver for '${key}' has already been set up, please check your config.`,\n );\n }\n placeholderResolvers[key] = resolver;\n },\n setOnProcessingErrorHandler: handler => {\n onProcessingError = handler;\n },\n }),\n });\n\n let locationAnalyzerFactory:\n | ((options: {\n scmLocationAnalyzers: ScmLocationAnalyzer[];\n }) => Promise<{ locationAnalyzer: LocationAnalyzer }>)\n | undefined = undefined;\n const scmLocationAnalyzers = new Array<ScmLocationAnalyzer>();\n env.registerExtensionPoint(catalogAnalysisExtensionPoint, {\n setLocationAnalyzer(analyzerOrFactory) {\n if (locationAnalyzerFactory) {\n throw new Error('LocationAnalyzer has already been set');\n }\n if (typeof analyzerOrFactory === 'function') {\n locationAnalyzerFactory = analyzerOrFactory;\n } else {\n locationAnalyzerFactory = async () => ({\n locationAnalyzer: analyzerOrFactory,\n });\n }\n },\n addScmLocationAnalyzer(analyzer: ScmLocationAnalyzer) {\n scmLocationAnalyzers.push(analyzer);\n },\n });\n\n const modelExtensions = new CatalogModelExtensionPointImpl();\n env.registerExtensionPoint(catalogModelExtensionPoint, modelExtensions);\n\n const locationTypeExtensions = new CatalogLocationsExtensionPointImpl();\n env.registerExtensionPoint(\n catalogLocationsExtensionPoint,\n locationTypeExtensions,\n );\n\n env.registerInit({\n deps: {\n logger: coreServices.logger,\n config: coreServices.rootConfig,\n reader: coreServices.urlReader,\n permissions: coreServices.permissions,\n permissionsRegistry: coreServices.permissionsRegistry,\n database: coreServices.database,\n httpRouter: coreServices.httpRouter,\n lifecycle: coreServices.rootLifecycle,\n scheduler: coreServices.scheduler,\n auth: coreServices.auth,\n httpAuth: coreServices.httpAuth,\n auditor: coreServices.auditor,\n events: eventsServiceRef,\n catalog: catalogServiceRef,\n actionsRegistry: actionsRegistryServiceRef,\n catalogScmEvents: catalogScmEventsServiceRef,\n metrics: metricsServiceRef,\n },\n async init({\n logger,\n config,\n reader,\n database,\n permissions,\n permissionsRegistry,\n httpRouter,\n lifecycle,\n scheduler,\n auth,\n httpAuth,\n catalog,\n actionsRegistry,\n auditor,\n events,\n catalogScmEvents,\n metrics,\n }) {\n const modelHolder = modelExtensions.modelSources.length\n ? await ModelHolder.create({\n sources: modelExtensions.modelSources,\n logger,\n lifecycle,\n })\n : undefined;\n\n const builder = await CatalogBuilder.create({\n config,\n modelHolder,\n reader,\n permissions,\n permissionsRegistry,\n database,\n scheduler,\n logger,\n auth,\n httpAuth,\n auditor,\n events,\n catalogScmEvents,\n metrics,\n });\n\n if (onProcessingError) {\n builder.subscribe({ onProcessingError });\n }\n builder.addProcessor(...processors);\n builder.addEntityProvider(...entityProviders);\n\n if (modelExtensions.entityDataParser) {\n builder.setEntityDataParser(modelExtensions.entityDataParser);\n }\n\n Object.entries(placeholderResolvers).forEach(([key, resolver]) =>\n builder.setPlaceholderResolver(key, resolver),\n );\n if (locationAnalyzerFactory) {\n const { locationAnalyzer } = await locationAnalyzerFactory({\n scmLocationAnalyzers,\n }).catch(e => {\n throw new ForwardedError('Failed to create LocationAnalyzer', e);\n });\n builder.setLocationAnalyzer(locationAnalyzer);\n } else {\n builder.addLocationAnalyzers(...scmLocationAnalyzers);\n }\n builder.setFieldFormatValidators(modelExtensions.fieldValidators);\n\n if (locationTypeExtensions.allowedLocationTypes) {\n builder.setAllowedLocationTypes(\n locationTypeExtensions.allowedLocationTypes,\n );\n }\n\n const { processingEngine, router } = await builder.build();\n\n if (config.getOptional('catalog.processingInterval') ?? true) {\n lifecycle.addStartupHook(async () => {\n await processingEngine.start();\n });\n lifecycle.addShutdownHook(() => processingEngine.stop());\n }\n\n httpRouter.use(router);\n\n createCatalogActions({\n catalog,\n actionsRegistry,\n modelHolder,\n useExperimentalCatalogLayersDescriptions:\n config.getOptionalBoolean(\n 'catalog.actions.experimentalCatalogLayersDescriptions.enabled',\n ) ?? false,\n });\n\n const scmEventsMessagesCounter = metrics.createCounter<{\n eventType: string;\n }>('catalog.events.scm.messages', {\n description:\n 'Number of SCM event messages received by the catalog backend',\n unit: 'short',\n });\n catalogScmEvents.subscribe({\n onEvents: async e => {\n for (const event of e) {\n const eventType = event.type.split('.')[0];\n scmEventsMessagesCounter.add(1, { eventType });\n }\n },\n });\n },\n });\n },\n});\n"],"names":["merge","createBackendPlugin","catalogProcessingExtensionPoint","catalogAnalysisExtensionPoint","catalogModelExtensionPoint","catalogLocationsExtensionPoint","coreServices","eventsServiceRef","catalogServiceRef","actionsRegistryServiceRef","catalogScmEventsServiceRef","metricsServiceRef","ModelHolder","CatalogBuilder","ForwardedError","createCatalogActions"],"mappings":";;;;;;;;;;;;;AAmDA,MAAM,kCAAA,CAEN;AAAA,EACE,cAAA;AAAA,EAEA,wBAAwB,aAAA,EAA8B;AACpD,IAAA,IAAA,CAAK,cAAA,GAAiB,aAAA;AAAA,EACxB;AAAA,EAEA,IAAI,oBAAA,GAAuB;AACzB,IAAA,OAAO,IAAA,CAAK,cAAA;AAAA,EACd;AACF;AAEA,MAAM,8BAAA,CAAqE;AAAA,EACzE,mBAAwC,EAAC;AAAA,EAEzC,mBAAmB,UAAA,EAAuC;AACxD,IAAAA,YAAA,CAAM,IAAA,CAAK,kBAAkB,UAAU,CAAA;AAAA,EACzC;AA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 -24,7 +24,9 @@ class DefaultStitcher {
|
|
|
24
24
|
knex: options.knex,
|
|
25
25
|
logger: options.logger,
|
|
26
26
|
metrics: options.metrics,
|
|
27
|
-
strategy: types.stitchingStrategyFromConfig(config
|
|
27
|
+
strategy: types.stitchingStrategyFromConfig(config, {
|
|
28
|
+
logger: options.logger
|
|
29
|
+
})
|
|
28
30
|
});
|
|
29
31
|
}
|
|
30
32
|
constructor(options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultStitcher.cjs.js","sources":["../../src/stitching/DefaultStitcher.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { durationToMilliseconds, HumanDuration } from '@backstage/types';\nimport { Knex } from 'knex';\nimport splitToChunks from 'lodash/chunk';\nimport { DateTime } from 'luxon';\nimport { getDeferredStitchableEntities } from '../database/operations/stitcher/getDeferredStitchableEntities';\nimport { markForStitching } from '../database/operations/stitcher/markForStitching';\nimport { performStitching } from '../database/operations/stitcher/performStitching';\nimport { DbRefreshStateRow } from '../database/tables';\nimport { startTaskPipeline } from '../processing/TaskPipeline';\nimport { progressTracker } from './progressTracker';\nimport {\n Stitcher,\n StitchingStrategy,\n stitchingStrategyFromConfig,\n} from './types';\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { MetricsService } from '@backstage/backend-plugin-api/alpha';\n\ntype DeferredStitchItem = Awaited<\n ReturnType<typeof getDeferredStitchableEntities>\n>[0];\n\ntype StitchProgressTracker = ReturnType<typeof progressTracker>;\n\n/**\n * Performs the act of stitching - to take all of the various outputs from the\n * ingestion process, and stitching them together into the final entity JSON\n * shape.\n */\nexport class DefaultStitcher implements Stitcher {\n private readonly knex: Knex;\n private readonly logger: LoggerService;\n private readonly strategy: StitchingStrategy;\n private readonly tracker: StitchProgressTracker;\n private stopFunc?: () => void;\n\n static fromConfig(\n config: Config,\n options: {\n knex: Knex;\n logger: LoggerService;\n metrics: MetricsService;\n },\n ): DefaultStitcher {\n return new DefaultStitcher({\n knex: options.knex,\n logger: options.logger,\n metrics: options.metrics,\n strategy: stitchingStrategyFromConfig(config),\n });\n }\n\n constructor(options: {\n knex: Knex;\n logger: LoggerService;\n metrics: MetricsService;\n strategy: StitchingStrategy;\n }) {\n this.knex = options.knex;\n this.logger = options.logger;\n this.strategy = options.strategy;\n this.tracker = progressTracker(\n options.knex,\n options.logger,\n options.metrics,\n );\n }\n\n async stitch(options: {\n entityRefs?: Iterable<string>;\n entityIds?: Iterable<string>;\n }) {\n const { entityRefs, entityIds } = options;\n\n if (this.strategy.mode === 'deferred') {\n await markForStitching({\n knex: this.knex,\n strategy: this.strategy,\n entityRefs,\n entityIds,\n });\n return;\n }\n\n if (entityRefs) {\n for (const entityRef of entityRefs) {\n await this.#stitchOne({ entityRef });\n }\n }\n\n if (entityIds) {\n const chunks = splitToChunks(\n Array.isArray(entityIds) ? entityIds : [...entityIds],\n 100,\n );\n for (const chunk of chunks) {\n const rows = await this.knex<DbRefreshStateRow>('refresh_state')\n .select('entity_ref')\n .whereIn('entity_id', chunk);\n for (const row of rows) {\n await this.#stitchOne({ entityRef: row.entity_ref });\n }\n }\n }\n }\n\n async start() {\n if (this.strategy.mode === 'deferred') {\n if (this.stopFunc) {\n throw new Error('Processing engine is already started');\n }\n\n const { pollingInterval, stitchTimeout } = this.strategy;\n\n const stopPipeline = startTaskPipeline<DeferredStitchItem>({\n lowWatermark: 2,\n highWatermark: 5,\n pollingIntervalMs: durationToMilliseconds(pollingInterval),\n loadTasks: async count => {\n return await this.#getStitchableEntities(count, stitchTimeout);\n },\n processTask: async item => {\n return await this.#stitchOne({\n entityRef: item.entityRef,\n stitchTicket: item.stitchTicket,\n stitchRequestedAt: item.stitchRequestedAt,\n });\n },\n });\n\n this.stopFunc = () => {\n stopPipeline();\n };\n }\n }\n\n async stop() {\n if (this.strategy.mode === 'deferred') {\n if (this.stopFunc) {\n this.stopFunc();\n this.stopFunc = undefined;\n }\n }\n }\n\n async #getStitchableEntities(count: number, stitchTimeout: HumanDuration) {\n try {\n return await getDeferredStitchableEntities({\n knex: this.knex,\n batchSize: count,\n stitchTimeout: stitchTimeout,\n });\n } catch (error) {\n this.logger.warn('Failed to load stitchable entities', error);\n return [];\n }\n }\n\n async #stitchOne(options: {\n entityRef: string;\n stitchTicket?: string;\n stitchRequestedAt?: DateTime;\n }) {\n const track = this.tracker.stitchStart({\n entityRef: options.entityRef,\n stitchRequestedAt: options.stitchRequestedAt,\n });\n\n try {\n const result = await performStitching({\n knex: this.knex,\n logger: this.logger,\n strategy: this.strategy,\n entityRef: options.entityRef,\n stitchTicket: options.stitchTicket,\n });\n track.markComplete(result);\n } catch (error) {\n track.markFailed(error);\n }\n }\n}\n"],"names":["stitchingStrategyFromConfig","progressTracker","markForStitching","splitToChunks","startTaskPipeline","durationToMilliseconds","getDeferredStitchableEntities","performStitching"],"mappings":";;;;;;;;;;;;;;;AA8CO,MAAM,eAAA,CAAoC;AAAA,EAC9B,IAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACT,QAAA;AAAA,EAER,OAAO,UAAA,CACL,MAAA,EACA,OAAA,EAKiB;AACjB,IAAA,OAAO,IAAI,eAAA,CAAgB;AAAA,MACzB,MAAM,OAAA,CAAQ,IAAA;AAAA,MACd,QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,SAAS,OAAA,CAAQ,OAAA;AAAA,MACjB,QAAA,EAAUA,kCAA4B,
|
|
1
|
+
{"version":3,"file":"DefaultStitcher.cjs.js","sources":["../../src/stitching/DefaultStitcher.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { durationToMilliseconds, HumanDuration } from '@backstage/types';\nimport { Knex } from 'knex';\nimport splitToChunks from 'lodash/chunk';\nimport { DateTime } from 'luxon';\nimport { getDeferredStitchableEntities } from '../database/operations/stitcher/getDeferredStitchableEntities';\nimport { markForStitching } from '../database/operations/stitcher/markForStitching';\nimport { performStitching } from '../database/operations/stitcher/performStitching';\nimport { DbRefreshStateRow } from '../database/tables';\nimport { startTaskPipeline } from '../processing/TaskPipeline';\nimport { progressTracker } from './progressTracker';\nimport {\n Stitcher,\n StitchingStrategy,\n stitchingStrategyFromConfig,\n} from './types';\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { MetricsService } from '@backstage/backend-plugin-api/alpha';\n\ntype DeferredStitchItem = Awaited<\n ReturnType<typeof getDeferredStitchableEntities>\n>[0];\n\ntype StitchProgressTracker = ReturnType<typeof progressTracker>;\n\n/**\n * Performs the act of stitching - to take all of the various outputs from the\n * ingestion process, and stitching them together into the final entity JSON\n * shape.\n */\nexport class DefaultStitcher implements Stitcher {\n private readonly knex: Knex;\n private readonly logger: LoggerService;\n private readonly strategy: StitchingStrategy;\n private readonly tracker: StitchProgressTracker;\n private stopFunc?: () => void;\n\n static fromConfig(\n config: Config,\n options: {\n knex: Knex;\n logger: LoggerService;\n metrics: MetricsService;\n },\n ): DefaultStitcher {\n return new DefaultStitcher({\n knex: options.knex,\n logger: options.logger,\n metrics: options.metrics,\n strategy: stitchingStrategyFromConfig(config, {\n logger: options.logger,\n }),\n });\n }\n\n constructor(options: {\n knex: Knex;\n logger: LoggerService;\n metrics: MetricsService;\n strategy: StitchingStrategy;\n }) {\n this.knex = options.knex;\n this.logger = options.logger;\n this.strategy = options.strategy;\n this.tracker = progressTracker(\n options.knex,\n options.logger,\n options.metrics,\n );\n }\n\n async stitch(options: {\n entityRefs?: Iterable<string>;\n entityIds?: Iterable<string>;\n }) {\n const { entityRefs, entityIds } = options;\n\n if (this.strategy.mode === 'deferred') {\n await markForStitching({\n knex: this.knex,\n strategy: this.strategy,\n entityRefs,\n entityIds,\n });\n return;\n }\n\n if (entityRefs) {\n for (const entityRef of entityRefs) {\n await this.#stitchOne({ entityRef });\n }\n }\n\n if (entityIds) {\n const chunks = splitToChunks(\n Array.isArray(entityIds) ? entityIds : [...entityIds],\n 100,\n );\n for (const chunk of chunks) {\n const rows = await this.knex<DbRefreshStateRow>('refresh_state')\n .select('entity_ref')\n .whereIn('entity_id', chunk);\n for (const row of rows) {\n await this.#stitchOne({ entityRef: row.entity_ref });\n }\n }\n }\n }\n\n async start() {\n if (this.strategy.mode === 'deferred') {\n if (this.stopFunc) {\n throw new Error('Processing engine is already started');\n }\n\n const { pollingInterval, stitchTimeout } = this.strategy;\n\n const stopPipeline = startTaskPipeline<DeferredStitchItem>({\n lowWatermark: 2,\n highWatermark: 5,\n pollingIntervalMs: durationToMilliseconds(pollingInterval),\n loadTasks: async count => {\n return await this.#getStitchableEntities(count, stitchTimeout);\n },\n processTask: async item => {\n return await this.#stitchOne({\n entityRef: item.entityRef,\n stitchTicket: item.stitchTicket,\n stitchRequestedAt: item.stitchRequestedAt,\n });\n },\n });\n\n this.stopFunc = () => {\n stopPipeline();\n };\n }\n }\n\n async stop() {\n if (this.strategy.mode === 'deferred') {\n if (this.stopFunc) {\n this.stopFunc();\n this.stopFunc = undefined;\n }\n }\n }\n\n async #getStitchableEntities(count: number, stitchTimeout: HumanDuration) {\n try {\n return await getDeferredStitchableEntities({\n knex: this.knex,\n batchSize: count,\n stitchTimeout: stitchTimeout,\n });\n } catch (error) {\n this.logger.warn('Failed to load stitchable entities', error);\n return [];\n }\n }\n\n async #stitchOne(options: {\n entityRef: string;\n stitchTicket?: string;\n stitchRequestedAt?: DateTime;\n }) {\n const track = this.tracker.stitchStart({\n entityRef: options.entityRef,\n stitchRequestedAt: options.stitchRequestedAt,\n });\n\n try {\n const result = await performStitching({\n knex: this.knex,\n logger: this.logger,\n strategy: this.strategy,\n entityRef: options.entityRef,\n stitchTicket: options.stitchTicket,\n });\n track.markComplete(result);\n } catch (error) {\n track.markFailed(error);\n }\n }\n}\n"],"names":["stitchingStrategyFromConfig","progressTracker","markForStitching","splitToChunks","startTaskPipeline","durationToMilliseconds","getDeferredStitchableEntities","performStitching"],"mappings":";;;;;;;;;;;;;;;AA8CO,MAAM,eAAA,CAAoC;AAAA,EAC9B,IAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACT,QAAA;AAAA,EAER,OAAO,UAAA,CACL,MAAA,EACA,OAAA,EAKiB;AACjB,IAAA,OAAO,IAAI,eAAA,CAAgB;AAAA,MACzB,MAAM,OAAA,CAAQ,IAAA;AAAA,MACd,QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,SAAS,OAAA,CAAQ,OAAA;AAAA,MACjB,QAAA,EAAUA,kCAA4B,MAAA,EAAQ;AAAA,QAC5C,QAAQ,OAAA,CAAQ;AAAA,OACjB;AAAA,KACF,CAAA;AAAA,EACH;AAAA,EAEA,YAAY,OAAA,EAKT;AACD,IAAA,IAAA,CAAK,OAAO,OAAA,CAAQ,IAAA;AACpB,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,WAAW,OAAA,CAAQ,QAAA;AACxB,IAAA,IAAA,CAAK,OAAA,GAAUC,+BAAA;AAAA,MACb,OAAA,CAAQ,IAAA;AAAA,MACR,OAAA,CAAQ,MAAA;AAAA,MACR,OAAA,CA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AK,CAAA;AAC5D,MAAA,OAAO,EAAC;AAAA,IACV;AAAA,EACF;AAAA,EAEA,MAAM,WAAW,OAAA,EAId;AACD,IAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,OAAA,CAAQ,WAAA,CAAY;AAAA,MACrC,WAAW,OAAA,CAAQ,SAAA;AAAA,MACnB,mBAAmB,OAAA,CAAQ;AAAA,KAC5B,CAAA;AAED,IAAA,IAAI;AACF,MAAA,MAAM,MAAA,GAAS,MAAMC,iCAAA,CAAiB;AAAA,QACpC,MAAM,IAAA,CAAK,IAAA;AAAA,QACX,QAAQ,IAAA,CAAK,MAAA;AAAA,QACb,UAAU,IAAA,CAAK,QAAA;AAAA,QACf,WAAW,OAAA,CAAQ,SAAA;AAAA,QACnB,cAAc,OAAA,CAAQ;AAAA,OACvB,CAAA;AACD,MAAA,KAAA,CAAM,aAAa,MAAM,CAAA;AAAA,IAC3B,SAAS,KAAA,EAAO;AACd,MAAA,KAAA,CAAM,WAAW,KAAK,CAAA;AAAA,IACxB;AAAA,EACF;AACF;;;;"}
|
|
@@ -2,11 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
var config = require('@backstage/config');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
let immediateDeprecationLogged = false;
|
|
6
|
+
function stitchingStrategyFromConfig(config$1, options) {
|
|
6
7
|
const strategyMode = config$1.getOptionalString(
|
|
7
8
|
"catalog.stitchingStrategy.mode"
|
|
8
9
|
);
|
|
9
10
|
if (strategyMode === "immediate") {
|
|
11
|
+
if (!immediateDeprecationLogged) {
|
|
12
|
+
immediateDeprecationLogged = true;
|
|
13
|
+
options.logger.warn(
|
|
14
|
+
"DEPRECATED: Immediate mode stitching has been deprecated, and will be removed in the next Backstage release."
|
|
15
|
+
);
|
|
16
|
+
}
|
|
10
17
|
return {
|
|
11
18
|
mode: "immediate"
|
|
12
19
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.cjs.js","sources":["../../src/stitching/types.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config, readDurationFromConfig } from '@backstage/config';\nimport { HumanDuration } from '@backstage/types';\n\n/**\n * Performs the act of stitching - to take all of the various outputs from the\n * ingestion process, and stitching them together into the final entity JSON\n * shape.\n */\nexport interface Stitcher {\n stitch(options: {\n entityRefs?: Iterable<string>;\n entityIds?: Iterable<string>;\n }): Promise<void>;\n}\n\n/**\n * The strategies supported by the stitching process, in terms of when to\n * perform stitching.\n *\n * @remarks\n *\n * In immediate mode, stitching happens \"in-band\" (blocking) immediately when\n * each processing task finishes. When set to `'deferred'`, stitching is instead\n * deferred to happen on a separate asynchronous worker queue just like\n * processing.\n *\n * Deferred stitching should make performance smoother when ingesting large\n * amounts of entities, and reduce p99 processing times and repeated\n * over-stitching of hot spot entities when fan-out/fan-in in terms of relations\n * is very large. It does however also come with some performance cost due to\n * the queuing with how much wall-clock time some types of task take.\n */\nexport type StitchingStrategy =\n | {\n mode: 'immediate';\n }\n | {\n mode: 'deferred';\n pollingInterval: HumanDuration;\n stitchTimeout: HumanDuration;\n };\n\nexport function stitchingStrategyFromConfig(config: Config): StitchingStrategy {\n const strategyMode = config.getOptionalString(\n 'catalog.stitchingStrategy.mode',\n );\n\n if (strategyMode === 'immediate') {\n return {\n mode: 'immediate',\n };\n } else if (strategyMode === undefined || strategyMode === 'deferred') {\n const pollingIntervalKey = 'catalog.stitchingStrategy.pollingInterval';\n const stitchTimeoutKey = 'catalog.stitchingStrategy.stitchTimeout';\n\n const pollingInterval = config.has(pollingIntervalKey)\n ? readDurationFromConfig(config, { key: pollingIntervalKey })\n : { seconds: 1 };\n const stitchTimeout = config.has(stitchTimeoutKey)\n ? readDurationFromConfig(config, { key: stitchTimeoutKey })\n : { seconds: 60 };\n\n return {\n mode: 'deferred',\n pollingInterval: pollingInterval,\n stitchTimeout: stitchTimeout,\n };\n }\n\n throw new Error(\n `Invalid stitching strategy mode '${strategyMode}', expected one of 'immediate' or 'deferred'`,\n );\n}\n"],"names":["config","readDurationFromConfig"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"types.cjs.js","sources":["../../src/stitching/types.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { Config, readDurationFromConfig } from '@backstage/config';\nimport { HumanDuration } from '@backstage/types';\n\n/**\n * Performs the act of stitching - to take all of the various outputs from the\n * ingestion process, and stitching them together into the final entity JSON\n * shape.\n */\nexport interface Stitcher {\n stitch(options: {\n entityRefs?: Iterable<string>;\n entityIds?: Iterable<string>;\n }): Promise<void>;\n}\n\n/**\n * The strategies supported by the stitching process, in terms of when to\n * perform stitching.\n *\n * @remarks\n *\n * In immediate mode, stitching happens \"in-band\" (blocking) immediately when\n * each processing task finishes. When set to `'deferred'`, stitching is instead\n * deferred to happen on a separate asynchronous worker queue just like\n * processing.\n *\n * Deferred stitching should make performance smoother when ingesting large\n * amounts of entities, and reduce p99 processing times and repeated\n * over-stitching of hot spot entities when fan-out/fan-in in terms of relations\n * is very large. It does however also come with some performance cost due to\n * the queuing with how much wall-clock time some types of task take.\n *\n * Note: Immediate mode is deprecated and will be removed in a future release.\n */\nexport type StitchingStrategy =\n | {\n mode: 'immediate';\n }\n | {\n mode: 'deferred';\n pollingInterval: HumanDuration;\n stitchTimeout: HumanDuration;\n };\n\nlet immediateDeprecationLogged = false;\n\nexport function stitchingStrategyFromConfig(\n config: Config,\n options: { logger: LoggerService },\n): StitchingStrategy {\n const strategyMode = config.getOptionalString(\n 'catalog.stitchingStrategy.mode',\n );\n\n if (strategyMode === 'immediate') {\n if (!immediateDeprecationLogged) {\n immediateDeprecationLogged = true;\n options.logger.warn(\n 'DEPRECATED: Immediate mode stitching has been deprecated, and will be removed in the next Backstage release.',\n );\n }\n return {\n mode: 'immediate',\n };\n } else if (strategyMode === undefined || strategyMode === 'deferred') {\n const pollingIntervalKey = 'catalog.stitchingStrategy.pollingInterval';\n const stitchTimeoutKey = 'catalog.stitchingStrategy.stitchTimeout';\n\n const pollingInterval = config.has(pollingIntervalKey)\n ? readDurationFromConfig(config, { key: pollingIntervalKey })\n : { seconds: 1 };\n const stitchTimeout = config.has(stitchTimeoutKey)\n ? readDurationFromConfig(config, { key: stitchTimeoutKey })\n : { seconds: 60 };\n\n return {\n mode: 'deferred',\n pollingInterval: pollingInterval,\n stitchTimeout: stitchTimeout,\n };\n }\n\n throw new Error(\n `Invalid stitching strategy mode '${strategyMode}', expected one of 'immediate' or 'deferred'`,\n );\n}\n"],"names":["config","readDurationFromConfig"],"mappings":";;;;AA6DA,IAAI,0BAAA,GAA6B,KAAA;AAE1B,SAAS,2BAAA,CACdA,UACA,OAAA,EACmB;AACnB,EAAA,MAAM,eAAeA,QAAA,CAAO,iBAAA;AAAA,IAC1B;AAAA,GACF;AAEA,EAAA,IAAI,iBAAiB,WAAA,EAAa;AAChC,IAAA,IAAI,CAAC,0BAAA,EAA4B;AAC/B,MAAA,0BAAA,GAA6B,IAAA;AAC7B,MAAA,OAAA,CAAQ,MAAA,CAAO,IAAA;AAAA,QACb;AAAA,OACF;AAAA,IACF;AACA,IAAA,OAAO;AAAA,MACL,IAAA,EAAM;AAAA,KACR;AAAA,EACF,CAAA,MAAA,IAAW,YAAA,KAAiB,MAAA,IAAa,YAAA,KAAiB,UAAA,EAAY;AACpE,IAAA,MAAM,kBAAA,GAAqB,2CAAA;AAC3B,IAAA,MAAM,gBAAA,GAAmB,yCAAA;AAEzB,IAAA,MAAM,eAAA,GAAkBA,QAAA,CAAO,GAAA,CAAI,kBAAkB,IACjDC,6BAAA,CAAuBD,QAAA,EAAQ,EAAE,GAAA,EAAK,kBAAA,EAAoB,CAAA,GAC1D,EAAE,SAAS,CAAA,EAAE;AACjB,IAAA,MAAM,aAAA,GAAgBA,QAAA,CAAO,GAAA,CAAI,gBAAgB,IAC7CC,6BAAA,CAAuBD,QAAA,EAAQ,EAAE,GAAA,EAAK,gBAAA,EAAkB,CAAA,GACxD,EAAE,SAAS,EAAA,EAAG;AAElB,IAAA,OAAO;AAAA,MACL,IAAA,EAAM,UAAA;AAAA,MACN,eAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAEA,EAAA,MAAM,IAAI,KAAA;AAAA,IACR,oCAAoC,YAAY,CAAA,4CAAA;AAAA,GAClD;AACF;;;;"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 The Backstage Authors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// @ts-check
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Adds an index on `relations.target_entity_ref`.
|
|
21
|
+
*
|
|
22
|
+
* The `relations` table had indexes on `originating_entity_id` and
|
|
23
|
+
* `source_entity_ref` but none on `target_entity_ref`. Several query
|
|
24
|
+
* paths (orphan deletion, entity ancestry, eager pruning) join or
|
|
25
|
+
* filter on `target_entity_ref`, causing full sequential scans of the
|
|
26
|
+
* table (~3.5M rows, ~714 MB heap).
|
|
27
|
+
*
|
|
28
|
+
* On PostgreSQL this uses CREATE INDEX CONCURRENTLY to avoid blocking
|
|
29
|
+
* reads and writes. The index is ~141 MB based on the column's average
|
|
30
|
+
* width of ~35 bytes across ~3.5M rows.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {import('knex').Knex} knex
|
|
35
|
+
*/
|
|
36
|
+
exports.up = async function up(knex) {
|
|
37
|
+
const client = knex.client.config.client;
|
|
38
|
+
|
|
39
|
+
if (client.includes('pg')) {
|
|
40
|
+
// Check if index already exists in the current schema (idempotent).
|
|
41
|
+
// The pg_class lookup is scoped via pg_namespace to handle
|
|
42
|
+
// schema-division mode where each plugin has its own schema.
|
|
43
|
+
const result = await knex.raw(
|
|
44
|
+
`SELECT c.oid, i.indisvalid
|
|
45
|
+
FROM pg_class c
|
|
46
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
47
|
+
LEFT JOIN pg_index i ON i.indexrelid = c.oid
|
|
48
|
+
WHERE c.relname = ?
|
|
49
|
+
AND c.relkind = 'i'
|
|
50
|
+
AND n.nspname = current_schema()`,
|
|
51
|
+
['relations_target_entity_ref_idx'],
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
if (result.rows.length > 0) {
|
|
55
|
+
if (result.rows[0].indisvalid) {
|
|
56
|
+
return; // Already exists and valid
|
|
57
|
+
}
|
|
58
|
+
// Invalid — drop and recreate
|
|
59
|
+
await knex.raw(
|
|
60
|
+
'DROP INDEX CONCURRENTLY IF EXISTS relations_target_entity_ref_idx',
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
await knex.raw(
|
|
65
|
+
'CREATE INDEX CONCURRENTLY relations_target_entity_ref_idx ON relations (target_entity_ref)',
|
|
66
|
+
);
|
|
67
|
+
} else {
|
|
68
|
+
// SQLite / MySQL — simple CREATE INDEX
|
|
69
|
+
await knex.schema.alterTable('relations', table => {
|
|
70
|
+
table.index(['target_entity_ref'], 'relations_target_entity_ref_idx');
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {import('knex').Knex} knex
|
|
77
|
+
*/
|
|
78
|
+
exports.down = async function down(knex) {
|
|
79
|
+
const client = knex.client.config.client;
|
|
80
|
+
|
|
81
|
+
if (client.includes('pg')) {
|
|
82
|
+
await knex.raw(
|
|
83
|
+
'DROP INDEX CONCURRENTLY IF EXISTS relations_target_entity_ref_idx',
|
|
84
|
+
);
|
|
85
|
+
} else {
|
|
86
|
+
await knex.schema.alterTable('relations', table => {
|
|
87
|
+
table.dropIndex([], 'relations_target_entity_ref_idx');
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
exports.config = { transaction: false };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-catalog-backend",
|
|
3
|
-
"version": "3.7.0
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "The Backstage backend plugin that provides the Backstage catalog",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "backend-plugin",
|
|
@@ -76,20 +76,20 @@
|
|
|
76
76
|
"test": "backstage-cli package test"
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"@backstage/backend-openapi-utils": "0.6.9
|
|
80
|
-
"@backstage/backend-plugin-api": "1.9.1
|
|
81
|
-
"@backstage/catalog-client": "1.15.1
|
|
82
|
-
"@backstage/catalog-model": "1.
|
|
83
|
-
"@backstage/config": "1.3.8
|
|
84
|
-
"@backstage/errors": "1.3.1
|
|
85
|
-
"@backstage/filter-predicates": "0.1.3
|
|
86
|
-
"@backstage/integration": "2.0.2
|
|
87
|
-
"@backstage/plugin-catalog-common": "1.1.10
|
|
88
|
-
"@backstage/plugin-catalog-node": "2.2.1
|
|
89
|
-
"@backstage/plugin-events-node": "0.4.22
|
|
90
|
-
"@backstage/plugin-permission-common": "0.9.9
|
|
91
|
-
"@backstage/plugin-permission-node": "0.
|
|
92
|
-
"@backstage/types": "1.2.2",
|
|
79
|
+
"@backstage/backend-openapi-utils": "^0.6.9",
|
|
80
|
+
"@backstage/backend-plugin-api": "^1.9.1",
|
|
81
|
+
"@backstage/catalog-client": "^1.15.1",
|
|
82
|
+
"@backstage/catalog-model": "^1.9.0",
|
|
83
|
+
"@backstage/config": "^1.3.8",
|
|
84
|
+
"@backstage/errors": "^1.3.1",
|
|
85
|
+
"@backstage/filter-predicates": "^0.1.3",
|
|
86
|
+
"@backstage/integration": "^2.0.2",
|
|
87
|
+
"@backstage/plugin-catalog-common": "^1.1.10",
|
|
88
|
+
"@backstage/plugin-catalog-node": "^2.2.1",
|
|
89
|
+
"@backstage/plugin-events-node": "^0.4.22",
|
|
90
|
+
"@backstage/plugin-permission-common": "^0.9.9",
|
|
91
|
+
"@backstage/plugin-permission-node": "^0.11.0",
|
|
92
|
+
"@backstage/types": "^1.2.2",
|
|
93
93
|
"@opentelemetry/api": "^1.9.0",
|
|
94
94
|
"ajv": "^8.10.0",
|
|
95
95
|
"ajv-errors": "^3.0.0",
|
|
@@ -112,12 +112,12 @@
|
|
|
112
112
|
"zod-validation-error": "^4.0.2"
|
|
113
113
|
},
|
|
114
114
|
"devDependencies": {
|
|
115
|
-
"@backstage/backend-defaults": "0.17.1
|
|
116
|
-
"@backstage/backend-test-utils": "1.11.3
|
|
117
|
-
"@backstage/cli": "0.36.2
|
|
118
|
-
"@backstage/plugin-catalog-backend-module-logs": "0.1.22
|
|
119
|
-
"@backstage/plugin-scaffolder-common": "2.2.0
|
|
120
|
-
"@backstage/repo-tools": "0.17.2
|
|
115
|
+
"@backstage/backend-defaults": "^0.17.1",
|
|
116
|
+
"@backstage/backend-test-utils": "^1.11.3",
|
|
117
|
+
"@backstage/cli": "^0.36.2",
|
|
118
|
+
"@backstage/plugin-catalog-backend-module-logs": "^0.1.22",
|
|
119
|
+
"@backstage/plugin-scaffolder-common": "^2.2.0",
|
|
120
|
+
"@backstage/repo-tools": "^0.17.2",
|
|
121
121
|
"@types/core-js": "^2.5.4",
|
|
122
122
|
"@types/express": "^4.17.6",
|
|
123
123
|
"@types/git-url-parse": "^9.0.0",
|