Skip to content

Commit

Permalink
Add endpoint to count all log entries and implement corresponding dat…
Browse files Browse the repository at this point in the history
…abase function
  • Loading branch information
abaldeweg authored Feb 6, 2025
1 parent 64e39dd commit d10bd07
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions logs_web/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ func GetEvents(c *gin.Context) {

c.JSON(http.StatusOK, d)
}

// Count handles the GET request to count all log entries.
func Count(c *gin.Context) {
h, err := db.NewDBHandler()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}
defer h.Close()

count, err := h.CountAll()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}

c.JSON(http.StatusOK, gin.H{"count": count})
}
9 changes: 9 additions & 0 deletions logs_web/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ func (handler *DBHandler) FindDemanded(filter map[string]interface{}, sortFields
}
return logs, nil
}

// CountAll counts all log entries in the database.
func (handler *DBHandler) CountAll() (int64, error) {
count, err := handler.collection.CountDocuments(context.TODO(), bson.M{})
if err != nil {
return 0, err
}
return count, nil
}
28 changes: 28 additions & 0 deletions logs_web/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ paths:
type: string
example: "Internal server error"

/apis/logs/1/count:
get:
summary: Count all log entries
description: Retrieve the count of all log entries in the server.
security:
- APIKeyAuth: []
responses:
"200":
description: Successfully retrieved count
content:
application/json:
schema:
type: object
properties:
count:
type: integer
example: 100
"500":
description: Internal server error
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: "Internal server error"

components:
securitySchemes:
APIKeyAuth:
Expand Down
1 change: 1 addition & 0 deletions logs_web/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Routes() *gin.Engine {
api := r.Group("/apis/logs/1", router.ApiKeyMiddleware(k))
{
api.POST("/events", controller.GetEvents)
api.GET("/count", controller.Count)
}

return r
Expand Down

0 comments on commit d10bd07

Please sign in to comment.