Skip to content
This repository was archived by the owner on Oct 8, 2022. It is now read-only.

Commit bcde53e

Browse files
author
C
committed
Add codestats insertion
+ rebase on main
1 parent b09d274 commit bcde53e

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

benchttp/service.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package benchttp
22

3-
import "github.com/benchttp/worker/stats"
4-
53
// InsertService defines the interface to implement by a
64
// data layer facade.
75
type InsertionService interface {
86
// Create stores stats in database.
9-
Insert(stats.Stats, string, string, string) error
7+
Insert(Stats, string, string, string) error
108
}

benchttp/stats.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,18 @@ type StatsDescriptor struct {
1313
FinishedAt time.Time `json:"finishedAt"`
1414
}
1515

16-
// Codestats represents the code stats related to a computed stats group
17-
type Codestats struct {
18-
Code1xx int `json:"code1xx"`
19-
Code2xx int `json:"code2xx"`
20-
Code3xx int `json:"code3xx"`
21-
Code4xx int `json:"code4xx"`
22-
Code5xx int `json:"code5xx"`
23-
}
24-
25-
// Timestats represents the time stats related to a computed stats group
26-
type Timestats struct {
27-
Min float64 `json:"min"`
28-
Max float64 `json:"max"`
29-
Mean float64 `json:"mean"`
30-
Median float64 `json:"median"`
31-
StandardDeviation float64 `json:"standardDeviation"`
32-
Deciles []float64 `json:"deciles"`
33-
}
34-
3516
// Stats contains StatsDescriptor, Codestats and stats.Stats of a given computed stats group
3617
type Stats struct {
37-
Descriptor StatsDescriptor `json:"descriptor"`
38-
Code Codestats `json:"code"`
39-
Time stats.Stats `json:"time"`
18+
Descriptor StatsDescriptor `json:"descriptor"`
19+
Code stats.StatusDistribution `json:"code"`
20+
Time stats.Common `json:"time"`
21+
}
22+
23+
type Config struct {
24+
Host string
25+
User string
26+
Password string
27+
DBName string
28+
IdleConn int
29+
MaxConn int
4030
}

main.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/googleapis/google-cloudevents-go/cloud/firestore/v1"
1010

11+
"github.com/benchttp/worker/benchttp"
1112
"github.com/benchttp/worker/firestoreconv"
1213
"github.com/benchttp/worker/postgresql"
1314
"github.com/benchttp/worker/stats"
@@ -43,17 +44,8 @@ func Digest(ctx context.Context, e firestore.DocumentEventData) error {
4344
return nil
4445
}
4546

46-
type Config struct {
47-
Host string
48-
User string
49-
Password string
50-
DBName string
51-
IdleConn int
52-
MaxConn int
53-
}
54-
55-
func envConfig() (Config, error) {
56-
var config Config
47+
func envConfig() (benchttp.Config, error) {
48+
var config benchttp.Config
5749

5850
config.Host = os.Getenv("PSQL_HOST")
5951
if config.Host == "" {

postgresql/connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66

77
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres" // blank import
88

9-
"github.com/benchttp/worker"
9+
"github.com/benchttp/worker/benchttp"
1010
)
1111

1212
type InsertionService struct {
1313
db *sql.DB
1414
}
1515

16-
func NewInsertionService(config worker.Config) (InsertionService, error) {
16+
func NewInsertionService(config benchttp.Config) (InsertionService, error) {
1717
dbURI := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
1818
config.Host,
1919
config.User,

postgresql/stats.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package postgresql
33
import (
44
"github.com/lib/pq"
55

6-
"github.com/benchttp/worker/stats"
6+
"github.com/benchttp/worker/benchttp"
77
)
88

99
// nolint:gocognit
10-
func (s InsertionService) Insert(statsToStore stats.Stats, statsID, userID, tag string) error {
10+
func (s InsertionService) Insert(stats benchttp.Stats, statsID, userID, tag string) error {
1111
tx, err := s.db.Begin()
1212
if err != nil {
1313
return err
@@ -59,12 +59,43 @@ func (s InsertionService) Insert(statsToStore stats.Stats, statsID, userID, tag
5959

6060
if _, err = insertTimestats.Exec(
6161
statsID,
62-
statsToStore.Min,
63-
statsToStore.Max,
64-
statsToStore.Mean,
65-
statsToStore.Median,
66-
statsToStore.StdDev,
67-
pq.Array(statsToStore.Deciles),
62+
stats.Time.Min,
63+
stats.Time.Max,
64+
stats.Time.Mean,
65+
stats.Time.Median,
66+
stats.Time.StdDev,
67+
pq.Array(stats.Time.Deciles),
68+
); err != nil {
69+
if err := tx.Rollback(); err != nil {
70+
return err
71+
}
72+
return err
73+
}
74+
75+
insertCodestats, err := tx.Prepare(`
76+
INSERT INTO codestats(
77+
stats_descriptor_id,
78+
code_1xx,
79+
code_2xx,
80+
code_3xx,
81+
code_4xx,
82+
code_5xx
83+
) VALUES($1, $2, $3, $4, $5, $6)`)
84+
if err != nil {
85+
if err := tx.Rollback(); err != nil {
86+
return err
87+
}
88+
return err
89+
}
90+
defer insertCodestats.Close()
91+
92+
if _, err = insertCodestats.Exec(
93+
statsID,
94+
stats.Code.Status1xx,
95+
stats.Code.Status2xx,
96+
stats.Code.Status3xx,
97+
stats.Code.Status4xx,
98+
stats.Code.Status5xx,
6899
); err != nil {
69100
if err := tx.Rollback(); err != nil {
70101
return err

0 commit comments

Comments
 (0)