Skip to content

Add optional stats files to heartbeat export - #1604

Draft
wroug wants to merge 8 commits into
hackclub:mainfrom
wroug:add-stats
Draft

Add optional stats files to heartbeat export#1604
wroug wants to merge 8 commits into
hackclub:mainfrom
wroug:add-stats

Conversation

@wroug

@wroug wroug commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary of the problem

These changes are made to provide the user an easier way to get the data shown in graphs.

Describe your changes

This pull request adds the ability to include detailed statistics in the heartbeat data export, providing users with additional CSV and JSON files containing breakdowns of their coding activity. The main changes involve updating the export flow to accept a new include_stats parameter, generating various stats files, and enhancing the export job to bundle these files into the exported ZIP archive.

Export Flow Enhancements:

  • The export endpoints in my/heartbeats_controller.rb now accept an include_stats parameter, which is passed to the export job to trigger stats file generation.

Heartbeat Export Job Improvements:

  • The HeartbeatExportJob now accepts and handles the include_stats parameter, generating additional CSV and JSON statistics files (such as project durations, language stats, editor stats, weekly project stats, coding rhythm, etc.) and includes them in the ZIP archive if requested.
  • Several helper methods were added to generate CSV files for different statistics and to collect all stats into the export archive.
  • The job now uses output streams to write files directly into the ZIP archive, improving efficiency and reducing temporary file handling.

Statistics Generation:

  • A new method processed_export_snapshot was added to DashboardData::Snapshots to aggregate all relevant statistics for the export, including project, language, editor, OS, category breakdowns, weekly project stats, and coding rhythm.

Other Minor Changes:

  • Required the csv library in heartbeat_export_job.rb to support CSV generation.
  • Minor formatting improvements for export metadata.
  • A checkbox will be added on the export page to toggle including stats

These changes collectively allow users to optionally receive detailed, structured statistics about their coding activity alongside the standard heartbeat export.

Screenshots / Media

image

@wroug
wroug marked this pull request as draft August 19, 2026 20:10
@greptile-apps

greptile-apps Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds optional CSV and JSON statistics to heartbeat export ZIP archives.

  • Passes an include_stats option from the export controller to the background job.
  • Generates project, language, editor, operating-system, category, weekly-project and coding-rhythm statistics.
  • Streams archive entries directly into the ZIP before Active Storage upload and email delivery.

Confidence Score: 2/5

This PR should not merge until the stats option is reachable, historical weekly exports use the selected range and CSV labels are safely encoded.

The user-facing flow cannot enable the feature, historical exports generate incomplete weekly statistics and formula-prefixed heartbeat labels remain active in generated CSV cells.

Files Needing Attention: app/controllers/my/heartbeats_controller.rb, app/jobs/heartbeat_export_job.rb and app/services/dashboard_data/snapshots.rb

Security Review

The new CSV files place user-controlled heartbeat labels directly into spreadsheet cells. Formula-prefixed labels can therefore be interpreted as formulas when recipients open an export in common spreadsheet software.

Important Files Changed

Filename Overview
app/controllers/my/heartbeats_controller.rb Propagates the optional stats flag, but the existing user-facing forms never submit it.
app/jobs/heartbeat_export_job.rb Adds streamed statistics files to the ZIP, but raw labels remain formula-active in CSVs and the new branch lacks tests.
app/services/dashboard_data/snapshots.rb Adds export aggregation, but its weekly breakdown remains tied to the current dashboard window rather than the export range.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  U[Export request] --> C[Heartbeats controller]
  C -->|include_stats| J[HeartbeatExportJob]
  J --> H[Selected heartbeat scope]
  H --> JSON[Heartbeat JSON]
  H --> S[Processed export snapshot]
  S --> CSV[Statistics CSV files]
  S --> SJ[Statistics JSON]
  JSON --> ZIP[ZIP archive]
  CSV --> ZIP
  SJ --> ZIP
  ZIP --> B[Active Storage blob]
  B --> M[Email download link]
Loading
Prompt To Fix All With AI
### Issue 1
app/controllers/my/heartbeats_controller.rb:13
**Stats option is unreachable**

When a user requests either export from the settings page, neither form submits `include_stats`, so this comparison always evaluates false and the archive never contains the new stats files.

### Issue 2
app/services/dashboard_data/snapshots.rb:256-259
**Weekly stats use wrong range**

When an export includes heartbeats older than the current twelve-week dashboard window, `weekly_project_stats` intersects the export scope with that fixed window, causing the weekly CSV and JSON stats to omit heartbeats that are present in the same archive.

### Issue 3
app/jobs/heartbeat_export_job.rb:115-117
**CSV labels remain formula-active**

When a project, language, editor, operating-system or category label starts with `=`, `+`, `-` or `@`, the CSV writer emits it unchanged, so spreadsheet software interprets the user-controlled value as a formula instead of inert text. **How this was verified:** The ingestion path preserves these prefixes and the new writer passes the resulting grouping keys directly to Ruby CSV.

### Issue 4
app/jobs/heartbeat_export_job.rb:59-64
**Stats branch lacks coverage**

The existing controller and job tests never enable `include_stats`, leaving the new parameter handling, archive filenames and generated CSV/JSON contents outside regression coverage.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "Add optional stats files to heartbeat ex..." | Re-trigger Greptile

Comment thread app/controllers/my/heartbeats_controller.rb
Comment thread app/services/dashboard_data/snapshots.rb Outdated
Comment thread app/jobs/heartbeat_export_job.rb Outdated
Comment thread app/jobs/heartbeat_export_job.rb Outdated
wroug and others added 3 commits August 20, 2026 00:03
@wroug

wroug commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Current checklist of things until ready for review:

  • Add support for date range exporting
  • Add the client side toggle

#1606 Needs to be merged for this PR to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant