Tech Insights Backend JSON Rules engine fact checker module
This is an extension to module to tech-insights-backend plugin, which provides basic framework and functionality to implement tech insights within Backstage.
This module provides functionality to run checks against a json-rules-engine and provide boolean logic by simply building checks using JSON conditions.
Getting started
To add this FactChecker into your Tech Insights you need to install the module into your backend application:
cd packages/backend
yarn add @backstage/plugin-tech-insights-backend-module-jsonfc
and modify the techInsights.ts
file to contain a reference to the FactCheckers implementation.
+import { JsonRulesEngineFactCheckerFactory } from '@backstage/plugin-tech-insights-backend-module-jsonfc';
+const myFactCheckerFactory = new JsonRulesEngineFactCheckerFactory({
+ checks: [],
+ logger,
+}),
const builder = buildTechInsightsContext({
logger,
config,
database,
discovery,
factRetrievers: [myFactRetrieverRegistration],
+ factCheckerFactory: myFactCheckerFactory
});
By default this implementation comes with an in-memory storage to store checks. You can inject an additional data store by adding an implementation of TechInsightCheckRegistry
into the constructor options when creating a JsonRulesEngineFactCheckerFactory
. That can be done as follows
const myTechInsightCheckRegistry: TechInsightCheckRegistry<MyCheckType> = // snip
const myFactCheckerFactory = new JsonRulesEngineFactCheckerFactory({
checks: [],
logger,
+ checkRegistry: myTechInsightCheckRegistry
}),
Adding checks
Checks for this FactChecker are constructed as json-rules-engine
compatible JSON rules. A check could look like the following for example:
import {
JSON_RULE_ENGINE_CHECK_TYPE,
TechInsightJsonRuleCheck,
} from '@backstage/plugin-tech-insights-backend-module-jsonfc';
export const exampleCheck: TechInsightJsonRuleCheck = {
id: 'demodatacheck',
name: 'demodatacheck',
type: JSON_RULE_ENGINE_CHECK_TYPE,
description: 'A fact check for demoing purposes',
factIds: ['documentation-number-factretriever'],
rule: {
conditions: {
all: [
{
fact: 'examplenumberfact',
operator: 'greaterThanInclusive',
value: 2,
},
],
},
},
successMetadata: {
link: 'https://link.to.some.information.com',
},
failureMetadata: {
link: 'https://sonar.mysonarqube.com/increasing-number-value',
},
};
Custom operators
json-rules-engine supports a limited number of built-in operators that can be used in conditions. You can add your own operators by adding them to the operators
array in the JsonRulesEngineFactCheckerFactory
constructor. For example:
+ import { Operator } from 'json-rules-engine';
const myFactCheckerFactory = new JsonRulesEngineFactCheckerFactory({
checks: [],
logger,
+ operators: [ new Operator("startsWith", (a, b) => a.startsWith(b) ]
})
And you can then use it in your checks like this:
...
rule: {
conditions: {
any: [
{
fact: 'version',
operator: 'startsWith',
value: '12',
},
],
},
}