Skip to content

Commit 9fd8de1

Browse files
authored
Merge pull request #117 from mahmednabil109/Refactor_admin_server_design
refactor admin server desing
2 parents 385d5d2 + 5e259ce commit 9fd8de1

5 files changed

Lines changed: 83 additions & 24 deletions

File tree

cmds/admin_server/server/server.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ func (l *Log) ToStorageLog() storage.Log {
6868
}
6969
}
7070

71+
func toServerLog(l *storage.Log) Log {
72+
return Log{
73+
JobID: l.JobID,
74+
LogData: l.LogData,
75+
Date: l.Date,
76+
LogLevel: l.LogLevel,
77+
}
78+
}
79+
80+
type Result struct {
81+
Logs []Log `json:"logs"`
82+
Count uint64 `json:"count"`
83+
Page uint `json:"page"`
84+
PageSize uint `json:"page_size"`
85+
}
86+
87+
func toServerResult(r *storage.Result) Result {
88+
var result Result
89+
result.Count = r.Count
90+
result.Page = r.Page
91+
result.PageSize = r.PageSize
92+
93+
for _, log := range r.Logs {
94+
result.Logs = append(result.Logs, toServerLog(&log))
95+
}
96+
return result
97+
}
98+
7199
type RouteHandler struct {
72100
storage storage.Storage
73101
log logger.Logger
@@ -125,7 +153,7 @@ func (r *RouteHandler) getLogs(c *gin.Context) {
125153
return
126154
}
127155

128-
c.JSON(http.StatusOK, result)
156+
c.JSON(http.StatusOK, toServerResult(result))
129157
}
130158

