fix: resolve settings/usage chart crash and add ClickHouse usage event seeds (#19039)

## Summary

- **Fix settings/usage page crash**: The `GraphWidgetLineChart`
component used on `settings/usage` was crashing with "Instance id is not
provided and cannot be found in context" because it requires
`WidgetComponentInstanceContext` (for tooltip/crosshair component
states) which is only provided inside the widget system. Wraps the
standalone chart usages with the required context provider.
- **Avoid mounting `GraphWidgetLegend` when hidden**: The legend
component calls `useIsPageLayoutInEditMode()` which requires
`PageLayoutEditModeProviderContext` — another context only available
inside the widget system. Since the settings page passes
`showLegend={false}`, the fix conditionally unmounts the legend instead
of always mounting it with a `show` prop. Applied consistently across
all four chart types (line, bar, pie, gauge).
- **Add ClickHouse usage event seeds**: Generates ~400 realistic
`usageEvent` rows spanning the past 35 days with weighted user activity,
weekday/weekend patterns, and gradual ramp-up. Enables developers to see
the usage analytics page with data locally.

## Test plan

- [ ] Navigate to `settings/usage` — page should render without errors
- [ ] Verify the daily usage line chart displays correctly
- [ ] Navigate to a user detail page from the usage list
- [ ] Verify the user detail chart renders without errors
- [ ] Run `npx nx clickhouse:seed twenty-server` and confirm usage
events are seeded
- [ ] Verify chart legend still works correctly on dashboard widgets (no
regression)


Made with [Cursor](https://cursor.com)
This commit is contained in:
Félix Malfait
2026-03-29 07:31:36 +02:00
committed by GitHub
parent 6efd9ad26e
commit a51b5ed589
54 changed files with 2072 additions and 1049 deletions
@@ -330,6 +330,7 @@ describe('AiBillingService', () => {
'gpt-4o',
{ usage: mockTokenUsage },
'workspace-1',
UsageOperationType.AI_CHAT_TOKEN,
'agent-id-123',
);
@@ -340,7 +341,7 @@ describe('AiBillingService', () => {
[
{
resourceType: UsageResourceType.AI,
operationType: UsageOperationType.AI_TOKEN,
operationType: UsageOperationType.AI_CHAT_TOKEN,
creditsUsedMicro: 7500,
quantity: 1500,
unit: UsageUnit.TOKEN,
@@ -55,6 +55,7 @@ export class AiBillingService {
modelId: ModelId,
billingInput: BillingUsageInput,
workspaceId: string,
operationType: UsageOperationType,
agentId?: string | null,
userWorkspaceId?: string | null,
): void {
@@ -73,6 +74,7 @@ export class AiBillingService {
creditsUsedMicro,
totalTokens,
modelId,
operationType,
agentId,
userWorkspaceId,
);
@@ -83,6 +85,7 @@ export class AiBillingService {
creditsUsedMicro: number,
totalTokens: number,
modelId: ModelId,
operationType: UsageOperationType,
agentId?: string | null,
userWorkspaceId?: string | null,
): void {
@@ -91,7 +94,7 @@ export class AiBillingService {
[
{
resourceType: UsageResourceType.AI,
operationType: UsageOperationType.AI_TOKEN,
operationType,
creditsUsedMicro,
quantity: totalTokens,
unit: UsageUnit.TOKEN,
@@ -13,6 +13,8 @@ import {
import { AppPath } from 'twenty-shared/types';
import { getAppPath, isDefined } from 'twenty-shared/utils';
import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
import { type CodeExecutionStreamEmitter } from 'src/engine/core-modules/tool-provider/interfaces/tool-provider.interface';
import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.service';
@@ -251,6 +253,7 @@ export class ChatExecutionService {
registeredModel.modelId,
{ usage, cacheCreationTokens },
workspace.id,
UsageOperationType.AI_CHAT_TOKEN,
null,
userWorkspaceId,
);