summaryrefslogtreecommitdiff
path: root/internal/controlplane/queries.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/controlplane/queries.go89
1 files changed, 83 insertions, 6 deletions
diff --git a/internal/controlplane/queries.go b/internal/controlplane/queries.go
index 732cb46..9b76fad 100644
--- a/internal/controlplane/queries.go
+++ b/internal/controlplane/queries.go
@@ -24,7 +24,18 @@ func (s *Store) Overview(ctx context.Context) (Overview, error) {
}
func (s *Store) ListTenants(ctx context.Context) ([]Tenant, error) {
- rows, err := s.db.Query(ctx, `SELECT id::text, slug, name, status, created_at FROM tenants ORDER BY created_at DESC`)
+ return s.ListTenantsFor(ctx, "")
+}
+
+func (s *Store) ListTenantsFor(ctx context.Context, tenantID string) ([]Tenant, error) {
+ query := `SELECT id::text, slug, name, status, created_at FROM tenants`
+ args := []any{}
+ if tenantID != "" {
+ query += ` WHERE id=$1`
+ args = append(args, tenantID)
+ }
+ query += ` ORDER BY created_at DESC`
+ rows, err := s.db.Query(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("query tenants: %w", err)
}
@@ -41,7 +52,18 @@ func (s *Store) ListTenants(ctx context.Context) ([]Tenant, error) {
}
func (s *Store) ListProjects(ctx context.Context) ([]Project, error) {
- rows, err := s.db.Query(ctx, `SELECT id::text, tenant_id::text, slug, name, status, created_at FROM projects ORDER BY created_at DESC`)
+ return s.ListProjectsFor(ctx, "")
+}
+
+func (s *Store) ListProjectsFor(ctx context.Context, tenantID string) ([]Project, error) {
+ query := `SELECT id::text, tenant_id::text, slug, name, status, created_at FROM projects`
+ args := []any{}
+ if tenantID != "" {
+ query += ` WHERE tenant_id=$1`
+ args = append(args, tenantID)
+ }
+ query += ` ORDER BY created_at DESC`
+ rows, err := s.db.Query(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("query projects: %w", err)
}
@@ -58,9 +80,20 @@ func (s *Store) ListProjects(ctx context.Context) ([]Project, error) {
}
func (s *Store) ListAPIKeys(ctx context.Context) ([]APIKey, error) {
- rows, err := s.db.Query(ctx, `
+ return s.ListAPIKeysFor(ctx, "")
+}
+
+func (s *Store) ListAPIKeysFor(ctx context.Context, tenantID string) ([]APIKey, error) {
+ query := `
SELECT id::text, tenant_id::text, project_id::text, name, key_prefix, scopes, status, created_at
- FROM api_keys ORDER BY created_at DESC`)
+ FROM api_keys`
+ args := []any{}
+ if tenantID != "" {
+ query += ` WHERE tenant_id=$1`
+ args = append(args, tenantID)
+ }
+ query += ` ORDER BY created_at DESC`
+ rows, err := s.db.Query(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("query API keys: %w", err)
}
@@ -80,6 +113,44 @@ func (s *Store) ListAPIKeys(ctx context.Context) ([]APIKey, error) {
return result, rows.Err()
}
+func (s *Store) OverviewFor(ctx context.Context, tenantID string) (Overview, error) {
+ if tenantID == "" {
+ return s.Overview(ctx)
+ }
+ var result Overview
+ err := s.db.QueryRow(ctx, `SELECT
+ (SELECT generation FROM control_state WHERE singleton=TRUE),
+ (SELECT count(*) FROM tenants WHERE id=$1 AND status='active'),
+ (SELECT count(*) FROM projects WHERE tenant_id=$1 AND status='active'),
+ (SELECT count(*) FROM api_keys WHERE tenant_id=$1 AND status='active'),
+ 0,
+ (SELECT count(*) FROM models WHERE enabled=TRUE)`, tenantID,
+ ).Scan(&result.Generation, &result.Tenants, &result.Projects, &result.APIKeys, &result.Providers, &result.Models)
+ if err != nil {
+ return Overview{}, fmt.Errorf("query tenant overview: %w", err)
+ }
+ return result, nil
+}
+
+func (s *Store) ResourceTenantID(ctx context.Context, resource, id string) (string, error) {
+ var query string
+ switch resource {
+ case "project":
+ query = `SELECT tenant_id::text FROM projects WHERE id=$1`
+ case "api_key":
+ query = `SELECT tenant_id::text FROM api_keys WHERE id=$1`
+ case "console_user":
+ query = `SELECT COALESCE(tenant_id::text,'') FROM console_users WHERE id=$1`
+ default:
+ return "", ErrNotFound
+ }
+ var tenantID string
+ if err := s.db.QueryRow(ctx, query, id).Scan(&tenantID); err != nil {
+ return "", ErrNotFound
+ }
+ return tenantID, nil
+}
+
func (s *Store) ListProviders(ctx context.Context) ([]Provider, error) {
rows, err := s.db.Query(ctx, `
SELECT p.id::text, p.name, p.protocol, p.base_url, p.enabled, count(r.id), p.created_at
@@ -101,7 +172,11 @@ func (s *Store) ListProviders(ctx context.Context) ([]Provider, error) {
}
func (s *Store) ListModels(ctx context.Context) ([]Model, error) {
- rows, err := s.db.Query(ctx, `SELECT id::text, public_id, owned_by, enabled, created_at FROM models ORDER BY public_id`)
+ rows, err := s.db.Query(ctx, `
+ SELECT id::text, public_id, owned_by, input_price_micros_per_million,
+ output_price_micros_per_million, cache_read_price_micros_per_million,
+ cache_write_price_micros_per_million, enabled, created_at
+ FROM models ORDER BY public_id`)
if err != nil {
return nil, fmt.Errorf("query models: %w", err)
}
@@ -109,7 +184,9 @@ func (s *Store) ListModels(ctx context.Context) ([]Model, error) {
positions := make(map[string]int)
for rows.Next() {
var item Model
- if err := rows.Scan(&item.ID, &item.PublicID, &item.OwnedBy, &item.Enabled, &item.CreatedAt); err != nil {
+ if err := rows.Scan(&item.ID, &item.PublicID, &item.OwnedBy, &item.InputPriceMicrosPerMillion,
+ &item.OutputPriceMicrosPerMillion, &item.CacheReadPriceMicrosPerMillion,
+ &item.CacheWritePriceMicrosPerMillion, &item.Enabled, &item.CreatedAt); err != nil {
rows.Close()
return nil, fmt.Errorf("scan model: %w", err)
}