Skip to content

Commit

Permalink
more funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
danstarns committed Feb 14, 2025
1 parent 8772943 commit 0768153
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/time-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ export class TimeBucketBuilder {
case 'avg':
this.metricStatements.push(`AVG(${column}) as ${escapeIdentifier(alias)}`);
break;

case 'min':
this.metricStatements.push(`MIN(${column}) as ${escapeIdentifier(alias)}`);
break;

case 'max':
this.metricStatements.push(`MAX(${column}) as ${escapeIdentifier(alias)}`);
break;

case 'first':
this.metricStatements.push(`FIRST(${column}) as ${escapeIdentifier(alias)}`);
break;

case 'last':
this.metricStatements.push(`LAST(${column}) as ${escapeIdentifier(alias)}`);
break;

default:
throw new Error(`Unsupported metric type: ${metric.type}`);
}
Expand Down
92 changes: 92 additions & 0 deletions packages/core/tests/__snapshots__/timebucket.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ FROM time_buckets;",
}
`;

exports[`TimeBucket build should generate query with first metric 1`] = `
{
"params": [
"1 hour",
2025-01-01T00:00:00.000Z,
2025-01-02T00:00:00.000Z,
],
"sql": "WITH time_buckets AS (
SELECT
time_bucket($1::interval, "time") AS interval,
FIRST("value") as "first_value"
FROM "my_table"
WHERE "time" >= $2 AND "time" <= $3
GROUP BY interval
ORDER BY interval DESC
)
SELECT
TO_CHAR(interval, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as interval,
"first_value" as "first_value"
FROM time_buckets;",
}
`;

exports[`TimeBucket build should generate query with interval 1 day 1`] = `
{
"interval": "1 day",
Expand Down Expand Up @@ -189,6 +212,75 @@ FROM time_buckets;",
}
`;

exports[`TimeBucket build should generate query with last metric 1`] = `
{
"params": [
"1 hour",
2025-01-01T00:00:00.000Z,
2025-01-02T00:00:00.000Z,
],
"sql": "WITH time_buckets AS (
SELECT
time_bucket($1::interval, "time") AS interval,
LAST("value") as "last_value"
FROM "my_table"
WHERE "time" >= $2 AND "time" <= $3
GROUP BY interval
ORDER BY interval DESC
)
SELECT
TO_CHAR(interval, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as interval,
"last_value" as "last_value"
FROM time_buckets;",
}
`;

exports[`TimeBucket build should generate query with max metric 1`] = `
{
"params": [
"1 hour",
2025-01-01T00:00:00.000Z,
2025-01-02T00:00:00.000Z,
],
"sql": "WITH time_buckets AS (
SELECT
time_bucket($1::interval, "time") AS interval,
MAX("value") as "max_value"
FROM "my_table"
WHERE "time" >= $2 AND "time" <= $3
GROUP BY interval
ORDER BY interval DESC
)
SELECT
TO_CHAR(interval, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as interval,
"max_value" as "max_value"
FROM time_buckets;",
}
`;

exports[`TimeBucket build should generate query with min metric 1`] = `
{
"params": [
"1 hour",
2025-01-01T00:00:00.000Z,
2025-01-02T00:00:00.000Z,
],
"sql": "WITH time_buckets AS (
SELECT
time_bucket($1::interval, "time") AS interval,
MIN("value") as "min_value"
FROM "my_table"
WHERE "time" >= $2 AND "time" <= $3
GROUP BY interval
ORDER BY interval DESC
)
SELECT
TO_CHAR(interval, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as interval,
"min_value" as "min_value"
FROM time_buckets;",
}
`;

exports[`TimeBucket build should generate query with multiple metrics 1`] = `
{
"params": [
Expand Down
80 changes: 80 additions & 0 deletions packages/core/tests/timebucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,86 @@ describe('TimeBucket', () => {
expect({ sql, params }).toMatchSnapshot();
});

it('should generate query with min metric', () => {
const hypertable = TimescaleDB.createHypertable('my_table', defaultOptions);
const { sql, params } = hypertable
.timeBucket({
interval: '1 hour',
metrics: [
{
type: 'min',
column: 'value',
alias: 'min_value',
},
],
})
.build({
range: timeRange,
});

expect({ sql, params }).toMatchSnapshot();
});

it('should generate query with max metric', () => {
const hypertable = TimescaleDB.createHypertable('my_table', defaultOptions);
const { sql, params } = hypertable
.timeBucket({
interval: '1 hour',
metrics: [
{
type: 'max',
column: 'value',
alias: 'max_value',
},
],
})
.build({
range: timeRange,
});

expect({ sql, params }).toMatchSnapshot();
});

it('should generate query with first metric', () => {
const hypertable = TimescaleDB.createHypertable('my_table', defaultOptions);
const { sql, params } = hypertable
.timeBucket({
interval: '1 hour',
metrics: [
{
type: 'first',
column: 'value',
alias: 'first_value',
},
],
})
.build({
range: timeRange,
});

expect({ sql, params }).toMatchSnapshot();
});

it('should generate query with last metric', () => {
const hypertable = TimescaleDB.createHypertable('my_table', defaultOptions);
const { sql, params } = hypertable
.timeBucket({
interval: '1 hour',
metrics: [
{
type: 'last',
column: 'value',
alias: 'last_value',
},
],
})
.build({
range: timeRange,
});

expect({ sql, params }).toMatchSnapshot();
});

it('should generate query with multiple metrics', () => {
const hypertable = TimescaleDB.createHypertable('my_table', defaultOptions);
const { sql, params } = hypertable
Expand Down

0 comments on commit 0768153

Please sign in to comment.