Skip to content

Commit

Permalink
feature: add useSKTimestamp
Browse files Browse the repository at this point in the history
Add the ability to use the timestamp in the incoming
SK data so that you can play back older data and get
it written to the db with correct timestamps.
  • Loading branch information
tkurki committed Dec 25, 2023
1 parent b29de90 commit 25ea85a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/influx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export interface SKInfluxConfig {
*/
ignoredSources: string[]

/**
* @title Use timestamps in SK data
* @default false
* @description Whether the timestamps in SK data should be used instead of time of insertion to InfluxDB
*/
useSKTimestamp: boolean

writeOptions: Partial<WriteOptions>
}

Expand Down Expand Up @@ -109,16 +116,18 @@ export class SKInflux {
} = {}
private ignoredPaths: string[]
private ignoredSources: string[]
private useSKTimestamp: boolean

constructor(config: SKInfluxConfig, private logging: Logging, triggerStatusUpdate: () => void) {
const { org, bucket, url, onlySelf, ignoredPaths, ignoredSources } = config
const { org, bucket, url, onlySelf, ignoredPaths, ignoredSources, useSKTimestamp } = 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.useSKTimestamp = useSKTimestamp
this.writeApi = this.influx.getWriteApi(org, bucket, 'ms', {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
writeFailed: (_error, lines, _attempt, _expires) => {
Expand Down Expand Up @@ -186,12 +195,14 @@ export class SKInflux {
}
}

handleValue(context: SKContext, isSelf: boolean, source: string, pathValue: PathValue) {
handleValue(context: SKContext, isSelf: boolean, source: string, _timestamp: Date, pathValue: PathValue) {
const timestamp = this.useSKTimestamp ? _timestamp : new Date()
if (this.isIgnored(pathValue.path, source)) {
return
}
if (!this.onlySelf || isSelf) {
const point = toPoint(context, isSelf, source, pathValue, this.logging.debug)
const point = toPoint(context, isSelf, source, timestamp, pathValue, this.logging.debug)
console.log(point)
if (point) {
this.writeApi.writePoint(point)
}
Expand Down Expand Up @@ -251,10 +262,11 @@ const toPoint = (
context: SKContext,
isSelf: boolean,
source: string,
timestamp: Date,
pathValue: PathValue,
debug: (s: string) => void,
) => {
const point = new Point(influxPath(pathValue.path)).tag('context', context).tag('source', source)
const point = new Point(influxPath(pathValue.path)).tag('context', context).tag('source', source).timestamp(timestamp)
if (isSelf) {
point.tag(SELF_TAG_NAME, SELF_TAG_VALUE)
}
Expand Down
1 change: 1 addition & 0 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Plugin', () => {
},
ignoredPaths: [],
ignoredSources: [],
useSKTimestamp: false,
},
],
})
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function InfluxPluginFactory(app: App): Plugin & InfluxPlugin {
update.values &&
update.values.forEach((pathValue) => {
skInfluxes.forEach((skInflux) =>
skInflux.handleValue(delta.context, isSelf, update.$source, pathValue),
skInflux.handleValue(delta.context, isSelf, update.$source, update.timestamp, pathValue),
)
})
})
Expand Down
1 change: 1 addition & 0 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const skinflux = new SKInflux(
writeOptions: {},
ignoredPaths: [],
ignoredSources: [],
useSKTimestamp: false,
},
logging,
() => undefined,
Expand Down

0 comments on commit 25ea85a

Please sign in to comment.