131159
func initRouter(ctx xcontext.Context, rh RouteHandler) *gin.Engine {

cmds/admin_server/storage/mongo/mongo.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/linuxboot/contest/cmds/admin_server/storage"
910
"github.com/linuxboot/contest/pkg/xcontext"
@@ -63,7 +64,7 @@ func toMongoQuery(query storage.Query) bson.D {
6364

6465
if query.Text != nil {
6566
q = append(q, bson.E{
66-
Key: "logdata",
67+
Key: "log_data",
6768
Value: bson.M{
6869
"$regex": primitive.Regex{Pattern: *query.Text, Options: "ig"},
6970
},
@@ -94,7 +95,7 @@ func toMongoQuery(query storage.Query) bson.D {
9495
levels := strings.Split(*query.LogLevel, ",")
9596
q = append(q,
9697
bson.E{
97-
Key: "loglevel",
98+
Key: "log_level",
9899
Value: bson.M{
99100
"$in": levels,
100101
},
@@ -106,7 +107,8 @@ func toMongoQuery(query storage.Query) bson.D {
106107
}
107108

108109
func (s *MongoStorage) StoreLog(ctx xcontext.Context, log storage.Log) error {
109-
_, err := s.collection.InsertOne(ctx, log)
110+
mongoLog := toMongoLog(&log)
111+
_, err := s.collection.InsertOne(ctx, mongoLog)
110112
if err != nil {
111113
// for better debugging
112114
ctx.Errorf("Error while inserting into the db: %v", err)
@@ -134,15 +136,20 @@ func (s *MongoStorage) GetLogs(ctx xcontext.Context, query storage.Query) (*stor
134136
return nil, storage.ErrQuery
135137
}
136138

137-
var logs []storage.Log
139+
var logs []Log
138140
err = cur.All(ctx, &logs)
139141
if err != nil {
140142
ctx.Errorf("Error while reading query result from db: %v", err)
141143
return nil, storage.ErrQuery
142144
}
145+
// convert to storage logs
146+
storageLogs := make([]storage.Log, 0, len(logs))
147+
for _, log := range logs {
148+
storageLogs = append(storageLogs, log.toStorageLog())
149+
}
143150

144151
return &storage.Result{
145-
Logs: logs,
152+
Logs: storageLogs,
146153
Count: uint64(count),
147154
Page: query.Page,
148155
PageSize: query.PageSize,
@@ -152,3 +159,28 @@ func (s *MongoStorage) GetLogs(ctx xcontext.Context, query storage.Query) (*stor
152159
func (s *MongoStorage) Close(ctx xcontext.Context) error {
153160
return s.dbClient.Disconnect(ctx)
154161
}
162+
163+
type Log struct {
164+
JobID uint64 `bson:"job_id"`
165+
LogData string `bson:"log_data"`
166+
Date time.Time `bson:"date"`
167+
LogLevel string `bson:"log_level"`
168+
}
169+
170+
func (l *Log) toStorageLog() storage.Log {
171+
return storage.Log{
172+
JobID: l.JobID,
173+
LogData: l.LogData,
174+
Date: l.Date,
175+
LogLevel: l.LogLevel,
176+
}
177+
}
178+
179+
func toMongoLog(l *storage.Log) Log {
180+
return Log{
181+
JobID: l.JobID,
182+
LogData: l.LogData,
183+
Date: l.Date,
184+
LogLevel: l.LogLevel,
185+
}
186+
}

cmds/admin_server/storage/storage.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ type Storage interface {
2727

2828
// Log defines the basic log info pushed by the server
2929
type Log struct {
30-
JobID uint64 `json:"jobID"`
31-
LogData string `json:"logData"`
32-
Date time.Time `json:"date"`
33-
LogLevel string `json:"logLevel"`
30+
JobID uint64
31+
LogData string
32+
Date time.Time
33+
LogLevel string
3434
}
3535

3636
// Query defines the different options to filter with
@@ -46,8 +46,8 @@ type Query struct {
4646

4747
//Result defines the expected result returned from the db
4848
type Result struct {
49-
Logs []Log `json:"logs"`
50-
Count uint64 `json:"count"`
51-
Page uint `json:"page"`
52-
PageSize uint `json:"pageSize"`
49+
Logs []Log
50+
Count uint64
51+
Page uint
52+
PageSize uint
5353
}

tests/integ/admin_server/getlogs_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func generateRandomLogs(db *mongo.Client, count int) error {
5555
level := logLevels[rand.Intn(len(logLevels))]
5656
data := randomText()
5757

58-
logs = append(logs, storage.Log{
58+
logs = append(logs, mongoStorage.Log{
5959
JobID: uint64(jobID),
6060
LogLevel: level,
6161
LogData: data,
@@ -231,7 +231,7 @@ func TestLogQuery(t *testing.T) {
231231
PageSize: &pageSize,
232232
},
233233
dbQuery: bson.M{
234-
"logdata": bson.M{"$regex": primitive.Regex{Pattern: "a", Options: "ig"}},
234+
"log_data": bson.M{"$regex": primitive.Regex{Pattern: "a", Options: "ig"}},
235235
},
236236
},
237237
{
@@ -243,7 +243,7 @@ func TestLogQuery(t *testing.T) {
243243
PageSize: &pageSize,
244244
},
245245
dbQuery: bson.M{
246-
"loglevel": "info",
246+
"log_level": "info",
247247
},
248248
},
249249
{
@@ -255,7 +255,7 @@ func TestLogQuery(t *testing.T) {
255255
PageSize: &pageSize,
256256
},
257257
dbQuery: bson.M{
258-
"loglevel": bson.M{"$in": []string{"info", "debug"}},
258+
"log_level": bson.M{"$in": []string{"info", "debug"}},
259259
},
260260
},
261261
{
@@ -313,12 +313,12 @@ func TestLogQuery(t *testing.T) {
313313
PageSize: &pageSize,
314314
},
315315
dbQuery: bson.M{
316-
"logdata": bson.M{"$regex": primitive.Regex{Pattern: "a", Options: "ig"}},
316+
"log_data": bson.M{"$regex": primitive.Regex{Pattern: "a", Options: "ig"}},
317317
"date": bson.M{
318318
"$lte": primitive.NewDateTimeFromTime(endDate),
319319
"$gte": primitive.NewDateTimeFromTime(startDate),
320320
},
321-
"loglevel": "info",
321+
"log_level": "info",
322322
},
323323
},
324324
}

tests/integ/admin_server/logendpoint_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"time"
2020

2121
"github.com/linuxboot/contest/cmds/admin_server/server"
22-
"github.com/linuxboot/contest/cmds/admin_server/storage"
2322
mongoStorage "github.com/linuxboot/contest/cmds/admin_server/storage/mongo"
2423
"github.com/stretchr/testify/require"
2524
"go.mongodb.org/mongo-driver/bson"
@@ -58,17 +57,17 @@ func submitLog(addr string, log server.Log) error {
5857
return err
5958
}
6059

61-
func getAllLogs(t *testing.T, db *mongo.Client) []storage.Log {
60+
func getAllLogs(t *testing.T, db *mongo.Client) []mongoStorage.Log {
6261
cur, err := db.Database(mongoStorage.DefaultDB).
6362
Collection(mongoStorage.DefaultCollection).
6463
Find(context.Background(), bson.D{{}})
6564
if err != nil {
6665
t.Fatal(err)
6766
}
6867

69-
var dbLogs []storage.Log
68+
var dbLogs []mongoStorage.Log
7069
for cur.Next(context.Background()) {
71-
var log storage.Log
70+
var log mongoStorage.Log
7271
err := cur.Decode(&log)
7372
if err != nil {
7473
t.Fatal(err)

0 commit comments

Comments
 (0)