Skip to content

Add the possibility to download statistics data as TSV or CSV - #953

Draft
giuscris wants to merge 3 commits into
2.xfrom
feature/statistics-download
Draft

giuscris wants to merge 3 commits into
2.xfrom
feature/statistics-download

Conversation

@giuscris

@giuscris giuscris commented Sep 6, 2026

Copy link
Copy Markdown
Member

This pull request introduces a new feature that allows users with the appropriate permission to export site statistics as CSV or TSV files. It adds a download endpoint, a CSV parser/encoder, and UI enhancements to support downloading statistics, as well as utility improvements for array handling.

New statistics export functionality:

  • Added a download action to StatisticsController that exports statistics data (devices, page views, sources, visits) as CSV or TSV files bundled in a ZIP archive, with permission checks and automatic file cleanup. (formwork/src/Panel/Controllers/StatisticsController.php, formwork/src/Panel/Controllers/StatisticsController.phpR46-R95)
  • Registered a new route panel.statistics.download for the download endpoint, supporting both CSV and TSV formats. (panel/config/routes/routes.php, panel/config/routes/routes.phpR177-R184)
  • Updated the statistics panel UI to include a download button (visible only to users with the correct permission) with options for CSV and TSV export. (panel/views/statistics/index.php, panel/views/statistics/index.phpR4-R21)

Core and utility enhancements:

  • Introduced a new Csv parser/encoder class supporting customizable separators and enclosures, used for encoding statistics data. (formwork/src/Parsers/Csv.php, formwork/src/Parsers/Csv.phpR1-R119)
  • Added a new Arr::zip utility method to combine multiple arrays element-wise, used for preparing tabular data for export. (formwork/src/Utils/Arr.php, formwork/src/Utils/Arr.phpR830-R865)
  • Enhanced the Statistics::getChartData method to accept a customizable label format for chart data export. (formwork/src/Statistics/Statistics.php, [1] [2]

These changes enable convenient, permission-protected export of site statistics for further analysis or reporting.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Address archive-open error handling, default TSV format normalization, and download-label translations.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Adds permission-protected CSV/TSV statistics exports packaged in a ZIP archive, with routing, UI controls, CSV encoding, and utility enhancements.

Changes:

  • Added the statistics download endpoint and route.
  • Added CSV/TSV encoding and ZIP generation.
  • Added download controls and customizable chart labels.
  • Added an element-wise array utility.
File Description
panel/​views/​statistics/​index.php Adds CSV/TSV download controls
panel/​config/​routes/​routes.php Registers the download route
formwork/​src/​Utils/​Arr.php Adds array zipping
formwork/​src/​Statistics/​Statistics.php Supports custom chart label formats
formwork/​src/​Parsers/​Csv.php Adds CSV/TSV parsing and encoding
formwork/​src/​Panel/​Controllers/​StatisticsController.php Generates and serves statistics archives

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

return $this->forward(ErrorsController::class, 'forbidden');
}

$format = $routeParams->get('format', 'tsv');
$file = FileSystem::joinPaths($this->config->getString('site.statistics.path'), sprintf('.export-%s.zip', FileSystem::randomName()));

$zipArchive = new ZipArchive();
$zipArchive->open($file, ZipArchive::CREATE);
Comment on lines +13 to +16
<button type="button" class="button button-accent dropdown-button caret" data-dropdown="dropdown-statistics-download"><?= $this->icon('cloud-download') ?> Download</button>
<div class="dropdown-menu" id="dropdown-statistics-download">
<a class="dropdown-item" href="<?= $panel->uri('/statistics/download/tsv/') ?>">Download TSV</a>
<a class="dropdown-item" href="<?= $panel->uri('/statistics/download/csv/') ?>">Download CSV</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants