Skip to content

Commit

Permalink
Add ability to specify created_at in record request (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
pijng authored Mar 21, 2024
1 parent 03de08e commit 906eb17
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 6 additions & 1 deletion internal/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.6.9
version: 1.7.0
externalDocs:
description: Find out more about spec
url: ''
Expand Down Expand Up @@ -1093,6 +1093,7 @@ paths:
$ref: '#/components/schemas/RecordRequest'
responses:
'200':
description: ''
content:
application/json:
schema:
Expand Down Expand Up @@ -1211,6 +1212,7 @@ components:
type: string
created_at:
type: string
description: ISO8601 date-time
group_hash:
type: string
level:
Expand Down Expand Up @@ -1350,6 +1352,9 @@ components:
type: string
schema_name:
type: string
created_at:
type: string
description: ISO8601 date-time
schema_id:
type: integer
query:
Expand Down
8 changes: 4 additions & 4 deletions internal/storage/mongodb_adapter/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *RecordStorage) CreateRecord(record entities.Record, schemaID int, group
document := bson.M{
"id": nextValue, "text": record.Text, "schema_name": record.SchemaName, "schema_id": schemaID, "query": formattedQuery,
"request": record.Request, "response": record.Response, "kind": record.Kind, "group_hash": groupHash,
"level": record.Level, "created_at": entities.RecordTime{Time: time.Now()},
"level": record.Level, "created_at": record.CreatedAt,
}
result, err := s.collection.InsertOne(s.ctx, document)
if err != nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *RecordStorage) GetRecordsByQuery(record entities.Record, from *time.Tim
findOptions := options.Find()
findOptions.SetLimit(int64(limit))
findOptions.SetSkip(int64(offset))
findOptions.SetSort(bson.D{{Key: "id", Value: -1}})
findOptions.SetSort(bson.D{{Key: "created_at", Value: -1}})

totalCount, err := s.collection.CountDocuments(s.ctx, filter)
if err != nil {
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *RecordStorage) GetAllRecords(limit int, offset int) ([]*entities.Record
findOptions := options.Find()
findOptions.SetLimit(int64(limit))
findOptions.SetSkip(int64(offset))
findOptions.SetSort(bson.D{{Key: "id", Value: -1}})
findOptions.SetSort(bson.D{{Key: "created_at", Value: -1}})

cursor, err := s.collection.Find(s.ctx, bson.M{}, findOptions)
if err != nil {
Expand All @@ -172,7 +172,7 @@ func (s *RecordStorage) GetRecordsByGroupHash(schemaName string, groupHash strin
filter := bson.M{"schema_name": schemaName, "group_hash": groupHash}

findOptions := options.Find()
findOptions.SetSort(bson.D{{Key: "id", Value: 1}})
findOptions.SetSort(bson.D{{Key: "created_at", Value: 1}})

cursor, err := s.collection.Find(s.ctx, filter, findOptions)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/sqlite_adapter/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *RecordStorage) CreateRecord(record entities.Record, schemaID int, group
query := "INSERT INTO records (text, schema_name, schema_id, query, request, response, kind, group_hash, level, created_at) VALUES (?,?,?,?,?,?,?,?,?,?) RETURNING *;"

row := tx.QueryRowContext(s.ctx, query, record.Text, record.SchemaName, schemaID, record.Query,
record.Request, record.Response, record.Kind, groupHash, record.Level, entities.RecordTime{Time: time.Now()})
record.Request, record.Response, record.Kind, groupHash, record.Level, record.CreatedAt)

var lr entities.Record
err = row.Scan(&lr.ID, &lr.Text, &lr.CreatedAt, &lr.SchemaName, &lr.SchemaID, &lr.Query, &lr.Kind, &lr.GroupHash, &lr.Level, &lr.Request, &lr.Response)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *RecordStorage) GetRecordsByQuery(record entities.Record, from *time.Tim
countBuilder := queryBuilder
countParams := queryParams

queryBuilder.WriteString(" ORDER BY id DESC LIMIT ? OFFSET ?")
queryBuilder.WriteString(" ORDER BY created_at DESC LIMIT ? OFFSET ?")
queryParams = append(queryParams, limit, offset)

query := fmt.Sprintf(`
Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *RecordStorage) GetAllRecords(limit int, offset int) ([]*entities.Record
}

func (s *RecordStorage) GetRecordsByGroupHash(schemaName string, groupHash string) ([]*entities.Record, error) {
query := "SELECT * FROM records WHERE schema_name = ? AND group_hash = ? ORDER BY id ASC;"
query := "SELECT * FROM records WHERE schema_name = ? AND group_hash = ? ORDER BY created_at ASC;"

stmt, err := s.readDB.PrepareContext(s.ctx, query)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/usecases/record_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (uc *RecordUseCase) CreateRecord(record entities.Record, schemaID int) (*en
record.Level = entities.InfoLevel
}

if record.CreatedAt.Equal(time.Time{}) {
record.CreatedAt = entities.RecordTime{Time: time.Now()}
}

bytes, err := serialize.JSONMarshal(record.Query)
if err != nil {
return nil, fmt.Errorf("failed creating record: %v", err)
Expand Down

0 comments on commit 906eb17

Please sign in to comment.