Skip to content

Add script for generating API usage report#5197

Merged
IvanIvanoff merged 1 commit into
masterfrom
create-script-api-call-analysis
Jun 4, 2026
Merged

Add script for generating API usage report#5197
IvanIvanoff merged 1 commit into
masterfrom
create-script-api-call-analysis

Conversation

@IvanIvanoff
Copy link
Copy Markdown
Member

@IvanIvanoff IvanIvanoff commented Jun 3, 2026

Changes

Ticket

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have tried to find clearer solution before commenting hard-to-understand parts of code
  • I have added tests that prove my fix is effective or that my feature works

Summary by CodeRabbit

  • New Features

    • Added a runnable analysis that generates a CSV report of per-user API call counts (30/60/90 days), distinguishes standard API vs script-based calls, and includes each user's subscription plans labeled as active or expired.
  • Chores

    • Filters users by a minimum 90-day activity threshold and logs progress and written-row counts during execution.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3f2088e2-7f22-43de-b429-f291ded90221

📥 Commits

Reviewing files that changed from the base of the PR and between c9c243b and 2492cd8.

📒 Files selected for processing (1)
  • scripts/api_call_subscription_analysis.exs
🚧 Files skipped from review as they are similar to previous changes (1)
  • scripts/api_call_subscription_analysis.exs

📝 Walkthrough

Walkthrough

A new Elixir script builds api_call_subscription_analysis.csv by querying ClickHouse for per-user API-key call counts (30/60/90 days), fetching user emails and subscriptions from Postgres, labeling plans as "(active)" or "(expired)", and writing one CSV row per eligible user.

Changes

API Call Subscription Analysis Report

Layer / File(s) Summary
Module definition and orchestration
scripts/api_call_subscription_analysis.exs
Module header, CSV filename/header constants, and run/0 orchestration that fetches ClickHouse aggregates, resolves emails and subscriptions, builds CSV rows, and invokes the script at file end.
Data fetching implementations
scripts/api_call_subscription_analysis.exs
fetch_api_calls/0 runs a ClickHouse SQL aggregation for 30/60/90-day counts, splitting "sansheets" vs "api" by user_agent and filtering by a 90-day minimum; fetch_emails/1 fetches {id,email} via Ecto; fetch_subscriptions/1 joins subscriptions, plans, and products, filters for active/past_due or recently-ended (90d) records, and reduces into per-user/per-product plan labels with "(active)" or "(expired)".

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A rabbit hops in with ClickHouse and Postgres bright,
Counting calls by days and splitting sansheets from API light,
It finds each user's plans, tags active or expired neat,
Writes a CSV row per user—data tidy and complete,
Hooray for scripts that make analysis sprightly!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: a new script for generating an API usage report based on API call and subscription data analysis.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch create-script-api-call-analysis

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/api_call_subscription_analysis.exs`:
- Around line 131-137: The query condition combining s.status and
s.current_period_end is causing non-active statuses (e.g., :trialing or
canceled-but-not-expired) to be treated as active; remove the current_period_end
check from the status filter and instead determine "active/expired" after
fetching by comparing s.current_period_end to DateTime.utc_now(). Concretely:
change the from(...) where clause that currently uses "s.status in [:active,
:past_due] or s.current_period_end >= ^cutoff" to only filter by status (or only
by user_ids if you want all subs), and update the labeling logic to use
s.current_period_end >= DateTime.utc_now() (or the cutoff variable computed from
DateTime.utc_now()) to decide if a subscription is expired.
- Around line 137-143: The query in fetch_subscriptions/1 currently selects rows
with any status when s.current_period_end >= ^cutoff, but the label logic only
distinguishes active/past_due vs "expired", causing future or pending
subscriptions to be mislabeled; update the where clause to only include
subscriptions that have actually ended in the recent window (e.g., add
s.current_period_end < ^now or require status in [:canceled, :past_due]
depending on intent) or alternatively change the label logic to check the
timestamp (s.current_period_end < ^now) when deciding between " (expired)" and
other states; make this change inside fetch_subscriptions/1, referencing
s.current_period_end, cutoff/now and the status check to keep labels consistent
with the query.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6642743d-8679-4ce1-84b3-c251f611b1f7

📥 Commits

Reviewing files that changed from the base of the PR and between 33f9c58 and 032da9b.

📒 Files selected for processing (1)
  • scripts/api_call_subscription_analysis.exs

Comment thread scripts/api_call_subscription_analysis.exs Outdated
Comment thread scripts/api_call_subscription_analysis.exs Outdated
@IvanIvanoff IvanIvanoff force-pushed the create-script-api-call-analysis branch from 032da9b to c9c243b Compare June 4, 2026 07:27
@IvanIvanoff IvanIvanoff force-pushed the create-script-api-call-analysis branch from c9c243b to 2492cd8 Compare June 4, 2026 09:54
@IvanIvanoff IvanIvanoff merged commit b154bde into master Jun 4, 2026
2 of 3 checks passed
@IvanIvanoff IvanIvanoff deleted the create-script-api-call-analysis branch June 4, 2026 09:58
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