Skip to content

Commit 7c42b38

Browse files
fix: trace list query and clean up
1 parent b2314f4 commit 7c42b38

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

cmd/smtpbridge/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func run(flags *flag.FlagSet) lieut.Executor {
101101
webTestFileStore := app.NewWebTestFileStore("apple-touch-icon.png", fmt.Sprintf("http://127.0.0.1:%d", cfg.HTTPPort))
102102
app, release := app.New(db, fileStore, bus, cfg.Config, cfg.EndpointFactory, webTestFileStore)
103103
defer release()
104+
app.Tracer("boot").Trace(ctx, "boot")
104105

105106
// Supervisor
106107
super := suture.New("root", suture.Spec{
@@ -202,8 +203,8 @@ func run(flags *flag.FlagSet) lieut.Executor {
202203

203204
// Web
204205
if web.DevMode {
205-
web := web.NewRefresher()
206-
super.Add(web)
206+
viteHotReload := web.NewViteHotReload()
207+
super.Add(viteHotReload)
207208
}
208209

209210
return super.Serve(ctx)

internal/app/app.go

+3
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ func (a App) EndpointTest(ctx context.Context, id int64) error {
177177
env := envelope.New(envelope.NewMessage(models.DTOMessageCreate{
178178
Subject: "Test Subject",
179179
Text: "Test Body",
180+
From: "from-test@example.com",
181+
To: []string{"to-test@example.com"},
182+
Date: time.Now(),
180183
}), datt.Attachment)
181184

182185
return end.Send(ctx, a.webTestFileStore, env)

internal/repo/trace.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package repo
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"time"
78

89
"github.com/ItsNotGoodName/smtpbridge/internal/database"
910
. "github.com/ItsNotGoodName/smtpbridge/internal/jet/table"
1011
"github.com/ItsNotGoodName/smtpbridge/internal/models"
12+
"github.com/ItsNotGoodName/smtpbridge/internal/repo/orm"
1113
"github.com/ItsNotGoodName/smtpbridge/pkg/pagination"
1214
. "github.com/go-jet/jet/v2/sqlite"
1315
"github.com/samber/lo"
@@ -60,49 +62,48 @@ func TraceCreate(ctx context.Context, db database.Querier, r models.Trace) (int6
6062

6163
func TraceList(ctx context.Context, db database.Querier, page pagination.Page, req models.DTOTraceListRequest) (models.DTOTraceListResult, error) {
6264
subQuery := Traces.
63-
SELECT(
64-
Traces.RequestID.AS("request_id"),
65-
COUNT(Raw("*")).OVER().AS("count"),
66-
).
65+
SELECT(Traces.RequestID.AS(Traces.RequestID.Name())).
6766
DISTINCT()
6867
// Order
69-
if req.Ascending {
70-
subQuery = subQuery.ORDER_BY(Traces.ID.ASC())
71-
} else {
72-
subQuery = subQuery.ORDER_BY(Traces.ID.DESC())
73-
}
68+
subQuery = traceListOrder(subQuery, req)
7469
// Pagination
7570
subQuery = subQuery.
7671
LIMIT(int64(page.Limit())).
7772
OFFSET(int64(page.Offset()))
7873

79-
var res struct {
80-
Count int `sql:"primary_key"`
81-
Trace []models.Trace
82-
}
83-
query := SELECT(tracePJ, Raw("t.count").AS("count")).
74+
var res []models.Trace
75+
query := SELECT(tracePJ).
8476
FROM(subQuery.AsTable("t").
85-
LEFT_JOIN(Traces, RawString("t.request_id").EQ(Traces.RequestID)))
77+
LEFT_JOIN(Traces, RawString(fmt.Sprintf("t.%s", Traces.RequestID.Name())).EQ(Traces.RequestID)))
8678
// Order
87-
if req.Ascending {
88-
query = query.ORDER_BY(Traces.Seq.ASC())
89-
} else {
90-
query = query.ORDER_BY(Traces.Seq.DESC())
91-
}
79+
query = traceListOrder(query, req)
9280
err := query.QueryContext(ctx, db, &res)
9381
if err != nil && !errors.Is(err, ErrNoRows) {
9482
return models.DTOTraceListResult{}, err
9583
}
9684

97-
traces := lo.PartitionBy(res.Trace, func(t models.Trace) string { return t.RequestID })
85+
traces := lo.PartitionBy(res, func(t models.Trace) string { return t.RequestID })
86+
87+
count, err := orm.CountQuery(ctx, db, Traces.
88+
SELECT(COUNT(Raw(fmt.Sprintf("DISTINCT %s", Traces.RequestID.Name()))).AS("count")))
89+
if err != nil {
90+
return models.DTOTraceListResult{}, err
91+
}
9892

99-
pageResult := pagination.NewPageResult(page, res.Count)
93+
pageResult := pagination.NewPageResult(page, count)
10094
return models.DTOTraceListResult{
10195
PageResult: pageResult,
10296
Traces: traces,
10397
}, nil
10498
}
10599

100+
func traceListOrder(s SelectStatement, req models.DTOTraceListRequest) SelectStatement {
101+
if req.Ascending {
102+
return s.ORDER_BY(Traces.ID.ASC())
103+
}
104+
return s.ORDER_BY(Traces.ID.DESC())
105+
}
106+
106107
func TraceDrop(ctx context.Context, db database.Querier) (int64, error) {
107108
res, err := Traces.
108109
DELETE().

web/web.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ func mustSubFS(currentFs fs.FS, fsRoot string) fs.FS {
3030
return subFs
3131
}
3232

33-
type Refresher struct {
33+
type ViteHotReload struct {
3434
}
3535

36-
func NewRefresher() Refresher {
37-
return Refresher{}
36+
func NewViteHotReload() ViteHotReload {
37+
return ViteHotReload{}
3838
}
3939

40-
func (r Refresher) String() string {
41-
return "web.Refresher"
40+
func (r ViteHotReload) String() string {
41+
return "web.ViteHotReloader"
4242
}
4343

44-
func (r Refresher) Serve(ctx context.Context) error {
44+
func (r ViteHotReload) Serve(ctx context.Context) error {
4545
reloadVite()
4646
return suture.ErrDoNotRestart
4747
}

0 commit comments

Comments
 (0)