runlight 0.5.6 → 0.6.1

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.
@@ -53,6 +53,11 @@ module Runlight
53
53
  @checked_all = false
54
54
  end
55
55
 
56
+ # Whether upkeep adds up each day into rollups for reports to read, which imported daily totals need.
57
+ def keeps_rollups?
58
+ true
59
+ end
60
+
56
61
  # The store's tables as named in the database, for an app removing Runlight: each one by name, since
57
62
  # another app's tables may well start with the same letters.
58
63
  def tables
@@ -179,7 +184,7 @@ module Runlight
179
184
  end
180
185
  end
181
186
  transaction do |store|
182
- %w[rl_events rl_sessions rl_links rl_link_domains rl_shares rl_goals rl_funnels rl_reports rl_tokens rl_rollups rl_rollup_days rl_sites].each do |table|
187
+ %w[rl_events rl_sessions rl_links rl_link_domains rl_shares rl_goals rl_funnels rl_reports rl_tokens rl_rollups rl_rollup_days rl_import_days rl_imports rl_sites].each do |table|
183
188
  store.db.run("DELETE FROM #{table} WHERE #{table == "rl_sites" ? "id" : "site"} = ?", [id])
184
189
  end
185
190
  end
@@ -214,6 +219,13 @@ module Runlight
214
219
  end
215
220
  # A day that lost any of its visits is built again later, from what is left.
216
221
  clear_rollups(site, { "before" => ts })
222
+ # Imported days go as visits do, and an import left with none goes too.
223
+ @db.all("SELECT day, import_id FROM rl_import_days WHERE site = ? AND start_at < ?", [site, ts]).each do |row|
224
+ transaction do |store|
225
+ %w[rl_rollups rl_rollup_days rl_import_days].each { |table| store.db.run("DELETE FROM #{table} WHERE site = ? AND day = ?", [site, Sql.string(row["day"])]) }
226
+ end
227
+ end
228
+ @db.run("DELETE FROM rl_imports WHERE site = ? AND id NOT IN (SELECT import_id FROM rl_import_days WHERE site = ?)", [site, site])
217
229
  end
218
230
 
219
231
  # Deletes a site's events from `from` on whose visit no longer exists, a day at a time.
@@ -229,6 +241,115 @@ module Runlight
229
241
  end
230
242
  end
231
243
 
