Skip to content

Commit 2d6ce37

Browse files
committed
feat(history): add pagination to history list
- Add count query to history - Update manager to return paginated history
1 parent 23e0355 commit 2d6ce37

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

db/queries/history.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ WHERE collection_id = ?
1717
ORDER BY executed_at DESC
1818
LIMIT ? OFFSET ?;
1919

20+
-- name: CountHistoryByCollection :one
21+
SELECT COUNT(*) FROM history
22+
WHERE collection_id = ?;
23+
2024
-- name: DeleteHistoryEntry :exec
2125
DELETE FROM history
2226
WHERE id = ?;

internal/database/history.sql.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/history/manager.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,51 +58,61 @@ func (h *HistoryManager) List(ctx context.Context) ([]HistoryEntity, error) {
5858
return nil, fmt.Errorf("use ListByCollection to list history entries")
5959
}
6060

61-
func (h *HistoryManager) ListByCollection(ctx context.Context, collectionID int64, limit, offset int) ([]HistoryEntity, error) {
61+
func (h *HistoryManager) ListByCollection(ctx context.Context, collectionID int64, limit, offset int) (PaginatedHistory, error) {
6262
if err := crud.ValidateID(collectionID); err != nil {
63-
return nil, err
63+
return PaginatedHistory{}, err
6464
}
6565

66+
// Get total count
67+
total, err := h.DB.CountHistoryByCollection(ctx, sql.NullInt64{Int64: collectionID, Valid: true})
68+
if err != nil {
69+
log.Error("failed to count history by collection", "collection_id", collectionID, "error", err)
70+
return PaginatedHistory{}, err
71+
}
72+
73+
// Get paginated results
6674
summaries, err := h.DB.GetHistoryByCollection(ctx, database.GetHistoryByCollectionParams{
6775
CollectionID: sql.NullInt64{Int64: collectionID, Valid: true},
6876
Limit: int64(limit),
6977
Offset: int64(offset),
7078
})
7179
if err != nil {
7280
log.Error("failed to list history by collection", "collection_id", collectionID, "error", err)
73-
return nil, err
81+
return PaginatedHistory{}, err
7482
}
7583

84+
// Convert to entities
7685
entities := make([]HistoryEntity, len(summaries))
7786
for i, summary := range summaries {
7887
entities[i] = HistoryEntity{History: database.History{
79-
ID: summary.ID,
80-
Method: summary.Method,
81-
Url: summary.Url,
82-
StatusCode: summary.StatusCode,
83-
ExecutedAt: summary.ExecutedAt,
88+
ID: summary.ID,
89+
Method: summary.Method,
90+
Url: summary.Url,
91+
StatusCode: summary.StatusCode,
92+
ExecutedAt: summary.ExecutedAt,
8493
EndpointName: summary.EndpointName,
8594
}}
8695
}
8796

88-
log.Info("listed history by collection", "collection_id", collectionID, "count", len(entities), "limit", limit, "offset", offset)
89-
return entities, nil
90-
}
97+
// Calculate pagination metadata
98+
totalPages := int((total + int64(limit) - 1) / int64(limit))
99+
currentPage := (offset / limit) + 1
100+
hasNext := (offset + limit) < int(total)
101+
hasPrev := offset > 0
102+
103+
result := PaginatedHistory{
104+
Items: entities,
105+
Total: total,
106+
HasNext: hasNext,
107+
HasPrev: hasPrev,
108+
Limit: limit,
109+
Offset: offset,
110+
TotalPages: totalPages,
111+
CurrentPage: currentPage,
112+
}
91113

92-
type ExecutionData struct {
93-
CollectionID int64
94-
CollectionName string
95-
EndpointName string
96-
Method string
97-
URL string
98-
Headers map[string]string
99-
QueryParams map[string]string
100-
RequestBody string
101-
StatusCode int
102-
ResponseBody string
103-
ResponseHeaders map[string][]string
104-
Duration time.Duration
105-
ResponseSize int64
114+
log.Info("listed history by collection", "collection_id", collectionID, "count", len(entities), "total", total, "page", currentPage, "total_pages", totalPages)
115+
return result, nil
106116
}
107117

108118
func (h *HistoryManager) RecordExecution(ctx context.Context, data ExecutionData) (HistoryEntity, error) {
@@ -158,4 +168,5 @@ func validateExecutionData(data ExecutionData) error {
158168
}
159169

160170
return nil
161-
}
171+
}
172+

internal/history/models.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,31 @@ func (h HistoryEntity) GetCreatedAt() time.Time {
2929

3030
func (h HistoryEntity) GetUpdatedAt() time.Time {
3131
return h.GetCreatedAt()
32-
}
32+
}
33+
34+
type PaginatedHistory struct {
35+
Items []HistoryEntity
36+
Total int64
37+
HasNext bool
38+
HasPrev bool
39+
Limit int
40+
Offset int
41+
TotalPages int
42+
CurrentPage int
43+
}
44+
45+
type ExecutionData struct {
46+
CollectionID int64
47+
CollectionName string
48+
EndpointName string
49+
Method string
50+
URL string
51+
Headers map[string]string
52+
QueryParams map[string]string
53+
RequestBody string
54+
StatusCode int
55+
ResponseBody string
56+
ResponseHeaders map[string][]string
57+
Duration time.Duration
58+
ResponseSize int64
59+
}

0 commit comments

Comments
 (0)