forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg_stats.go
More file actions
43 lines (35 loc) · 1.33 KB
/
org_stats.go
File metadata and controls
43 lines (35 loc) · 1.33 KB
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
package database
import (
"context"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/types"
)
type OrgStatsStore interface {
Upsert(ctx context.Context, orgID int32, codeHostRepoCount int32) (*types.OrgStats, error)
With(basestore.ShareableStore) OrgStatsStore
basestore.ShareableStore
}
type orgStatsStore struct {
*basestore.Store
}
// OrgStatsWith instantiates and returns a new OrgStatsStore using the other store handle.
func OrgStatsWith(other basestore.ShareableStore) OrgStatsStore {
return &orgStatsStore{Store: basestore.NewWithHandle(other.Handle())}
}
func (o *orgStatsStore) With(other basestore.ShareableStore) OrgStatsStore {
return &orgStatsStore{Store: o.Store.With(other)}
}
func (o *orgStatsStore) Upsert(ctx context.Context, orgID int32, codeHostRepoCount int32) (*types.OrgStats, error) {
newStatistic := &types.OrgStats{
OrgID: orgID,
CodeHostRepoCount: codeHostRepoCount,
}
err := o.Handle().QueryRowContext(
ctx,
"INSERT INTO org_stats(org_id, code_host_repo_count) VALUES($1, $2) ON CONFLICT (org_id) DO UPDATE SET code_host_repo_count = $2 RETURNING code_host_repo_count;",
newStatistic.OrgID, newStatistic.CodeHostRepoCount).Scan(&newStatistic.CodeHostRepoCount)
if err != nil {
return nil, err
}
return newStatistic, nil
}