244
+ # Imported daily totals
245
+
246
+ def create_import(row)
247
+ @db.run(
248
+ "INSERT INTO rl_imports (id, site, source, state, created_at, kept_at) VALUES (?, ?, ?, ?, ?, ?)",
249
+ [row["id"], row["site"], row["source"], row["state"], row["createdAt"], row["keptAt"]],
250
+ )
251
+ end
252
+
253
+ # A site's imports, newest first.
254
+ def imports(site)
255
+ @db.all("SELECT * FROM rl_imports WHERE site = ? ORDER BY created_at DESC, id DESC", [site]).map { |r| import_row(r) }
256
+ end
257
+
258
+ def import_by_id(site, id)
259
+ row = @db.all("SELECT * FROM rl_imports WHERE site = ? AND id = ?", [site, id])[0]
260
+ row.nil? ? nil : import_row(row)
261
+ end
262
+
263
+ # The days of a site marked built, from its visits or a kept import.
264
+ def built_days(site)
265
+ @db.all("SELECT day FROM rl_rollup_days WHERE site = ?", [site]).to_h { |r| [Sql.string(r["day"]), true] }
266
+ end
267
+
268
+ # The days each import of a site holds, so a new one takes none of them: day => import id.
269
+ def imported_days(site)
270
+ @db.all("SELECT day, import_id FROM rl_import_days WHERE site = ?", [site]).to_h { |r| [Sql.string(r["day"]), Sql.string(r["import_id"])] }
271
+ end
272
+
273
+ # Writes the days of an import, each whole: what the import had for a day before is replaced, so sending a
274
+ # day again does no harm. The rows wait without a built mark until the import is kept.
275
+ # days: Array<{"day", "start", "end"}>; rows: ImportedRollup Hashes.
276
+ def put_import_days(site, id, days, rows)
277
+ transaction do |store|
278
+ db = store.db
279
+ days.each do |d|
280
+ db.run("DELETE FROM rl_rollups WHERE site = ? AND day = ?", [site, d["day"]])
281
+ db.run("DELETE FROM rl_import_days WHERE site = ? AND day = ?", [site, d["day"]])
282
+ db.run("INSERT INTO rl_import_days (site, day, import_id, start_at, end_at) VALUES (?, ?, ?, ?, ?)", [site, d["day"], id, d["start"], d["end"]])
283
+ end
284
+ cols = "(site, day, dim, value, visitors, visits, pageviews, bounced, duration, engaged, views, scroll_sum, scroll_n, events)"
285
+ rows.each_slice(Sql::MAX_PARAMS / 14) do |piece|
286
+ db.run(
287
+ "INSERT INTO rl_rollups #{cols} VALUES #{(["(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"] * piece.length).join(", ")}",
288
+ piece.flat_map do |r|
289
+ [site, r["day"], r["dim"], r["value"], r["visitors"], r["visits"], r["pageviews"], r["bounced"], r["duration"],
290
+ r["engaged"], r["views"], r["scrollSum"], r["scrollN"], r["events"]]
291
+ end,
292
+ )
293
+ end
294
+ end
295
+ end
296
+
297
+ # Marks an import's days built, so every report reads them from now on.
298
+ def keep_import(site, id, now)
299
+ transaction do |store|
300
+ db = store.db
301
+ db.run("DELETE FROM rl_rollup_days WHERE site = ? AND day IN (SELECT day FROM rl_import_days WHERE site = ? AND import_id = ?)", [site, site, id])
302
+ db.run(
303
+ "INSERT INTO rl_rollup_days (site, day, start_at, end_at) SELECT site, day, start_at, end_at FROM rl_import_days WHERE site = ? AND import_id = ?",
304
+ [site, id],
305
+ )
306
+ db.run("UPDATE rl_imports SET state = 'kept', kept_at = ? WHERE site = ? AND id = ?", [now, site, id])
307
+ end
308
+ end
309
+
310
+ # Takes an import away, in review or kept: its rows, its built marks, its days, and itself.
311
+ def delete_import(site, id)
312
+ days = @db.all("SELECT day FROM rl_import_days WHERE site = ? AND import_id = ?", [site, id]).map { |r| Sql.string(r["day"]) }
313
+ # The days stop counting as built first, so a stop part way never leaves a day marked built without its rows.
314
+ @db.run("DELETE FROM rl_rollup_days WHERE site = ? AND day IN (SELECT day FROM rl_import_days WHERE site = ? AND import_id = ?)", [site, site, id])
315
+ days.each_slice(20) do |piece|
316
+ transaction do |store|
317
+ piece.each do |day|
318
+ store.db.run("DELETE FROM rl_rollups WHERE site = ? AND day = ?", [site, day])
319
+ store.db.run("DELETE FROM rl_import_days WHERE site = ? AND day = ?", [site, day])
320
+ end
321
+ end
322
+ end
323
+ @db.run("DELETE FROM rl_imports WHERE site = ? AND id = ?", [site, id])
324
+ end
325
+
326
+ # An import's first and last day, how many days, and its visitors, visits, and pageviews, by month too.
327
+ def import_totals(site, id)
328
+ rows = @db.all(
329
+ "SELECT d.day AS day, r.visitors AS visitors, r.visits AS visits, r.pageviews AS pageviews
330
+ FROM rl_import_days d LEFT JOIN rl_rollups r ON r.site = d.site AND r.day = d.day AND r.dim = '' AND r.value = ''
331
+ WHERE d.site = ? AND d.import_id = ? ORDER BY d.day",
332
+ [site, id],
333
+ )
334
+ months = {}
335
+ total = { "visitors" => 0, "visits" => 0, "pageviews" => 0 }
336
+ rows.each do |row|
337
+ month = Sql.string(row["day"])[0, 7]
338
+ m = months[month] ||= { "month" => month, "visitors" => 0, "visits" => 0, "pageviews" => 0 }
339
+ total.each_key do |k|
340
+ m[k] += Sql.num(row[k])
341
+ total[k] += Sql.num(row[k])
342
+ end
343
+ end
344
+ {
345
+ "from" => rows.empty? ? "" : Sql.string(rows[0]["day"]),
346
+ "to" => rows.empty? ? "" : Sql.string(rows[-1]["day"]),
347
+ "days" => rows.length,
348
+ **total,
349
+ "months" => months.values,
350
+ }
351
+ end
352
+
232
353
  # Daily rollups
233
354
 
234
355
  # Adds up one local day of a site: totals, each visit dimension, and pages.
@@ -304,7 +425,8 @@ module Runlight
304
425
 
305
426
  # The days of a site already built.
306
427
  def rollup_days(site)
307
- @db.all("SELECT day FROM rl_rollup_days WHERE site = ?", [site]).map { |r| Sql.string(r["day"]) }.uniq
428
+ # An imported day, kept or in review, is never built from visits, which would replace its totals.
429
+ @db.all("SELECT day FROM rl_rollup_days WHERE site = ? UNION SELECT day FROM rl_import_days WHERE site = ?", [site, site]).map { |r| Sql.string(r["day"]) }.uniq
308
430
  end
309
431
 
310
432
  # Forgets built days, all of a site's or those touching a stretch of time, so they are built again.
@@ -319,6 +441,9 @@ module Runlight
319
441
  where << " AND start_at < ? AND end_at > ?"
320
442
  params.push(range["to"], range["from"])
321
443
  end
444
+ # Imported days have no visits to be built again from, so they stay; retention deletes them (drop_before).
445
+ where << " AND day NOT IN (SELECT day FROM rl_import_days WHERE site = ?)"
446
+ params << site
322
447
  days = @db.all("SELECT day FROM rl_rollup_days WHERE #{where}", params).map { |r| Sql.string(r["day"]) }
323
448
  # The days stop counting as built first, so if this stops part way, no day is left marked built
324
449
  # without its rows. Rows of a day not built are never read, and building it replaces them. Another
@@ -845,7 +970,18 @@ module Runlight
845
970
  AND day IN (SELECT day FROM rl_rollups WHERE site = ? AND dim = 'event' AND value = ?)",
846
971
  [g["site"], before["name"], g["site"], g["name"]],
847
972
  ).map { |r| Sql.string(r["day"]) }
