// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: promql_tags.sql

package lrdb

import (
	"context"

	"github.com/google/uuid"
)

const getMetricType = `-- name: GetMetricType :one
SELECT metric_type
FROM lrdb_exemplar_metrics
WHERE organization_id = $1
  AND metric_name = $2
ORDER BY 1
LIMIT 1
`

type GetMetricTypeParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	MetricName     string    `json:"metric_name"`
}

func (q *Queries) GetMetricType(ctx context.Context, arg GetMetricTypeParams) (string, error) {
	row := q.db.QueryRow(ctx, getMetricType, arg.OrganizationID, arg.MetricName)
	var metric_type string
	err := row.Scan(&metric_type)
	return metric_type, err
}

const listPromMetricTags = `-- name: ListPromMetricTags :many
SELECT DISTINCT key::text AS tag_key
FROM metric_seg,
     LATERAL jsonb_object_keys(label_name_map) AS key
WHERE organization_id = $1
  AND dateint >= $2
  AND dateint <= $3
  AND $4::BIGINT = ANY(fingerprints)
  AND label_name_map IS NOT NULL
ORDER BY tag_key
`

type ListPromMetricTagsParams struct {
	OrganizationID    uuid.UUID `json:"organization_id"`
	StartDateint      int32     `json:"start_dateint"`
	EndDateint        int32     `json:"end_dateint"`
	MetricFingerprint int64     `json:"metric_fingerprint"`
}

// Extract tag keys from label_name_map in metric_seg table for a specific metric
// Filters by metric fingerprint to return tags only for the requested metric
// Returns underscored tag keys (for v2 APIs)
// Legacy API uses denormalizer to convert to dotted names
// Includes today's and yesterday's dateint for partition pruning
func (q *Queries) ListPromMetricTags(ctx context.Context, arg ListPromMetricTagsParams) ([]string, error) {
	rows, err := q.db.Query(ctx, listPromMetricTags,
		arg.OrganizationID,
		arg.StartDateint,
		arg.EndDateint,
		arg.MetricFingerprint,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []string
	for rows.Next() {
		var tag_key string
		if err := rows.Scan(&tag_key); err != nil {
			return nil, err
		}
		items = append(items, tag_key)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listPromMetrics = `-- name: ListPromMetrics :many
SELECT DISTINCT
  metric_name,
  metric_type
FROM lrdb_exemplar_metrics
WHERE organization_id = $1
ORDER BY metric_name
`

type ListPromMetricsRow struct {
	MetricName string `json:"metric_name"`
	MetricType string `json:"metric_type"`
}

func (q *Queries) ListPromMetrics(ctx context.Context, organizationID uuid.UUID) ([]ListPromMetricsRow, error) {
	rows, err := q.db.Query(ctx, listPromMetrics, organizationID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListPromMetricsRow
	for rows.Next() {
		var i ListPromMetricsRow
		if err := rows.Scan(&i.MetricName, &i.MetricType); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
