Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions coderd/database/queries/chats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,7 @@ WHERE chats.id = deletable.id
-- snapshot collection. Uses updated_at so that long-running chats
-- still appear in each snapshot window while they are active.
SELECT
c.id, c.owner_id, c.created_at, c.updated_at, c.status,
c.id, c.owner_id, c.organization_id, c.created_at, c.updated_at, c.status,
(c.parent_chat_id IS NOT NULL)::bool AS has_parent,
c.root_chat_id, c.workspace_id,
c.mode, c.archived, c.last_model_config_id, c.client_type,
Expand Down Expand Up @@ -2280,9 +2280,8 @@ WHERE cm.created_at > @created_after
GROUP BY cm.chat_id;

-- name: GetChatModelConfigsForTelemetry :many
-- Returns all model configurations for telemetry snapshot collection.
-- deleted = false guarantees ai_provider_id is non-null, so INNER JOIN is safe.
SELECT cmc.id, ap.type::text AS provider, cmc.model, cmc.context_limit, cmc.enabled, cmc.is_default
SELECT cmc.id, ap.type::text AS provider, cmc.model, cmc.context_limit, cmc.enabled, cmc.is_default, cmc.organization_id
Comment thread
ethanndickson marked this conversation as resolved.
FROM chat_model_configs cmc
JOIN ai_providers ap ON ap.id = cmc.ai_provider_id
WHERE cmc.deleted = false;
Expand Down
29 changes: 17 additions & 12 deletions coderd/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ func ConvertChat(dbChat database.GetChatsUpdatedAfterRow) Chat {
c := Chat{
ID: dbChat.ID,
OwnerID: dbChat.OwnerID,
OrganizationID: dbChat.OrganizationID,
CreatedAt: dbChat.CreatedAt,
UpdatedAt: dbChat.UpdatedAt,
Status: string(dbChat.Status),
Expand Down Expand Up @@ -2319,12 +2320,13 @@ func ConvertChatMessageSummary(dbRow database.GetChatMessageSummariesPerChatRow)
// telemetry ChatModelConfig.
func ConvertChatModelConfig(dbRow database.GetChatModelConfigsForTelemetryRow) ChatModelConfig {
return ChatModelConfig{
ID: dbRow.ID,
Provider: dbRow.Provider,
Model: dbRow.Model,
ContextLimit: dbRow.ContextLimit,
Enabled: dbRow.Enabled,
IsDefault: dbRow.IsDefault,
ID: dbRow.ID,
OrganizationID: dbRow.OrganizationID,
Provider: dbRow.Provider,
Model: dbRow.Model,
ContextLimit: dbRow.ContextLimit,
Enabled: dbRow.Enabled,
IsDefault: dbRow.IsDefault,
}
}

Expand Down Expand Up @@ -2577,6 +2579,7 @@ type BoundaryUsageSummary struct {
type Chat struct {
ID uuid.UUID `json:"id"`
OwnerID uuid.UUID `json:"owner_id"`
OrganizationID uuid.UUID `json:"organization_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Status string `json:"status"`
Expand Down Expand Up @@ -2612,12 +2615,14 @@ type ChatMessageSummary struct {
// ChatModelConfig contains model configuration metadata for
// telemetry. Sensitive fields like API keys are excluded.
type ChatModelConfig struct {
ID uuid.UUID `json:"id"`
Provider string `json:"provider"`
Model string `json:"model"`
ContextLimit int64 `json:"context_limit"`
Enabled bool `json:"enabled"`
IsDefault bool `json:"is_default"`
ID uuid.UUID `json:"id"`
OrganizationID uuid.UUID `json:"organization_id"`
Comment thread
ethanndickson marked this conversation as resolved.
Provider string `json:"provider"`
Model string `json:"model"`
ContextLimit int64 `json:"context_limit"`
Enabled bool `json:"enabled"`
// Each organization has at most one default configuration.
Comment thread
ethanndickson marked this conversation as resolved.
IsDefault bool `json:"is_default"`
}

// ChatDiffStatusSummary contains aggregate PR counts across all
Expand Down
18 changes: 10 additions & 8 deletions coderd/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,14 +1648,15 @@ func TestChatsTelemetry(t *testing.T) {
ContextLimit: 200000,
})

// Create a second model config to test full dump.
org2 := dbgen.Organization(t, db, database.Organization{})
modelCfg2 := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{
AIProviderID: uuid.NullUUID{UUID: openaiProvider.ID, Valid: true},
Model: "gpt-4o",
DisplayName: "GPT-4o",
AIProviderID: uuid.NullUUID{UUID: openaiProvider.ID, Valid: true},
OrganizationID: org2.ID,
Model: "gpt-4o",
DisplayName: "GPT-4o",
})

// Create a soft-deleted model config — should NOT appear in telemetry.
// Soft-deleted model configurations must not appear in telemetry.
deletedCfg := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{
AIProviderID: uuid.NullUUID{UUID: anthropicProvider.ID, Valid: true},
Model: "claude-deleted",
Expand Down Expand Up @@ -1814,9 +1815,7 @@ func TestChatsTelemetry(t *testing.T) {
ProviderResponseID: sql.NullString{String: "resp-3", Valid: true},
})

// Insert a soft-deleted message on root chat with large token values.
// This acts as "poison" — if the deleted filter is missing, totals
// will be inflated and assertions below will fail.
// Large token values expose a missing soft-delete filter in the totals.
poisonMsg := dbgen.ChatMessage(t, db, database.ChatMessage{
ChatID: rootChat.ID,
ModelConfigID: uuid.NullUUID{UUID: modelCfg.ID, Valid: true},
Expand Down Expand Up @@ -1854,6 +1853,7 @@ func TestChatsTelemetry(t *testing.T) {
// Root chat assertions.
assert.Equal(t, rootChat.ID, foundRoot.ID)
assert.Equal(t, user.ID, foundRoot.OwnerID)
assert.Equal(t, org.ID, foundRoot.OrganizationID)
assert.Equal(t, "running", foundRoot.Status)
assert.False(t, foundRoot.HasParent)
assert.Nil(t, foundRoot.RootChatID)
Expand Down Expand Up @@ -1935,6 +1935,7 @@ func TestChatsTelemetry(t *testing.T) {

cfg1, ok := configMap[modelCfg.ID]
require.True(t, ok)
assert.Equal(t, org.ID, cfg1.OrganizationID)
Comment thread
ethanndickson marked this conversation as resolved.
assert.Equal(t, "anthropic", cfg1.Provider)
assert.Equal(t, "claude-sonnet-4-20250514", cfg1.Model)
assert.Equal(t, int64(200000), cfg1.ContextLimit)
Expand All @@ -1943,6 +1944,7 @@ func TestChatsTelemetry(t *testing.T) {

cfg2, ok := configMap[modelCfg2.ID]
require.True(t, ok)
assert.Equal(t, org2.ID, cfg2.OrganizationID)
assert.Equal(t, "openai", cfg2.Provider)
assert.Equal(t, "gpt-4o", cfg2.Model)
assert.Equal(t, int64(128000), cfg2.ContextLimit)
Expand Down
Loading