diff --git a/logs_web/controller/controller.go b/logs_web/controller/controller.go index 93b376e..670fd31 100644 --- a/logs_web/controller/controller.go +++ b/logs_web/controller/controller.go @@ -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}) +} diff --git a/logs_web/db/db.go b/logs_web/db/db.go index 02f8710..a3f10d3 100644 --- a/logs_web/db/db.go +++ b/logs_web/db/db.go @@ -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 +} diff --git a/logs_web/openapi.yaml b/logs_web/openapi.yaml index af2869e..fc17b43 100644 --- a/logs_web/openapi.yaml +++ b/logs_web/openapi.yaml @@ -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: diff --git a/logs_web/router/router.go b/logs_web/router/router.go index 5f0459c..b10550d 100644 --- a/logs_web/router/router.go +++ b/logs_web/router/router.go @@ -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