-
Notifications
You must be signed in to change notification settings - Fork 17
/
analytics.go
171 lines (149 loc) · 4.85 KB
/
analytics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package controllers
import (
"fmt"
"net/http"
"strconv"
"context"
"time"
"gopkg.in/guregu/null.v3"
"github.com/gin-gonic/gin"
"github.com/twreporter/go-api/storage"
"github.com/twreporter/go-api/models"
"github.com/twreporter/go-api/internal/news"
"go.mongodb.org/mongo-driver/bson/primitive"
log "github.com/sirupsen/logrus"
)
func NewAnalyticsController(gs storage.AnalyticsGormStorage, ms storage.AnalyticsMongoStorage) *AnalyticsController {
return &AnalyticsController{gs, ms}
}
type AnalyticsController struct {
gs storage.AnalyticsGormStorage
ms storage.AnalyticsMongoStorage
}
type (
reqBody struct {
PostID null.String `json:"post_id"`
ReadPostsCount null.Bool `json:"read_posts_count"`
ReadPostsSec null.Int `json:"read_posts_sec"`
}
respBody struct {
UserID string `json:"user_id"`
PostID string `json:"post_id"`
ReadPostsCount null.Bool `json:"read_posts_count"`
ReadPostsSec null.Int `json:"read_posts_sec"`
}
reqBodyFootprint struct {
PostID null.String `json:"post_id"`
}
)
func (ac *AnalyticsController) SetUserAnalytics(c *gin.Context) (int, gin.H, error) {
var req reqBody
var resp respBody
var isExisted bool
var err error
const twoHour = 7200 // seconds
userID := c.Param("userID")
if err = c.BindJSON(&req); err != nil {
fmt.Println("Error decoding JSON:", err)
return http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()}, nil
}
if req.PostID.Valid == false {
return http.StatusBadRequest, gin.H{"status": "fail", "message": "post_id is required"}, nil
}
if req.ReadPostsSec.Valid && req.ReadPostsSec.Int64 < 0 {
return http.StatusBadRequest, gin.H{"status": "fail", "message": "read_posts_sec cannot be negative"}, nil
}
readPostsSec := req.ReadPostsSec
// read_post_sec maximum: 7200 seconds
if readPostsSec.Valid && readPostsSec.Int64 > twoHour {
log.Infof("read_posts_sec exceed two hour. seconds: %d, user: %s", req.ReadPostsSec.Int64, userID)
readPostsSec = null.NewInt(twoHour, true)
}
resp.UserID = userID
resp.PostID = req.PostID.String
if null.Bool.ValueOrZero(req.ReadPostsCount) == true {
isExisted, err = ac.gs.UpdateUserReadingPostCount(userID, req.PostID.String)
if err != nil {
return toResponse(err)
}
resp.ReadPostsCount = null.NewBool(true, true)
}
if null.Int.IsZero(readPostsSec) == false {
// update read post time
err = ac.gs.UpdateUserReadingPostTime(userID, req.PostID.String, int(readPostsSec.Int64))
if err != nil {
return toResponse(err)
}
isExisted = false
resp.ReadPostsSec = readPostsSec
}
if isExisted {
return http.StatusOK, gin.H{"status": "success", "data": resp}, nil
}
return http.StatusCreated, gin.H{"status": "success", "data": resp}, nil
}
func (ac *AnalyticsController) GetUserAnalyticsReadingFootprint(c *gin.Context) (int, gin.H, error) {
// parameter validation
userID := c.Param("userID")
limit, _ := strconv.Atoi(c.Query("limit"))
offset, _ := strconv.Atoi(c.Query("offset"))
if limit == 0 {
limit = 10
}
// get footprint posts of target user
footprints, total, err := ac.gs.GetFootprintsOfAUser(userID, limit, offset)
if err != nil {
return toResponse(err)
}
// fetch posts meta from mongo db
type footprintMeta struct {
bookmarkID string
updatedAt time.Time
}
postIds := make([]string, len(footprints))
bookmarkMap := make(map[primitive.ObjectID]footprintMeta)
for index, footprint := range footprints {
postIds[index] = footprint.PostID
objectID, err := primitive.ObjectIDFromHex(footprint.PostID)
if err != nil {
continue;
}
bookmarkMap[objectID] = footprintMeta{bookmarkID:footprint.BookmarkID, updatedAt:footprint.UpdatedAt}
}
var posts []news.MetaOfFootprint
posts, err = ac.ms.GetPostsOfIDs(context.Background(), postIds)
if err != nil {
return toResponse(err)
}
// add bookmarks in posts
for index, post := range posts {
posts[index].BookmarkID = bookmarkMap[post.ID].bookmarkID
posts[index].UpdatedAt = bookmarkMap[post.ID].updatedAt
}
return http.StatusOK, gin.H{"status": "ok", "records": posts, "meta": models.MetaOfResponse{
Total: total,
Offset: offset,
Limit: limit,
}}, nil
}
func (ac *AnalyticsController) SetUserAnalyticsReadingFootprint(c *gin.Context) (int, gin.H, error) {
var req reqBodyFootprint
var isExisted bool
var err error
userID := c.Param("userID")
if err = c.BindJSON(&req); err != nil {
fmt.Println("Error decoding JSON:", err)
return http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()}, nil
}
if req.PostID.Valid == false {
return http.StatusBadRequest, gin.H{"status": "fail", "message": "post_id is required"}, nil
}
isExisted, err = ac.gs.UpdateUserReadingFootprint(userID, req.PostID.String)
if err != nil {
return toResponse(err)
}
if isExisted {
return http.StatusOK, gin.H{"status": "success"}, nil
}
return http.StatusCreated, gin.H{"status": "success"}, nil
}