Skip to content

feat(scripts): sketch Edge Script statistics command#93

Merged
jamie-at-bunny merged 3 commits into
mainfrom
feat/scripts-stats
Jun 8, 2026
Merged

feat(scripts): sketch Edge Script statistics command#93
jamie-at-bunny merged 3 commits into
mainfrom
feat/scripts-stats

Conversation

@jamie-at-bunny

Copy link
Copy Markdown
Member

Wait until #91 lands

@changeset-bot

changeset-bot Bot commented Jun 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0077324

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@bunny.net/cli Minor
@bunny.net/cli-linux-x64 Minor
@bunny.net/cli-linux-arm64 Minor
@bunny.net/cli-darwin-x64 Minor
@bunny.net/cli-darwin-arm64 Minor
@bunny.net/cli-windows-x64 Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jamie-at-bunny jamie-at-bunny marked this pull request as ready for review June 8, 2026 07:56
@bogdan-at-bunny

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@greptile-apps

greptile-apps Bot commented Jun 8, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds a bunny scripts stats command that shows Edge Script request, CPU, and cost statistics over a date range, with an optional per-bucket bar chart. It also refactors the shared bar-chart and sum helpers from dns/zone/stats.ts into a new core/stats.ts module consumed by both the DNS and scripts stat commands.

  • core/stats.ts extracts sumChart, renderBarChart, and formatBucketLabel as reusable utilities, and a new test file covers all three; formatBucketLabel uses getUTCHours()/getUTCMinutes() with manual zero-padding to avoid the 24:00 midnight edge-case from toLocaleTimeString.
  • scripts/interactive.ts adds resolveScriptInteractive (explicit ID → linked manifest → interactive picker) and maybeLinkScript, following the same spinner/try-finally pattern as the DNS zone interactive resolver.
  • scripts/stats.ts wires them together; it guards the list-fetch spinner correctly with try/finally but uses the .finally() Promise method for the statistics fetch (flagged in a prior review thread).

Confidence Score: 5/5

Safe to merge; the new command is additive and the shared stat utilities are well-tested. The one open inconsistency (spinner cleanup style in stats.ts) is already tracked in a prior review thread.

All changed paths are additive. The refactored dns/zone/stats.ts is a drop-in swap of the local helpers for the shared equivalents with identical logic. The core/stats.ts helpers are covered by unit tests including the midnight edge-case. No data mutations, migrations, or auth boundaries are touched.

packages/cli/src/commands/scripts/stats.ts — spinner cleanup style is inconsistent with interactive.ts (prior thread); no new blockers here.

Important Files Changed

Filename Overview
packages/cli/src/core/stats.ts New shared module with sumChart, renderBarChart, and formatBucketLabel; midnight UTC fix applied correctly using getUTCHours()/getUTCMinutes() with manual zero-padding.
packages/cli/src/core/stats.test.ts Good test coverage for all three exported helpers; explicitly tests the midnight 00:00 case, an empty chart, and the bar-width invariant.
packages/cli/src/commands/scripts/interactive.ts New interactive resolver following established patterns; spinners correctly wrapped with try/finally; gracefully handles Ctrl+C and json-output mode.
packages/cli/src/commands/scripts/stats.ts New stats command; correctly delegates resolution and linking; the statistics-fetch spinner uses .finally() (inconsistent with interactive.ts try/finally style, already flagged in prior review thread).
packages/cli/src/commands/dns/zone/stats.ts Refactored to import sumChart and renderBarChart from core/stats.ts; local implementations removed cleanly; no behavioural changes.

Sequence Diagram

sequenceDiagram
    participant User
    participant stats as scripts/stats.ts
    participant interactive as interactive.ts
    participant manifest as loadManifest
    participant API as Compute API

    User->>stats: bunny scripts stats [id]
    stats->>interactive: resolveScriptInteractive(client, id, output)
    alt Explicit ID or linked manifest
        interactive->>manifest: loadManifest(SCRIPT_MANIFEST)
        manifest-->>interactive: id?
        interactive->>API: GET /compute/script/id
        API-->>interactive: EdgeScript
        interactive-->>stats: script, picked false
    else No ID and no manifest
        alt json output
            interactive-->>User: UserError
        else interactive
            interactive->>API: GET /compute/scripts
            API-->>interactive: EdgeScript[]
            interactive->>User: prompts select
            User-->>interactive: chosen script
            interactive-->>stats: script, picked true
        end
    end
    stats->>API: GET /compute/script/id/statistics
    API-->>stats: StatisticsData
    alt json output
        stats-->>User: JSON
    else text
        stats-->>User: formatKeyValue totals plus renderBarChart
    end
    alt picked true
        stats->>interactive: maybeLinkScript(script, link)
        interactive->>User: confirm link
        User-->>interactive: yes or no
        interactive->>manifest: saveManifest
    end
Loading

Reviews (2): Last reviewed commit: "apply code suggestions" | Re-trigger Greptile

Comment thread packages/cli/src/commands/scripts/stats.ts Outdated
Comment thread packages/cli/src/core/stats.ts Outdated
@jamie-at-bunny jamie-at-bunny merged commit 4b68307 into main Jun 8, 2026
1 check passed
@jamie-at-bunny jamie-at-bunny deleted the feat/scripts-stats branch June 8, 2026 09:37
@github-actions github-actions Bot mentioned this pull request Jun 8, 2026
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.

2 participants