@gscdump/cloudflare 0.35.2 → 0.35.3

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.
@@ -25,7 +25,17 @@ function metricExpr(metric) {
25
25
  case "clicks": return "SUM(clicks) AS clicks";
26
26
  case "impressions": return "SUM(impressions) AS impressions";
27
27
  case "ctr": return "SUM(clicks) / NULLIF(SUM(impressions), 0) AS ctr";
28
- case "position": return "SUM(sum_position) / NULLIF(SUM(impressions), 0) AS position";
28
+ case "position": return "SUM(sum_position) / NULLIF(SUM(impressions), 0) + 1 AS position";
29
+ default: throw new Error(`[archetype-sql] unknown metric: ${JSON.stringify(metric)}`);
30
+ }
31
+ }
32
+ function metricAlias(metric) {
33
+ switch (metric) {
34
+ case "clicks":
35
+ case "impressions":
36
+ case "ctr":
37
+ case "position": return metric;
38
+ default: throw new Error(`[archetype-sql] unknown order metric: ${JSON.stringify(metric)}`);
29
39
  }
30
40
  }
31
41
  const DEVICE_SUFFIXES = [
@@ -38,7 +48,8 @@ function metricExprForSource(metric, source) {
38
48
  case "clicks": return `SUM(${source.clicks}) AS clicks`;
39
49
  case "impressions": return `SUM(${source.impressions}) AS impressions`;
40
50
  case "ctr": return `SUM(${source.clicks}) / NULLIF(SUM(${source.impressions}), 0) AS ctr`;
41
- case "position": return `SUM(${source.sumPosition}) / NULLIF(SUM(${source.impressions}), 0) AS position`;
51
+ case "position": return `SUM(${source.sumPosition}) / NULLIF(SUM(${source.impressions}), 0) + 1 AS position`;
52
+ default: throw new Error(`[archetype-sql] unknown metric: ${JSON.stringify(metric)}`);
42
53
  }
43
54
  }
44
55
  function deviceSource(suffix) {
@@ -170,7 +181,8 @@ function buildEntityDailySparkline(q, pruned, mode) {
170
181
  function buildTopNBreakdown(q, pruned, mode) {
171
182
  const table = tableForDimensions([q.dimension]);
172
183
  const w = partitionWhere(q, pruned, mode);
173
- const order = `${q.orderBy.metric} ${q.orderBy.dir.toUpperCase()}`;
184
+ if (!q.orderBy || !q.orderBy.metric || !q.orderBy.dir) throw new Error(`[archetype-sql] top-n-breakdown requires orderBy.{metric,dir}, got: ${JSON.stringify(q.orderBy)}`);
185
+ const order = `${metricAlias(q.orderBy.metric)} ${q.orderBy.dir.toUpperCase()}`;
174
186
  const limit = `LIMIT ${Math.max(0, Math.floor(q.limit))}`;
175
187
  const offset = q.offset && q.offset > 0 ? ` OFFSET ${Math.floor(q.offset)}` : "";
176
188
  const metricList = q.metrics.includes(q.orderBy.metric) ? q.metrics : [...q.metrics, q.orderBy.metric];
@@ -297,7 +309,7 @@ function buildTwoDimensionDetail(q, pruned, mode) {
297
309
  clause += facet.sql;
298
310
  params.push(...facet.params);
299
311
  let sql = `SELECT url, query, ${(q.orderBy && !q.metrics.includes(q.orderBy.metric) ? [...q.metrics, q.orderBy.metric] : q.metrics).map(metricExpr).join(", ")} FROM ${TABLE_PLACEHOLDER} WHERE ${clause} GROUP BY url, query`;
300
- if (q.orderBy) sql += ` ORDER BY ${q.orderBy.metric} ${q.orderBy.dir.toUpperCase()}`;
312
+ if (q.orderBy) sql += ` ORDER BY ${metricAlias(q.orderBy.metric)} ${q.orderBy.dir.toUpperCase()}`;
301
313
  if (q.limit && q.limit > 0) sql += ` LIMIT ${Math.floor(q.limit)}`;
302
314
  return {
303
315
  table: "page_queries",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gscdump/cloudflare",
3
3
  "type": "module",
4
- "version": "0.35.2",
4
+ "version": "0.35.3",
5
5
  "description": "Cloudflare-Workers-flavored helpers for the gscdump analytics stack: AnalyticsEnv binding contract, R2 SigV4 presigner, size-hint HMAC, DuckDB Workers shims, engine factory.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -46,10 +46,10 @@
46
46
  "dependencies": {
47
47
  "@uwdata/flechette": "^2.5.0",
48
48
  "aws4fetch": "^1.0.20",
49
- "@gscdump/engine": "0.35.2",
50
- "@gscdump/engine-sqlite": "0.35.2",
51
- "gscdump": "0.35.2",
52
- "@gscdump/contracts": "0.35.2"
49
+ "@gscdump/contracts": "0.35.3",
50
+ "@gscdump/engine-sqlite": "0.35.3",
51
+ "gscdump": "0.35.3",
52
+ "@gscdump/engine": "0.35.3"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@cloudflare/vitest-pool-workers": "^0.16.20",