Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
5.3 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: app_tool_call_quota.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const claimAppToolCallQuotaRejectionNotice = `-- name: ClaimAppToolCallQuotaRejectionNotice :one
INSERT INTO app_tool_call_quota_period (
    workspace_id, period_start, period_end, rejection_notified_at, rejection_notified_limits
) VALUES ($1, $2, $3, now(), ARRAY[$4::bigint])
ON CONFLICT (workspace_id, period_start, period_end) DO UPDATE
SET rejection_notified_at = now(),
    rejection_notified_limits = array_append(app_tool_call_quota_period.rejection_notified_limits, $4::bigint),
    updated_at = now()
WHERE app_tool_call_quota_period.rejection_notified_at IS NULL
   OR ($5::bool
       AND NOT ($4::bigint = ANY (app_tool_call_quota_period.rejection_notified_limits)))
RETURNING used_count
`

type ClaimAppToolCallQuotaRejectionNoticeParams struct {
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	PeriodStart pgtype.Timestamptz `json:"period_start"`
	PeriodEnd   pgtype.Timestamptz `json:"period_end"`
	LimitValue  int64              `json:"limit_value"`
	PerLimit    bool               `json:"per_limit"`
}

// Marks the period's limit notice as sent and returns a row only to the one
// caller that should send it: the first rejection of the period, or with
// per_limit the first at a limit not yet notified in the period. Run it in the
// transaction that writes the Inbox items, so a failed delivery leaves the
// marker unset for the next rejection.
func (q *Queries) ClaimAppToolCallQuotaRejectionNotice(ctx context.Context, arg ClaimAppToolCallQuotaRejectionNoticeParams) (int64, error) {
	row := q.db.QueryRow(ctx, claimAppToolCallQuotaRejectionNotice,
		arg.WorkspaceID,
		arg.PeriodStart,
		arg.PeriodEnd,
		arg.LimitValue,
		arg.PerLimit,
	)
	var used_count int64
	err := row.Scan(&used_count)
	return used_count, err
}

const getAppToolCallQuotaUsed = `-- name: GetAppToolCallQuotaUsed :one
SELECT used_count FROM app_tool_call_quota_period
WHERE workspace_id = $1 AND period_start = $2 AND period_end = $3
`

type GetAppToolCallQuotaUsedParams struct {
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	PeriodStart pgtype.Timestamptz `json:"period_start"`
	PeriodEnd   pgtype.Timestamptz `json:"period_end"`
}

func (q *Queries) GetAppToolCallQuotaUsed(ctx context.Context, arg GetAppToolCallQuotaUsedParams) (int64, error) {
	row := q.db.QueryRow(ctx, getAppToolCallQuotaUsed, arg.WorkspaceID, arg.PeriodStart, arg.PeriodEnd)
	var used_count int64
	err := row.Scan(&used_count)
	return used_count, err
}

const incrementAppToolCallQuota = `-- name: IncrementAppToolCallQuota :one
INSERT INTO app_tool_call_quota_period (workspace_id, period_start, period_end, used_count)
SELECT $1::uuid, $2::timestamptz, $3::timestamptz, 1
WHERE NOT $4::bool OR $5::bigint > 0
ON CONFLICT (workspace_id, period_start, period_end) DO UPDATE
SET used_count = app_tool_call_quota_period.used_count + 1,
    updated_at = now()
WHERE NOT $4::bool OR app_tool_call_quota_period.used_count < $5::bigint
RETURNING used_count
`

type IncrementAppToolCallQuotaParams struct {
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	PeriodStart pgtype.Timestamptz `json:"period_start"`
	PeriodEnd   pgtype.Timestamptz `json:"period_end"`
	Enforce     bool               `json:"enforce"`
	LimitValue  int64              `json:"limit_value"`
}

// Counts one app tool call against the workspace's period in one statement.
// Under enforce the row only moves while used_count is below the limit, so
// concurrent calls can never take a workspace past it; no row back means the
// call is refused. Under observe every call is counted.
func (q *Queries) IncrementAppToolCallQuota(ctx context.Context, arg IncrementAppToolCallQuotaParams) (int64, error) {
	row := q.db.QueryRow(ctx, incrementAppToolCallQuota,
		arg.WorkspaceID,
		arg.PeriodStart,
		arg.PeriodEnd,
		arg.Enforce,
		arg.LimitValue,
	)
	var used_count int64
	err := row.Scan(&used_count)
	return used_count, err
}

const listAppToolCallUsageByMember = `-- name: ListAppToolCallUsageByMember :many
SELECT triggered_by, count(*)::bigint AS calls
FROM app_tool_call
WHERE workspace_id = $1
  AND metered_period_start = $2::timestamptz
GROUP BY triggered_by
ORDER BY calls DESC
`

type ListAppToolCallUsageByMemberParams struct {
	WorkspaceID        pgtype.UUID        `json:"workspace_id"`
	MeteredPeriodStart pgtype.Timestamptz `json:"metered_period_start"`
}

type ListAppToolCallUsageByMemberRow struct {
	TriggeredBy pgtype.UUID `json:"triggered_by"`
	Calls       int64       `json:"calls"`
}

// Calls counted in a period, per person who started the run or approved the
// call. A NULL triggered_by is a run no person started.
func (q *Queries) ListAppToolCallUsageByMember(ctx context.Context, arg ListAppToolCallUsageByMemberParams) ([]ListAppToolCallUsageByMemberRow, error) {
	rows, err := q.db.Query(ctx, listAppToolCallUsageByMember, arg.WorkspaceID, arg.MeteredPeriodStart)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListAppToolCallUsageByMemberRow{}
	for rows.Next() {
		var i ListAppToolCallUsageByMemberRow
		if err := rows.Scan(&i.TriggeredBy, &i.Calls); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}