-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrisqus_stats.go
120 lines (102 loc) · 3.46 KB
/
drisqus_stats.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
// Copyright Piero de Salvia.
// All Rights Reserved
package drisqus
import (
"strconv"
"github.com/pierods/gisqus"
)
// AuthorID aliases string to signal intent of type
type AuthorID string
// ReplyCount aliases int to signal intent of type
type ReplyCount int
// PostCount aliases int to signal intent of type
type PostCount int
// LikeCount aliases int to signal intent of type
type LikeCount int
//DislikeCount aliases int to signal intent of type
type DislikeCount int
// PostID aliases string to signal intent of type
type PostID string
// AuthorUsername aliases string to signal intent of type
type AuthorUsername string
// AuthorStats contains statistics for a user as far as posting to a single thread goes
type AuthorStats struct {
ID AuthorID `json:"id"`
Username AuthorUsername `json:"username"`
Posts PostCount `json:"posts"`
Replies ReplyCount `json:"replies"`
RepliesWritten ReplyCount `json:"repliesWritten"`
Likes LikeCount `json:"likes"`
Dislikes DislikeCount `json:"dislikes"`
}
// ThreadStats contains statistics for a single forum thread
type ThreadStats struct {
AuthorStatsMap *map[AuthorID]*AuthorStats `json:"authorStatsMap"`
PostMap *map[PostID]*gisqus.Post `json:"postMap"`
OrphanPosts int `json:"orphanPosts"`
RootPosts int `json:"rootPosts"`
TotalLikes LikeCount `json:"totalLikes"`
Totaldislikes DislikeCount `json:"totalDislikes"`
TotalReplies ReplyCount `json:"totalReplies"`
}
// MakeThreadStats produces an instance of ThreadStats given a sampling of posts.
func (d *Drisqus) MakeThreadStats(posts []*gisqus.Post) *ThreadStats {
postMap := make(map[PostID]*gisqus.Post)
authorMap := make(map[AuthorID]*AuthorStats)
var likes, dislikes, replies int
for _, post := range posts {
postMap[PostID(post.ID)] = post
}
var orphanCount, roots int
for _, post := range postMap {
var author *AuthorStats
var exists bool
if author, exists = authorMap[AuthorID(post.Author.ID)]; !exists {
author = &AuthorStats{
ID: AuthorID(post.Author.ID),
Username: AuthorUsername(post.Author.Username),
}
authorMap[AuthorID(post.Author.ID)] = author
}
author.Posts++
if post.Likes > 0 {
author.Likes += LikeCount(post.Likes)
likes += post.Likes
}
if post.Dislikes > 0 {
author.Dislikes += DislikeCount(post.Dislikes)
dislikes += post.Dislikes
}
if post.Parent != 0 { // let's count replies. roots don't have parents (are not replies) and are not orphans
parentID := strconv.Itoa(post.Parent)
parentPost, exists := postMap[PostID(parentID)]
if !exists {
// it's an orphan post - cannot attribute reply to parent
orphanCount++
} else {
var parentAuthor *AuthorStats
if parentAuthor, exists = authorMap[AuthorID(parentPost.Author.ID)]; !exists {
parentAuthor = &AuthorStats{
ID: AuthorID(parentPost.Author.ID),
Username: AuthorUsername(parentPost.Author.Username),
}
authorMap[AuthorID(parentPost.Author.ID)] = parentAuthor
}
parentAuthor.Replies++
author.RepliesWritten++
replies++
}
} else {
roots++
}
}
return &ThreadStats{
AuthorStatsMap: &authorMap,
PostMap: &postMap,
OrphanPosts: orphanCount,
RootPosts: roots,
TotalLikes: LikeCount(likes),
Totaldislikes: DislikeCount(dislikes),
TotalReplies: ReplyCount(replies),
}
}