Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add filteringrules #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/HistoryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ interface SimpleRequest {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ValuesResultRow = any[]

function getPositions(
export function getPositions(
v1Client: InfluxV1,
context: string,
from: ZonedDateTime,
Expand Down
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 allowing and/or ignoring data for writing. First matching rule decides whether to allow writing or ignore and not write the value. If there are rules but none of them match the value will be written. 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 isIgnored = firstMatchingRule !== undefined && !firstMatchingRule.allow
this.logging.debug(
`${path} from ${sourceRef} will be ${isIgnored ? 'ignored' : 'written'}, matching rule is ${JSON.stringify(
firstMatchingRule,
)}`,
)
return isIgnored
}

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
Loading