@protobase/query 0.121.13 → 0.122.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rollup-measure.js +11 -7
- package/package.json +2 -2
package/dist/rollup-measure.js
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
import { QueryError } from "./fields/query-error.js";
|
|
2
2
|
import { columnRef, findField } from "./columns.js";
|
|
3
3
|
import { sql } from "kysely";
|
|
4
|
-
import { keptMeasure } from "@protobase/schema/internal";
|
|
4
|
+
import { keptMeasure, measureFieldName } from "@protobase/schema/internal";
|
|
5
5
|
//#region src/rollup-measure.ts
|
|
6
6
|
const refused = (message) => new QueryError("invalid_option", message);
|
|
7
7
|
/**
|
|
8
8
|
* A measure over a time rollup's stored columns, so it gives what the same measure over the source's records gives:
|
|
9
|
-
* a sum adds up `<field>_sum` (or `events`, the
|
|
10
|
-
* maximum takes the greatest `<field>_max`. Throws on what the buckets cannot give, such as an average of
|
|
9
|
+
* a sum adds up `<field>_sum` (or `events` or `<field>_count`, the counts), an average divides that sum by the records with
|
|
10
|
+
* a value, and a maximum takes the greatest `<field>_max`. Throws on what the buckets cannot give, such as an average of
|
|
11
|
+
* greatest values.
|
|
11
12
|
*/
|
|
12
13
|
const rollupMeasureSql = (model, buckets, measure, column) => {
|
|
13
|
-
const events = columnRef(findField(model, "events"));
|
|
14
14
|
if (measure.field === "events") {
|
|
15
15
|
if (measure.kind === "sum") return sql`sum(${column})`;
|
|
16
16
|
throw refused(`"events" counts each bucket's records, so a time rollup gives only their sum, the count`);
|
|
17
17
|
}
|
|
18
18
|
const kept = keptMeasure(buckets, measure.field);
|
|
19
|
-
if (!kept) throw refused(`"${measure.field}" is no measure of the time rollup "${model.name}": give events, or a <field>_sum or <field>
|
|
19
|
+
if (!kept) throw refused(`"${measure.field}" is no measure of the time rollup "${model.name}": give events, or a <field>_sum, <field>_max or <field>_count field`);
|
|
20
|
+
if (kept.measure === "count") {
|
|
21
|
+
if (measure.kind === "sum") return sql`sum(${column})`;
|
|
22
|
+
throw refused(`"${measure.field}" counts each bucket's records with a value, so a time rollup gives only their sum, the count`);
|
|
23
|
+
}
|
|
20
24
|
if (kept.measure === "max") {
|
|
21
25
|
if (measure.kind === "maximum") return sql`max(${column})`;
|
|
22
26
|
throw refused(`"${measure.field}" keeps each bucket's greatest value, so a time rollup gives only its maximum`);
|
|
23
27
|
}
|
|
24
28
|
if (measure.kind === "sum") return sql`sum(${column})`;
|
|
25
29
|
if (measure.kind === "maximum") throw refused(`"${measure.field}" keeps each bucket's sum, so a time rollup gives its sum or average, not a maximum`);
|
|
26
|
-
|
|
27
|
-
return sql`(sum(${column}) / sum(${
|
|
30
|
+
const counted = columnRef(findField(model, measureFieldName(kept.field, "count")));
|
|
31
|
+
return sql`(sum(${column}) / nullif(sum(${counted}), 0))`;
|
|
28
32
|
};
|
|
29
33
|
/** How many records a group holds: its rows, or for a time rollup the records its buckets count. */
|
|
30
34
|
const countSql = (model) => model.buckets ? sql`sum(${columnRef(findField(model, "events"))})` : sql`count(*)`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.122.0",
|
|
4
4
|
"description": "Kysely list, filter, count and aggregate queries over Protobase resources.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@protobase/schema": "0.
|
|
32
|
+
"@protobase/schema": "0.122.0",
|
|
33
33
|
"kysely": "^0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|