Skip to content

Commit fc01567

Browse files
Add job ID to query struct
Signed-off-by: Mohamed Abokammer <mahmednabil109@gmail.com>
1 parent 1db6172 commit fc01567

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

cmds/admin_server/server/server.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ var (
1919
)
2020

2121
type Query struct {
22+
JobID *uint64 `form:"job_id"`
2223
Text *string `form:"text"`
23-
LogLevel *string `form:"logLevel"`
24-
StartDate *time.Time `form:"startDate" time_format:"2006-01-02T15:04:05.000Z07:00"`
25-
EndDate *time.Time `form:"endDate" time_format:"2006-01-02T15:04:05.000Z07:00"`
26-
PageSize *uint `form:"pageSize"`
24+
LogLevel *string `form:"log_level"`
25+
StartDate *time.Time `form:"start_date" time_format:"2006-01-02T15:04:05.000Z07:00"`
26+
EndDate *time.Time `form:"end_date" time_format:"2006-01-02T15:04:05.000Z07:00"`
27+
PageSize *uint `form:"page_size"`
2728
Page *uint `form:"page"`
2829
}
2930

@@ -34,6 +35,7 @@ func (q *Query) ToStorageQuery() storage.Query {
3435
PageSize: MaxPageSize,
3536
}
3637

38+
storageQuery.JobID = q.JobID
3739
storageQuery.Text = q.Text
3840
storageQuery.LogLevel = q.LogLevel
3941
storageQuery.StartDate = q.StartDate
@@ -51,10 +53,10 @@ func (q *Query) ToStorageQuery() storage.Query {
5153
}
5254

5355
type Log struct {
54-
JobID uint64 `json:"jobID"`
55-
LogData string `json:"logData"`
56+
JobID uint64 `json:"job_id"`
57+
LogData string `json:"log_data"`
5658
Date time.Time `json:"date"`
57-
LogLevel string `json:"logLevel"`
59+
LogLevel string `json:"log_level"`
5860
}
5961

6062
func (l *Log) ToStorageLog() storage.Log {

cmds/admin_server/storage/mongo/mongo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ func NewMongoStorage(ctx xcontext.Context, uri string) (storage.Storage, error)
5454
func toMongoQuery(query storage.Query) bson.D {
5555
q := bson.D{}
5656

57+
if query.JobID != nil {
58+
q = append(q, bson.E{
59+
Key: "jobid",
60+
Value: *query.JobID,
61+
})
62+
}
63+
5764
if query.Text != nil {
5865
q = append(q, bson.E{
5966
Key: "logdata",

cmds/admin_server/storage/storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Log struct {
3535

3636
// Query defines the different options to filter with
3737
type Query struct {
38+
JobID *uint64
3839
Text *string
3940
LogLevel *string
4041
StartDate *time.Time

tests/integ/admin_server/getlogs_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"math/rand"
1616
"net/http"
1717
"net/url"
18+
"strconv"
1819
"testing"
1920
"time"
2021

@@ -100,23 +101,26 @@ func queryLogsEndpoint(query server.Query, retrieveAll bool) (*storage.Result, i
100101
var tmp storage.Result
101102

102103
q := u.Query()
104+
if query.JobID != nil {
105+
q.Set("job_id", strconv.FormatUint(*query.JobID, 10))
106+
}
103107
if query.Text != nil {
104108
q.Set("text", *query.Text)
105109
}
106110
if query.StartDate != nil {
107-
q.Set("startDate", query.StartDate.Format(storage.DefaultTimestampFormat))
111+
q.Set("start_date", query.StartDate.Format(storage.DefaultTimestampFormat))
108112
}
109113
if query.EndDate != nil {
110-
q.Set("endDate", query.EndDate.Format(storage.DefaultTimestampFormat))
114+
q.Set("end_date", query.EndDate.Format(storage.DefaultTimestampFormat))
111115
}
112116
if query.LogLevel != nil {
113-
q.Set("logLevel", *query.LogLevel)
117+
q.Set("log_level", *query.LogLevel)
114118
}
115119
if query.Page != nil {
116-
q.Set("page", fmt.Sprint(currentPage))
120+
q.Set("page", strconv.FormatUint(uint64(currentPage), 10))
117121
}
118122
if query.PageSize != nil {
119-
q.Set("pageSize", fmt.Sprint(*query.PageSize))
123+
q.Set("page_size", strconv.FormatUint(uint64(*query.PageSize), 10))
120124
}
121125

122126
u.RawQuery = q.Encode()
@@ -196,6 +200,7 @@ func TestLogQuery(t *testing.T) {
196200
endDate = time.Now().Add(1 * time.Second)
197201
searchText string = "a"
198202
emptySearchText string = ""
203+
jobID uint64 = 3
199204
page uint = 0
200205
pageSize uint = 20
201206
logLevel string = "info"
@@ -207,6 +212,17 @@ func TestLogQuery(t *testing.T) {
207212
apiQuery server.Query
208213
dbQuery bson.M
209214
}{
215+
{
216+
name: "jobID-search",
217+
apiQuery: server.Query{
218+
JobID: &jobID,
219+
Page: &page,
220+
PageSize: &pageSize,
221+
},
222+
dbQuery: bson.M{
223+
"jobid": jobID,
224+
},
225+
},
210226
{
211227
name: "text-search",
212228
apiQuery: server.Query{

0 commit comments

Comments
 (0)