gloomberb 0.10.4 → 0.11.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/LICENSE +1 -1
- package/README.md +30 -3
- package/README.zh-CN.md +1 -1
- package/package.json +5 -5
- package/src/api-client/auth.ts +34 -0
- package/src/api-client/data.ts +25 -0
- package/src/api-client/index.test.ts +81 -0
- package/src/api-client/index.ts +37 -0
- package/src/api-client/request.test.ts +30 -0
- package/src/api-client/request.ts +44 -20
- package/src/api-client/socket-health.test.ts +79 -0
- package/src/api-client/socket.ts +108 -5
- package/src/api-client/types.ts +139 -0
- package/src/app/global-shortcuts.test.tsx +144 -9
- package/src/app/global-shortcuts.ts +33 -11
- package/src/app/runtime/broker-import.ts +39 -5
- package/src/app/runtime/startup.ts +7 -1
- package/src/app/runtime/update.ts +26 -1
- package/src/app.tsx +36 -19
- package/src/brokers/broker-ticker-sync.ts +3 -8
- package/src/brokers/sync-broker-instance.test.ts +59 -0
- package/src/brokers/sync-broker-instance.ts +37 -13
- package/src/capabilities/chart-series.ts +318 -0
- package/src/capabilities/factories.ts +46 -0
- package/src/capabilities/index.ts +1 -0
- package/src/capabilities/registry.test.ts +135 -0
- package/src/capabilities/registry.ts +31 -4
- package/src/capabilities/types.ts +52 -0
- package/src/cli/commands/overview.ts +2 -2
- package/src/cli/desktop-pane-shot.ts +2 -0
- package/src/cli/entry.ts +11 -1
- package/src/cli/native-smoke.ts +5 -0
- package/src/cli/pane-functions/catalog.ts +4 -0
- package/src/cli/pane-functions/data.ts +6 -1
- package/src/cli/pane-functions/discovery.ts +76 -10
- package/src/cli/pane-functions/report.test.ts +76 -0
- package/src/cli/pane-functions/report.ts +7 -1
- package/src/cli/pane-functions/resolver.ts +4 -1
- package/src/cli/pane-functions/screenshot.test.ts +56 -3
- package/src/cli/pane-functions/screenshot.ts +68 -5
- package/src/components/chart/composite/composite-chart.test.tsx +54 -0
- package/src/components/chart/composite/composite-chart.tsx +92 -15
- package/src/components/chart/composite/downsample.test.ts +136 -0
- package/src/components/chart/composite/downsample.ts +208 -0
- package/src/components/chart/composite/panel-series.test.ts +66 -0
- package/src/components/chart/composite/panel-series.ts +101 -0
- package/src/components/chart/multi-line/renderer.ts +1 -0
- package/src/components/chart/native/kitty/index.test.ts +25 -3
- package/src/components/chart/native/kitty/support.ts +37 -3
- package/src/components/chart/native/use-chart-text-fallback.ts +19 -0
- package/src/components/chart/static/chart/surface.tsx +43 -12
- package/src/components/chart/static/multi-line-chart-surface.tsx +41 -5
- package/src/components/command-bar/commands/direct/index.ts +18 -0
- package/src/components/command-bar/commands/registry.ts +14 -0
- package/src/components/command-bar/routes/root/article-results.test.ts +83 -0
- package/src/components/command-bar/routes/root/article-results.ts +55 -0
- package/src/components/command-bar/routes/root/command-items.ts +3 -0
- package/src/components/command-bar/routes/root/provider-search.ts +7 -3
- package/src/components/command-bar/routes/root/results.ts +38 -11
- package/src/components/command-bar/routes/root/runtime.ts +6 -2
- package/src/components/command-bar/routes/ticker-search/actions.ts +1 -1
- package/src/components/command-bar/routes/ticker-search/results.test.ts +34 -0
- package/src/components/command-bar/routes/ticker-search/results.ts +4 -6
- package/src/components/command-bar/routes/ticker-search/route.ts +2 -2
- package/src/components/command-bar/routing/list-state.ts +2 -1
- package/src/components/command-bar/surface/assist.test.tsx +13 -6
- package/src/components/command-bar/surface/index.test.tsx +65 -0
- package/src/components/command-bar/surface/index.tsx +35 -0
- package/src/components/command-bar/view-model.test.ts +43 -0
- package/src/components/command-bar/view-model.ts +41 -4
- package/src/components/command-bar/workflow/collection-actions.test.ts +94 -0
- package/src/components/command-bar/workflow/collection-actions.ts +14 -3
- package/src/components/command-bar/workflow/ops.test.ts +136 -0
- package/src/components/command-bar/workflow/ops.ts +71 -1
- package/src/components/index.ts +1 -1
- package/src/components/layout/header.test.tsx +35 -0
- package/src/components/layout/header.tsx +101 -9
- package/src/components/layout/pane/cycle-order.test.ts +0 -1
- package/src/components/layout/pane/footer/index.test.tsx +19 -19
- package/src/components/layout/pane/header.tsx +2 -13
- package/src/components/layout/shell/index.tsx +3 -0
- package/src/components/layout/status-bar.test.tsx +36 -1
- package/src/components/layout/status-bar.tsx +48 -2
- package/src/components/onboarding/account-step/chooser-panel.tsx +27 -50
- package/src/components/onboarding/account-step/form-panel.tsx +85 -29
- package/src/components/onboarding/account-step/index.tsx +10 -1
- package/src/components/onboarding/account-step/model.ts +3 -3
- package/src/components/onboarding/account-step/qr-panel.tsx +68 -0
- package/src/components/onboarding/account-step/signed-in-panel.tsx +4 -7
- package/src/components/onboarding/onboarding-frame.tsx +517 -0
- package/src/components/onboarding/onboarding-steps.tsx +0 -264
- package/src/components/onboarding/onboarding-wizard.test.tsx +626 -200
- package/src/components/onboarding/onboarding-wizard.tsx +848 -266
- package/src/components/onboarding/portfolio-step/broker-fields-panel.tsx +81 -51
- package/src/components/onboarding/portfolio-step/broker-setup-panel.tsx +14 -20
- package/src/components/onboarding/portfolio-step/broker-sync-panel.tsx +8 -11
- package/src/components/onboarding/portfolio-step/choose-panel.tsx +18 -22
- package/src/components/onboarding/portfolio-step/index.tsx +3 -0
- package/src/components/onboarding/portfolio-step/types.ts +3 -0
- package/src/components/onboarding/wizard-account.ts +38 -3
- package/src/components/onboarding/wizard-broker-sync.ts +45 -67
- package/src/components/onboarding/wizard-model.ts +18 -2
- package/src/components/ui/data-table/opentui/index.tsx +29 -9
- package/src/components/ui/data-table/types.ts +2 -0
- package/src/components/ui/external-link.tsx +4 -2
- package/src/components/ui/loading.tsx +1 -1
- package/src/components/ui/status.tsx +76 -4
- package/src/components/ui/table-layout.test.ts +24 -0
- package/src/components/ui/table-layout.ts +90 -23
- package/src/components/ui/tabs.tsx +86 -4
- package/src/core/app-services.ts +14 -3
- package/src/core/connection-health.test.ts +143 -0
- package/src/core/connection-health.ts +295 -0
- package/src/core/state/app/layout-reducer.ts +34 -0
- package/src/core/state/app/layout.ts +21 -0
- package/src/core/state/app/state.ts +10 -0
- package/src/core/state/app/types.ts +7 -1
- package/src/data/config/chart-settings.ts +11 -2
- package/src/data/config/store/index.test.ts +83 -9
- package/src/data/config/store/migrations.ts +16 -0
- package/src/data/config/store/normalize.ts +59 -1
- package/src/data/fred-series.ts +8 -2
- package/src/i18n/es.ts +126 -0
- package/src/i18n/ja.ts +126 -0
- package/src/i18n/ko.ts +126 -0
- package/src/i18n/zh-cn.ts +126 -0
- package/src/i18n/zh-tw.ts +126 -0
- package/src/market-data/coordinator/chart.ts +3 -2
- package/src/market-data/hooks.tsx +3 -2
- package/src/market-data/market/status.test.ts +12 -0
- package/src/market-data/market/status.ts +34 -17
- package/src/news/aggregator.test.ts +39 -0
- package/src/news/aggregator.ts +78 -22
- package/src/news/hooks.ts +4 -0
- package/src/notifications/app-notifier.test.ts +28 -6
- package/src/notifications/app-notifier.ts +24 -5
- package/src/plugins/broker-sync/broker-sync.test.ts +130 -0
- package/src/plugins/broker-sync/native-loader.ts +10 -0
- package/src/plugins/broker-sync/normalize.ts +271 -0
- package/src/plugins/broker-sync/public.ts +122 -0
- package/src/plugins/broker-sync/robinhood-native.ts +367 -0
- package/src/plugins/broker-sync/robinhood.ts +114 -0
- package/src/plugins/broker-sync/simplefin-native.ts +201 -0
- package/src/plugins/broker-sync/simplefin.ts +70 -0
- package/src/plugins/builtin/account-management/footer.ts +8 -19
- package/src/plugins/builtin/account-management/pane.tsx +13 -8
- package/src/plugins/builtin/ai/pi/host.ts +6 -5
- package/src/plugins/builtin/ai/providers.ts +1 -1
- package/src/plugins/builtin/ai/screener/footer.ts +0 -7
- package/src/plugins/builtin/ai/screener/pane.test.tsx +2 -1
- package/src/plugins/builtin/ai/screener/pane.tsx +3 -1
- package/src/plugins/builtin/ai/screener/results-view.tsx +8 -0
- package/src/plugins/builtin/ai/screener/runner.ts +7 -3
- package/src/plugins/builtin/ai/workspace/pane.tsx +13 -2
- package/src/plugins/builtin/alerts/alert-engine.ts +13 -0
- package/src/plugins/builtin/alerts/constants.ts +2 -1
- package/src/plugins/builtin/alerts/index.tsx +67 -36
- package/src/plugins/builtin/alerts/pane.tsx +17 -65
- package/src/plugins/builtin/analytics/index.tsx +24 -11
- package/src/plugins/builtin/analytics/pane-model.ts +48 -14
- package/src/plugins/builtin/analytics/portfolio-selection.ts +19 -0
- package/src/plugins/builtin/analytics/view.tsx +5 -3
- package/src/plugins/builtin/broker-manager/detail.tsx +7 -2
- package/src/plugins/builtin/broker-manager/footer.ts +4 -9
- package/src/plugins/builtin/broker-manager/index.tsx +1 -6
- package/src/plugins/builtin/buildout/detail/ui.tsx +15 -4
- package/src/plugins/builtin/buildout/pane/header.tsx +1 -0
- package/src/plugins/builtin/changelog/index.tsx +60 -45
- package/src/plugins/builtin/chart-composer/catalog-inventory.test.ts +97 -0
- package/src/plugins/builtin/chart-composer/catalog-inventory.ts +366 -0
- package/src/plugins/builtin/chart-composer/chart-spec.ts +21 -1
- package/src/plugins/builtin/chart-composer/data-catalog-pane.tsx +307 -0
- package/src/plugins/builtin/chart-composer/editor/controller.ts +1 -1
- package/src/plugins/builtin/chart-composer/index.tsx +45 -5
- package/src/plugins/builtin/chart-composer/pane.tsx +7 -88
- package/src/plugins/builtin/chart-composer/presets.test.ts +48 -0
- package/src/plugins/builtin/chart-composer/presets.ts +69 -3
- package/src/plugins/builtin/chart-composer/quick-add.tsx +8 -5
- package/src/plugins/builtin/chart-composer/semantic.ts +18 -14
- package/src/plugins/builtin/chart-composer/series-catalog.test.ts +39 -0
- package/src/plugins/builtin/chart-composer/series-catalog.ts +104 -3
- package/src/plugins/builtin/chart-composer/shortcuts.test.ts +10 -10
- package/src/plugins/builtin/chart-composer/shortcuts.ts +4 -12
- package/src/plugins/builtin/chart-composer/use-series-catalog.ts +230 -23
- package/src/plugins/builtin/chat/chat.test.tsx +22 -0
- package/src/plugins/builtin/chat/content/composer.tsx +6 -4
- package/src/plugins/builtin/chat/content/index.tsx +7 -0
- package/src/plugins/builtin/chat/content/snapshot.ts +9 -2
- package/src/plugins/builtin/chat/content/transcript.tsx +10 -0
- package/src/plugins/builtin/chat/controller/channel-actions.ts +11 -1
- package/src/plugins/builtin/chat/controller/channels.ts +13 -3
- package/src/plugins/builtin/chat/controller/index.ts +61 -7
- package/src/plugins/builtin/chat/controller/lifecycle.ts +3 -0
- package/src/plugins/builtin/chat/controller/message-loading.ts +5 -1
- package/src/plugins/builtin/chat/controller/messages.ts +3 -1
- package/src/plugins/builtin/chat/controller/notifications.ts +32 -11
- package/src/plugins/builtin/chat/controller/state.ts +7 -0
- package/src/plugins/builtin/chat/controller/view.ts +1 -0
- package/src/plugins/builtin/chat/controller.test.ts +117 -28
- package/src/plugins/builtin/cloud/auth-commands.ts +18 -0
- package/src/plugins/builtin/cloud/device-signin-dialog.tsx +243 -0
- package/src/plugins/builtin/cloud/device-signin.test.ts +217 -0
- package/src/plugins/builtin/cloud/device-signin.ts +240 -0
- package/src/plugins/builtin/cloud/plugin.tsx +7 -1
- package/src/plugins/builtin/cloud-tweets/table.tsx +57 -10
- package/src/plugins/builtin/composite-plugins.ts +39 -5
- package/src/plugins/builtin/congress-trades/detail.tsx +2 -4
- package/src/plugins/builtin/congress-trades/footer.ts +7 -16
- package/src/plugins/builtin/congress-trades/pane.tsx +12 -20
- package/src/plugins/builtin/connections/index.ts +53 -0
- package/src/plugins/builtin/connections/pane.test.tsx +79 -0
- package/src/plugins/builtin/connections/pane.tsx +258 -0
- package/src/plugins/builtin/correlation/index.test.tsx +2 -1
- package/src/plugins/builtin/correlation/index.tsx +58 -19
- package/src/plugins/builtin/correlation/matrix/model.ts +18 -7
- package/src/plugins/builtin/correlation/relationship/pane.tsx +43 -19
- package/src/plugins/builtin/correlation/settings.ts +1 -1
- package/src/plugins/builtin/credit-conditions/client.test.ts +80 -0
- package/src/plugins/builtin/credit-conditions/client.ts +105 -0
- package/src/plugins/builtin/credit-conditions/index.test.tsx +111 -0
- package/src/plugins/builtin/credit-conditions/index.tsx +227 -0
- package/src/plugins/builtin/credit-conditions/model.test.ts +43 -0
- package/src/plugins/builtin/credit-conditions/model.ts +64 -0
- package/src/plugins/builtin/debug/index.tsx +11 -6
- package/src/plugins/builtin/dividend-yield/client.test.ts +58 -0
- package/src/plugins/builtin/dividend-yield/client.ts +252 -0
- package/src/plugins/builtin/dividend-yield/index.tsx +61 -0
- package/src/plugins/builtin/dividend-yield/model.ts +83 -0
- package/src/plugins/builtin/dividend-yield/pane.tsx +305 -0
- package/src/plugins/builtin/dividend-yield/types.ts +22 -0
- package/src/plugins/builtin/earnings/data/cache.test.ts +10 -7
- package/src/plugins/builtin/earnings/data/cache.ts +23 -9
- package/src/plugins/builtin/earnings/index.tsx +57 -6
- package/src/plugins/builtin/econ/calendar-model.test.ts +37 -3
- package/src/plugins/builtin/econ/calendar-model.ts +32 -26
- package/src/plugins/builtin/econ/fred-cache.test.ts +8 -1
- package/src/plugins/builtin/econ/fred-series-map.ts +18 -0
- package/src/plugins/builtin/econ/index.test.tsx +105 -0
- package/src/plugins/builtin/econ/index.tsx +95 -52
- package/src/plugins/builtin/fear-greed/cache.test.ts +18 -3
- package/src/plugins/builtin/fear-greed/cache.ts +18 -6
- package/src/plugins/builtin/fear-greed/charts.tsx +9 -2
- package/src/plugins/builtin/fear-greed/format.ts +0 -9
- package/src/plugins/builtin/fear-greed/pane.tsx +21 -21
- package/src/plugins/builtin/futures/contracts.ts +100 -0
- package/src/plugins/builtin/futures/index.tsx +298 -0
- package/src/plugins/builtin/futures/model.test.ts +129 -0
- package/src/plugins/builtin/futures/model.ts +136 -0
- package/src/plugins/builtin/futures/pane.test.tsx +239 -0
- package/src/plugins/builtin/futures/table.test.ts +155 -0
- package/src/plugins/builtin/futures/table.tsx +241 -0
- package/src/plugins/builtin/fx-matrix/index.tsx +160 -134
- package/src/plugins/builtin/fx-matrix/pairs.ts +12 -1
- package/src/plugins/builtin/fx-matrix/pane.test.tsx +109 -0
- package/src/plugins/builtin/help/components.tsx +1 -30
- package/src/plugins/builtin/help/index.tsx +5 -25
- package/src/plugins/builtin/holders/pane.tsx +17 -12
- package/src/plugins/builtin/holders/treemap.tsx +4 -2
- package/src/plugins/builtin/insider/index.tsx +20 -10
- package/src/plugins/builtin/ipo-calendar/cache.ts +101 -0
- package/src/plugins/builtin/ipo-calendar/client.test.ts +31 -0
- package/src/plugins/builtin/ipo-calendar/client.ts +296 -0
- package/src/plugins/builtin/ipo-calendar/index.tsx +65 -0
- package/src/plugins/builtin/ipo-calendar/model.ts +145 -0
- package/src/plugins/builtin/ipo-calendar/pane.tsx +328 -0
- package/src/plugins/builtin/ipo-calendar/types.ts +19 -0
- package/src/plugins/builtin/kelly-sizer/index.test.tsx +3 -3
- package/src/plugins/builtin/kelly-sizer/model.ts +8 -1
- package/src/plugins/builtin/kelly-sizer/pane.tsx +33 -15
- package/src/plugins/builtin/kelly-sizer/view.tsx +12 -5
- package/src/plugins/builtin/market-halts/client.ts +114 -0
- package/src/plugins/builtin/market-halts/index.tsx +29 -0
- package/src/plugins/builtin/market-halts/model.test.ts +187 -0
- package/src/plugins/builtin/market-halts/model.ts +291 -0
- package/src/plugins/builtin/market-halts/pane.tsx +256 -0
- package/src/plugins/builtin/market-heatmap/data.test.ts +1 -0
- package/src/plugins/builtin/market-heatmap/data.ts +12 -3
- package/src/plugins/builtin/market-heatmap/index.tsx +131 -46
- package/src/plugins/builtin/market-movers/index.tsx +145 -63
- package/src/plugins/builtin/market-movers/model.ts +19 -3
- package/src/plugins/builtin/market-movers/screener.test.ts +108 -0
- package/src/plugins/builtin/market-movers/screener.ts +168 -10
- package/src/plugins/builtin/news/index.tsx +78 -99
- package/src/plugins/builtin/news/wire/article-search.test.ts +72 -0
- package/src/plugins/builtin/news/wire/article-search.ts +140 -0
- package/src/plugins/builtin/news/wire/breaking/pane.tsx +18 -8
- package/src/plugins/builtin/news/wire/categories.ts +35 -0
- package/src/plugins/builtin/news/wire/default-feeds.ts +47 -1
- package/src/plugins/builtin/news/wire/feed-config.test.ts +10 -5
- package/src/plugins/builtin/news/wire/index.tsx +41 -4
- package/src/plugins/builtin/news/wire/industry-pane.tsx +38 -19
- package/src/plugins/builtin/news/wire/news/detail-view.tsx +10 -5
- package/src/plugins/builtin/news/wire/news/footer.ts +1 -0
- package/src/plugins/builtin/news/wire/news/preset-pane.tsx +15 -6
- package/src/plugins/builtin/news/wire/news/query-presets.ts +16 -2
- package/src/plugins/builtin/news/wire/news/table.test.tsx +54 -0
- package/src/plugins/builtin/news/wire/news/table.tsx +111 -17
- package/src/plugins/builtin/notes/files.test.ts +18 -0
- package/src/plugins/builtin/notes/files.ts +10 -4
- package/src/plugins/builtin/notes/index.tsx +4 -1
- package/src/plugins/builtin/notes/quick-notes-pane.test.tsx +182 -0
- package/src/plugins/builtin/notes/quick-notes-pane.tsx +74 -31
- package/src/plugins/builtin/notes/ticker-notes-tab.test.tsx +380 -0
- package/src/plugins/builtin/notes/ticker-notes-tab.tsx +97 -40
- package/src/plugins/builtin/options/calc-seed.test.ts +118 -0
- package/src/plugins/builtin/options/calc-seed.ts +52 -0
- package/src/plugins/builtin/options/footer.ts +4 -1
- package/src/plugins/builtin/options/index.tsx +22 -1
- package/src/plugins/builtin/options/live-quotes.ts +8 -0
- package/src/plugins/builtin/options/table.ts +3 -1
- package/src/plugins/builtin/options/view.test.tsx +5 -1
- package/src/plugins/builtin/options/view.tsx +112 -17
- package/src/plugins/builtin/options-calculator/index.tsx +35 -0
- package/src/plugins/builtin/options-calculator/model.test.ts +172 -0
- package/src/plugins/builtin/options-calculator/model.ts +273 -0
- package/src/plugins/builtin/options-calculator/pane.test.tsx +138 -0
- package/src/plugins/builtin/options-calculator/pane.tsx +218 -0
- package/src/plugins/builtin/plugin-module.test.ts +16 -2
- package/src/plugins/builtin/plugin-module.ts +5 -1
- package/src/plugins/builtin/polls/client.ts +57 -0
- package/src/plugins/builtin/polls/hooks.ts +20 -0
- package/src/plugins/builtin/polls/index.tsx +35 -0
- package/src/plugins/builtin/polls/normalize.test.ts +230 -0
- package/src/plugins/builtin/polls/normalize.ts +319 -0
- package/src/plugins/builtin/polls/pane.tsx +722 -0
- package/src/plugins/builtin/polls/types.ts +77 -0
- package/src/plugins/builtin/portfolio-list/header.tsx +15 -4
- package/src/plugins/builtin/portfolio-list/pane/index.tsx +7 -3
- package/src/plugins/builtin/portfolio-list/pane/supplemental.ts +1 -1
- package/src/plugins/builtin/portfolio-list/quick-add/index.tsx +2 -0
- package/src/plugins/builtin/portfolio-list/summary/index.tsx +2 -1
- package/src/plugins/builtin/research/analyst-pane.tsx +40 -67
- package/src/plugins/builtin/research/corporate-actions-pane.tsx +25 -19
- package/src/plugins/builtin/research/index.test.ts +6 -25
- package/src/plugins/builtin/research/index.tsx +11 -5
- package/src/plugins/builtin/research/relative-valuation-pane.tsx +86 -26
- package/src/plugins/builtin/scanner/denied.tsx +31 -0
- package/src/plugins/builtin/scanner/feed.ts +94 -0
- package/src/plugins/builtin/scanner/flow-model.test.ts +85 -0
- package/src/plugins/builtin/scanner/flow-model.ts +133 -0
- package/src/plugins/builtin/scanner/flow-pane.tsx +197 -0
- package/src/plugins/builtin/scanner/hilo-bars.tsx +121 -0
- package/src/plugins/builtin/scanner/hilo-model.test.ts +44 -0
- package/src/plugins/builtin/scanner/hilo-model.ts +75 -0
- package/src/plugins/builtin/scanner/hilo-pane.tsx +165 -0
- package/src/plugins/builtin/scanner/index.ts +109 -0
- package/src/plugins/builtin/scanner/waiting.tsx +11 -0
- package/src/plugins/builtin/sec/index.tsx +11 -7
- package/src/plugins/builtin/sectors/index.tsx +105 -81
- package/src/plugins/builtin/sectors/move-bar.tsx +43 -0
- package/src/plugins/builtin/sectors/sector-data.ts +18 -0
- package/src/plugins/builtin/sectors/sector-model.ts +13 -15
- package/src/plugins/builtin/shared/auto-refresh.ts +51 -0
- package/src/plugins/builtin/shared/cloud-upgrade.ts +29 -5
- package/src/plugins/builtin/shared/pane-footer.ts +16 -3
- package/src/plugins/builtin/shared/screener-live-quotes.test.ts +91 -0
- package/src/plugins/builtin/shared/screener-live-quotes.ts +134 -0
- package/src/plugins/builtin/shared/table-pane.ts +1 -5
- package/src/plugins/builtin/shared/use-quote-board.test.tsx +211 -0
- package/src/plugins/builtin/shared/use-quote-board.ts +195 -0
- package/src/plugins/builtin/short-interest/client.ts +124 -0
- package/src/plugins/builtin/short-interest/index.tsx +62 -0
- package/src/plugins/builtin/short-interest/model.ts +113 -0
- package/src/plugins/builtin/short-interest/pane.tsx +252 -0
- package/src/plugins/builtin/short-interest/types.ts +9 -0
- package/src/plugins/builtin/substack/article-detail.tsx +15 -1
- package/src/plugins/builtin/substack/pane-footer.ts +3 -22
- package/src/plugins/builtin/substack/pane.tsx +1 -2
- package/src/plugins/builtin/thirteenf/api.ts +4 -1
- package/src/plugins/builtin/thirteenf/model.ts +2 -0
- package/src/plugins/builtin/thirteenf/pane.tsx +6 -2
- package/src/plugins/builtin/ticker-detail/data-panes/historical-prices.tsx +10 -4
- package/src/plugins/builtin/ticker-detail/data-panes/provider-search.tsx +67 -11
- package/src/plugins/builtin/ticker-detail/financials/tab.tsx +15 -8
- package/src/plugins/builtin/ticker-detail/index.tsx +5 -21
- package/src/plugins/builtin/ticker-detail/overview/components.tsx +72 -32
- package/src/plugins/builtin/ticker-detail/overview-tab.tsx +0 -10
- package/src/plugins/builtin/ticker-detail/pane.tsx +4 -1
- package/src/plugins/builtin/ticker-detail/quote-monitor/card.test.tsx +70 -0
- package/src/plugins/builtin/ticker-detail/quote-monitor/card.tsx +34 -26
- package/src/plugins/builtin/ticker-detail/quote-monitor/index.tsx +96 -49
- package/src/plugins/builtin/ticker-detail/research-tabs.ts +32 -0
- package/src/plugins/builtin/ticker-research-backend-plugin.ts +2 -9
- package/src/plugins/builtin/ticker-research-plugin.ts +6 -0
- package/src/plugins/builtin/treasury-auctions/cache.test.ts +173 -0
- package/src/plugins/builtin/treasury-auctions/cache.ts +104 -0
- package/src/plugins/builtin/treasury-auctions/client.test.ts +181 -0
- package/src/plugins/builtin/treasury-auctions/client.ts +151 -0
- package/src/plugins/builtin/treasury-auctions/index.tsx +90 -0
- package/src/plugins/builtin/treasury-auctions/model.test.ts +147 -0
- package/src/plugins/builtin/treasury-auctions/model.ts +188 -0
- package/src/plugins/builtin/treasury-auctions/pane.test.tsx +170 -0
- package/src/plugins/builtin/treasury-auctions/pane.tsx +417 -0
- package/src/plugins/builtin/treasury-auctions/types.ts +59 -0
- package/src/plugins/builtin/tv/pane.tsx +67 -7
- package/src/plugins/builtin/tv/youtube-embed.test.ts +84 -0
- package/src/plugins/builtin/tv/youtube-embed.ts +173 -0
- package/src/plugins/builtin/tv/youtube-stream.ts +27 -2
- package/src/plugins/builtin/volatility/client.ts +87 -0
- package/src/plugins/builtin/volatility/index.test.tsx +107 -0
- package/src/plugins/builtin/volatility/index.tsx +193 -0
- package/src/plugins/builtin/volatility/model.test.ts +66 -0
- package/src/plugins/builtin/volatility/model.ts +113 -0
- package/src/plugins/builtin/world-indices/footer.ts +43 -23
- package/src/plugins/builtin/world-indices/index.tsx +44 -93
- package/src/plugins/builtin/world-indices/indices.ts +11 -2
- package/src/plugins/builtin/world-indices/model.ts +17 -24
- package/src/plugins/builtin/world-indices/pane.test.tsx +116 -0
- package/src/plugins/builtin/world-indices/table.tsx +82 -37
- package/src/plugins/builtin/yield-curve/index.tsx +16 -7
- package/src/plugins/builtin/yield-curve/treasury-data.test.ts +25 -0
- package/src/plugins/builtin/yield-curve/treasury-data.ts +16 -0
- package/src/plugins/catalog-backend.ts +8 -0
- package/src/plugins/catalog-ui.ts +8 -0
- package/src/plugins/event-bus.ts +1 -0
- package/src/plugins/ibkr/gateway/account-loaders.ts +1 -1
- package/src/plugins/ibkr/trade/tab/footer.ts +9 -23
- package/src/plugins/ibkr/trade/tab/index.tsx +1 -5
- package/src/plugins/ibkr/trading/pane-view.tsx +149 -87
- package/src/plugins/ibkr/trading/pane.tsx +2 -2
- package/src/plugins/ownership.ts +3 -0
- package/src/plugins/pane-manager/floating.ts +8 -2
- package/src/plugins/pane-manager.test.ts +13 -0
- package/src/plugins/prediction-markets/backend-plugin.ts +2 -0
- package/src/plugins/prediction-markets/capability.test.ts +281 -0
- package/src/plugins/prediction-markets/capability.ts +171 -0
- package/src/plugins/prediction-markets/chart.tsx +18 -26
- package/src/plugins/prediction-markets/columns.ts +3 -2
- package/src/plugins/prediction-markets/controller/catalog.ts +15 -12
- package/src/plugins/prediction-markets/controller/detail.ts +4 -0
- package/src/plugins/prediction-markets/controller/index.ts +0 -15
- package/src/plugins/prediction-markets/detail/book.tsx +3 -23
- package/src/plugins/prediction-markets/detail/overview.tsx +0 -18
- package/src/plugins/prediction-markets/detail/pane.tsx +36 -12
- package/src/plugins/prediction-markets/detail.test.tsx +1 -1
- package/src/plugins/prediction-markets/index.tsx +2 -0
- package/src/plugins/prediction-markets/metrics.ts +28 -18
- package/src/plugins/prediction-markets/navigation.ts +1 -0
- package/src/plugins/prediction-markets/pane.test.tsx +1 -1
- package/src/plugins/prediction-markets/pane.tsx +0 -1
- package/src/plugins/prediction-markets/services/fetch.ts +3 -2
- package/src/plugins/prediction-markets/services/kalshi/adapter.ts +82 -23
- package/src/plugins/prediction-markets/services/kalshi/normalize.ts +3 -1
- package/src/plugins/prediction-markets/services/kalshi/types.ts +14 -0
- package/src/plugins/prediction-markets/services/polymarket/adapter.ts +26 -14
- package/src/plugins/prediction-markets/services/polymarket/detail.ts +67 -18
- package/src/plugins/prediction-markets/test-helpers.tsx +0 -1
- package/src/plugins/prediction-markets/types.ts +3 -9
- package/src/plugins/registry/context.ts +6 -1
- package/src/plugins/registry/index.test.ts +2 -0
- package/src/plugins/registry/index.ts +53 -3
- package/src/plugins/runtime/context.tsx +4 -2
- package/src/plugins/runtime/index.tsx +9 -0
- package/src/remote/controller.test.ts +17 -4
- package/src/renderers/electrobun/bun/application-menu/index.ts +1 -1
- package/src/renderers/electrobun/bun/core-capabilities.ts +2 -2
- package/src/renderers/electrobun/bun/desktop/capability-bridge.test.ts +64 -0
- package/src/renderers/electrobun/bun/desktop/capability-bridge.ts +41 -7
- package/src/renderers/electrobun/bun/desktop/host-requests.ts +5 -2
- package/src/renderers/electrobun/bun/desktop/http-fetch.test.ts +33 -0
- package/src/renderers/electrobun/bun/desktop/http-fetch.ts +53 -6
- package/src/renderers/electrobun/bun/desktop/initialization.test.ts +27 -0
- package/src/renderers/electrobun/bun/desktop/initialization.ts +6 -1
- package/src/renderers/electrobun/bun/index.ts +1 -0
- package/src/renderers/electrobun/shared/protocol.ts +2 -0
- package/src/renderers/electrobun/view/app-services.ts +25 -2
- package/src/renderers/electrobun/view/build-assets.ts +5 -0
- package/src/renderers/electrobun/view/cli-pane-shot-entry.tsx +186 -65
- package/src/renderers/electrobun/view/cli-pane-shot-health.test.ts +25 -0
- package/src/renderers/electrobun/view/cli-pane-shot-health.ts +31 -0
- package/src/renderers/electrobun/view/data-table/index.tsx +10 -11
- package/src/renderers/electrobun/view/data-table/row.tsx +2 -0
- package/src/renderers/electrobun/view/desktop/controls.test.tsx +68 -0
- package/src/renderers/electrobun/view/desktop/controls.tsx +25 -1
- package/src/renderers/electrobun/view/host/box.test.tsx +80 -0
- package/src/renderers/electrobun/view/host/media-surface.tsx +70 -3
- package/src/renderers/electrobun/view/host/tabs.tsx +97 -5
- package/src/renderers/electrobun/view/host/text.tsx +5 -2
- package/src/renderers/electrobun/view/input-host.test.tsx +140 -0
- package/src/renderers/electrobun/view/input-host.tsx +4 -2
- package/src/renderers/electrobun/view/native-stubs/backend-rpc.ts +19 -1
- package/src/renderers/electrobun/view/native-stubs/broker-sync-native-loader.ts +7 -0
- package/src/renderers/electrobun/view/native-stubs/chart/kitty-support.ts +10 -0
- package/src/renderers/electrobun/view/remote/capability-invoker.test.ts +46 -0
- package/src/renderers/electrobun/view/remote/capability-invoker.ts +34 -13
- package/src/renderers/electrobun/view/remote/connection-health-backend.ts +19 -0
- package/src/renderers/electrobun/view/remote/connection-health.test.ts +44 -0
- package/src/renderers/electrobun/view/remote/connection-health.ts +49 -0
- package/src/renderers/electrobun/view/styles.css +5 -0
- package/src/renderers/opentui/dialog-host.tsx +279 -80
- package/src/renderers/opentui/host.tsx +15 -1
- package/src/renderers/opentui/terminal-media.test.ts +102 -0
- package/src/renderers/opentui/terminal-media.ts +142 -0
- package/src/renderers/opentui/test-utils.tsx +9 -6
- package/src/renderers/opentui/toast-host.tsx +136 -7
- package/src/renderers/opentui/ui-host.tsx +19 -3
- package/src/sources/provider-router/connection-health.test.ts +28 -0
- package/src/sources/provider-router/connection-health.ts +47 -0
- package/src/sources/provider-router/history.test.ts +53 -4
- package/src/sources/provider-router/history.ts +119 -14
- package/src/sources/provider-router/index.ts +12 -1
- package/src/sources/yahoo-finance/history.ts +2 -9
- package/src/sources/yahoo-finance/http.ts +1 -1
- package/src/sources/yahoo-finance/symbols.ts +1 -1
- package/src/sources/yahoo-finance/types.ts +19 -0
- package/src/state/app/context/index.test.tsx +101 -0
- package/src/state/app/context/index.tsx +10 -0
- package/src/state/config-save-scheduler.ts +1 -6
- package/src/state/persist-scheduler.test.ts +31 -0
- package/src/state/persist-scheduler.ts +21 -8
- package/src/sync/auth-transition.test.ts +36 -0
- package/src/sync/auth-transition.ts +22 -0
- package/src/sync/core-contributors.test.ts +58 -1
- package/src/sync/core-contributors.ts +39 -2
- package/src/sync/react.ts +9 -0
- package/src/test-support/plugin-runtime.ts +3 -0
- package/src/theme/font-scale.test.ts +14 -0
- package/src/theme/font-scale.ts +69 -0
- package/src/tickers/search/index.test.ts +284 -2
- package/src/tickers/search/index.ts +55 -18
- package/src/tickers/search/ranking.ts +143 -18
- package/src/tickers/search/types.ts +1 -0
- package/src/time-series/alignment.ts +15 -11
- package/src/time-series/field-catalog.ts +10 -0
- package/src/time-series/fundamentals.ts +19 -11
- package/src/time-series/hooks.ts +8 -2
- package/src/time-series/parsed-history-cache.ts +35 -0
- package/src/time-series/resolution.ts +10 -0
- package/src/time-series/resolve.test.ts +332 -3
- package/src/time-series/resolve.ts +225 -25
- package/src/time-series/spec-studies.test.ts +23 -0
- package/src/time-series/spec.ts +21 -2
- package/src/time-series/transforms.test.ts +76 -1
- package/src/time-series/transforms.ts +39 -8
- package/src/time-series/types.ts +9 -2
- package/src/time-series/use-chart-resolution.test.tsx +105 -1
- package/src/time-series/use-chart-resolution.ts +67 -13
- package/src/types/config.ts +30 -27
- package/src/types/media.ts +2 -0
- package/src/types/plugin.ts +8 -1
- package/src/ui/host.tsx +4 -0
- package/src/ui/pointer-drag.ts +12 -0
- package/src/ui/qr.test.ts +79 -0
- package/src/ui/qr.ts +82 -0
- package/src/updater/github-releases.test.ts +3 -3
- package/src/updater/github-releases.ts +1 -1
- package/src/updater.test.ts +117 -16
- package/src/updater.ts +32 -5
- package/src/utils/exchanges.ts +1 -0
- package/src/utils/external-url.test.ts +32 -0
- package/src/utils/external-url.ts +23 -0
- package/src/utils/sort-values.ts +31 -0
- package/src/utils/zoned-date-time.ts +67 -0
- package/src/components/onboarding/wizard-keyboard.ts +0 -306
- package/src/plugins/builtin/chart-composer/date-window-dialog.tsx +0 -120
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createThrottledFetch } from "../../../utils/throttled-fetch";
|
|
2
|
+
import { httpFetch } from "../../../utils/http-transport";
|
|
3
|
+
import type { VoteHubPoll } from "./types";
|
|
4
|
+
|
|
5
|
+
const BASE_URL = "https://api.votehub.com";
|
|
6
|
+
|
|
7
|
+
const VOTEHUB_FETCH = createThrottledFetch({
|
|
8
|
+
requestsPerMinute: 30,
|
|
9
|
+
maxRetries: 2,
|
|
10
|
+
timeoutMs: 12_000,
|
|
11
|
+
backoffBaseMs: 500,
|
|
12
|
+
dedupeGetRequests: true,
|
|
13
|
+
defaultHeaders: {
|
|
14
|
+
Accept: "application/json",
|
|
15
|
+
"User-Agent": "gloomberb-polls",
|
|
16
|
+
},
|
|
17
|
+
transport: httpFetch,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export function parseVoteHubPollsPayload(body: unknown): VoteHubPoll[] {
|
|
21
|
+
if (Array.isArray(body)) return body.filter(isVoteHubPoll);
|
|
22
|
+
if (body && typeof body === "object" && Array.isArray((body as { polls?: unknown }).polls)) {
|
|
23
|
+
return (body as { polls: unknown[] }).polls.filter(isVoteHubPoll);
|
|
24
|
+
}
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isVoteHubPoll(value: unknown): value is VoteHubPoll {
|
|
29
|
+
if (!value || typeof value !== "object") return false;
|
|
30
|
+
const poll = value as Record<string, unknown>;
|
|
31
|
+
return typeof poll.id === "string" && typeof poll.pollster === "string" && typeof poll.subject === "string";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function buildUrl(path: string, params?: Record<string, string | undefined>): string {
|
|
35
|
+
const url = new URL(`${BASE_URL}${path}`);
|
|
36
|
+
if (params) {
|
|
37
|
+
for (const [key, value] of Object.entries(params)) {
|
|
38
|
+
if (value) url.searchParams.set(key, value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return url.toString();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function fetchVoteHubPolls(params?: {
|
|
45
|
+
pollType?: string;
|
|
46
|
+
subject?: string;
|
|
47
|
+
}): Promise<VoteHubPoll[]> {
|
|
48
|
+
const url = buildUrl("/polls", {
|
|
49
|
+
poll_type: params?.pollType,
|
|
50
|
+
subject: params?.subject,
|
|
51
|
+
});
|
|
52
|
+
const response = await VOTEHUB_FETCH.fetch(url);
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
throw new Error(`VoteHub request failed (${response.status})`);
|
|
55
|
+
}
|
|
56
|
+
return parseVoteHubPollsPayload(await response.json());
|
|
57
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface StackSortPreference<C extends string> {
|
|
2
|
+
columnId: C;
|
|
3
|
+
direction: "asc" | "desc";
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Cycles a table's sort preference when a header is clicked: toggles direction
|
|
8
|
+
* on the active column, otherwise switches to the new column at its default
|
|
9
|
+
* direction.
|
|
10
|
+
*/
|
|
11
|
+
export function nextStackSortPreference<C extends string>(
|
|
12
|
+
current: StackSortPreference<C>,
|
|
13
|
+
columnId: C,
|
|
14
|
+
defaultDirection: "asc" | "desc" = "desc",
|
|
15
|
+
): StackSortPreference<C> {
|
|
16
|
+
if (current.columnId === columnId) {
|
|
17
|
+
return { columnId, direction: current.direction === "asc" ? "desc" : "asc" };
|
|
18
|
+
}
|
|
19
|
+
return { columnId, direction: defaultDirection };
|
|
20
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { GloomPlugin } from "../../../types/plugin";
|
|
2
|
+
import { PollsPane } from "./pane";
|
|
3
|
+
import { POLLS_PANE_ID, POLLS_PLUGIN_ID } from "./types";
|
|
4
|
+
|
|
5
|
+
export const pollsPlugin: GloomPlugin = {
|
|
6
|
+
id: POLLS_PLUGIN_ID,
|
|
7
|
+
name: "Polls",
|
|
8
|
+
version: "1.0.0",
|
|
9
|
+
description: "Political polls from VoteHub (CC BY 4.0)",
|
|
10
|
+
toggleable: true,
|
|
11
|
+
|
|
12
|
+
panes: [
|
|
13
|
+
{
|
|
14
|
+
id: POLLS_PANE_ID,
|
|
15
|
+
name: "Polls",
|
|
16
|
+
icon: "P",
|
|
17
|
+
component: PollsPane,
|
|
18
|
+
defaultPosition: "right",
|
|
19
|
+
defaultMode: "floating",
|
|
20
|
+
defaultFloatingSize: { width: 100, height: 32 },
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
|
|
24
|
+
paneTemplates: [
|
|
25
|
+
{
|
|
26
|
+
id: "polls-pane",
|
|
27
|
+
paneId: POLLS_PANE_ID,
|
|
28
|
+
label: "Polls",
|
|
29
|
+
description: "Browse VoteHub political polls by type — approval, favorability, generic ballot, Senate, governor, House — with trend charts, pollster breakdowns, search, and source links.",
|
|
30
|
+
keywords: ["polls", "votehub", "approval", "favorability", "generic", "ballot", "senate", "governor"],
|
|
31
|
+
shortcut: { prefix: "POLL" },
|
|
32
|
+
createInstance: () => ({ placement: "floating" }),
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { parseVoteHubPollsPayload } from "./client";
|
|
3
|
+
import {
|
|
4
|
+
computeMarginOfError,
|
|
5
|
+
computeMovingAverage,
|
|
6
|
+
computePollAverages,
|
|
7
|
+
computePollsterAverages,
|
|
8
|
+
computePollTrend,
|
|
9
|
+
filterPollRows,
|
|
10
|
+
normalizeVoteHubPoll,
|
|
11
|
+
parseSampleSize,
|
|
12
|
+
populationLabel,
|
|
13
|
+
sortPollRows,
|
|
14
|
+
summarizeAnswers,
|
|
15
|
+
} from "./normalize";
|
|
16
|
+
import type { PollRow, VoteHubPoll } from "./types";
|
|
17
|
+
|
|
18
|
+
function makePoll(overrides: Partial<VoteHubPoll> = {}): VoteHubPoll {
|
|
19
|
+
return {
|
|
20
|
+
id: "p1",
|
|
21
|
+
poll_type: "approval",
|
|
22
|
+
sample_size: 1000,
|
|
23
|
+
population: "rv",
|
|
24
|
+
url: null,
|
|
25
|
+
created_at: null,
|
|
26
|
+
start_date: "2026-01-01",
|
|
27
|
+
end_date: "2026-01-03",
|
|
28
|
+
pollster: "Test Pollster",
|
|
29
|
+
answers: [
|
|
30
|
+
{ choice: "Approve", pct: 45 },
|
|
31
|
+
{ choice: "Disapprove", pct: 50 },
|
|
32
|
+
],
|
|
33
|
+
seat_name: null,
|
|
34
|
+
sponsors: [],
|
|
35
|
+
internal: false,
|
|
36
|
+
partisan: null,
|
|
37
|
+
subject: "Donald Trump",
|
|
38
|
+
...overrides,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe("VoteHub normalize", () => {
|
|
43
|
+
test("accepts a bare poll array or a { polls } envelope", () => {
|
|
44
|
+
const poll = makePoll();
|
|
45
|
+
expect(parseVoteHubPollsPayload([poll])).toHaveLength(1);
|
|
46
|
+
expect(parseVoteHubPollsPayload({ polls: [poll] })).toHaveLength(1);
|
|
47
|
+
expect(parseVoteHubPollsPayload({ data: [poll] })).toHaveLength(0);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("summarizes a two-way result and lead", () => {
|
|
51
|
+
const summary = summarizeAnswers([
|
|
52
|
+
{ choice: "Disapprove", pct: 51 },
|
|
53
|
+
{ choice: "Approve", pct: 44 },
|
|
54
|
+
]);
|
|
55
|
+
expect(summary.leadChoice).toBe("Disapprove");
|
|
56
|
+
expect(summary.lead).toBeCloseTo(7);
|
|
57
|
+
expect(summary.result).toContain("Disapprove");
|
|
58
|
+
expect(summary.result).toContain("Approve");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("maps wire fields onto list rows including margin of error", () => {
|
|
62
|
+
const row = normalizeVoteHubPoll(makePoll({
|
|
63
|
+
poll_type: "generic-ballot",
|
|
64
|
+
sample_size: "1,500",
|
|
65
|
+
population: "lv",
|
|
66
|
+
answers: [
|
|
67
|
+
{ choice: "Dem", pct: 44.4 },
|
|
68
|
+
{ choice: "Rep", pct: 48.4 },
|
|
69
|
+
],
|
|
70
|
+
}));
|
|
71
|
+
expect(row.pollTypeLabel).toBe("Generic");
|
|
72
|
+
expect(row.population).toBe("Likely");
|
|
73
|
+
expect(row.sampleSize).toBe(1500);
|
|
74
|
+
expect(row.leadChoice).toBe("Rep");
|
|
75
|
+
expect(row.lead).toBeCloseTo(4);
|
|
76
|
+
expect(row.marginOfError).not.toBeNull();
|
|
77
|
+
expect(parseSampleSize("800")).toBe(800);
|
|
78
|
+
expect(populationLabel("rv")).toBe("Reg");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("sorts poll rows by date, pollster, or lead", () => {
|
|
82
|
+
const older = normalizeVoteHubPoll(makePoll({
|
|
83
|
+
id: "old",
|
|
84
|
+
start_date: "2026-07-01",
|
|
85
|
+
end_date: "2026-07-02",
|
|
86
|
+
pollster: "YouGov",
|
|
87
|
+
answers: [{ choice: "Approve", pct: 40 }, { choice: "Disapprove", pct: 50 }],
|
|
88
|
+
}));
|
|
89
|
+
const newer = normalizeVoteHubPoll(makePoll({
|
|
90
|
+
id: "new",
|
|
91
|
+
start_date: "2026-08-01",
|
|
92
|
+
end_date: "2026-08-10",
|
|
93
|
+
pollster: "Emerson",
|
|
94
|
+
answers: [{ choice: "Approve", pct: 55 }, { choice: "Disapprove", pct: 40 }],
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
expect(sortPollRows([older, newer]).map((row) => row.id)).toEqual(["new", "old"]);
|
|
98
|
+
expect(sortPollRows([older, newer], { columnId: "pollster", direction: "asc" }).map((row) => row.id))
|
|
99
|
+
.toEqual(["new", "old"]);
|
|
100
|
+
expect(sortPollRows([older, newer], { columnId: "result", direction: "desc" }).map((row) => row.id))
|
|
101
|
+
.toEqual(["new", "old"]);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe("computeMarginOfError", () => {
|
|
106
|
+
test("returns null for missing or invalid sample sizes", () => {
|
|
107
|
+
expect(computeMarginOfError(null)).toBeNull();
|
|
108
|
+
expect(computeMarginOfError(0)).toBeNull();
|
|
109
|
+
expect(computeMarginOfError(-5)).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("computes MoE decreasing with larger samples", () => {
|
|
113
|
+
const small = computeMarginOfError(100);
|
|
114
|
+
const large = computeMarginOfError(10_000);
|
|
115
|
+
expect(small).not.toBeNull();
|
|
116
|
+
expect(large).not.toBeNull();
|
|
117
|
+
expect(small!).toBeGreaterThan(large!);
|
|
118
|
+
// 0.98 / sqrt(1000) ≈ 3.1
|
|
119
|
+
expect(computeMarginOfError(1000)).toBeCloseTo(3.1, 0);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("filterPollRows", () => {
|
|
124
|
+
const rows = [
|
|
125
|
+
normalizeVoteHubPoll(makePoll({ id: "a", subject: "Donald Trump", pollster: "Ipsos" })),
|
|
126
|
+
normalizeVoteHubPoll(makePoll({ id: "b", subject: "Congress", pollster: "Gallup" })),
|
|
127
|
+
normalizeVoteHubPoll(makePoll({ id: "c", subject: "Donald Trump", pollster: "Emerson" })),
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
test("returns all rows for empty query", () => {
|
|
131
|
+
expect(filterPollRows(rows, "")).toHaveLength(3);
|
|
132
|
+
expect(filterPollRows(rows, " ")).toHaveLength(3);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("filters by subject case-insensitively", () => {
|
|
136
|
+
expect(filterPollRows(rows, "trump").map((r) => r.id)).toEqual(["a", "c"]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("filters by pollster case-insensitively", () => {
|
|
140
|
+
expect(filterPollRows(rows, "gallup").map((r) => r.id)).toEqual(["b"]);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("computePollTrend", () => {
|
|
145
|
+
const rows = [
|
|
146
|
+
normalizeVoteHubPoll(makePoll({ id: "t1", end_date: "2026-01-10", subject: "Donald Trump", answers: [{ choice: "Approve", pct: 45 }, { choice: "Disapprove", pct: 50 }] })),
|
|
147
|
+
normalizeVoteHubPoll(makePoll({ id: "t2", end_date: "2026-02-10", subject: "Donald Trump", answers: [{ choice: "Approve", pct: 42 }, { choice: "Disapprove", pct: 53 }] })),
|
|
148
|
+
normalizeVoteHubPoll(makePoll({ id: "t3", end_date: "2026-03-10", subject: "Congress", answers: [{ choice: "Approve", pct: 20 }, { choice: "Disapprove", pct: 70 }] })),
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
test("collects sorted time series for matching subject and choice", () => {
|
|
152
|
+
const trend = computePollTrend(rows, "Donald Trump", "Approve");
|
|
153
|
+
expect(trend).toHaveLength(2);
|
|
154
|
+
expect(trend[0]!.date).toBe("2026-01-10");
|
|
155
|
+
expect(trend[1]!.date).toBe("2026-02-10");
|
|
156
|
+
expect(trend[0]!.value).toBe(45);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("returns empty for non-matching subject", () => {
|
|
160
|
+
expect(computePollTrend(rows, "Unknown", "Approve")).toHaveLength(0);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("computeMovingAverage", () => {
|
|
165
|
+
const points = [
|
|
166
|
+
{ date: "2026-01-01", value: 40, pollster: "A" },
|
|
167
|
+
{ date: "2026-01-02", value: 50, pollster: "B" },
|
|
168
|
+
{ date: "2026-01-03", value: 60, pollster: "C" },
|
|
169
|
+
{ date: "2026-01-04", value: 30, pollster: "D" },
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
test("computes rolling mean with the given window", () => {
|
|
173
|
+
const ma = computeMovingAverage(points, 2);
|
|
174
|
+
expect(ma).toHaveLength(3);
|
|
175
|
+
expect(ma[0]!.value).toBe(45); // (40+50)/2
|
|
176
|
+
expect(ma[1]!.value).toBe(55); // (50+60)/2
|
|
177
|
+
expect(ma[2]!.value).toBe(45); // (60+30)/2
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("returns empty when fewer points than window", () => {
|
|
181
|
+
expect(computeMovingAverage(points, 5)).toHaveLength(0);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe("computePollsterAverages", () => {
|
|
186
|
+
const rows: PollRow[] = [
|
|
187
|
+
normalizeVoteHubPoll(makePoll({ id: "pa1", pollster: "Ipsos", sample_size: 1000, subject: "Donald Trump", answers: [{ choice: "Approve", pct: 45 }, { choice: "Disapprove", pct: 50 }] })),
|
|
188
|
+
normalizeVoteHubPoll(makePoll({ id: "pa2", pollster: "Ipsos", sample_size: 2000, subject: "Donald Trump", answers: [{ choice: "Approve", pct: 48 }, { choice: "Disapprove", pct: 47 }] })),
|
|
189
|
+
normalizeVoteHubPoll(makePoll({ id: "pa3", pollster: "Gallup", sample_size: 1000, subject: "Donald Trump", answers: [{ choice: "Approve", pct: 50 }, { choice: "Disapprove", pct: 45 }] })),
|
|
190
|
+
];
|
|
191
|
+
|
|
192
|
+
test("groups by pollster with sample-weighted average", () => {
|
|
193
|
+
const result = computePollsterAverages(rows, "Donald Trump", "Approve");
|
|
194
|
+
expect(result).toHaveLength(2);
|
|
195
|
+
const ipsos = result.find((r) => r.pollster === "Ipsos")!;
|
|
196
|
+
expect(ipsos.count).toBe(2);
|
|
197
|
+
expect(ipsos.totalSample).toBe(3000);
|
|
198
|
+
// (45*1000 + 48*2000) / 3000 = 147000/3000 = 47
|
|
199
|
+
expect(ipsos.avgPct).toBeCloseTo(47, 0);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("sorts by poll count descending", () => {
|
|
203
|
+
const result = computePollsterAverages(rows, "Donald Trump", "Approve");
|
|
204
|
+
expect(result[0]!.count).toBeGreaterThanOrEqual(result[1]!.count);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe("computePollAverages", () => {
|
|
209
|
+
const rows: PollRow[] = [
|
|
210
|
+
normalizeVoteHubPoll(makePoll({ id: "avg1", end_date: "2026-03-01", pollster: "Ipsos", sample_size: 1000, subject: "Donald Trump", answers: [{ choice: "Approve", pct: 44 }, { choice: "Disapprove", pct: 50 }] })),
|
|
211
|
+
normalizeVoteHubPoll(makePoll({ id: "avg2", end_date: "2026-02-01", pollster: "Gallup", sample_size: 2000, subject: "Donald Trump", answers: [{ choice: "Approve", pct: 46 }, { choice: "Disapprove", pct: 48 }] })),
|
|
212
|
+
];
|
|
213
|
+
|
|
214
|
+
test("computes sample-weighted average per choice across recent polls", () => {
|
|
215
|
+
const result = computePollAverages(rows, "Donald Trump", 10);
|
|
216
|
+
expect(result).toHaveLength(2);
|
|
217
|
+
expect(result[0]!.choice).toBe("Disapprove");
|
|
218
|
+
// Disapprove: (50*1000 + 48*2000) / 3000 = 146000/3000 ≈ 48.67
|
|
219
|
+
expect(result[0]!.avgPct).toBeCloseTo(48.67, 0);
|
|
220
|
+
expect(result[0]!.pollCount).toBe(2);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("respects recentCount limit", () => {
|
|
224
|
+
const result = computePollAverages(rows, "Donald Trump", 1);
|
|
225
|
+
// Only the most recent poll (avg1, end_date 2026-03-01)
|
|
226
|
+
const approve = result.find((r) => r.choice === "Approve")!;
|
|
227
|
+
expect(approve.pollCount).toBe(1);
|
|
228
|
+
expect(approve.avgPct).toBe(44);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PollAverageSummary,
|
|
3
|
+
PollRow,
|
|
4
|
+
PollTrendPoint,
|
|
5
|
+
PollsterAverage,
|
|
6
|
+
VoteHubPoll,
|
|
7
|
+
VoteHubPollAnswer,
|
|
8
|
+
} from "./types";
|
|
9
|
+
|
|
10
|
+
const POLL_TYPE_LABELS: Record<string, string> = {
|
|
11
|
+
approval: "Approval",
|
|
12
|
+
favorability: "Favorability",
|
|
13
|
+
"generic-ballot": "Generic",
|
|
14
|
+
"us-senator": "Senate",
|
|
15
|
+
governor: "Governor",
|
|
16
|
+
"us-representative": "House",
|
|
17
|
+
mayor: "Mayor",
|
|
18
|
+
"attorney-general": "AG",
|
|
19
|
+
"presidential-primary": "Primary",
|
|
20
|
+
"proposition-50": "Prop",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Readable sample populations; "A"/"RV"/"LV" meant nothing without a legend. */
|
|
24
|
+
const POPULATION_LABELS: Record<string, string> = {
|
|
25
|
+
a: "Adults",
|
|
26
|
+
rv: "Reg",
|
|
27
|
+
lv: "Likely",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function pollTypeLabel(pollType: string): string {
|
|
31
|
+
return POLL_TYPE_LABELS[pollType] ?? pollType.replace(/-/g, " ");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function populationLabel(population: string | null | undefined): string {
|
|
35
|
+
if (!population) return "—";
|
|
36
|
+
return POPULATION_LABELS[population.toLowerCase()] ?? population.toUpperCase();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function parseSampleSize(value: number | string | null | undefined): number | null {
|
|
40
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
41
|
+
if (typeof value === "string") {
|
|
42
|
+
const parsed = Number(value.replace(/,/g, ""));
|
|
43
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Approximate 95% confidence margin of error for a proportion at 50/50,
|
|
50
|
+
* derived from sample size: MoE ≈ 0.98 / sqrt(n).
|
|
51
|
+
* Returns null when sample size is missing or too small to be meaningful.
|
|
52
|
+
*/
|
|
53
|
+
export function computeMarginOfError(sampleSize: number | null): number | null {
|
|
54
|
+
if (sampleSize == null || sampleSize < 1) return null;
|
|
55
|
+
const moe = (0.98 / Math.sqrt(sampleSize)) * 100;
|
|
56
|
+
return Number.isFinite(moe) ? Math.round(moe * 10) / 10 : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function formatPollDate(value: string | null | undefined): string {
|
|
60
|
+
if (!value) return "—";
|
|
61
|
+
const date = new Date(`${value}T00:00:00Z`);
|
|
62
|
+
if (!Number.isFinite(date.getTime())) return value;
|
|
63
|
+
return date.toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function sortedAnswers(answers: VoteHubPollAnswer[] | undefined): VoteHubPollAnswer[] {
|
|
67
|
+
return [...(answers ?? [])]
|
|
68
|
+
.filter((answer) => typeof answer.choice === "string" && Number.isFinite(answer.pct))
|
|
69
|
+
.sort((left, right) => right.pct - left.pct);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function summarizeAnswers(answers: VoteHubPollAnswer[] | undefined): {
|
|
73
|
+
result: string;
|
|
74
|
+
lead: number | null;
|
|
75
|
+
leadChoice: string | null;
|
|
76
|
+
} {
|
|
77
|
+
const ranked = sortedAnswers(answers);
|
|
78
|
+
const first = ranked[0];
|
|
79
|
+
const second = ranked[1];
|
|
80
|
+
if (!first) return { result: "—", lead: null, leadChoice: null };
|
|
81
|
+
if (!second) {
|
|
82
|
+
return {
|
|
83
|
+
result: `${shortChoice(first.choice)} ${formatPct(first.pct)}`,
|
|
84
|
+
lead: first.pct,
|
|
85
|
+
leadChoice: first.choice,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const lead = first.pct - second.pct;
|
|
89
|
+
return {
|
|
90
|
+
result: `${shortChoice(first.choice)} ${formatPct(first.pct)} ${shortChoice(second.choice)} ${formatPct(second.pct)}`,
|
|
91
|
+
lead,
|
|
92
|
+
leadChoice: first.choice,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function shortChoice(choice: string): string {
|
|
97
|
+
const trimmed = choice.trim();
|
|
98
|
+
if (trimmed.length <= 10) return trimmed;
|
|
99
|
+
return trimmed.slice(0, 9);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function formatPct(value: number): string {
|
|
103
|
+
return Number.isInteger(value) ? String(value) : value.toFixed(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function normalizeVoteHubPoll(poll: VoteHubPoll): PollRow {
|
|
107
|
+
const summary = summarizeAnswers(poll.answers);
|
|
108
|
+
const sampleSize = parseSampleSize(poll.sample_size);
|
|
109
|
+
return {
|
|
110
|
+
id: poll.id,
|
|
111
|
+
subject: poll.subject,
|
|
112
|
+
pollType: poll.poll_type,
|
|
113
|
+
pollTypeLabel: pollTypeLabel(poll.poll_type),
|
|
114
|
+
pollster: poll.pollster,
|
|
115
|
+
population: populationLabel(poll.population),
|
|
116
|
+
sampleSize,
|
|
117
|
+
marginOfError: computeMarginOfError(sampleSize),
|
|
118
|
+
startDate: poll.start_date,
|
|
119
|
+
endDate: poll.end_date,
|
|
120
|
+
result: summary.result,
|
|
121
|
+
lead: summary.lead,
|
|
122
|
+
leadChoice: summary.leadChoice,
|
|
123
|
+
url: poll.url,
|
|
124
|
+
sponsors: Array.isArray(poll.sponsors) ? poll.sponsors.filter((sponsor) => typeof sponsor === "string") : [],
|
|
125
|
+
partisan: typeof poll.partisan === "string" ? poll.partisan : null,
|
|
126
|
+
internal: poll.internal === true,
|
|
127
|
+
answers: sortedAnswers(poll.answers),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export type PollSortColumnId = "date" | "subject" | "pollster" | "pop" | "result";
|
|
132
|
+
|
|
133
|
+
export interface PollSortPreference {
|
|
134
|
+
columnId: PollSortColumnId;
|
|
135
|
+
direction: "asc" | "desc";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const DEFAULT_POLL_SORT: PollSortPreference = { columnId: "date", direction: "desc" };
|
|
139
|
+
|
|
140
|
+
export function comparePollRows(
|
|
141
|
+
left: PollRow,
|
|
142
|
+
right: PollRow,
|
|
143
|
+
columnId: PollSortColumnId,
|
|
144
|
+
): number {
|
|
145
|
+
switch (columnId) {
|
|
146
|
+
case "date":
|
|
147
|
+
return dateValue(left.endDate ?? left.startDate) - dateValue(right.endDate ?? right.startDate);
|
|
148
|
+
case "subject":
|
|
149
|
+
return left.subject.localeCompare(right.subject);
|
|
150
|
+
case "pollster":
|
|
151
|
+
return left.pollster.localeCompare(right.pollster);
|
|
152
|
+
case "pop":
|
|
153
|
+
return left.population.localeCompare(right.population);
|
|
154
|
+
case "result":
|
|
155
|
+
return (left.lead ?? Number.NEGATIVE_INFINITY) - (right.lead ?? Number.NEGATIVE_INFINITY);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function sortPollRows(
|
|
160
|
+
rows: PollRow[],
|
|
161
|
+
preference: PollSortPreference = DEFAULT_POLL_SORT,
|
|
162
|
+
): PollRow[] {
|
|
163
|
+
const sign = preference.direction === "asc" ? 1 : -1;
|
|
164
|
+
return [...rows].sort((left, right) => {
|
|
165
|
+
const primary = comparePollRows(left, right, preference.columnId);
|
|
166
|
+
if (primary !== 0) return sign * primary;
|
|
167
|
+
return left.subject.localeCompare(right.subject);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function dateValue(value: string | null): number {
|
|
172
|
+
if (!value) return 0;
|
|
173
|
+
const time = new Date(`${value}T00:00:00Z`).getTime();
|
|
174
|
+
return Number.isFinite(time) ? time : 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Case-insensitive substring filter across subject, pollster, and seat name.
|
|
179
|
+
* Returns the original array when the query is empty.
|
|
180
|
+
*/
|
|
181
|
+
export function filterPollRows(rows: PollRow[], query: string): PollRow[] {
|
|
182
|
+
const trimmed = query.trim().toLowerCase();
|
|
183
|
+
if (!trimmed) return rows;
|
|
184
|
+
return rows.filter((row) =>
|
|
185
|
+
row.subject.toLowerCase().includes(trimmed) ||
|
|
186
|
+
row.pollster.toLowerCase().includes(trimmed) ||
|
|
187
|
+
row.pollTypeLabel.toLowerCase().includes(trimmed),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Collect a time series of percentage values for a specific choice across all
|
|
193
|
+
* polls sharing the same subject. Points are sorted chronologically by end date.
|
|
194
|
+
*/
|
|
195
|
+
export function computePollTrend(
|
|
196
|
+
rows: PollRow[],
|
|
197
|
+
subject: string,
|
|
198
|
+
choice: string,
|
|
199
|
+
): PollTrendPoint[] {
|
|
200
|
+
const target = choice.toLowerCase();
|
|
201
|
+
const points: PollTrendPoint[] = [];
|
|
202
|
+
for (const row of rows) {
|
|
203
|
+
if (row.subject !== subject) continue;
|
|
204
|
+
const answer = row.answers.find((a) => a.choice.toLowerCase() === target);
|
|
205
|
+
if (!answer) continue;
|
|
206
|
+
const date = row.endDate ?? row.startDate;
|
|
207
|
+
if (!date) continue;
|
|
208
|
+
points.push({ date, value: answer.pct, pollster: row.pollster });
|
|
209
|
+
}
|
|
210
|
+
return points.sort((a, b) => dateValue(a.date) - dateValue(b.date));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Simple moving average over a sorted trend series. Each output point is the
|
|
215
|
+
* mean of the current and preceding `window - 1` values. Returns an empty
|
|
216
|
+
* array when there are fewer than `window` points.
|
|
217
|
+
*/
|
|
218
|
+
export function computeMovingAverage(
|
|
219
|
+
points: PollTrendPoint[],
|
|
220
|
+
window: number,
|
|
221
|
+
): Array<{ date: string; value: number }> {
|
|
222
|
+
if (window < 1 || points.length < window) return [];
|
|
223
|
+
const result: Array<{ date: string; value: number }> = [];
|
|
224
|
+
for (let i = window - 1; i < points.length; i++) {
|
|
225
|
+
let sum = 0;
|
|
226
|
+
for (let j = i - window + 1; j <= i; j++) {
|
|
227
|
+
sum += points[j]!.value;
|
|
228
|
+
}
|
|
229
|
+
result.push({ date: points[i]!.date, value: sum / window });
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Group polls by pollster for a given subject and compute the sample-size-
|
|
236
|
+
* weighted average of the leading choice's percentage, poll count, total
|
|
237
|
+
* sample, and most recent end date. Sorted by poll count descending.
|
|
238
|
+
*/
|
|
239
|
+
export function computePollsterAverages(
|
|
240
|
+
rows: PollRow[],
|
|
241
|
+
subject: string,
|
|
242
|
+
choice: string | null,
|
|
243
|
+
): PollsterAverage[] {
|
|
244
|
+
const target = choice?.toLowerCase();
|
|
245
|
+
const byPollster = new Map<string, { sum: number; weight: number; count: number; lastDate: string | null }>();
|
|
246
|
+
for (const row of rows) {
|
|
247
|
+
if (row.subject !== subject) continue;
|
|
248
|
+
const answer = target
|
|
249
|
+
? row.answers.find((a) => a.choice.toLowerCase() === target)
|
|
250
|
+
: row.answers[0];
|
|
251
|
+
if (!answer) continue;
|
|
252
|
+
const weight = row.sampleSize ?? 0;
|
|
253
|
+
const entry = byPollster.get(row.pollster);
|
|
254
|
+
if (entry) {
|
|
255
|
+
entry.sum += answer.pct * weight;
|
|
256
|
+
entry.weight += weight;
|
|
257
|
+
entry.count += 1;
|
|
258
|
+
const d = row.endDate ?? row.startDate;
|
|
259
|
+
if (d && (!entry.lastDate || dateValue(d) > dateValue(entry.lastDate))) {
|
|
260
|
+
entry.lastDate = d;
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
byPollster.set(row.pollster, {
|
|
264
|
+
sum: answer.pct * weight,
|
|
265
|
+
weight,
|
|
266
|
+
count: 1,
|
|
267
|
+
lastDate: row.endDate ?? row.startDate,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return [...byPollster.entries()]
|
|
272
|
+
.map(([pollster, entry]) => ({
|
|
273
|
+
pollster,
|
|
274
|
+
count: entry.count,
|
|
275
|
+
avgPct: entry.weight > 0 ? entry.sum / entry.weight : entry.sum / entry.count,
|
|
276
|
+
totalSample: entry.weight,
|
|
277
|
+
lastDate: entry.lastDate,
|
|
278
|
+
}))
|
|
279
|
+
.sort((a, b) => b.count - a.count);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Compute a sample-size-weighted average for each choice across recent polls
|
|
284
|
+
* sharing a subject. `recentCount` limits to the most recent N polls by end
|
|
285
|
+
* date. Returns summaries sorted by average percentage descending.
|
|
286
|
+
*/
|
|
287
|
+
export function computePollAverages(
|
|
288
|
+
rows: PollRow[],
|
|
289
|
+
subject: string,
|
|
290
|
+
recentCount: number,
|
|
291
|
+
): PollAverageSummary[] {
|
|
292
|
+
const matching = rows
|
|
293
|
+
.filter((row) => row.subject === subject)
|
|
294
|
+
.sort((a, b) => dateValue(b.endDate ?? b.startDate) - dateValue(a.endDate ?? a.startDate))
|
|
295
|
+
.slice(0, Math.max(1, recentCount));
|
|
296
|
+
|
|
297
|
+
const byChoice = new Map<string, { sum: number; weight: number; count: number }>();
|
|
298
|
+
for (const row of matching) {
|
|
299
|
+
for (const answer of row.answers) {
|
|
300
|
+
const weight = row.sampleSize ?? 1;
|
|
301
|
+
const entry = byChoice.get(answer.choice);
|
|
302
|
+
if (entry) {
|
|
303
|
+
entry.sum += answer.pct * weight;
|
|
304
|
+
entry.weight += weight;
|
|
305
|
+
entry.count += 1;
|
|
306
|
+
} else {
|
|
307
|
+
byChoice.set(answer.choice, { sum: answer.pct * weight, weight, count: 1 });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return [...byChoice.entries()]
|
|
312
|
+
.map(([choice, entry]) => ({
|
|
313
|
+
choice,
|
|
314
|
+
avgPct: entry.weight > 0 ? entry.sum / entry.weight : entry.sum / entry.count,
|
|
315
|
+
pollCount: entry.count,
|
|
316
|
+
totalSample: entry.weight,
|
|
317
|
+
}))
|
|
318
|
+
.sort((a, b) => b.avgPct - a.avgPct);
|
|
319
|
+
}
|