Skip to content

Enhance usage and spending tracking logic - #17

Open
genseric wants to merge 1 commit into
YossiSaadi:masterfrom
genseric:master
Open

Enhance usage and spending tracking logic#17
genseric wants to merge 1 commit into
YossiSaadi:masterfrom
genseric:master

Conversation

@genseric

Copy link
Copy Markdown

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.

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.
Copilot AI review requested due to automatic review settings November 19, 2025 18:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 fetchUsageSummary and fetchHardLimit API functions to retrieve on-demand usage data
  • Implemented resolveHardLimitDollars function 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.

Comment thread src/models.ts
Comment on lines +66 to +77
export interface UsageSummaryResponse {
billingCycleStart: string;
billingCycleEnd: string;
membershipType?: string;
limitType?: string;
isUnlimited?: boolean;
individualUsage?: {
plan?: UsageSummaryMetric;
onDemand?: UsageSummaryMetric;
};
teamUsage?: Record<string, unknown>;
}

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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 {

Copilot uses AI. Check for mistakes.
Comment thread src/models.ts
};
teamUsage?: Record<string, unknown>;
}

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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 {
Suggested change
/**
* Represents the hard limit response for on-demand spending.
* Returned by the /api/dashboard/get-hard-limit endpoint.
*/

Copilot uses AI. Check for mistakes.
Comment thread src/extension.ts
if (!isValidNumber(amount)) {
return undefined;
}
return Math.round(amount );

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
return Math.round(amount );
return Math.round(amount * CENTS_PER_DOLLAR);

Copilot uses AI. Check for mistakes.
Comment thread src/extension.ts
Comment on lines +301 to +304
hardLimitDollars = dollarsToCents(onDemandUsage.limit);
if (hardLimitDollars !== undefined) {
hardLimitDollars = hardLimitDollars / 100;
}

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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:

  1. Use hardLimitDollars = onDemandUsage.limit directly, or
  2. Fix the dollarsToCents function to properly multiply by 100, then keep this division

The current code will set hardLimitDollars to 1/100th of the intended value.

Suggested change
hardLimitDollars = dollarsToCents(onDemandUsage.limit);
if (hardLimitDollars !== undefined) {
hardLimitDollars = hardLimitDollars / 100;
}
hardLimitDollars = onDemandUsage.limit;

Copilot uses AI. Check for mistakes.
Comment thread src/extension.ts
// 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;

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

Inconsistent indentation: this line should be indented with 4 spaces to align with the surrounding code block, not 2 spaces.

Suggested change
let teamSpendData: SpendData | undefined;
let teamSpendData: SpendData | undefined;

Copilot uses AI. Check for mistakes.
Comment thread src/extension.ts
try {
const userDetails = await api.fetchTeamDetails(teamId, cookie);
const spendData = await api.fetchTeamSpend(teamId, cookie);
teamSpendData = await api.fetchTeamSpend(teamId, cookie);

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

Inconsistent indentation: this line should be indented with 6 spaces to align with the code inside the try block, not 4 spaces.

Suggested change
teamSpendData = await api.fetchTeamSpend(teamId, cookie);
teamSpendData = await api.fetchTeamSpend(teamId, cookie);

Copilot uses AI. Check for mistakes.
Comment thread src/extension.ts
teamSpendData = await api.fetchTeamSpend(teamId, cookie);

mySpend = spendData.teamMemberSpend.find(
mySpend = teamSpendData.teamMemberSpend.find(

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

Inconsistent indentation: this line should be indented with 6 spaces to align with the code inside the try block, not 4 spaces.

Suggested change
mySpend = teamSpendData.teamMemberSpend.find(
mySpend = teamSpendData.teamMemberSpend.find(

Copilot uses AI. Check for mistakes.
Comment thread src/extension.ts
import { SpendData, TeamMemberSpend } from "./models";

let refreshTimer: NodeJS.Timeout | undefined;
const CENTS_PER_DOLLAR = 100;

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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;

Copilot uses AI. Check for mistakes.
Comment thread src/models.ts
Comment on lines +52 to +56
interface UsageSummaryBreakdown {
included?: number;
bonus?: number;
total?: number;
}

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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 {

Copilot uses AI. Check for mistakes.
Comment thread src/models.ts
Comment on lines +58 to +64
export interface UsageSummaryMetric {
enabled: boolean;
used: number;
limit: number;
remaining: number;
breakdown?: UsageSummaryBreakdown;
}

Copilot AI Nov 19, 2025

Copy link

Choose a reason for hiding this comment

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

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 {

Copilot uses AI. Check for mistakes.
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