package storage

import (
	"context"
	"time"
)

type TaskMetric struct {
	Timestamp       time.Time `json:"timestamp"`
	Service         string    `json:"service"`
	Environment     string    `json:"environment"`
	Queue           string    `json:"queue"`
	Success         bool      `json:"success"`
	ExecutionTimeMs int       `json:"execution_time_ms"`
}

func AddTaskMetric(metric TaskMetric) error {
	batch, err := connection().PrepareBatch(context.Background(), "INSERT INTO tasks")
	if err != nil {
		return err
	}

	err = batch.Append(
		metric.Timestamp,
		metric.Service,
		metric.Environment,
		metric.Queue,
		metric.Success,
		metric.ExecutionTimeMs,
	)
	if err != nil {
		return err
	}

	return batch.Send()
}
