Created
December 17, 2022 17:17
-
-
Save kevcodez/a614ad227d45ef7edd966265a95a5590 to your computer and use it in GitHub Desktop.
ioredis timeseries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async timestampCreateSeries({ | |
key, | |
retentionInSeconds, | |
labels, | |
}: { | |
key: string; | |
retentionInSeconds: number; | |
labels: string[][]; | |
}) { | |
const args: (string | number)[] = [key, 'RETENTION', retentionInSeconds, 'DUPLICATE_POLICY', 'LAST']; | |
if (labels.length) { | |
args.push('LABELS'); | |
labels.forEach((label) => { | |
args.push(label[0]); | |
args.push(label[1]); | |
}); | |
} | |
await this.redisInstance().callBuffer('TS.CREATE', args); | |
} | |
async timestampAddMultiple(values: { key: string; timestamp: Date; value: number }[]) { | |
const args: (string | number)[] = []; | |
values.forEach((v) => { | |
args.push(v.key); | |
args.push(dateUtils.getUnixTime(v.timestamp)); | |
args.push(v.value); | |
}); | |
await this.redisInstance().callBuffer('TS.MADD', args); | |
} | |
async timestampRangeMultipleWithLabels({ | |
from, | |
to, | |
filters, | |
}: { | |
from: string | number; | |
to: string | number; | |
filters: string[]; | |
}): Promise<{ key: string; labels: Record<string, string>; values: [] }[]> { | |
return this.withCircuitBreaker( | |
async () => { | |
const args = [from, to, 'WITHLABELS', 'FILTER', ...filters]; | |
const data = await this.redisInstance().call('TS.MRANGE', args); | |
return data.map((it) => { | |
const labels = {}; | |
it[1].forEach(([k, v]) => (labels[k] = v)); | |
return { | |
key: it[0], | |
labels, | |
values: it[2], | |
}; | |
}); | |
}, | |
() => [], | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment