Adds a configuration option to fact retrievers to define lifecycle for facts the retriever persists. Possible values are either 'items-to-live' or 'time-to-live'. The former will only n number of items in to the database for each fact per entity. The latter will remove all facts that are older than the TTL value.

Possible values:
* { itl: 5 } // Deletes all facts for the retriever/entity pair, apart from the last five
* { ttl: 1209600000 } // (2 weeks) Deletes all facts older than 2 weeks for the retriever/entity pair
* { ttl: { weeks: 2 } } // Deletes all facts older than 2 weeks for the retriever/entity pair

Signed-off-by: Jussi Hallila <jussi@hallila.com>
This commit is contained in:
Jussi Hallila
2022-01-10 15:38:16 +01:00
parent bc5fe6c6c4
commit dfd5e81721
11 changed files with 286 additions and 19 deletions
+38 -1
View File
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DateTime } from 'luxon';
import { DateTime, DurationLike } from 'luxon';
import { Config } from '@backstage/config';
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { Logger } from 'winston';
@@ -190,6 +190,36 @@ export interface FactRetriever {
| Record<string, string | symbol | (string | symbol)[]>;
}
/**
* @public
*
* A Luxon duration like object for time to live value
*
* @example
* \{ ttl: 1209600000 \}
* \{ ttl: \{ weeks: 4 \} \}
*
**/
export type TTL = { ttl: DurationLike };
/**
* @public
*
* A number for items to live value
*
* @example
* \{ itl: 10 \}
*
**/
export type ITL = { itl: number };
/**
* @public
*
* A fact lifecycle definition. Determines which strategy to use to purge expired facts from the database.
*/
export type FactLifecycle = TTL | ITL;
/**
* @public
*
@@ -216,4 +246,11 @@ export type FactRetrieverRegistration = {
*
*/
cadence?: string;
/**
* Fact lifecycle definition
*
* If defined this value will be used to determine expired items which will deleted when this fact retriever is run
*/
lifecycle?: FactLifecycle;
};
@@ -18,6 +18,7 @@ import {
TechInsightFact,
FlatTechInsightFact,
FactSchemaDefinition,
FactLifecycle,
} from './facts';
import { DateTime } from 'luxon';
@@ -35,8 +36,13 @@ export interface TechInsightsStore {
*
* @param id - Unique identifier of the fact retriever these facts relate to
* @param facts - A collection of TechInsightFacts
* @param lifecycle - (Optional) Fact lifecycle object indicating the expiration logic for these items
*/
insertFacts(id: string, facts: TechInsightFact[]): Promise<void>;
insertFacts(
id: string,
facts: TechInsightFact[],
lifecycle?: FactLifecycle,
): Promise<void>;
/**
* @param ids - A collection of fact row identifiers