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
130 changes: 130 additions & 0 deletions internal/database/queries.sql.go

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

50 changes: 50 additions & 0 deletions internal/database/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,41 @@ SELECT "user_handle"
FROM users
ORDER BY "user_handle" ASC LIMIT $1 OFFSET $2;

-- name: GetAllUsersWithCounts :many
SELECT
users."user_handle",
users."name",
COALESCE(project_counts.count, 0) AS project_count,
COALESCE(definition_counts.count, 0) AS definition_count,
COALESCE(instance_counts.count, 0) AS instance_count
FROM users
LEFT JOIN (
SELECT "owner", COUNT(DISTINCT "project_id") AS count
FROM projects
GROUP BY "owner"
) project_counts ON users."user_handle" = project_counts."owner"
LEFT JOIN (
SELECT "owner", COUNT(*) AS count
FROM definitions
GROUP BY "owner"
) definition_counts ON users."user_handle" = definition_counts."owner"
LEFT JOIN (
SELECT "owner", COUNT(*) AS count
FROM instances
GROUP BY "owner"
) instance_counts ON users."user_handle" = instance_counts."owner"
ORDER BY users."user_handle" ASC LIMIT $1 OFFSET $2;

-- name: CountProjectsByUser :one
SELECT COUNT(DISTINCT projects."project_id")
FROM projects
WHERE projects."owner" = $1;

-- name: CountDefinitionsByUser :one
SELECT COUNT(*)
FROM definitions
WHERE "owner" = $1;

-- name: GetUsersByProject :many
SELECT users."user_handle", users_projects."role"
FROM users JOIN users_projects
Expand Down Expand Up @@ -186,6 +221,11 @@ ORDER BY "owner" ASC, "project_handle" ASC LIMIT $1 OFFSET $2;
SELECT COUNT(*)
FROM projects;

-- name: CountProjectsUsingInstance :one
SELECT COUNT(*)
FROM projects
WHERE "instance_id" = $1;

-- name: LinkProjectToUser :one
INSERT
INTO users_projects (
Expand Down Expand Up @@ -305,6 +345,11 @@ WHERE definitions."owner" = $1
ORDER BY definitions."owner" ASC, definitions."definition_handle" ASC
LIMIT $2 OFFSET $3;

-- name: CountInstancesByDefinition :one
SELECT COUNT(*)
FROM instances
WHERE "definition_id" = $1;


-- === LLM Service Instances (user-specific instances with optional API keys) ===

Expand Down Expand Up @@ -564,6 +609,11 @@ SELECT COUNT(*)
FROM instances
WHERE "owner" = $1;

-- name: CountSharedUsersForInstance :one
SELECT COUNT(*)
FROM instances_shared_with
WHERE "instance_id" = $1;


-- === EMBEDDINGS ===

Expand Down
35 changes: 28 additions & 7 deletions internal/handlers/llm_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,18 @@ func getUserDefinitionsFunc(ctx context.Context, input *models.GetUserDefinition
// Build response
ls := []models.DefinitionBrief{}
for _, d := range def {
// Count instances using this definition
instanceCount, err := queries.CountInstancesByDefinition(ctx, pgtype.Int4{Int32: d.DefinitionID, Valid: true})
if err != nil {
instanceCount = 0
}

ls = append(ls, models.DefinitionBrief{
Owner: d.Owner,
DefinitionHandle: d.DefinitionHandle,
DefinitionID: int(d.DefinitionID),
IsPublic: d.IsPublic,
Owner: d.Owner,
DefinitionHandle: d.DefinitionHandle,
DefinitionID: int(d.DefinitionID),
IsPublic: d.IsPublic,
NumberOfInstances: int(instanceCount),
})
}
response := &models.GetUserDefinitionsResponse{}
Expand Down Expand Up @@ -765,10 +772,24 @@ func getUserInstancesFunc(ctx context.Context, input *models.GetUserInstancesReq
// Build response (hide API keys for shared instances)
ls := []models.InstanceBrief{}
for _, llm := range llms {
// Count projects using this instance
projectCount, err := queries.CountProjectsUsingInstance(ctx, pgtype.Int4{Int32: llm.InstanceID, Valid: true})
if err != nil {
projectCount = 0
}

// Count shared users for this instance
sharedUserCount, err := queries.CountSharedUsersForInstance(ctx, llm.InstanceID)
if err != nil {
sharedUserCount = 0
}

ls = append(ls, models.InstanceBrief{
Owner: llm.Owner,
InstanceHandle: llm.InstanceHandle,
InstanceID: int(llm.InstanceID),
Owner: llm.Owner,
InstanceHandle: llm.InstanceHandle,
InstanceID: int(llm.InstanceID),
NumberOfProjects: int(projectCount),
NumberOfSharedUsers: int(sharedUserCount),
})
}
response := &models.GetUserInstancesResponse{}
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/llm_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestInstancesFunc(t *testing.T) {
requestPath: "/v1/llm-instances/alice",
bodyPath: "",
EmbAPIKey: options.AdminKey,
expectBody: "{\n \"$schema\": \"http://localhost:8080/schemas/GetUserInstancesResponseBody.json\",\n \"instances\": [\n {\n \"owner\": \"alice\",\n \"instance_handle\": \"embedding1\",\n \"instance_id\": 1\n }\n ]\n}\n",
expectBody: "{\n \"$schema\": \"http://localhost:8080/schemas/GetUserInstancesResponseBody.json\",\n \"instances\": [\n {\n \"owner\": \"alice\",\n \"instance_handle\": \"embedding1\",\n \"instance_id\": 1,\n \"number_of_projects\": 0,\n \"number_of_shared_users\": 0\n }\n ]\n}\n",
expectStatus: http.StatusOK,
},
{
Expand All @@ -114,7 +114,7 @@ func TestInstancesFunc(t *testing.T) {
requestPath: "/v1/llm-instances/alice",
bodyPath: "",
EmbAPIKey: aliceAPIKey,
expectBody: "{\n \"$schema\": \"http://localhost:8080/schemas/GetUserInstancesResponseBody.json\",\n \"instances\": [\n {\n \"owner\": \"alice\",\n \"instance_handle\": \"embedding1\",\n \"instance_id\": 1\n }\n ]\n}\n",
expectBody: "{\n \"$schema\": \"http://localhost:8080/schemas/GetUserInstancesResponseBody.json\",\n \"instances\": [\n {\n \"owner\": \"alice\",\n \"instance_handle\": \"embedding1\",\n \"instance_id\": 1,\n \"number_of_projects\": 0,\n \"number_of_shared_users\": 0\n }\n ]\n}\n",
expectStatus: http.StatusOK,
},
{
Expand Down
Loading