973
+ imported = db.all("SELECT day FROM rl_import_days WHERE site = ?", [g["site"]]).to_h { |r| [Sql.string(r["day"]), true] }
848
974
  both.each do |day|
975
+ if imported.key?(day)
976
+ # An imported day cannot be built again, so the old name's counts join the new name's.
977
+ old = db.all("SELECT visitors, events FROM rl_rollups WHERE site = ? AND day = ? AND dim = 'event' AND value = ?", [g["site"], day, before["name"]])[0]
978
+ db.run(
979
+ "UPDATE rl_rollups SET visitors = visitors + ?, events = events + ? WHERE site = ? AND day = ? AND dim = 'event' AND value = ?",
980
+ [Sql.num(old&.fetch("visitors", nil)), Sql.num(old&.fetch("events", nil)), g["site"], day, g["name"]],
981
+ )
982
+ db.run("DELETE FROM rl_rollups WHERE site = ? AND day = ? AND dim = 'event' AND value = ?", [g["site"], day, before["name"]])
983
+ next
984
+ end
849
985
  db.run("DELETE FROM rl_rollup_days WHERE site = ? AND day = ?", [g["site"], day])
850
986
  db.run("DELETE FROM rl_rollups WHERE site = ? AND day = ?", [g["site"], day])
851
987
  end
@@ -1481,16 +1617,22 @@ module Runlight
1481
1617
  "SELECT COUNT(DISTINCT visitor) AS n FROM rl_events WHERE site = ? AND ts >= ? AND kind IN ('pageview', 'event', 'engagement')",
1482
1618
  [site, since],
1483
1619
  )[0] || {}
1620
+ # Each visitor on the site is where their latest row says: a pageview, an event, or an engagement
1621
+ # ping from a tab still open. So the pages, sources, and countries share out the same visitors as
1622
+ # the count, rather than leaving out anyone whose pageview is older than the five minutes. Through
1623
+ # the kind and time index: SQLite otherwise reads the site's whole history by path.
1624
+ latest = "(SELECT e.visitor AS visitor, e.path AS path, e.session AS session,
1625
+ ROW_NUMBER() OVER (PARTITION BY e.visitor ORDER BY e.ts DESC, e.id DESC) AS n
1626
+ FROM #{Sql.event_kind_table(@db.dialect)}
1627
+ WHERE e.site = ? AND e.ts >= ? AND e.kind IN ('pageview', 'event', 'engagement') AND e.visitor IS NOT NULL) l"
1484
1628
  pages = @db.all(
1485
- # Through the kind and time index: SQLite otherwise reads the site's whole history by path to group it.
1486
- "SELECT e.path AS value, COUNT(DISTINCT e.visitor) AS visitors FROM #{Sql.event_kind_table(@db.dialect)}
1487
- WHERE e.site = ? AND e.ts >= ? AND e.kind = 'pageview' GROUP BY e.path ORDER BY visitors DESC, e.path#{text_order} LIMIT 10",
1629
+ "SELECT l.path AS value, COUNT(*) AS visitors FROM #{latest}
1630
+ WHERE l.n = 1 GROUP BY l.path ORDER BY visitors DESC, l.path#{text_order} LIMIT 10",
1488
1631
  [site, since],
1489
1632
  )
