-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathusage.go
83 lines (71 loc) · 2.92 KB
/
usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package db
import (
"context"
"fmt"
"github.com/google/uuid"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type Usage struct {
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"`
Description string `gorm:"column:description;type:varchar;size:255;" json:"description"`
CreditCents int64 `gorm:"column:creditCents;type:bigint;" json:"creditCents"`
EffectiveTime VarcharTime `gorm:"column:effectiveTime;type:varchar;size:255;" json:"effectiveTime"`
Kind string `gorm:"column:kind;type:char;size:10;" json:"kind"`
WorkspaceInstanceID uuid.UUID `gorm:"column:workspaceInstanceId;type:char;size:36;" json:"workspaceInstanceId"`
Draft bool `gorm:"column:draft;type:boolean;" json:"draft"`
Metadata datatypes.JSON `gorm:"column:metadata;type:text;size:65535" json:"metadata"`
}
type FindUsageResult struct {
UsageEntries []Usage
}
// TableName sets the insert table name for this struct type
func (u *Usage) TableName() string {
return "d_b_usage"
}
func InsertUsage(ctx context.Context, conn *gorm.DB, records ...Usage) error {
return conn.WithContext(ctx).
Clauses(clause.OnConflict{DoNothing: true}).
CreateInBatches(records, 1000).Error
}
func UpdateUsage(ctx context.Context, conn *gorm.DB, record Usage) error {
return conn.WithContext(ctx).Save(record).Error
}
func FindAllDraftUsage(ctx context.Context, conn *gorm.DB) ([]Usage, error) {
var usageRecords []Usage
var usageRecordsBatch []Usage
result := conn.WithContext(ctx).
Where("draft = TRUE").
Order("effectiveTime DESC").
FindInBatches(&usageRecordsBatch, 1000, func(_ *gorm.DB, _ int) error {
usageRecords = append(usageRecords, usageRecordsBatch...)
return nil
})
if result.Error != nil {
return nil, fmt.Errorf("failed to get usage records: %s", result.Error)
}
return usageRecords, nil
}
func FindUsage(ctx context.Context, conn *gorm.DB, attributionId AttributionID, from, to VarcharTime, offset int64, limit int64) ([]Usage, error) {
var usageRecords []Usage
var usageRecordsBatch []Usage
result := conn.WithContext(ctx).
Where("attributionId = ?", attributionId).
Where("? <= effectiveTime AND effectiveTime < ?", from.String(), to.String()).
Order("effectiveTime DESC").
Offset(int(offset)).
Limit(int(limit)).
FindInBatches(&usageRecordsBatch, 1000, func(_ *gorm.DB, _ int) error {
usageRecords = append(usageRecords, usageRecordsBatch...)
return nil
})
if result.Error != nil {
return nil, fmt.Errorf("failed to get usage records: %s", result.Error)
}
return usageRecords, nil
}