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

Commit 25a0da7

Browse files
author
C
committed
Add buildStats() method, call NewInsertService() in main.go
+ slight refactor of Insert() method to use less parameters
1 parent bcde53e commit 25a0da7

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

benchttp/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ package benchttp
44
// data layer facade.
55
type InsertionService interface {
66
// Create stores stats in database.
7-
Insert(Stats, string, string, string) error
7+
Insert(Stats) error
88
}

benchttp/stats.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// StatsDescriptor contains a computed stats group description information
1010
type StatsDescriptor struct {
1111
ID string `json:"id"`
12+
UserID string `json:"userID"`
1213
Tag string `json:"tag"`
1314
FinishedAt time.Time `json:"finishedAt"`
1415
}

cmd/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/benchttp/worker"
8+
"github.com/benchttp/worker/benchttp"
9+
"github.com/benchttp/worker/postgresql"
10+
"github.com/benchttp/worker/stats"
11+
)
12+
13+
func main() {
14+
config, err := worker.EnvConfig()
15+
if err != nil {
16+
fmt.Println("error getting env var")
17+
}
18+
fmt.Println(config)
19+
s, err := postgresql.NewInsertionService(config)
20+
if err != nil {
21+
fmt.Println("error creating service")
22+
}
23+
24+
// set timezone,
25+
now := time.Now()
26+
27+
fmt.Println(now)
28+
29+
deciles := [9]float64{5, 5, 5, 5, 5, 5, 5, 5, 5}
30+
31+
so := stats.Common{Min: 5, Max: 5, Mean: 5, Median: 5, StdDev: 5, Deciles: deciles}
32+
33+
c := stats.StatusDistribution{Status1xx: 10, Status2xx: 10, Status3xx: 10, Status4xx: 10, Status5xx: 10}
34+
35+
st := benchttp.Stats{Descriptor: benchttp.StatsDescriptor{UserID: "1", Tag: "test", ID: "test-fin", FinishedAt: now}, Code: c, Time: so}
36+
37+
// between each test, increment args number 3 and 5 (descriptor_id and tag, because they must be unique)
38+
err = s.Insert(st)
39+
if err != nil {
40+
fmt.Println("error creating")
41+
}
42+
}

main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"log"
77
"os"
8+
"time"
89

910
"github.com/googleapis/google-cloudevents-go/cloud/firestore/v1"
1011

@@ -23,7 +24,13 @@ func Digest(ctx context.Context, e firestore.DocumentEventData) error {
2324
}
2425

2526
cfg, err := envConfig()
27+
if err != nil {
28+
return err
29+
}
2630
insertionService, err := postgresql.NewInsertionService(cfg)
31+
if err != nil {
32+
return err
33+
}
2734

2835
codes, times := r.Benchmark.Values()
2936

@@ -41,6 +48,13 @@ func Digest(ctx context.Context, e firestore.DocumentEventData) error {
4148

4249
log.Printf("codestats: %+v", codestats)
4350

51+
// TO DO: get user id. Using "1" here for the moment.
52+
statsToInsert := buildStats(timestats, codestats, "firestore_id", "1")
53+
54+
if err := insertionService.Insert(statsToInsert); err != nil {
55+
return err
56+
}
57+
4458
return nil
4559
}
4660

@@ -72,3 +86,16 @@ func envConfig() (benchttp.Config, error) {
7286

7387
return config, nil
7488
}
89+
90+
func buildStats(timestats stats.Common, codestats stats.StatusDistribution, reportID, userID string) benchttp.Stats {
91+
stats := benchttp.Stats{
92+
Descriptor: benchttp.StatsDescriptor{
93+
ID: reportID,
94+
UserID: userID,
95+
FinishedAt: time.Now(),
96+
},
97+
Time: timestats,
98+
Code: codestats,
99+
}
100+
return stats
101+
}

postgresql/stats.go

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

99
// nolint:gocognit
10-
func (s InsertionService) Insert(stats benchttp.Stats, statsID, userID, tag string) error {
10+
func (s InsertionService) Insert(stats benchttp.Stats) error {
1111
tx, err := s.db.Begin()
1212
if err != nil {
1313
return err
@@ -17,9 +17,8 @@ func (s InsertionService) Insert(stats benchttp.Stats, statsID, userID, tag stri
1717
INSERT INTO public.stats_descriptor(
1818
id,
1919
user_id,
20-
tag,
2120
finished_at)
22-
VALUES($1, $2, $3, '2022-03-03 17:36:38-02')`)
21+
VALUES($1, $2, $3)`)
2322
if err != nil {
2423
if err := tx.Rollback(); err != nil {
2524
return err
@@ -29,9 +28,9 @@ func (s InsertionService) Insert(stats benchttp.Stats, statsID, userID, tag stri
2928
defer insertStatsDescriptor.Close()
3029

3130
if _, err = insertStatsDescriptor.Exec(
32-
statsID,
33-
userID,
34-
tag,
31+
stats.Descriptor.ID,
32+
stats.Descriptor.UserID,
33+
stats.Descriptor.FinishedAt,
3534
); err != nil {
3635
if err := tx.Rollback(); err != nil {
3736
return err
@@ -58,7 +57,7 @@ func (s InsertionService) Insert(stats benchttp.Stats, statsID, userID, tag stri
5857
defer insertTimestats.Close()
5958

6059
if _, err = insertTimestats.Exec(
61-
statsID,
60+
stats.Descriptor.ID,
6261
stats.Time.Min,
6362
stats.Time.Max,
6463
stats.Time.Mean,
@@ -90,7 +89,7 @@ func (s InsertionService) Insert(stats benchttp.Stats, statsID, userID, tag stri
9089
defer insertCodestats.Close()
9190

9291
if _, err = insertCodestats.Exec(
93-
statsID,
92+
stats.Descriptor.ID,
9493
stats.Code.Status1xx,
9594
stats.Code.Status2xx,
9695
stats.Code.Status3xx,

0 commit comments

Comments
 (0)