1490
1633
  sources = @db.all(
1491
- "SELECT s.source AS value, COUNT(DISTINCT e.visitor) AS visitors FROM rl_events e JOIN rl_sessions s ON s.id = e.session
1492
- WHERE e.site = ? AND e.ts >= ? AND e.kind IN ('pageview', 'event')
1493
- GROUP BY s.source ORDER BY visitors DESC, s.source#{text_order} LIMIT 10",
1634
+ "SELECT s.source AS value, COUNT(*) AS visitors FROM #{latest} JOIN rl_sessions s ON s.id = l.session
1635
+ WHERE l.n = 1 GROUP BY s.source ORDER BY visitors DESC, s.source#{text_order} LIMIT 10",
1494
1636
  [site, since],
1495
1637
  )
1496
1638
  start = now.fdiv(60_000).floor * 60_000 - 29 * 60_000
@@ -1505,9 +1647,8 @@ module Runlight
1505
1647
  minutes[index] += Sql.num(row["n"]) if index >= 0 && index < 30
1506
1648
  end
1507
1649
  countries = @db.all(
1508
- "SELECT s.country AS value, COUNT(DISTINCT e.visitor) AS visitors FROM rl_events e JOIN rl_sessions s ON s.id = e.session
1509
- WHERE e.site = ? AND e.ts >= ? AND e.kind IN ('pageview', 'event') AND s.country <> ''
1510
- GROUP BY s.country ORDER BY visitors DESC, s.country#{text_order} LIMIT 10",
1650
+ "SELECT s.country AS value, COUNT(*) AS visitors FROM #{latest} JOIN rl_sessions s ON s.id = l.session
1651
+ WHERE l.n = 1 AND s.country <> '' GROUP BY s.country ORDER BY visitors DESC, s.country#{text_order} LIMIT 10",
1511
1652
  [site, since],
1512
1653
  )
1513
1654
  recent = @db.all(
@@ -1799,6 +1940,17 @@ module Runlight
1799
1940
  stats_of(add.call("visitors"), add.call("visits"), add.call("pageviews"), add.call("bounced"), add.call("duration"))
1800
1941
  end
1801
1942
 
1943
+ def import_row(r)
1944
+ {
1945
+ "id" => Sql.string(r["id"]),
1946
+ "site" => Sql.string(r["site"]),
1947
+ "source" => Sql.string(r["source"]),
1948
+ "state" => Sql.string(r["state"]) == "kept" ? "kept" : "review",
1949
+ "createdAt" => Sql.num(r["created_at"]),
1950
+ "keptAt" => r["kept_at"].nil? ? nil : Sql.num(r["kept_at"]),
1951
+ }
1952
+ end
1953
+
1802
1954
  def share_row(r)
1803
1955
  { "id" => Sql.string(r["id"]), "site" => Sql.string(r["site"]), "name" => Sql.string(r["name"]), "createdAt" => Sql.number(r["created_at"]) }
1804
1956
  end
@@ -4,7 +4,7 @@ require "json"
4
4
 
5
5
  module Runlight
6
6
  # The gem's own version, which the release keeps equal to the npm package's.
7
- GEM_VERSION = "0.5.6"
7
+ GEM_VERSION = "0.6.1"
8
8
 
9
9
  # The SDK's version and the HTTP API's, read from assets/build.json, which
10
10
  # scripts/ruby-assets.mts writes from the TypeScript SDK, so the two always
data/lib/runlight.rb CHANGED
@@ -30,6 +30,8 @@ module Runlight
30
30
  autoload :Goals, "runlight/goals"
31
31
  autoload :Hashing, "runlight/hashing"
32
32
  autoload :Icon, "runlight/icon"
33
+ autoload :ImportRowsError, "runlight/import_rows_error"
34
+ autoload :Imports, "runlight/imports"
33
35
  autoload :Intl, "runlight/intl"
34
36
  autoload :Journeys, "runlight/journeys"
35
37
  autoload :Js, "runlight/js"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon C. Phillips
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-27 00:00:00.000000000 Z
11
+ date: 2026-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -92,6 +92,7 @@ files:
92
92
  - lib/runlight/http/search_params.rb
93
93
  - lib/runlight/http/url.rb
94
94
  - lib/runlight/icon.rb
95
+ - lib/runlight/import_rows_error.rb
95
96
  - lib/runlight/importers/bitly.rb
96
97
  - lib/runlight/importers/client.rb
97
98
  - lib/runlight/importers/csv_visits.rb
@@ -105,6 +106,7 @@ files:
105
106
  - lib/runlight/importers/umami.rb
106
107
  - lib/runlight/importers/visits.rb
107
108
  - lib/runlight/importers/write.rb
109
+ - lib/runlight/imports.rb
108
110
  - lib/runlight/intl.rb
109
111
  - lib/runlight/journeys.rb
110
112
  - lib/runlight/js.rb