Improving AI Usage Tracking with Refactored Queries

Optimizing AI Token Usage Queries

We've recently refactored and optimized our AI token usage tracking to improve performance and maintainability. This involved extracting duplicated queries and enhancing user filtering.

The Changes

The primary focus was on improving the efficiency of retrieving daily and monthly tenant AI usage data. This was achieved through two key changes:

1. Introducing the AiTokenUsageService

To eliminate redundancy and promote code reuse, we extracted the duplicated daily and monthly tenant usage queries from the TenantResource into a dedicated AiTokenUsageService. This service now encapsulates the logic for querying AI token usage, providing a centralized and consistent interface.

Here's a simplified example of how the service might be used:

public class AiTokenUsageService {

    public List<UsageData> getDailyTenantUsage(String tenantId) {
        // Query database for daily tenant usage
        return queryUsageData(tenantId, "daily");
    }

    public List<UsageData> getMonthlyTenantUsage(String tenantId) {
        // Query database for monthly tenant usage
        return queryUsageData(tenantId, "monthly");
    }

    private List<UsageData> queryUsageData(String tenantId, String period) {
        // Generic query logic
        // Example: SELECT * FROM ai_usage WHERE tenant_id = ? AND period = ?
        return null; // Replace with actual query execution
    }
}

2. Enhancing User Filtering

The user filter in the TenantAiUsageResource was converted to a searchable select component. This change significantly improves performance, especially when dealing with a large number of users. Instead of loading all users upfront, the searchable select allows for efficient filtering and retrieval of relevant users based on search input.

Consider this example of how the searchable select might work in a front-end context:

// Example using a hypothetical search component
<SearchableSelect
  options={users}
  onSearch={(searchTerm) => {
    // Filter users based on searchTerm
    const filteredUsers = users.filter(user =>
      user.name.toLowerCase().includes(searchTerm.toLowerCase())
    );
    // Update the displayed options
    updateOptions(filteredUsers);
  }}
/>

Benefits

  • Improved Performance: The searchable select for user filtering reduces the initial load time and improves the responsiveness of the user interface.
  • Code Reusability: The AiTokenUsageService promotes code reuse and reduces redundancy, making the codebase more maintainable.
  • Centralized Logic: Encapsulating AI token usage queries in a dedicated service simplifies the management and modification of these queries.

Conclusion

By refactoring AI usage queries and improving user filtering, we've made significant strides in optimizing our AI token usage tracking. The AiTokenUsageService provides a centralized and reusable interface for querying usage data, while the searchable select component enhances the performance of user filtering. These changes contribute to a more efficient and maintainable system.

Gerardo Ruiz

Gerardo Ruiz

Author

Share: