Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62046ec239 | ||
|
|
e876101688 | ||
|
|
03ace00a4b | ||
|
|
57ef3b92db | ||
|
|
ce8d17b3c9 | ||
|
|
376ba27a9c | ||
|
|
f2d20e83d6 | ||
|
|
81e05851bc |
@@ -8,14 +8,9 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type counter struct {
|
||||
event int
|
||||
total int
|
||||
}
|
||||
|
||||
func (a *APIHandler) GetReport(c *gin.Context) {
|
||||
if !a.DBHandlerIsInitialized() {
|
||||
a.logger.Error("DeleteEvent", "database handler is not initialized")
|
||||
a.logger.Error("GetReport", "database handler is not initialized")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
@@ -31,6 +26,13 @@ func (a *APIHandler) GetReport(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(request.Date) == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": report,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var events []models.Event
|
||||
|
||||
if len(request.Name) > 0 {
|
||||
@@ -38,6 +40,7 @@ func (a *APIHandler) GetReport(c *gin.Context) {
|
||||
} else {
|
||||
events, err = a.DbHandler.GetEvent(0)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
a.logger.Error("GetReport", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
@@ -48,76 +51,53 @@ func (a *APIHandler) GetReport(c *gin.Context) {
|
||||
|
||||
report.Data = make(map[string]*models.Data)
|
||||
|
||||
//helper
|
||||
count := make(map[string]*counter)
|
||||
|
||||
addedMember := make(map[uint]bool)
|
||||
|
||||
if len(events) == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{})
|
||||
return
|
||||
}
|
||||
|
||||
for _, e := range events {
|
||||
// skip if date lower or not equal from date as well as greater than to date
|
||||
if (e.Date < request.Date.From || e.Date > request.Date.From) && e.Date != request.Date.From {
|
||||
var includesDate bool
|
||||
|
||||
if len(e.Date) < 10 {
|
||||
continue
|
||||
}
|
||||
for _, d := range request.Date {
|
||||
if len(d) < 10 {
|
||||
continue
|
||||
} else if d[:10] == e.Date[:10] {
|
||||
includesDate = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !includesDate {
|
||||
continue
|
||||
}
|
||||
|
||||
// skip if day not selected
|
||||
switch e.Day {
|
||||
case "Monday":
|
||||
if !request.Weekdays.Monday {
|
||||
continue
|
||||
}
|
||||
case "Tuesday":
|
||||
if !request.Weekdays.Tuesday {
|
||||
continue
|
||||
}
|
||||
case "Wednesday":
|
||||
if !request.Weekdays.Wednesday {
|
||||
continue
|
||||
}
|
||||
case "Thursday":
|
||||
if !request.Weekdays.Thursday {
|
||||
continue
|
||||
}
|
||||
case "Friday":
|
||||
if !request.Weekdays.Friday {
|
||||
continue
|
||||
}
|
||||
case "Saturday":
|
||||
if !request.Weekdays.Saturday {
|
||||
continue
|
||||
}
|
||||
case "Sunday":
|
||||
if !request.Weekdays.Sunday {
|
||||
continue
|
||||
}
|
||||
}
|
||||
attendanceLength := len(e.Attendees)
|
||||
|
||||
var data *models.Data
|
||||
if _, ok := count[e.Day]; !ok {
|
||||
count[e.Day] = &counter{}
|
||||
total, ok := report.Data["total"]
|
||||
if !ok {
|
||||
total = models.AddNewData(attendanceLength)
|
||||
report.Data["total"] = total
|
||||
}
|
||||
count[e.Day].event += 1
|
||||
|
||||
data, ok := report.Data[e.Day]
|
||||
if !ok {
|
||||
data = &models.Data{}
|
||||
data = models.AddNewData(attendanceLength)
|
||||
report.Data[e.Day] = data
|
||||
}
|
||||
|
||||
if data.Minimal > len(e.Attendees) {
|
||||
data.Minimal = len(e.Attendees)
|
||||
}
|
||||
|
||||
if data.Maximal < len(e.Attendees) {
|
||||
data.Maximal = len(e.Attendees)
|
||||
}
|
||||
|
||||
count[e.Day].total = +len(e.Attendees)
|
||||
data.Average = count[e.Day].total / count[e.Day].event
|
||||
data.AddData(attendanceLength)
|
||||
total.AddData(attendanceLength)
|
||||
|
||||
for _, a := range e.Attendees {
|
||||
if _, ok := addedMember[a.Id]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
report.Attendees = append(report.Attendees, a)
|
||||
addedMember[a.Id] = true
|
||||
}
|
||||
@@ -136,8 +116,12 @@ func (a *APIHandler) GetReport(c *gin.Context) {
|
||||
for _, m := range members {
|
||||
if _, ok := addedMember[m.Id]; ok {
|
||||
continue
|
||||
} else if !slices.Contains(request.Groups, m.Group.Id) {
|
||||
continue
|
||||
} else if len(request.Groups) > 0 {
|
||||
if m.Group == nil {
|
||||
continue
|
||||
} else if !slices.Contains(request.Groups, m.Group.Id) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
report.NonAttendees = append(report.NonAttendees, &m)
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module gitea.tecamino.com/paadi/memberDB
|
||||
go 1.25.4
|
||||
|
||||
require (
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.10
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.11
|
||||
gitea.tecamino.com/paadi/tecamino-logger v0.2.1
|
||||
github.com/gin-contrib/cors v1.7.6
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.10 h1:zZQbDTJ0bu6CIW90Zms8yYIzTLHtWPNhVKRxLUXEDuE=
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.10/go.mod h1:y/xn/POJg1DO++67uKvnO23lJQgh+XFQq7HZCS9Getw=
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.11 h1:hTpMWRr4dW7TkiBnEku0/3ggDC7/uP82U9paRKY/QEs=
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.11/go.mod h1:y/xn/POJg1DO++67uKvnO23lJQgh+XFQq7HZCS9Getw=
|
||||
gitea.tecamino.com/paadi/tecamino-logger v0.2.1 h1:sQTBKYPdzn9mmWX2JXZBtGBvNQH7cuXIwsl4TD0aMgE=
|
||||
gitea.tecamino.com/paadi/tecamino-logger v0.2.1/go.mod h1:FkzRTldUBBOd/iy2upycArDftSZ5trbsX5Ira5OzJgM=
|
||||
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
|
||||
|
||||
@@ -7,16 +7,15 @@ import (
|
||||
"gitea.tecamino.com/paadi/memberDB/models"
|
||||
)
|
||||
|
||||
type counter struct {
|
||||
event int
|
||||
total int
|
||||
}
|
||||
|
||||
func (dh *DatabaseHandler) GetReport(filter models.ReportFilter) (report models.Report, err error) {
|
||||
if !dh.DatabaseOpened() {
|
||||
return report, errors.New("database not opened")
|
||||
}
|
||||
|
||||
if len(filter.Date) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var events []models.Event
|
||||
|
||||
if len(filter.Name) > 0 {
|
||||
@@ -30,76 +29,49 @@ func (dh *DatabaseHandler) GetReport(filter models.ReportFilter) (report models.
|
||||
|
||||
report.Data = make(map[string]*models.Data)
|
||||
|
||||
//helper
|
||||
count := make(map[string]*counter)
|
||||
|
||||
addedMember := make(map[uint]bool)
|
||||
|
||||
for _, e := range events {
|
||||
// skip if date lower or not equal from date as well as greater than to date
|
||||
if (e.Date < filter.Date.From || e.Date > filter.Date.From) && e.Date != filter.Date.From {
|
||||
|
||||
var includesDate bool
|
||||
|
||||
if len(e.Date) < 10 {
|
||||
continue
|
||||
}
|
||||
for _, d := range filter.Date {
|
||||
if len(d) < 10 {
|
||||
continue
|
||||
} else if d[:10] == e.Date[:10] {
|
||||
includesDate = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !includesDate {
|
||||
continue
|
||||
}
|
||||
|
||||
// skip if day not selected
|
||||
switch e.Day {
|
||||
case "Monday":
|
||||
if !filter.Weekdays.Monday {
|
||||
continue
|
||||
}
|
||||
case "Tuesday":
|
||||
if !filter.Weekdays.Tuesday {
|
||||
continue
|
||||
}
|
||||
case "Wednesday":
|
||||
if !filter.Weekdays.Wednesday {
|
||||
continue
|
||||
}
|
||||
case "Thursday":
|
||||
if !filter.Weekdays.Thursday {
|
||||
continue
|
||||
}
|
||||
case "Friday":
|
||||
if !filter.Weekdays.Friday {
|
||||
continue
|
||||
}
|
||||
case "Saturday":
|
||||
if !filter.Weekdays.Saturday {
|
||||
continue
|
||||
}
|
||||
case "Sunday":
|
||||
if !filter.Weekdays.Sunday {
|
||||
continue
|
||||
}
|
||||
}
|
||||
attendanceLength := len(e.Attendees)
|
||||
|
||||
var data *models.Data
|
||||
if _, ok := count[e.Day]; !ok {
|
||||
count[e.Day] = &counter{}
|
||||
total, ok := report.Data["total"]
|
||||
if !ok {
|
||||
total = models.AddNewData(attendanceLength)
|
||||
report.Data["total"] = total
|
||||
}
|
||||
count[e.Day].event += 1
|
||||
|
||||
data, ok := report.Data[e.Day]
|
||||
if !ok {
|
||||
data = &models.Data{}
|
||||
data = models.AddNewData(attendanceLength)
|
||||
report.Data[e.Day] = data
|
||||
}
|
||||
|
||||
if data.Minimal > len(e.Attendees) {
|
||||
data.Minimal = len(e.Attendees)
|
||||
}
|
||||
|
||||
if data.Maximal < len(e.Attendees) {
|
||||
data.Maximal = len(e.Attendees)
|
||||
}
|
||||
|
||||
count[e.Day].total = +len(e.Attendees)
|
||||
data.Average = count[e.Day].total / count[e.Day].event
|
||||
data.AddData(attendanceLength)
|
||||
total.AddData(attendanceLength)
|
||||
|
||||
for _, a := range e.Attendees {
|
||||
if _, ok := addedMember[a.Id]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
report.Attendees = append(report.Attendees, a)
|
||||
addedMember[a.Id] = true
|
||||
}
|
||||
@@ -114,8 +86,12 @@ func (dh *DatabaseHandler) GetReport(filter models.ReportFilter) (report models.
|
||||
for _, m := range members {
|
||||
if _, ok := addedMember[m.Id]; ok {
|
||||
continue
|
||||
} else if !slices.Contains(filter.Groups, m.Group.Id) {
|
||||
continue
|
||||
} else if len(filter.Groups) > 0 {
|
||||
if m.Group == nil {
|
||||
continue
|
||||
} else if !slices.Contains(filter.Groups, m.Group.Id) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
report.NonAttendees = append(report.NonAttendees, &m)
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package models
|
||||
|
||||
type Date struct {
|
||||
From string ` json:"from,omitempty"`
|
||||
To string ` json:"to,omitempty"`
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package models
|
||||
|
||||
type ReportFilter struct {
|
||||
Weekdays Weekdays `json:"weekdays"`
|
||||
Date Date `json:"date"`
|
||||
Groups []uint `json:"groupIds,omitempty"`
|
||||
Name []string `json:"name,omitempty"`
|
||||
Date []string `json:"date"`
|
||||
Groups []uint `json:"groupIds,omitempty"`
|
||||
Name []string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
@@ -14,7 +13,26 @@ type Report struct {
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Minimal int `json:"minimal,omitempty"`
|
||||
Average int `json:"average,omitempty"`
|
||||
Maximal int `json:"maximal,omitempty"`
|
||||
Events int `json:"events"`
|
||||
Minimal int `json:"minimal"`
|
||||
Average int `json:"average"`
|
||||
Maximal int `json:"maximal"`
|
||||
averageCount int `json:"-"`
|
||||
}
|
||||
|
||||
func AddNewData(value int) *Data {
|
||||
return &Data{Minimal: value}
|
||||
}
|
||||
|
||||
func (d *Data) AddData(value int) {
|
||||
d.Events += 1
|
||||
if d.Minimal > value {
|
||||
d.Minimal = value
|
||||
}
|
||||
if d.Maximal < value {
|
||||
d.Maximal = value
|
||||
}
|
||||
|
||||
d.averageCount += value
|
||||
d.Average = d.averageCount / d.Events
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package models
|
||||
|
||||
type Weekdays struct {
|
||||
Monday bool `json:"monday,omitempty"`
|
||||
Tuesday bool `json:"tuesday,omitempty"`
|
||||
Wednesday bool `json:"wednesday,omitempty"`
|
||||
Thursday bool `json:"thursday,omitempty"`
|
||||
Friday bool `json:"friday,omitempty"`
|
||||
Saturday bool `json:"saturday,omitempty"`
|
||||
Sunday bool `json:"sunday,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user