mirror of
https://github.com/harness/drone.git
synced 2025-05-19 02:20:03 +08:00
fix: [code-2912]: issue with pq sql (#3185)
* requested changes * fix issue with pqsql
This commit is contained in:
parent
db38802e83
commit
a178e629ea
@ -39,7 +39,7 @@ type SpaceStore interface {
|
||||
FindByIDs(ctx context.Context, spaceIDs ...int64) ([]*types.Space, error)
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
type MetricStore interface {
|
||||
UpsertOptimistic(ctx context.Context, in *types.UsageMetric) error
|
||||
GetMetrics(
|
||||
ctx context.Context,
|
||||
@ -66,8 +66,8 @@ type Mediator struct {
|
||||
spaces map[string]Size
|
||||
workers []*worker
|
||||
|
||||
spaceStore SpaceStore
|
||||
usageMetricsStore Store
|
||||
spaceStore SpaceStore
|
||||
metricsStore MetricStore
|
||||
|
||||
wg sync.WaitGroup
|
||||
|
||||
@ -77,17 +77,17 @@ type Mediator struct {
|
||||
func NewMediator(
|
||||
ctx context.Context,
|
||||
spaceStore SpaceStore,
|
||||
usageMetricsStore Store,
|
||||
usageMetricsStore MetricStore,
|
||||
config Config,
|
||||
) *Mediator {
|
||||
m := &Mediator{
|
||||
queue: newQueue(),
|
||||
chunks: make(map[string]Size),
|
||||
spaces: make(map[string]Size),
|
||||
spaceStore: spaceStore,
|
||||
usageMetricsStore: usageMetricsStore,
|
||||
workers: make([]*worker, config.MaxWorkers),
|
||||
config: config,
|
||||
queue: newQueue(),
|
||||
chunks: make(map[string]Size),
|
||||
spaces: make(map[string]Size),
|
||||
spaceStore: spaceStore,
|
||||
metricsStore: usageMetricsStore,
|
||||
workers: make([]*worker, config.MaxWorkers),
|
||||
config: config,
|
||||
}
|
||||
|
||||
m.initialize(ctx)
|
||||
@ -133,7 +133,7 @@ func (m *Mediator) initialize(ctx context.Context) {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
metrics, err := m.usageMetricsStore.List(ctx, now.Add(-m.days30()).UnixMilli(), now.UnixMilli())
|
||||
metrics, err := m.metricsStore.List(ctx, now.Add(-m.days30()).UnixMilli(), now.UnixMilli())
|
||||
if err != nil {
|
||||
log.Ctx(ctx).Err(err).Msg("failed to list usage metrics")
|
||||
return
|
||||
@ -187,7 +187,7 @@ func (m *Mediator) process(ctx context.Context, payload *Metric) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = m.usageMetricsStore.UpsertOptimistic(ctx, &types.UsageMetric{
|
||||
if err = m.metricsStore.UpsertOptimistic(ctx, &types.UsageMetric{
|
||||
RootSpaceID: space.ID,
|
||||
Bandwidth: newSize.Bandwidth,
|
||||
Storage: newSize.Storage,
|
||||
@ -202,7 +202,7 @@ func (m *Mediator) process(ctx context.Context, payload *Metric) {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
metric, err := m.usageMetricsStore.GetMetrics(ctx, space.ID, now.Add(-m.days30()).UnixMilli(), now.UnixMilli())
|
||||
metric, err := m.metricsStore.GetMetrics(ctx, space.ID, now.Add(-m.days30()).UnixMilli(), now.UnixMilli())
|
||||
if err != nil {
|
||||
log.Ctx(ctx).Err(err).Msg("failed to get usage metrics")
|
||||
return
|
||||
|
@ -51,7 +51,7 @@ func (s *UsageMetricsStore) getVersion(
|
||||
SELECT
|
||||
usage_metric_version
|
||||
FROM usage_metrics
|
||||
WHERE usage_metric_space_id = ? AND usage_metric_date = ?
|
||||
WHERE usage_metric_space_id = $1 AND usage_metric_date = $2
|
||||
`
|
||||
var version int64
|
||||
err := s.db.QueryRowContext(ctx, sqlQuery, rootSpaceID, date).Scan(&version)
|
||||
@ -85,9 +85,9 @@ func (s *UsageMetricsStore) Upsert(ctx context.Context, in *types.UsageMetric) e
|
||||
SET
|
||||
usage_metric_version = EXCLUDED.usage_metric_version
|
||||
,usage_metric_updated = EXCLUDED.usage_metric_updated
|
||||
,usage_metric_bandwidth = usage_metric_bandwidth + EXCLUDED.usage_metric_bandwidth
|
||||
,usage_metric_storage = usage_metric_storage + EXCLUDED.usage_metric_storage
|
||||
WHERE usage_metric_version = EXCLUDED.usage_metric_version - 1`
|
||||
,usage_metric_bandwidth = usage_metrics.usage_metric_bandwidth + EXCLUDED.usage_metric_bandwidth
|
||||
,usage_metric_storage = usage_metrics.usage_metric_storage + EXCLUDED.usage_metric_storage
|
||||
WHERE usage_metrics.usage_metric_version = EXCLUDED.usage_metric_version - 1`
|
||||
|
||||
db := dbtx.GetAccessor(ctx, s.db)
|
||||
today := s.Date(time.Now())
|
||||
@ -146,8 +146,8 @@ func (s *UsageMetricsStore) GetMetrics(
|
||||
COALESCE(SUM(usage_metric_storage), 0) AS usage_metric_storage
|
||||
FROM usage_metrics
|
||||
WHERE
|
||||
usage_metric_space_id = ? AND
|
||||
usage_metric_date BETWEEN ? AND ?`
|
||||
usage_metric_space_id = $1 AND
|
||||
usage_metric_date BETWEEN $2 AND $3`
|
||||
|
||||
result := &types.UsageMetric{
|
||||
RootSpaceID: rootSpaceID,
|
||||
@ -185,7 +185,7 @@ func (s *UsageMetricsStore) List(
|
||||
COALESCE(SUM(usage_metric_storage), 0) AS usage_metric_storage
|
||||
FROM usage_metrics
|
||||
WHERE
|
||||
usage_metric_date BETWEEN ? AND ?
|
||||
usage_metric_date BETWEEN $1 AND $2
|
||||
GROUP BY usage_metric_space_id
|
||||
ORDER BY usage_metric_bandwidth DESC, usage_metric_storage DESC`
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user