create-expo-updates-server 0.1.5 → 0.2.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.
Files changed (141) hide show
  1. package/dist/template/.dockerignore +16 -0
  2. package/dist/template/README.md +51 -660
  3. package/dist/template/app/api/admin/device-targets/route.test.ts +43 -0
  4. package/dist/template/app/api/admin/device-targets/route.ts +91 -0
  5. package/dist/template/app/api/admin/devices/route.test.ts +41 -0
  6. package/dist/template/app/api/admin/devices/route.ts +15 -0
  7. package/dist/template/app/api/admin/updates/[encodedKey]/audience/route.ts +75 -0
  8. package/dist/template/app/api/device-heartbeat/route.ts +2 -0
  9. package/dist/template/app/globals.css +114 -15
  10. package/dist/template/app/layout.tsx +12 -4
  11. package/dist/template/app/login/page.tsx +22 -18
  12. package/dist/template/app/updates/[encodedKey]/page.tsx +112 -88
  13. package/dist/template/app/updates/devices/page.tsx +264 -0
  14. package/dist/template/app/updates/embedded/page.tsx +102 -68
  15. package/dist/template/app/updates/emergency-redirects/page.tsx +144 -111
  16. package/dist/template/app/updates/layout.tsx +6 -1
  17. package/dist/template/app/updates/page.tsx +262 -212
  18. package/dist/template/components/admin/admin-shell.tsx +196 -0
  19. package/dist/template/components/admin/language-switcher.tsx +25 -0
  20. package/dist/template/components/admin/theme-provider.tsx +15 -0
  21. package/dist/template/components/ui/alert.tsx +65 -0
  22. package/dist/template/components/ui/badge.tsx +47 -0
  23. package/dist/template/components/ui/button.tsx +63 -0
  24. package/dist/template/components/ui/card.tsx +91 -0
  25. package/dist/template/components/ui/chart.tsx +373 -0
  26. package/dist/template/components/ui/checkbox.tsx +31 -0
  27. package/dist/template/components/ui/dialog.tsx +158 -0
  28. package/dist/template/components/ui/dropdown-menu.tsx +256 -0
  29. package/dist/template/components/ui/input.tsx +20 -0
  30. package/dist/template/components/ui/label.tsx +23 -0
  31. package/dist/template/components/ui/native-select.tsx +61 -0
  32. package/dist/template/components/ui/popover.tsx +88 -0
  33. package/dist/template/components/ui/scroll-area.tsx +57 -0
  34. package/dist/template/components/ui/select.tsx +189 -0
  35. package/dist/template/components/ui/separator.tsx +27 -0
  36. package/dist/template/components/ui/sheet.tsx +142 -0
  37. package/dist/template/components/ui/sidebar.tsx +736 -0
  38. package/dist/template/components/ui/skeleton.tsx +13 -0
  39. package/dist/template/components/ui/switch.tsx +34 -0
  40. package/dist/template/components/ui/table.tsx +115 -0
  41. package/dist/template/components/ui/tabs.tsx +90 -0
  42. package/dist/template/components/ui/textarea.tsx +17 -0
  43. package/dist/template/components/ui/tooltip.tsx +56 -0
  44. package/dist/template/components.json +22 -0
  45. package/dist/template/docs/admin-ui.md +35 -0
  46. package/dist/template/docs/device-targeting.md +129 -0
  47. package/dist/template/docs/doctor.md +137 -0
  48. package/dist/template/docs/expo-app-setup.md +174 -47
  49. package/dist/template/docs/getting-started.md +105 -146
  50. package/dist/template/docs/getting-started.ru.md +106 -191
  51. package/dist/template/docs/index.md +14 -33
  52. package/dist/template/docs/installation-telemetry.md +233 -0
  53. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +89 -0
  54. package/dist/template/docs/migrations/2026-10-19-device-heartbeat.sql +131 -0
  55. package/dist/template/docs/migrations/2026-10-22-device-analytics.sql +264 -0
  56. package/dist/template/docs/migrations/2026-10-22-device-targeting-activation.sql +108 -0
  57. package/dist/template/docs/migrations/2026-10-22-device-targeting.sql +130 -0
  58. package/dist/template/docs/migrations/2026-10-22-device-version-metadata.sql +154 -0
  59. package/dist/template/docs/migrations/schema.sql +868 -6
  60. package/dist/template/docs/npm-installer.md +15 -30
  61. package/dist/template/docs/operations-guide.md +87 -0
  62. package/dist/template/docs/ota-client-lifecycle.md +263 -0
  63. package/dist/template/docs/ota-update-policy.md +36 -2
  64. package/dist/template/docs/supabase-setup.md +4 -0
  65. package/dist/template/hooks/use-mobile.ts +21 -0
  66. package/dist/template/lib/utils.ts +6 -0
  67. package/dist/template/package-lock.json +2325 -101
  68. package/dist/template/package.json +13 -3
  69. package/dist/template/scripts/configure-expo-app.mjs +11 -15
  70. package/dist/template/scripts/doctor.mjs +51 -0
  71. package/dist/template/scripts/ensure-test-bucket.mjs +21 -0
  72. package/dist/template/scripts/lib/device-identity.test.ts +29 -0
  73. package/dist/template/scripts/lib/doctor-probes.mjs +139 -0
  74. package/dist/template/scripts/lib/doctor.mjs +208 -0
  75. package/dist/template/scripts/lib/doctor.test.mjs +262 -0
  76. package/dist/template/scripts/lib/expo-app-configurator.mjs +14 -9
  77. package/dist/template/scripts/lib/expo-app-configurator.test.ts +3 -2
  78. package/dist/template/scripts/lib/ota-client-configurator.mjs +69 -0
  79. package/dist/template/scripts/lib/ota-client-configurator.test.ts +88 -0
  80. package/dist/template/scripts/lib/ota-client.test.ts +55 -0
  81. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +173 -0
  82. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +123 -0
  83. package/dist/template/scripts/lib/ota-update-controller.test.ts +231 -0
  84. package/dist/template/scripts/tests/device-analytics-migrations.mjs +80 -0
  85. package/dist/template/scripts/tests/device-analytics.mjs +126 -0
  86. package/dist/template/scripts/tests/device-heartbeat.mjs +86 -0
  87. package/dist/template/scripts/tests/device-targeting-activation.mjs +111 -0
  88. package/dist/template/scripts/tests/device-targeting.mjs +39 -0
  89. package/dist/template/scripts/tests/device-version-metadata.mjs +31 -0
  90. package/dist/template/scripts/tests/manifest-identity-concurrency.mjs +106 -0
  91. package/dist/template/scripts/tests/manifest-identity.sql +204 -0
  92. package/dist/template/src/client/admin-types.ts +1 -0
  93. package/dist/template/src/client/devices/analytics.tsx +429 -0
  94. package/dist/template/src/client/devices/device-name.tsx +82 -0
  95. package/dist/template/src/client/devices/registry.tsx +399 -0
  96. package/dist/template/src/client/devices/use-devices-report.ts +108 -0
  97. package/dist/template/src/client/devices/version-adoption-chart.tsx +269 -0
  98. package/dist/template/src/client/devices/version-groups.test.ts +57 -0
  99. package/dist/template/src/client/devices/version-groups.ts +101 -0
  100. package/dist/template/src/client/distribution-control.tsx +135 -81
  101. package/dist/template/src/client/guard-action-combobox.tsx +13 -24
  102. package/dist/template/src/client/i18n/en.json +286 -0
  103. package/dist/template/src/client/i18n/messages.test.ts +42 -0
  104. package/dist/template/src/client/i18n/messages.ts +31 -0
  105. package/dist/template/src/client/i18n/provider.tsx +55 -0
  106. package/dist/template/src/client/i18n/use-formatters.ts +38 -0
  107. package/dist/template/src/client/logout-button.tsx +3 -1
  108. package/dist/template/src/client/update-audience-editor.tsx +285 -0
  109. package/dist/template/src/client/update-policy-editor.tsx +53 -41
  110. package/dist/template/src/server/controllers/manifest.test.ts +250 -7
  111. package/dist/template/src/server/controllers/manifest.ts +39 -31
  112. package/dist/template/src/server/devices/audience.test.ts +48 -0
  113. package/dist/template/src/server/devices/audience.ts +86 -0
  114. package/dist/template/src/server/devices/heartbeat.test.ts +39 -0
  115. package/dist/template/src/server/devices/heartbeat.ts +75 -0
  116. package/dist/template/src/server/devices/report-types.ts +114 -0
  117. package/dist/template/src/server/devices/report.test.ts +29 -0
  118. package/dist/template/src/server/devices/report.ts +27 -0
  119. package/dist/template/src/server/lib/admin-audience-activation.test.ts +94 -0
  120. package/dist/template/src/server/lib/admin-updates.ts +32 -168
  121. package/dist/template/src/server/lib/distribution-control.test.ts +23 -2
  122. package/dist/template/src/server/lib/distribution-control.ts +13 -4
  123. package/dist/template/src/server/lib/supabase.js +10 -10
  124. package/dist/template/src/server/lib/supabase.test.ts +11 -0
  125. package/dist/template/src/server/manifest/manifest-identity.test.ts +30 -0
  126. package/dist/template/src/server/manifest/manifest-identity.ts +24 -0
  127. package/dist/template/src/server/manifest/manifest-repository.ts +31 -102
  128. package/dist/template/src/server/manifest/manifest-response.ts +1 -1
  129. package/dist/template/templates/expo-app/ota-client/README.md +59 -0
  130. package/dist/template/templates/expo-app/ota-client/controller.js +60 -0
  131. package/dist/template/templates/expo-app/ota-client/device-identity.js +26 -0
  132. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +52 -0
  133. package/dist/template/templates/expo-app/ota-client/index.d.ts +47 -0
  134. package/dist/template/templates/expo-app/ota-client/index.js +3 -0
  135. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.js +48 -0
  136. package/dist/template/templates/expo-app/ota-client/model/update-controller.js +182 -0
  137. package/dist/template/templates/expo-app/ota-client/runtime.js +112 -0
  138. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.js +56 -0
  139. package/dist/template-manifest.json +582 -77
  140. package/lib/cli.mjs +4 -6
  141. package/package.json +1 -1
