feat: update activity logs for statistic
This commit is contained in:
parent
40f20d50bb
commit
344eef5c8a
|
|
@ -45,7 +45,8 @@ func (_i *ActivityLogsRouter) RegisterActivityLogsRoutes() {
|
|||
// define routes
|
||||
_i.App.Route("/activity-logs", func(router fiber.Router) {
|
||||
router.Get("/", activityLogsController.All)
|
||||
router.Get("/:id", activityLogsController.Show)
|
||||
router.Get("/statistics", activityLogsController.GetActivityStats)
|
||||
router.Get("/detail/:id", activityLogsController.Show)
|
||||
router.Post("/", activityLogsController.Save)
|
||||
router.Put("/:id", activityLogsController.Update)
|
||||
router.Delete("/:id", activityLogsController.Delete)
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ type ActivityLogsController interface {
|
|||
Save(c *fiber.Ctx) error
|
||||
Update(c *fiber.Ctx) error
|
||||
Delete(c *fiber.Ctx) error
|
||||
GetActivityStats(c *fiber.Ctx) error
|
||||
}
|
||||
|
||||
func NewActivityLogsController(activityLogsService service.ActivityLogsService, log zerolog.Logger) ActivityLogsController {
|
||||
|
|
@ -82,7 +83,7 @@ func (_i *activityLogsController) All(c *fiber.Ctx) error {
|
|||
// @Failure 400 {object} response.BadRequestError
|
||||
// @Failure 401 {object} response.UnauthorizedError
|
||||
// @Failure 500 {object} response.InternalServerError
|
||||
// @Router /activity-logs/{id} [get]
|
||||
// @Router /activity-logs/detail/{id} [get]
|
||||
func (_i *activityLogsController) Show(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
||||
if err != nil {
|
||||
|
|
@ -205,6 +206,30 @@ func (_i *activityLogsController) Delete(c *fiber.Ctx) error {
|
|||
})
|
||||
}
|
||||
|
||||
// GetActivityStats get activity stats ActivityLogs
|
||||
// @Summary Get activity stats ActivityLogs
|
||||
// @Description API for get activity stats ActivityLogs
|
||||
// @Tags ActivityLogs
|
||||
// @Security Bearer
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 400 {object} response.BadRequestError
|
||||
// @Failure 401 {object} response.UnauthorizedError
|
||||
// @Failure 500 {object} response.InternalServerError
|
||||
// @Router /activity-logs/statistics [get]
|
||||
func (_i *activityLogsController) GetActivityStats(c *fiber.Ctx) error {
|
||||
_i.Log.Info().Interface("GetActivityStats", "checker controller").Msg("")
|
||||
activityStatsData, err := _i.activityLogsService.GetActivityStats()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return utilRes.Resp(c, utilRes.Response{
|
||||
Success: true,
|
||||
Messages: utilRes.Messages{"ActivityLogs Stats successfully retrieved"},
|
||||
Data: activityStatsData,
|
||||
})
|
||||
}
|
||||
|
||||
func GetVisitorIP(c *fiber.Ctx) string {
|
||||
ip := c.Get("X-Forwarded-For")
|
||||
if ip == "" {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ type ActivityLogsRepository interface {
|
|||
Delete(id uint) (err error)
|
||||
CountUniqueVisitorAllTime() (count int64, err error)
|
||||
CountUniqueVisitorToday() (count int64, err error)
|
||||
CountTotalViewAllTime() (count int64, err error)
|
||||
}
|
||||
|
||||
func NewActivityLogsRepository(db *database.Database, logger zerolog.Logger) ActivityLogsRepository {
|
||||
|
|
@ -108,6 +109,15 @@ func (_i *activityLogsRepository) CountUniqueVisitorAllTime() (count int64, err
|
|||
return
|
||||
}
|
||||
|
||||
func (_i *activityLogsRepository) CountTotalViewAllTime() (count int64, err error) {
|
||||
err = _i.DB.DB.
|
||||
Model(&entity.ActivityLogs{}).
|
||||
Where("activity_type_id = ?", 2).
|
||||
Count(&count).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (_i *activityLogsRepository) CountUniqueVisitorToday() (count int64, err error) {
|
||||
tenMinutesAgo := time.Now().Add(-10 * time.Minute)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,3 +10,9 @@ type ActivityLogsResponse struct {
|
|||
UserId *uint `json:"userId"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type ActivityStatsResponse struct {
|
||||
TotalVisitorAllTime int64 `json:"totalVisitorAllTime"`
|
||||
TotalVisitorToday int64 `json:"totalVisitorToday"`
|
||||
TotalViewAllTime int64 `json:"totalViewAllTime"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ type ActivityLogsService interface {
|
|||
Save(req request.ActivityLogsCreateRequest, authToken *string) (activityLogs *entity.ActivityLogs, err error)
|
||||
Update(id uint, req request.ActivityLogsUpdateRequest) (err error)
|
||||
Delete(id uint) error
|
||||
GetActivityStats() (activityStats *response.ActivityStatsResponse, err error)
|
||||
}
|
||||
|
||||
// NewActivityLogsService init ActivityLogsService
|
||||
|
|
@ -96,3 +97,26 @@ func (_i *activityLogsService) Update(id uint, req request.ActivityLogsUpdateReq
|
|||
func (_i *activityLogsService) Delete(id uint) error {
|
||||
return _i.Repo.Delete(id)
|
||||
}
|
||||
|
||||
func (_i *activityLogsService) GetActivityStats() (activityStats *response.ActivityStatsResponse, err error) {
|
||||
_i.Log.Info().Interface("GetActivityStats", "checker").Msg("")
|
||||
countUniqueVisitorAllTime, err := _i.Repo.CountUniqueVisitorAllTime()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
countUniqueVisitorToday, err := _i.Repo.CountUniqueVisitorToday()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
countTotalViewAllTime, err := _i.Repo.CountTotalViewAllTime()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
getActivityStats := &response.ActivityStatsResponse{
|
||||
TotalVisitorAllTime: countUniqueVisitorAllTime,
|
||||
TotalVisitorToday: countUniqueVisitorToday,
|
||||
TotalViewAllTime: countTotalViewAllTime,
|
||||
}
|
||||
return getActivityStats, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"/activity-logs/{id}": {
|
||||
"/activity-logs/detail/{id}": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
|
|
@ -227,7 +227,49 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/activity-logs/statistics": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"description": "API for get activity stats ActivityLogs",
|
||||
"tags": [
|
||||
"ActivityLogs"
|
||||
],
|
||||
"summary": "Get activity stats ActivityLogs",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.Response"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.BadRequestError"
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.UnauthorizedError"
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal Server Error",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.InternalServerError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/activity-logs/{id}": {
|
||||
"put": {
|
||||
"security": [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"/activity-logs/{id}": {
|
||||
"/activity-logs/detail/{id}": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
|
|
@ -216,7 +216,49 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/activity-logs/statistics": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"description": "API for get activity stats ActivityLogs",
|
||||
"tags": [
|
||||
"ActivityLogs"
|
||||
],
|
||||
"summary": "Get activity stats ActivityLogs",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.Response"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.BadRequestError"
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.UnauthorizedError"
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal Server Error",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.InternalServerError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/activity-logs/{id}": {
|
||||
"put": {
|
||||
"security": [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1088,36 +1088,6 @@ paths:
|
|||
summary: delete ActivityLogs
|
||||
tags:
|
||||
- ActivityLogs
|
||||
get:
|
||||
description: API for getting one ActivityLogs
|
||||
parameters:
|
||||
- description: ActivityLogs ID
|
||||
in: path
|
||||
name: id
|
||||
required: true
|
||||
type: integer
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/response.Response'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/response.BadRequestError'
|
||||
"401":
|
||||
description: Unauthorized
|
||||
schema:
|
||||
$ref: '#/definitions/response.UnauthorizedError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/response.InternalServerError'
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Get one ActivityLogs
|
||||
tags:
|
||||
- ActivityLogs
|
||||
put:
|
||||
description: API for update ActivityLogs
|
||||
parameters:
|
||||
|
|
@ -1159,6 +1129,62 @@ paths:
|
|||
summary: update ActivityLogs
|
||||
tags:
|
||||
- ActivityLogs
|
||||
/activity-logs/detail/{id}:
|
||||
get:
|
||||
description: API for getting one ActivityLogs
|
||||
parameters:
|
||||
- description: ActivityLogs ID
|
||||
in: path
|
||||
name: id
|
||||
required: true
|
||||
type: integer
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/response.Response'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/response.BadRequestError'
|
||||
"401":
|
||||
description: Unauthorized
|
||||
schema:
|
||||
$ref: '#/definitions/response.UnauthorizedError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/response.InternalServerError'
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Get one ActivityLogs
|
||||
tags:
|
||||
- ActivityLogs
|
||||
/activity-logs/statistics:
|
||||
get:
|
||||
description: API for get activity stats ActivityLogs
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/response.Response'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/response.BadRequestError'
|
||||
"401":
|
||||
description: Unauthorized
|
||||
schema:
|
||||
$ref: '#/definitions/response.UnauthorizedError'
|
||||
"500":
|
||||
description: Internal Server Error
|
||||
schema:
|
||||
$ref: '#/definitions/response.InternalServerError'
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Get activity stats ActivityLogs
|
||||
tags:
|
||||
- ActivityLogs
|
||||
/advertisement:
|
||||
get:
|
||||
description: API for getting all Advertisement
|
||||
|
|
|
|||
Loading…
Reference in New Issue