substreams-sink-redis
Version:
Substreams Redis sink module
65 lines • 3.02 kB
JavaScript
import { TimeSeriesDuplicatePolicies, TimeSeriesAggregationType } from "redis";
import { logger } from "substreams-sink";
import { toTimestamp } from "./utils.js";
export function TS_CREATE(client, key, labels, kvRetentionPeriod) {
logger.info("TS.CREATE", { key, kvRetentionPeriod });
return client.ts.CREATE(key, { RETENTION: kvRetentionPeriod, LABELS: labels, DUPLICATE_POLICY: TimeSeriesDuplicatePolicies.SUM });
}
export function TS_CREATERULE(client, sourceKey, destinationKey, options) {
logger.info("TS.CREATERULE", { sourceKey, destinationKey, aggregation: TimeSeriesAggregationType.SUM, bucketDuration: options.kvBucketDuration });
return client.ts.CREATERULE(sourceKey, destinationKey, TimeSeriesAggregationType.SUM, options.kvBucketDuration);
}
export async function TS_ADD(client, key, value, clock, labels, options) {
const timestamp = toTimestamp(clock);
logger.info("TS.ADD", { key, timestamp, value, labels });
if (!value)
return; // no value
return client.ts.ADD(key, timestamp, value, { ON_DUPLICATE: TimeSeriesDuplicatePolicies.SUM, LABELS: labels, RETENTION: options.kvRetentionPeriod });
}
// https://redis.io/commands/set/
export function SET(client, key, value) {
logger.info("SET", { key, value });
return client.SET(key, value);
}
// https://redis.io/commands/get/
export function GET(client, params) {
const key = params.get("key");
if (!key)
throw new Error(`[key] is required`);
logger.info("GET", { key });
return client.GET(key);
}
// https://redis.io/commands/info/
export function INFO(client) {
logger.info("INFO");
return client.INFO();
}
// https://redis.io/commands/ts.info/
export async function TS_INFO(client, params) {
const key = params.get("key");
if (!key)
throw new Error(`[key] is required`);
logger.info("TS.INFO", { key });
return client.ts.INFO(key);
}
// https://redis.io/commands/ts.range/
export function TS_RANGE(client, params) {
const key = params.get("key");
const fromTimestamp = params.get("fromTimestamp") ?? "-";
const toTimestamp = params.get("toTimestamp") ?? "+";
const aggregationType = params.get("aggregationType") ?? TimeSeriesAggregationType.SUM;
const bucketDuration = params.get("bucketDuration") ?? "1";
if (!key)
throw new Error(`[key] is required`);
if (!fromTimestamp)
throw new Error(`[fromTimestamp] is required`);
if (!toTimestamp)
throw new Error(`[toTimestamp] is required`);
if (!Object.values(TimeSeriesAggregationType).includes(aggregationType))
throw new Error(`[aggregationType] must be one of ${Object.values(TimeSeriesAggregationType).join(", ")}`);
if (parseInt(bucketDuration) <= 0)
throw new Error(`[bucketDuration] must be greater than 0`);
logger.info("TS.RANGE", params);
return client.ts.RANGE(key, fromTimestamp, toTimestamp, { AGGREGATION: { type: aggregationType, timeBucket: bucketDuration } });
}
//# sourceMappingURL=redis.js.map