Compare commits

..

7 Commits

Author SHA1 Message Date
Adrian Zürcher
e876101688 simplify code and add total 2026-02-12 08:16:01 +01:00
Adrian Zürcher
03ace00a4b remove omitempty 2026-02-11 15:39:36 +01:00
Adrian Zürcher
57ef3b92db go mod tidy 2026-02-11 14:54:00 +01:00
Adrian Zürcher
ce8d17b3c9 update dbhandler for key search 2026-02-11 14:52:31 +01:00
Adrian Zürcher
376ba27a9c update getreport function 2026-02-11 14:52:20 +01:00
Adrian Zürcher
f2d20e83d6 update report function 2026-02-11 13:42:02 +01:00
Adrian Zürcher
81e05851bc fix wrong log message 2026-02-10 21:58:25 +01:00
7 changed files with 99 additions and 144 deletions

View File

@@ -3,19 +3,15 @@ package api
import (
"net/http"
"slices"
"strings"
"gitea.tecamino.com/paadi/memberDB/models"
"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 +27,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 +41,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 +52,49 @@ 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
for i, d := range request.Date {
if strings.Contains(e.Date, d) {
request.Date = slices.Delete(request.Date, i, min(len(request.Date), i+1))
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 +113,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
View File

@@ -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
View File

@@ -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=

View File

@@ -3,20 +3,20 @@ package handlers
import (
"errors"
"slices"
"strings"
"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 +30,45 @@ 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
for i, d := range filter.Date {
if strings.Contains(e.Date, d) {
filter.Date = slices.Delete(filter.Date, i, min(len(filter.Date), i+1))
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 +83,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)
}

View File

@@ -1,6 +0,0 @@
package models
type Date struct {
From string ` json:"from,omitempty"`
To string ` json:"to,omitempty"`
}

View File

@@ -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
}

View File

@@ -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"`
}