Enhance usage and spending tracking logic - #17
Conversation
Adds support for fetching and displaying aggregate usage summary and hard limit data from new API endpoints. Improves logic for resolving hard limit values, updates models to include new fields, and refines status bar display to show both requests and spending information. This provides more accurate and comprehensive usage reporting for both individual and team flows.
There was a problem hiding this comment.
Pull Request Overview
This PR enhances usage and spending tracking by integrating new API endpoints for fetching aggregate usage summaries and hard limits. It adds fallback logic to resolve hard limit values from multiple sources and updates the status bar to display both request usage and spending information more comprehensively.
Key changes:
- Added
fetchUsageSummaryandfetchHardLimitAPI functions to retrieve on-demand usage data - Implemented
resolveHardLimitDollarsfunction with prioritized fallback logic across member, team, and default hard limit values - Enhanced status bar display to show spending alongside request counts when available
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| src/api.ts | Added two new API functions to fetch usage summary and hard limit data |
| src/models.ts | Extended interfaces with new fields for hard limits and added models for usage summary and hard limit responses |
| src/extension.ts | Implemented hard limit resolution logic and integrated new API calls with fallback mechanism for missing data |
| src/statusBar.ts | Updated to accept and display used requests parameter, showing both usage and spending information in status bar |
Comments suppressed due to low confidence (1)
src/extension.ts:8
- Unused variable CENTS_PER_DOLLAR.
const CENTS_PER_DOLLAR = 100;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| export interface UsageSummaryResponse { | ||
| billingCycleStart: string; | ||
| billingCycleEnd: string; | ||
| membershipType?: string; | ||
| limitType?: string; | ||
| isUnlimited?: boolean; | ||
| individualUsage?: { | ||
| plan?: UsageSummaryMetric; | ||
| onDemand?: UsageSummaryMetric; | ||
| }; | ||
| teamUsage?: Record<string, unknown>; | ||
| } |
There was a problem hiding this comment.
Missing documentation comment. For consistency with other interfaces in this file, this interface should have a JSDoc comment explaining what it represents and which endpoint returns it. For example:
/**
* Represents the aggregate usage summary response.
* Returned by the /api/usage-summary endpoint.
*/
export interface UsageSummaryResponse {| }; | ||
| teamUsage?: Record<string, unknown>; | ||
| } | ||
|
|
There was a problem hiding this comment.
Missing documentation comment. For consistency with other interfaces in this file, this interface should have a JSDoc comment explaining what it represents and which endpoint returns it. For example:
/**
* Represents the hard limit response for on-demand spending.
* Returned by the /api/dashboard/get-hard-limit endpoint.
*/
export interface HardLimitResponse {| /** | |
| * Represents the hard limit response for on-demand spending. | |
| * Returned by the /api/dashboard/get-hard-limit endpoint. | |
| */ |
| if (!isValidNumber(amount)) { | ||
| return undefined; | ||
| } | ||
| return Math.round(amount ); |
There was a problem hiding this comment.
The dollarsToCents function is missing multiplication by 100. It should convert dollars to cents by multiplying the amount by 100 (or using the CENTS_PER_DOLLAR constant defined on line 8). Currently, it just rounds the dollar amount without conversion.
Example: dollarsToCents(1.50) should return 150, but currently returns 2.
| return Math.round(amount ); | |
| return Math.round(amount * CENTS_PER_DOLLAR); |
| hardLimitDollars = dollarsToCents(onDemandUsage.limit); | ||
| if (hardLimitDollars !== undefined) { | ||
| hardLimitDollars = hardLimitDollars / 100; | ||
| } |
There was a problem hiding this comment.
This logic is incorrect. dollarsToCents(onDemandUsage.limit) returns a value in cents, but then divides by 100 again on line 303. This results in hardLimitDollars being set to the wrong value (off by a factor of 100).
Since onDemandUsage.limit is already in dollars and we want hardLimitDollars in dollars, we should either:
- Use
hardLimitDollars = onDemandUsage.limitdirectly, or - Fix the
dollarsToCentsfunction to properly multiply by 100, then keep this division
The current code will set hardLimitDollars to 1/100th of the intended value.
| hardLimitDollars = dollarsToCents(onDemandUsage.limit); | |
| if (hardLimitDollars !== undefined) { | |
| hardLimitDollars = hardLimitDollars / 100; | |
| } | |
| hardLimitDollars = onDemandUsage.limit; |
| // TEAM FLOW: Try to get team-based usage data if user has related team from api/sets a team ID manually | ||
| // If we couldn't get team spend data, we'll show a simplified view with just the individual user data | ||
| if (teamId) { | ||
| let teamSpendData: SpendData | undefined; |
There was a problem hiding this comment.
Inconsistent indentation: this line should be indented with 4 spaces to align with the surrounding code block, not 2 spaces.
| let teamSpendData: SpendData | undefined; | |
| let teamSpendData: SpendData | undefined; |
| try { | ||
| const userDetails = await api.fetchTeamDetails(teamId, cookie); | ||
| const spendData = await api.fetchTeamSpend(teamId, cookie); | ||
| teamSpendData = await api.fetchTeamSpend(teamId, cookie); |
There was a problem hiding this comment.
Inconsistent indentation: this line should be indented with 6 spaces to align with the code inside the try block, not 4 spaces.
| teamSpendData = await api.fetchTeamSpend(teamId, cookie); | |
| teamSpendData = await api.fetchTeamSpend(teamId, cookie); |
| teamSpendData = await api.fetchTeamSpend(teamId, cookie); | ||
|
|
||
| mySpend = spendData.teamMemberSpend.find( | ||
| mySpend = teamSpendData.teamMemberSpend.find( |
There was a problem hiding this comment.
Inconsistent indentation: this line should be indented with 6 spaces to align with the code inside the try block, not 4 spaces.
| mySpend = teamSpendData.teamMemberSpend.find( | |
| mySpend = teamSpendData.teamMemberSpend.find( |
| import { SpendData, TeamMemberSpend } from "./models"; | ||
|
|
||
| let refreshTimer: NodeJS.Timeout | undefined; | ||
| const CENTS_PER_DOLLAR = 100; |
There was a problem hiding this comment.
The CENTS_PER_DOLLAR constant is defined but never used. It should be used in the dollarsToCents function (line 91) for the conversion: return Math.round(amount * CENTS_PER_DOLLAR); and could also be used in resolveHardLimitDollars (line 76) for clarity: return candidate / CENTS_PER_DOLLAR;
| interface UsageSummaryBreakdown { | ||
| included?: number; | ||
| bonus?: number; | ||
| total?: number; | ||
| } |
There was a problem hiding this comment.
Missing documentation comment. For consistency with other interfaces in this file, this interface should have a JSDoc comment explaining what it represents. For example:
/**
* Represents the breakdown of usage limits in a usage summary metric.
*/
interface UsageSummaryBreakdown {| export interface UsageSummaryMetric { | ||
| enabled: boolean; | ||
| used: number; | ||
| limit: number; | ||
| remaining: number; | ||
| breakdown?: UsageSummaryBreakdown; | ||
| } |
There was a problem hiding this comment.
Missing documentation comment. For consistency with other interfaces in this file, this interface should have a JSDoc comment explaining what it represents. For example:
/**
* Represents a metric with usage, limit, and remaining values.
*/
export interface UsageSummaryMetric {
Adds support for fetching and displaying aggregate usage summary and hard limit data from new API endpoints. Improves logic for resolving hard limit values, updates models to include new fields, and refines status bar display to show both requests and spending information. This provides more accurate and comprehensive usage reporting for both individual and team flows.