@@ -0,0 +1,264 @@
1
+ -- Apply after 2026-10-19-device-heartbeat.sql. Safe to reapply.
2
+ -- Existing last_seen_at and transitions cannot reconstruct daily activity.
3
+ -- Collection starts here; there is intentionally no historical backfill.
4
+ begin;
5
+
6
+ create table if not exists public.ota_device_analytics_metadata (
7
+ singleton boolean primary key default true check (singleton),
8
+ collection_started_at timestamptz not null,
9
+ last_pruned_on date not null
10
+ );
11
+
12
+ create table if not exists public.ota_device_activity_daily (
13
+ activity_date date not null,
14
+ installation_id text not null,
15
+ platform text not null check (platform in ('ios', 'android')),
16
+ channel text not null,
17
+ runtime_version text not null,
18
+ launch_kind text not null check (launch_kind in ('ota', 'embedded', 'unknown')),
19
+ native_version text,
20
+ native_build text,
21
+ reported_update_id uuid,
22
+ resolved_update_id uuid,
23
+ emergency_launch boolean not null,
24
+ first_received_at timestamptz not null,
25
+ last_received_at timestamptz not null,
26
+ primary key (activity_date, installation_id, platform)
27
+ );
28
+ create index if not exists idx_ota_device_activity_installation
29
+ on public.ota_device_activity_daily(installation_id, platform, activity_date);
30
+
31
+ -- Only installations whose first instrumented heartbeat arrives after collection
32
+ -- starts enter retention cohorts. Existing installations still count as active.
33
+ -- Entry dimensions never change when an installation later changes channel/runtime.
34
+ create table if not exists public.ota_device_activity_cohorts (
35
+ installation_id text not null,
36
+ platform text not null check (platform in ('ios', 'android')),
37
+ first_observed_at timestamptz not null,
38
+ channel text not null,
39
+ runtime_version text not null,
40
+ launch_kind text not null check (launch_kind in ('ota', 'embedded', 'unknown')),
41
+ primary key (installation_id, platform)
42
+ );
43
+ create index if not exists idx_ota_device_activity_cohort_date
44
+ on public.ota_device_activity_cohorts(first_observed_at);
45
+
46
+ -- Callable by a scheduler; heartbeat also invokes it. Only the first successful
47
+ -- call each UTC day deletes expired rows. Subsequent calls perform no table scan.
48
+ create or replace function public.ota_prune_device_activity()
49
+ returns void language plpgsql set search_path = public as $$
50
+ declare
51
+ today date := (clock_timestamp() at time zone 'UTC')::date;
52
+ begin
53
+ update public.ota_device_analytics_metadata set last_pruned_on = today
54
+ where singleton and last_pruned_on < today;
55
+ if not found then return; end if;
56
+ delete from public.ota_device_activity_daily where activity_date < today - 89;
57
+ delete from public.ota_device_activity_cohorts
58
+ where first_observed_at < ((today - 89)::timestamp at time zone 'UTC');
59
+ end $$;
60
+
61
+ create or replace function public.ota_capture_device_activity()
62
+ returns trigger language plpgsql set search_path = public as $$
63
+ declare
64
+ previous_sequence bigint := 0;
65
+ receipt_day date := (new.last_seen_at at time zone 'UTC')::date;
66
+ begin
67
+ if tg_op = 'UPDATE' then previous_sequence := old.last_sequence; end if;
68
+ -- FK cleanup, ordinary edits, rejected retries, and registration placeholders
69
+ -- must never manufacture another day of activity.
70
+ if new.last_sequence <= previous_sequence then return new; end if;
71
+ perform public.ota_prune_device_activity();
72
+ if previous_sequence = 0 then
73
+ insert into public.ota_device_activity_cohorts
74
+ (installation_id, platform, first_observed_at, channel, runtime_version, launch_kind)
75
+ values (new.installation_id, new.platform, new.last_seen_at, new.channel, new.runtime_version, new.launch_kind)
76
+ on conflict (installation_id, platform) do nothing;
77
+ end if;
78
+ insert into public.ota_device_activity_daily
79
+ (activity_date, installation_id, platform, channel, runtime_version, launch_kind,
80
+ native_version, native_build, reported_update_id, resolved_update_id, emergency_launch,
81
+ first_received_at, last_received_at)
82
+ values
83
+ (receipt_day, new.installation_id, new.platform, new.channel, new.runtime_version, new.launch_kind,
84
+ new.native_version, new.native_build, new.reported_update_id, new.resolved_update_id, new.emergency_launch,
85
+ new.last_seen_at, new.last_seen_at)
86
+ on conflict (activity_date, installation_id, platform) do update set
87
+ channel = excluded.channel, runtime_version = excluded.runtime_version, launch_kind = excluded.launch_kind,
88
+ native_version = excluded.native_version, native_build = excluded.native_build,
89
+ reported_update_id = excluded.reported_update_id, resolved_update_id = excluded.resolved_update_id,
90
+ emergency_launch = excluded.emergency_launch, last_received_at = excluded.last_received_at;
91
+ return new;
92
+ end $$;
93
+
94
+ drop trigger if exists trg_ota_capture_device_activity on public.ota_device_state;
95
+ create trigger trg_ota_capture_device_activity after insert or update on public.ota_device_state
96
+ for each row execute function public.ota_capture_device_activity();
97
+
98
+ insert into public.ota_device_analytics_metadata(singleton, collection_started_at, last_pruned_on)
99
+ values (true, clock_timestamp(), (clock_timestamp() at time zone 'UTC')::date)
100
+ on conflict (singleton) do nothing;
101
+
102
+ -- Keep the original five-argument report RPC intact for older consumers.
103
+ -- One SQL statement gives every chart and the device page a consistent snapshot.
104
+ create or replace function public.ota_devices_report_v2(
105
+ p_days integer default 7, p_platform text default null, p_channel text default null,
106
+ p_runtime text default null, p_offset integer default 0,
107
+ p_launch_kind text default null, p_search text default null)
108
+ returns table(report jsonb) language sql stable set search_path = public as $$
109
+ with parameters as (
110
+ select (now() at time zone 'UTC')::date as today,
111
+ (now() at time zone 'UTC')::date - (p_days - 1) as first_day,
112
+ now() - make_interval(days => p_days) as window_start,
113
+ m.collection_started_at,
114
+ greatest((m.collection_started_at at time zone 'UTC')::date,
115
+ (now() at time zone 'UTC')::date - 89) as retained_from
116
+ from public.ota_device_analytics_metadata m where singleton
117
+ ), recent as (
118
+ select s.* from public.ota_device_state s, parameters p
119
+ where s.last_sequence > 0 and s.last_seen_at >= p.window_start
120
+ ), filtered as (
121
+ select * from recent
122
+ where (p_platform is null or platform = p_platform)
123
+ and (p_channel is null or channel = p_channel)
124
+ and (p_runtime is null or runtime_version = p_runtime)
125
+ and (p_launch_kind is null or launch_kind = p_launch_kind)
126
+ and (p_search is null or position(lower(p_search) in lower(installation_id)) > 0)
127
+ ), versions as (
128
+ select platform, channel, runtime_version, native_version, native_build, launch_kind,
129
+ coalesce(resolved_update_id, reported_update_id) as update_id,
130
+ max(update_label) as update_label, count(*) as installations
131
+ from filtered group by platform, channel, runtime_version, native_version, native_build,
132
+ launch_kind, coalesce(resolved_update_id, reported_update_id)
133
+ ), page as (
134
+ select * from filtered order by last_seen_at desc, installation_id, platform limit 50 offset p_offset
135
+ ), daily as (
136
+ select d.* from public.ota_device_activity_daily d, parameters p
137
+ where d.activity_date between greatest(p.first_day, p.retained_from) and p.today
138
+ ), filtered_daily as (
139
+ select d.* from daily d
140
+ where (p_platform is null or d.platform = p_platform)
141
+ and (p_channel is null or d.channel = p_channel)
142
+ and (p_runtime is null or d.runtime_version = p_runtime)
143
+ and (p_launch_kind is null or d.launch_kind = p_launch_kind)
144
+ and (p_search is null or position(lower(p_search) in lower(d.installation_id)) > 0)
145
+ ), calendar as (
146
+ select p.first_day + n as date,
147
+ case when p.first_day + n < p.retained_from then 'unavailable'
148
+ when p.first_day + n = p.today
149
+ or ((p.first_day + n)::timestamp at time zone 'UTC') < p.collection_started_at then 'partial'
150
+ else 'complete' end as coverage
151
+ from parameters p cross join generate_series(0, p_days - 1) n
152
+ ), day_counts as (
153
+ select d.activity_date, count(*) as active,
154
+ count(*) filter (where (c.first_observed_at at time zone 'UTC')::date = d.activity_date) as newly_observed
155
+ from filtered_daily d left join public.ota_device_activity_cohorts c
156
+ on c.installation_id = d.installation_id and c.platform = d.platform
157
+ group by d.activity_date
158
+ ), activity as (
159
+ select c.date, c.coverage,
160
+ case when c.coverage <> 'unavailable' then coalesce(d.active, 0) end as active,
161
+ case when c.coverage <> 'unavailable' then coalesce(d.newly_observed, 0) end as newly_observed
162
+ from calendar c left join day_counts d on c.date = d.activity_date
163
+ ), cohort_source as (
164
+ select c.*, (c.first_observed_at at time zone 'UTC')::date as first_day,
165
+ date_trunc('week', c.first_observed_at at time zone 'UTC')::date as week_start
166
+ from public.ota_device_activity_cohorts c, parameters p
167
+ where c.first_observed_at >= p.collection_started_at
168
+ and (c.first_observed_at at time zone 'UTC')::date between p.retained_from and p.today
169
+ ), cohort_members as (
170
+ select * from cohort_source
171
+ where (p_platform is null or platform = p_platform)
172
+ and (p_channel is null or channel = p_channel)
173
+ and (p_runtime is null or runtime_version = p_runtime)
174
+ and (p_launch_kind is null or launch_kind = p_launch_kind)
175
+ and (p_search is null or position(lower(p_search) in lower(installation_id)) > 0)
176
+ ), cohort_sizes as (
177
+ select week_start, count(*) as installations from cohort_members group by week_start
178
+ ), retention_members as (
179
+ select c.week_start, n.day,
180
+ (n.day = 0 or c.first_day + n.day < p.today) as eligible,
181
+ exists (select 1 from public.ota_device_activity_daily d
182
+ where d.installation_id = c.installation_id and d.platform = c.platform
183
+ and d.activity_date = c.first_day + n.day) as returned
184
+ from cohort_members c cross join (values (0), (1), (7), (14), (30)) n(day)
185
+ cross join parameters p
186
+ ), retention_counts as (
187
+ select week_start, day, count(*) filter (where eligible) as eligible,
188
+ count(*) filter (where eligible and returned) as retained
189
+ from retention_members group by week_start, day
190
+ ), retention_rows as (
191
+ select c.week_start, c.installations,
192
+ jsonb_agg(jsonb_build_object('day', r.day, 'eligibleInstallations', r.eligible,
193
+ 'retainedInstallations', case when r.eligible > 0 then r.retained end,
194
+ 'rate', case when r.eligible > 0 then r.retained::numeric / r.eligible end) order by r.day) as cells
195
+ from cohort_sizes c join retention_counts r using (week_start) group by c.week_start, c.installations
196
+ ), adoption as (
197
+ select date_trunc('week', first_seen_at at time zone 'UTC')::date as week_start,
198
+ count(*) as installations,
199
+ count(*) filter (where launch_kind = 'ota') as ota,
200
+ count(*) filter (where launch_kind = 'embedded') as embedded,
201
+ count(*) filter (where launch_kind = 'unknown') as unknown
202
+ from filtered group by date_trunc('week', first_seen_at at time zone 'UTC')::date
203
+ ), option_source as (
204
+ select platform, channel, runtime_version, launch_kind from recent
205
+ union select platform, channel, runtime_version, launch_kind from daily
206
+ union select platform, channel, runtime_version, launch_kind from cohort_source
207
+ )
208
+ select jsonb_build_object(
209
+ 'total', (select count(*) from filtered), 'pageSize', 50, 'offset', p_offset,
210
+ 'devices', coalesce((select jsonb_agg(to_jsonb(page) order by last_seen_at desc, installation_id, platform) from page), '[]'::jsonb),
211
+ 'versions', coalesce((select jsonb_agg(to_jsonb(versions) order by installations desc, platform, channel, runtime_version,
212
+ native_version, native_build, launch_kind, update_id) from versions), '[]'::jsonb),
213
+ 'filters', jsonb_build_object('days', p_days, 'platform', p_platform, 'channel', p_channel,
214
+ 'runtime', p_runtime, 'launchKind', p_launch_kind, 'search', p_search),
215
+ 'options', jsonb_build_object(
216
+ 'platforms', coalesce((select jsonb_agg(v order by v) from (select distinct platform as v from option_source) t), '[]'::jsonb),
217
+ 'channels', coalesce((select jsonb_agg(v order by v) from (select distinct channel as v from option_source) t), '[]'::jsonb),
218
+ 'runtimes', coalesce((select jsonb_agg(v order by v) from (select distinct runtime_version as v from option_source) t), '[]'::jsonb),
219
+ 'launchKinds', coalesce((select jsonb_agg(v order by v) from (select distinct launch_kind as v from option_source) t), '[]'::jsonb)),
220
+ 'summary', (select jsonb_build_object('activeInstallations', count(*),
221
+ 'newInstallations', count(*) filter (where first_seen_at >= (select window_start from parameters)),
222
+ 'otaInstallations', count(*) filter (where launch_kind = 'ota'),
223
+ 'embeddedInstallations', count(*) filter (where launch_kind = 'embedded'),
224
+ 'unknownInstallations', count(*) filter (where launch_kind = 'unknown'),
225
+ 'emergencyInstallations', count(*) filter (where emergency_launch),
226
+ 'reportedVersions', (select count(*) from versions)) from filtered),
227
+ 'activity', coalesce((select jsonb_agg(jsonb_build_object('date', date, 'activeInstallations', active,
228
+ 'newInstallations', newly_observed, 'coverage', coverage) order by date) from activity), '[]'::jsonb),
229
+ 'adoptionCohorts', coalesce((select jsonb_agg(jsonb_build_object('weekStart', week_start, 'installations', installations,
230
+ 'otaInstallations', ota, 'embeddedInstallations', embedded, 'unknownInstallations', unknown) order by week_start) from adoption), '[]'::jsonb),
231
+ 'cohorts', coalesce((select jsonb_agg(jsonb_build_object('weekStart', week_start, 'installations', installations,
232
+ 'cells', cells) order by week_start) from retention_rows), '[]'::jsonb),
233
+ 'coverage', (select jsonb_build_object('collectionStartedAt', collection_started_at, 'retainedFrom', retained_from,
234
+ 'timezone', 'UTC', 'retentionDays', 90, 'cohortWindowDays', 90, 'activityBasis', 'accepted-heartbeat-receipt',
235
+ 'cohortBasis', 'first-recorded-heartbeat', 'cohortFilters', 'entry-state', 'todayIsPartial', true,
236
+ 'generatedAt', now(), 'windowStartedAt', window_start) from parameters)
237
+ );
238
+ $$;
239
+
240
+ alter table public.ota_device_analytics_metadata enable row level security;
241
+ alter table public.ota_device_activity_daily enable row level security;
242
+ alter table public.ota_device_activity_cohorts enable row level security;
243
+ revoke all on public.ota_device_analytics_metadata, public.ota_device_activity_daily, public.ota_device_activity_cohorts from public;
244
+ revoke all on function public.ota_prune_device_activity() from public;
245
+ revoke all on function public.ota_capture_device_activity() from public;
246
+ revoke all on function public.ota_devices_report_v2(integer,text,text,text,integer,text,text) from public;
247
+ do $$ declare role_name text; begin
248
+ foreach role_name in array array['anon', 'authenticated'] loop
249
+ if exists(select 1 from pg_roles where rolname = role_name) then
250
+ execute format('revoke all on public.ota_device_analytics_metadata, public.ota_device_activity_daily, public.ota_device_activity_cohorts from %I', role_name);
251
+ execute format('revoke all on function public.ota_prune_device_activity() from %I', role_name);
252
+ execute format('revoke all on function public.ota_capture_device_activity() from %I', role_name);
253
+ execute format('revoke all on function public.ota_devices_report_v2(integer,text,text,text,integer,text,text) from %I', role_name);
254
+ end if;
255
+ end loop;
256
+ if exists(select 1 from pg_roles where rolname = 'service_role') then
257
+ grant select, update on public.ota_device_analytics_metadata to service_role;
258
+ grant select, insert, update, delete on public.ota_device_activity_daily, public.ota_device_activity_cohorts to service_role;
259
+ grant execute on function public.ota_prune_device_activity() to service_role;
260
+ grant execute on function public.ota_capture_device_activity() to service_role;
261
+ grant execute on function public.ota_devices_report_v2(integer,text,text,text,integer,text,text) to service_role;
262
+ end if;
263
+ end $$;
264
+ commit;
@@ -0,0 +1,108 @@
1
+ -- Apply after 2026-10-22-device-targeting.sql. Repeatable; no release backfill.
2
+ begin;
3
+
4
+ -- JS OTA forcing is distinct from asking for a new binary from the store.
5
+ insert into public.ota_guard_actions (action_key)
6
+ values ('ota-force-update')
7
+ on conflict (action_key) do nothing;
8
+
9
+ -- Audience changes and activations take the existing distribution advisory lock
10
+ -- before any row lock. Evaluate NEW.audience_mode in the activation statement,
11
+ -- rather than trusting an earlier application read followed by separate writes.
12
+ create or replace function public.ota_activate_audience()
13
+ returns trigger language plpgsql security definer set search_path = public, pg_temp as $$
14
+ begin
15
+ if new.is_active and new.audience_mode = 'all' then
16
+ perform public.assert_ota_distribution_mutation_allowed('activating a public OTA update');
17
+ update public.ota_updates set is_active = false,
18
+ disabled_at = clock_timestamp(), updated_at = clock_timestamp()
19
+ where runtime_version = new.runtime_version and channel = new.channel
20
+ and platform = new.platform and update_id <> new.update_id and is_active;
21
+ end if;
22
+ return new;
23
+ end $$;
24
+ revoke all on function public.ota_activate_audience() from public;
25
+ drop trigger if exists trg_ota_updates_activate_audience on public.ota_updates;
26
+ create trigger trg_ota_updates_activate_audience
27
+ before insert or update of is_active on public.ota_updates
28
+ for each row execute function public.ota_activate_audience();
29
+
30
+ create or replace function public.guard_ota_channel_distribution()
31
+ returns trigger
32
+ language plpgsql
33
+ security definer
34
+ set search_path = public, pg_temp
35
+ as $$
36
+ declare
37
+ changes_distribution_state boolean;
38
+ latest_active public.ota_updates%rowtype;
39
+ surviving_rollback uuid;
40
+ begin
41
+ if tg_op = 'INSERT' then
42
+ -- Empty channel rows do not distribute an update.
43
+ changes_distribution_state :=
44
+ new.latest_update_id is not null
45
+ or new.active_update_id is not null;
46
+ else
47
+ changes_distribution_state :=
48
+ new.runtime_version is distinct from old.runtime_version
49
+ or new.channel is distinct from old.channel
50
+ or new.platform is distinct from old.platform
51
+ or new.latest_update_id is distinct from old.latest_update_id
52
+ or new.latest_created_at is distinct from old.latest_created_at
53
+ or new.latest_created_at_path is distinct from old.latest_created_at_path
54
+ or new.active_update_id is distinct from old.active_update_id
55
+ or new.active_changed_at is distinct from old.active_changed_at
56
+ or new.served_manifest_id is distinct from old.served_manifest_id
57
+ or new.served_manifest_changed_at
58
+ is distinct from old.served_manifest_changed_at;
59
+ end if;
60
+
61
+ if not changes_distribution_state then
62
+ return new;
63
+ end if;
64
+
65
+ -- Clearing every serving pointer only removes distribution and is the
66
+ -- channel-side part of deactivation. Clearing a rollback pointer while a
67
+ -- latest pointer remains would promote latest, so it is still guarded.
68
+ if new.latest_update_id is null and new.active_update_id is null then
69
+ return new;
70
+ end if;
71
+
72
+ -- Disabling a targeted update can leave an active public fallback. Updating
73
+ -- pointers to reflect that removal must remain possible while distribution is
74
+ -- blocked, just as clearing the last active pointer was before targeting.
75
+ -- Require an already retired pointer; do not permit a promotion or a new scope.
76
+ if tg_op = 'UPDATE'
77
+ and new.runtime_version = old.runtime_version
78
+ and new.channel = old.channel and new.platform = old.platform
79
+ and (
80
+ (old.latest_update_id is not null and not exists (
81
+ select 1 from public.ota_updates where update_id = old.latest_update_id and is_active))
82
+ or (old.active_update_id is not null and not exists (
83
+ select 1 from public.ota_updates where update_id = old.active_update_id and is_active))
84
+ ) then
85
+ select * into latest_active from public.ota_updates
86
+ where runtime_version = old.runtime_version and channel = old.channel
87
+ and platform = old.platform and is_active
88
+ order by created_at desc, update_id desc limit 1;
89
+ select update_id into surviving_rollback from public.ota_updates
90
+ where update_id = old.active_update_id and is_active
91
+ and update_id is distinct from latest_active.update_id;
92
+ if new.latest_update_id is not distinct from latest_active.update_id
93
+ and new.latest_created_at is not distinct from latest_active.created_at
94
+ and new.latest_created_at_path is not distinct from latest_active.created_at_path
95
+ and new.active_update_id is not distinct from surviving_rollback then
96
+ return new;
97
+ end if;
98
+ end if;
99
+
100
+ perform public.assert_ota_distribution_mutation_allowed(
101
+ 'changing OTA channel distribution state'
102
+ );
103
+ return new;
104
+ end;
105
+ $$;
106
+
107
+
108
+ commit;
@@ -0,0 +1,130 @@
1
+ -- Apply after device-version-metadata. Existing releases remain public.
2
+ begin;
3
+ -- Targeted releases coexist with their public fallback.
4
+ drop index if exists public.idx_ota_updates_one_active_per_scope;
5
+ alter table public.ota_updates
6
+ add column if not exists audience_mode text not null default 'all' check (audience_mode in ('all','devices')),
7
+ add column if not exists audience_installation_ids text[] not null default '{}',
8
+ add column if not exists audience_device_ids text[] not null default '{}',
9
+ add column if not exists audience_version integer not null default 1 check (audience_version > 0);
10
+ alter table public.ota_device_state add column if not exists device_id text check (device_id ~ '^[0-9a-f]{64}$');
11
+ create index if not exists idx_ota_device_state_device on public.ota_device_state(device_id,platform);
12
+ create table if not exists public.ota_named_devices (
13
+ device_id text primary key check (device_id ~ '^[0-9a-f]{64}$'),
14
+ name text not null check (length(name) between 1 and 80 and name = btrim(name) and name !~ '[[:cntrl:]]')
15
+ );
16
+ alter table public.ota_named_devices enable row level security;
17
+ revoke all on public.ota_named_devices from public;
18
+
19
+ create or replace function public.ota_latest_eligible_update(p_runtime text, p_channel text, p_platform text,
20
+ p_installation text default null, p_device text default null)
21
+ returns setof public.ota_updates language sql stable set search_path=public as $$
22
+ select * from public.ota_updates where runtime_version=p_runtime and channel=p_channel and platform=p_platform and is_active
23
+ and (audience_mode='all' or (audience_mode='devices' and
24
+ (p_installation=any(audience_installation_ids) or p_device=any(audience_device_ids))))
25
+ order by created_at desc, update_id desc limit 1;
26
+ $$;
27
+
28
+ create or replace function public.ota_replace_audience(p_update uuid, p_mode text, p_installations jsonb,
29
+ p_devices jsonb, p_version integer)
30
+ returns setof public.ota_updates language plpgsql set search_path=public as $$
31
+ declare ids text[]; devices text[];
32
+ begin
33
+ perform public.assert_ota_distribution_mutation_allowed('change update audience');
34
+ if p_mode is null or p_mode not in ('all','devices') or jsonb_typeof(p_installations) is distinct from 'array'
35
+ or jsonb_typeof(p_devices) is distinct from 'array' then raise exception 'INVALID_AUDIENCE'; end if;
36
+ select coalesce(array_agg(distinct value),'{}') into ids from jsonb_array_elements_text(p_installations);
37
+ select coalesce(array_agg(distinct value),'{}') into devices from jsonb_array_elements_text(p_devices);
38
+ if cardinality(ids)>100 or cardinality(devices)>100 or
39
+ exists(select 1 from unnest(ids) id where id is null or id !~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') or
40
+ exists(select 1 from unnest(devices) id where id is null or id !~ '^[0-9a-f]{64}$') or
41
+ (p_mode='devices' and cardinality(ids)+cardinality(devices)=0)
42
+ then raise exception 'INVALID_AUDIENCE'; end if;
43
+ return query update public.ota_updates set audience_mode=p_mode, audience_installation_ids=ids,
44
+ audience_device_ids=devices,audience_version=audience_version+1
45
+ where update_id=p_update and audience_version=p_version returning *;
46
+ if not found then raise exception 'AUDIENCE_CONFLICT'; end if;
47
+ end $$;
48
+
49
+ create or replace function public.ota_record_heartbeat(p_event jsonb)
50
+ returns table(result text) language plpgsql set search_path = public as $$
51
+ declare
52
+ previous public.ota_device_state%rowtype;
53
+ artifact uuid;
54
+ label text;
55
+ kind text;
56
+ before_version jsonb;
57
+ after_version jsonb;
58
+ seq bigint := (p_event->>'sequence')::bigint;
59
+ reported uuid := (p_event->>'updateId')::uuid;
60
+ receipt timestamptz;
61
+ begin
62
+ if seq < 1 or seq > 9007199254740991 then raise exception 'INVALID_SEQUENCE'; end if;
63
+ insert into public.ota_device_state(installation_id,platform,runtime_version,channel)
64
+ values(p_event->>'installationId',p_event->>'platform',p_event->>'runtimeVersion',p_event->>'channel')
65
+ on conflict (installation_id,platform) do nothing;
66
+ select * into strict previous from public.ota_device_state
67
+ where installation_id=p_event->>'installationId' and platform=p_event->>'platform' for update;
68
+ if seq < previous.last_sequence then return query select 'stale'::text; return; end if;
69
+ if seq = previous.last_sequence then
70
+ if previous.last_event_id = (p_event->>'eventId')::uuid then
71
+ return query select 'duplicate'::text; return;
72
+ end if;
73
+ raise exception 'HEARTBEAT_SEQUENCE_CONFLICT';
74
+ end if;
75
+ receipt := greatest(previous.last_seen_at, clock_timestamp());
76
+ kind := case when (p_event->>'isEmbeddedLaunch')::boolean then 'embedded' else 'unknown' end;
77
+ if kind <> 'embedded' and reported is not null then
78
+ select u.update_id,u.comment into artifact,label from public.ota_updates u
79
+ where u.runtime_version=p_event->>'runtimeVersion' and u.channel=p_event->>'channel' and u.platform=p_event->>'platform'
80
+ and (u.update_id=reported or exists(select 1 from public.ota_served_manifest_log m
81
+ where m.served_manifest_id=reported and m.update_id=u.update_id and m.runtime_version=u.runtime_version
82
+ and m.channel=u.channel and m.platform=u.platform))
83
+ order by (u.update_id=reported) asc limit 1;
84
+ if artifact is null and previous.launch_kind='ota' and previous.reported_update_id=reported
85
+ and previous.runtime_version=p_event->>'runtimeVersion' and previous.channel=p_event->>'channel' then
86
+ artifact := previous.resolved_update_id; label := previous.update_label;
87
+ end if;
88
+ if artifact is not null then kind := 'ota'; end if;
89
+ end if;
90
+ before_version := jsonb_build_object('runtime',previous.runtime_version,'channel',previous.channel,
91
+ 'nativeVersion',previous.native_version,'nativeBuild',previous.native_build,'kind',previous.launch_kind,
92
+ 'updateId',coalesce(previous.resolved_update_id,previous.reported_update_id));
93
+ after_version := jsonb_build_object('runtime',p_event->>'runtimeVersion','channel',p_event->>'channel',
94
+ 'nativeVersion',p_event->>'nativeVersion','nativeBuild',p_event->>'nativeBuild','kind',kind,
95
+ 'updateId',coalesce(artifact,reported));
96
+ update public.ota_device_state set
97
+ device_id=coalesce(p_event->>'deviceId',previous.device_id),
98
+ runtime_version=p_event->>'runtimeVersion',channel=p_event->>'channel',
99
+ embedded_update_id=case when kind='embedded' then reported else previous.embedded_update_id end,
100
+ current_update_id=(select update_id from public.ota_updates where update_id=artifact),
101
+ served_manifest_id=case when kind<>'embedded' then reported end,
102
+ reported_update_id=reported,resolved_update_id=artifact,update_label=label,launch_kind=kind,
103
+ native_version=p_event->>'nativeVersion',native_build=p_event->>'nativeBuild',
104
+ emergency_launch=(p_event->>'isEmergencyLaunch')::boolean,observed_at=(p_event->>'observedAt')::timestamptz,
105
+ last_event_id=(p_event->>'eventId')::uuid,last_sequence=seq,last_seen_at=receipt
106
+ where installation_id=previous.installation_id and platform=previous.platform;
107
+ if previous.last_sequence=0 or before_version is distinct from after_version then
108
+ insert into public.ota_device_transitions(installation_id,platform,from_runtime,from_update_id,to_runtime,to_update_id,
109
+ transition_type,occurred_at,event_id,before_state,after_state)
110
+ values(previous.installation_id,previous.platform,case when previous.last_sequence>0 then previous.runtime_version end,
111
+ previous.resolved_update_id,p_event->>'runtimeVersion',artifact,
112
+ case when previous.last_sequence=0 then 'first_seen' else 'version_changed' end,receipt,
113
+ (p_event->>'eventId')::uuid,case when previous.last_sequence>0 then before_version end,after_version);
114
+ end if;
115
+ return query select 'accepted'::text;
116
+ end $$;
117
+
118
+
119
+ revoke all on function public.ota_latest_eligible_update(text,text,text,text,text) from public;
120
+ revoke all on function public.ota_replace_audience(uuid,text,jsonb,jsonb,integer) from public;
121
+ do $$ begin
122
+ if exists(select 1 from pg_roles where rolname='service_role') then
123
+ grant all on public.ota_named_devices to service_role;
124
+ grant execute on function public.ota_latest_eligible_update(text,text,text,text,text) to service_role;
125
+ grant execute on function public.ota_replace_audience(uuid,text,jsonb,jsonb,integer) to service_role;
126
+ drop policy if exists ota_named_devices_service on public.ota_named_devices;
127
+ create policy ota_named_devices_service on public.ota_named_devices to service_role using (true) with check (true);
128
+ end if;
129
+ end $$;
130
+ commit;
@@ -0,0 +1,154 @@
1
+ -- Apply after 2026-10-22-device-analytics.sql. Safe to reapply.
2
+ -- Version names come from manifest metadata, never the publication message.
3
+ begin;
4
+
5
+ create or replace function public.ota_devices_report_v2(
6
+ p_days integer default 7, p_platform text default null, p_channel text default null,
7
+ p_runtime text default null, p_offset integer default 0,
8
+ p_launch_kind text default null, p_search text default null)
9
+ returns table(report jsonb) language sql stable set search_path = public as $$
10
+ with parameters as (
11
+ select (now() at time zone 'UTC')::date as today,
12
+ (now() at time zone 'UTC')::date - (p_days - 1) as first_day,
13
+ now() - make_interval(days => p_days) as window_start,
14
+ m.collection_started_at,
15
+ greatest((m.collection_started_at at time zone 'UTC')::date,
16
+ (now() at time zone 'UTC')::date - 89) as retained_from
17
+ from public.ota_device_analytics_metadata m where singleton
18
+ ), recent as (
19
+ select s.* from public.ota_device_state s, parameters p
20
+ where s.last_sequence > 0 and s.last_seen_at >= p.window_start
21
+ ), filtered as (
22
+ select * from recent
23
+ where (p_platform is null or platform = p_platform)
24
+ and (p_channel is null or channel = p_channel)
25
+ and (p_runtime is null or runtime_version = p_runtime)
26
+ and (p_launch_kind is null or launch_kind = p_launch_kind)
27
+ and (p_search is null or position(lower(p_search) in lower(installation_id)) > 0)
28
+ ), version_counts as (
29
+ select platform, channel, runtime_version, native_version, native_build, launch_kind,
30
+ coalesce(resolved_update_id, reported_update_id) as update_id,
31
+ max(update_label) as update_label, count(*) as installations
32
+ from filtered group by platform, channel, runtime_version, native_version, native_build,
33
+ launch_kind, coalesce(resolved_update_id, reported_update_id)
34
+ ), versions as (
35
+ select v.*, case when v.launch_kind = 'ota' then coalesce(
36
+ case when jsonb_typeof(u.manifest #> '{extra,expoClient,version}') = 'string'
37
+ then nullif(btrim(u.manifest #>> '{extra,expoClient,version}'), '') end,
38
+ case when jsonb_typeof(u.manifest #> '{expoClient,version}') = 'string'
39
+ then nullif(btrim(u.manifest #>> '{expoClient,version}'), '') end
40
+ ) when v.launch_kind = 'embedded' then coalesce(nullif(btrim(e.app_version), ''), v.native_version)
41
+ else null end as app_version
42
+ from version_counts v
43
+ left join public.ota_updates u on v.launch_kind = 'ota' and u.update_id = v.update_id
44
+ and u.runtime_version = v.runtime_version and u.channel = v.channel and u.platform = v.platform
45
+ left join public.ota_embedded_updates e on v.launch_kind = 'embedded' and e.embedded_update_id = v.update_id
46
+ and e.channel = v.channel and e.platform = v.platform
47
+ ), page as (
48
+ select * from filtered order by last_seen_at desc, installation_id, platform limit 50 offset p_offset
49
+ ), daily as (
50
+ select d.* from public.ota_device_activity_daily d, parameters p
51
+ where d.activity_date between greatest(p.first_day, p.retained_from) and p.today
52
+ ), filtered_daily as (
53
+ select d.* from daily d
54
+ where (p_platform is null or d.platform = p_platform)
55
+ and (p_channel is null or d.channel = p_channel)
56
+ and (p_runtime is null or d.runtime_version = p_runtime)
57
+ and (p_launch_kind is null or d.launch_kind = p_launch_kind)
58
+ and (p_search is null or position(lower(p_search) in lower(d.installation_id)) > 0)
59
+ ), calendar as (
60
+ select p.first_day + n as date,
61
+ case when p.first_day + n < p.retained_from then 'unavailable'
62
+ when p.first_day + n = p.today
63
+ or ((p.first_day + n)::timestamp at time zone 'UTC') < p.collection_started_at then 'partial'
64
+ else 'complete' end as coverage
65
+ from parameters p cross join generate_series(0, p_days - 1) n
66
+ ), day_counts as (
67
+ select d.activity_date, count(*) as active,
68
+ count(*) filter (where (c.first_observed_at at time zone 'UTC')::date = d.activity_date) as newly_observed
69
+ from filtered_daily d left join public.ota_device_activity_cohorts c
70
+ on c.installation_id = d.installation_id and c.platform = d.platform
71
+ group by d.activity_date
72
+ ), activity as (
73
+ select c.date, c.coverage,
74
+ case when c.coverage <> 'unavailable' then coalesce(d.active, 0) end as active,
75
+ case when c.coverage <> 'unavailable' then coalesce(d.newly_observed, 0) end as newly_observed
76
+ from calendar c left join day_counts d on c.date = d.activity_date
77
+ ), cohort_source as (
78
+ select c.*, (c.first_observed_at at time zone 'UTC')::date as first_day,
79
+ date_trunc('week', c.first_observed_at at time zone 'UTC')::date as week_start
80
+ from public.ota_device_activity_cohorts c, parameters p
81
+ where c.first_observed_at >= p.collection_started_at
82
+ and (c.first_observed_at at time zone 'UTC')::date between p.retained_from and p.today
83
+ ), cohort_members as (
84
+ select * from cohort_source
85
+ where (p_platform is null or platform = p_platform)
86
+ and (p_channel is null or channel = p_channel)
87
+ and (p_runtime is null or runtime_version = p_runtime)
88
+ and (p_launch_kind is null or launch_kind = p_launch_kind)
89
+ and (p_search is null or position(lower(p_search) in lower(installation_id)) > 0)
90
+ ), cohort_sizes as (
91
+ select week_start, count(*) as installations from cohort_members group by week_start
92
+ ), retention_members as (
93
+ select c.week_start, n.day,
94
+ (n.day = 0 or c.first_day + n.day < p.today) as eligible,
95
+ exists (select 1 from public.ota_device_activity_daily d
96
+ where d.installation_id = c.installation_id and d.platform = c.platform
97
+ and d.activity_date = c.first_day + n.day) as returned
98
+ from cohort_members c cross join (values (0), (1), (7), (14), (30)) n(day)
99
+ cross join parameters p
100
+ ), retention_counts as (
101
+ select week_start, day, count(*) filter (where eligible) as eligible,
102
+ count(*) filter (where eligible and returned) as retained
103
+ from retention_members group by week_start, day
104
+ ), retention_rows as (
105
+ select c.week_start, c.installations,
106
+ jsonb_agg(jsonb_build_object('day', r.day, 'eligibleInstallations', r.eligible,
107
+ 'retainedInstallations', case when r.eligible > 0 then r.retained end,
108
+ 'rate', case when r.eligible > 0 then r.retained::numeric / r.eligible end) order by r.day) as cells
109
+ from cohort_sizes c join retention_counts r using (week_start) group by c.week_start, c.installations
110
+ ), adoption as (
111
+ select date_trunc('week', first_seen_at at time zone 'UTC')::date as week_start,
112
+ count(*) as installations,
113
+ count(*) filter (where launch_kind = 'ota') as ota,
114
+ count(*) filter (where launch_kind = 'embedded') as embedded,
115
+ count(*) filter (where launch_kind = 'unknown') as unknown
116
+ from filtered group by date_trunc('week', first_seen_at at time zone 'UTC')::date
117
+ ), option_source as (
118
+ select platform, channel, runtime_version, launch_kind from recent
119
+ union select platform, channel, runtime_version, launch_kind from daily
120
+ union select platform, channel, runtime_version, launch_kind from cohort_source
121
+ )
122
+ select jsonb_build_object(
123
+ 'total', (select count(*) from filtered), 'pageSize', 50, 'offset', p_offset,
124
+ 'devices', coalesce((select jsonb_agg(to_jsonb(page) order by last_seen_at desc, installation_id, platform) from page), '[]'::jsonb),
125
+ 'versions', coalesce((select jsonb_agg(to_jsonb(versions) order by installations desc, platform, channel, runtime_version,
126
+ native_version, native_build, launch_kind, update_id) from versions), '[]'::jsonb),
127
+ 'filters', jsonb_build_object('days', p_days, 'platform', p_platform, 'channel', p_channel,
128
+ 'runtime', p_runtime, 'launchKind', p_launch_kind, 'search', p_search),
129
+ 'options', jsonb_build_object(
130
+ 'platforms', coalesce((select jsonb_agg(v order by v) from (select distinct platform as v from option_source) t), '[]'::jsonb),
131
+ 'channels', coalesce((select jsonb_agg(v order by v) from (select distinct channel as v from option_source) t), '[]'::jsonb),
132
+ 'runtimes', coalesce((select jsonb_agg(v order by v) from (select distinct runtime_version as v from option_source) t), '[]'::jsonb),
133
+ 'launchKinds', coalesce((select jsonb_agg(v order by v) from (select distinct launch_kind as v from option_source) t), '[]'::jsonb)),
134
+ 'summary', (select jsonb_build_object('activeInstallations', count(*),
135
+ 'newInstallations', count(*) filter (where first_seen_at >= (select window_start from parameters)),
136
+ 'otaInstallations', count(*) filter (where launch_kind = 'ota'),
137
+ 'embeddedInstallations', count(*) filter (where launch_kind = 'embedded'),
138
+ 'unknownInstallations', count(*) filter (where launch_kind = 'unknown'),
139
+ 'emergencyInstallations', count(*) filter (where emergency_launch),
140
+ 'reportedVersions', (select count(*) from versions)) from filtered),
141
+ 'activity', coalesce((select jsonb_agg(jsonb_build_object('date', date, 'activeInstallations', active,
142
+ 'newInstallations', newly_observed, 'coverage', coverage) order by date) from activity), '[]'::jsonb),
143
+ 'adoptionCohorts', coalesce((select jsonb_agg(jsonb_build_object('weekStart', week_start, 'installations', installations,
144
+ 'otaInstallations', ota, 'embeddedInstallations', embedded, 'unknownInstallations', unknown) order by week_start) from adoption), '[]'::jsonb),
145
+ 'cohorts', coalesce((select jsonb_agg(jsonb_build_object('weekStart', week_start, 'installations', installations,
146
+ 'cells', cells) order by week_start) from retention_rows), '[]'::jsonb),
147
+ 'coverage', (select jsonb_build_object('collectionStartedAt', collection_started_at, 'retainedFrom', retained_from,
148
+ 'timezone', 'UTC', 'retentionDays', 90, 'cohortWindowDays', 90, 'activityBasis', 'accepted-heartbeat-receipt',
149
+ 'cohortBasis', 'first-recorded-heartbeat', 'cohortFilters', 'entry-state', 'todayIsPartial', true,
150
+ 'generatedAt', now(), 'windowStartedAt', window_start) from parameters)
151
+ );
152
+ $$;
153
+
154
+ commit;