-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocketbase_mod.go
158 lines (130 loc) · 4.42 KB
/
pocketbase_mod.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
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/labstack/echo/v5"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
)
const limit = 3
func chooseRandomlyInResult(records []*models.Record, limit int) []*models.Record {
selectedVotes := make([]*models.Record, 0)
// misafidy 3 votes/contre_votes atao patiny par hasard
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < limit; i++ {
randIndex := r.Intn(len(records))
selectedVotes = append(selectedVotes, records[randIndex])
// esorina izay efa vo safidy mba tsy afaka voafidy indray
records = append(records[:randIndex], records[randIndex+1:]...)
}
return selectedVotes
}
func sortByIndiceCountRecord(records []*models.Record, indice []int) []*models.Record {
for i := 0; i < len(records); i++ {
for j := i + 1; j < len(records); j++ {
if indice[i] < indice[j] {
indice[i], indice[j] = indice[j], indice[i]
records[i], records[j] = records[j], records[i]
}
}
}
return records
}
func main() {
app := pocketbase.New()
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET(
"/api/custom/participants/lite",
func(c echo.Context) error {
var total_voters int
var vote_participant, contre_votes_participant int
records, _ := app.Dao().FindRecordsByExpr("participants")
app.Dao().DB().Select("count(*)").From("votes").Row(&total_voters)
indice := make([]int, len(records))
for i := 0; i < len(records); i++ {
records[i].Set("description", nil)
app.Dao().DB().Select("count(*)").From("votes").Where(dbx.HashExp{"participant": records[i].GetId()}).Row(&vote_participant)
app.Dao().DB().Select("count(*)").From("contre_votes").Where(dbx.HashExp{"participant": records[i].GetId()}).Row(&contre_votes_participant)
// set all data in expand
records[i].SetExpand(
map[string]interface{}{
"contre_votes_count": contre_votes_participant,
"voters_count": vote_participant,
"participant_pourcent": fmt.Sprintf("%.2f", float64(vote_participant)/float64(total_voters)*100) + " %",
},
)
indice[i] = vote_participant
}
records = sortByIndiceCountRecord(records, indice)
return c.JSON(http.StatusOK, records)
},
apis.ActivityLogger(app),
)
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET(
"/api/custom/participants",
func(c echo.Context) error {
var votes_tmp, contre_votes_tmp []*models.Record
var contre_votes_count, total_voters int
records, _ := app.Dao().FindRecordsByExpr("participants")
app.DB().Select("count(*)").From("votes").Row(&total_voters)
indice := make([]int, len(records))
for i := 0; i < len(records); i++ {
// esorina aloha le description fa mavesatra
records[i].Set("description", nil)
apis.EnrichRecord(c, app.Dao(), records[i])
data := records[i].Expand()
if votes, ok := data["votes(participant)"].([]*models.Record); ok {
indice[i] = len(votes)
// get only max 3 votes in votes_tmp variable
if indice[i] > limit {
votes_tmp = chooseRandomlyInResult(votes, limit)
} else {
votes_tmp = votes
}
} else {
indice[i] = 0
votes_tmp = []*models.Record{}
}
if contre_votes, ok := data["contre_votes(participant)"].([]*models.Record); ok {
// get only max 3 votes in contre_votes variable
contre_votes_count = len(contre_votes)
if contre_votes_count > limit {
contre_votes_tmp = chooseRandomlyInResult(contre_votes, limit)
} else {
contre_votes_tmp = contre_votes
}
} else {
contre_votes_tmp = []*models.Record{}
contre_votes_count = 0
}
// set all data in expand
records[i].SetExpand(
map[string]interface{}{
"contre_votes_count": contre_votes_count,
"contre_votes_preview": contre_votes_tmp,
"voters_count": indice[i],
"participant_pourcent": fmt.Sprintf("%.2f", float64(indice[i])/float64(total_voters)*100) + " %",
"votes_preview": votes_tmp,
},
)
}
records = sortByIndiceCountRecord(records, indice)
return c.JSON(http.StatusOK, records)
},
apis.ActivityLogger(app),
)
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}