Skip to content

Commit

Permalink
feature: add filteringrules
Browse files Browse the repository at this point in the history
Add more flexible filteringRules so that you can use both
allow and ignore logic in determining what should or should not
be written to the database.

Fixes #27.
  • Loading branch information
tkurki committed Sep 17, 2024
1 parent 28d7adf commit 28fdb6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/influx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ import { S2 } from 's2-geometry'
export const SELF_TAG_NAME = 'self'
export const SELF_TAG_VALUE = 'true'

interface FilteringRule {
/**
* @title Allow rule
* @description Check to use this rule for picking data for writing, otherwise this rule will cause data to be ignored.
*/
allow: boolean
/**
* @title Path
* @description Literal value or JS regular expression.
*/
path: string

/**
* @title Source
* @description Literal value or JS regular expression. You can copypaste values from server's Data Browser
*/
source: string
}

export interface SKInfluxConfig {
/**
* Url of the InfluxDb 2 server
Expand Down Expand Up @@ -55,6 +74,13 @@ export interface SKInfluxConfig {
*/
onlySelf: boolean

/**
* @title Filtering Rules
* @default []
* @description Filtering rules for picking and/or ignoring data for writing. First matching rule decides whether to allow writing or ignore and not write the data. Adding rules disables ignoredPaths and ignoredValues. To pick some values add allow rules for them and an "ignore all" rule at the end with .* in either path or source field. Activate Debug logging in plugin configuration to get logging on server startup about ignored and allowed data.
*/
filteringRules: FilteringRule[]

/**
* @title Ignored paths
* @default []
Expand Down Expand Up @@ -116,6 +142,7 @@ export class SKInflux {
private ignoreStatusForPathSources: {
[path: string]: boolean
} = {}
private filteringRules: FilteringRule[]
private ignoredPaths: string[]
private ignoredSources: string[]
private useSKTimestamp: boolean
Expand All @@ -130,14 +157,16 @@ export class SKInflux {
private resolution: number

constructor(config: SKInfluxConfig, private logging: Logging, triggerStatusUpdate: () => void) {
const { org, bucket, url, onlySelf, ignoredPaths, ignoredSources, resolution, useSKTimestamp } = config
const { org, bucket, url, onlySelf, ignoredPaths, ignoredSources, resolution, useSKTimestamp, filteringRules } =
config
this.influx = new InfluxDB(config)
this.org = org
this.bucket = bucket
this.url = url
this.onlySelf = onlySelf
this.ignoredPaths = ignoredPaths
this.ignoredSources = ignoredSources
this.filteringRules = filteringRules || []
this.useSKTimestamp = useSKTimestamp
this.resolution = resolution
this.writeApi = this.influx.getWriteApi(org, bucket, 'ms', {
Expand Down Expand Up @@ -284,6 +313,31 @@ export class SKInflux {
}

ignoreStatusByConfig(path: string, sourceRef: string): boolean {
if (this.filteringRules?.length > 0) {
return this.ignoreStatusByRule(path, sourceRef)
} else {
return this.ignoreStatusByIgnoredPathOrSource(path, sourceRef)
}
}

ignoreStatusByRule(path: string, sourceRef: string): boolean {
const firstMatchingRule = this.filteringRules.find(
(rule) => {
return (rule.path === undefined || rule.path.length === 0 ||
new RegExp(rule.path).test(path)) &&
(rule.source === undefined || rule.source.length === 0 ||
new RegExp(rule.source).test(sourceRef))
}
)
//ignore if there is a matching rule AND the rule is not an allow rule
const ignored = firstMatchingRule !== undefined && !firstMatchingRule.allow
this.logging.debug(
`${path} from ${sourceRef} will be ${ignored ? 'ignored' : 'written'}, matching rule is ${JSON.stringify(firstMatchingRule)}`,
)
return ignored
}

ignoreStatusByIgnoredPathOrSource(path: string, sourceRef: string): boolean {
try {
const ignoredByPath =
this.ignoredPaths?.reduce<boolean>((acc, ignoredPathExp) => {
Expand Down
2 changes: 2 additions & 0 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('Plugin', () => {
flushInterval: 10,
maxRetries: 1,
},
filteringRules: [],
ignoredPaths: [],
ignoredSources: [],
useSKTimestamp: false,
Expand Down Expand Up @@ -255,6 +256,7 @@ describe('Plugin', () => {
flushInterval: 10,
maxRetries: 1,
},
filteringRules: [],
ignoredPaths: [],
ignoredSources: [],
useSKTimestamp: true, // <===============
Expand Down
1 change: 1 addition & 0 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const skinflux = new SKInflux(
bucket: process.env.BUCKET || '!!!',
org: process.env.ORG || '!!!',
writeOptions: {},
filteringRules: [],
ignoredPaths: [],
ignoredSources: [],
useSKTimestamp: false,
Expand Down

0 comments on commit 28fdb6e

Please sign in to comment.