simplify code and add total
This commit is contained in:
@@ -9,11 +9,6 @@ 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("GetReport", "database handler is not initialized")
|
||||
@@ -57,9 +52,6 @@ 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 {
|
||||
@@ -70,37 +62,33 @@ func (a *APIHandler) GetReport(c *gin.Context) {
|
||||
for _, e := range events {
|
||||
var includesDate bool
|
||||
|
||||
for _, d := range request.Date {
|
||||
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
|
||||
}
|
||||
|
||||
var data *models.Data
|
||||
if _, ok := count[e.Day]; !ok {
|
||||
count[e.Day] = &counter{}
|
||||
attendanceLength := len(e.Attendees)
|
||||
|
||||
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{Minimal: len(e.Attendees)}
|
||||
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 {
|
||||
|
||||
@@ -8,11 +8,6 @@ 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")
|
||||
@@ -35,46 +30,39 @@ 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 {
|
||||
|
||||
var includesDate bool
|
||||
|
||||
for _, d := range filter.Date {
|
||||
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
|
||||
}
|
||||
|
||||
var data *models.Data
|
||||
if _, ok := count[e.Day]; !ok {
|
||||
count[e.Day] = &counter{}
|
||||
attendanceLength := len(e.Attendees)
|
||||
|
||||
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{Minimal: len(e.Attendees)}
|
||||
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 {
|
||||
|
||||
@@ -13,7 +13,26 @@ type Report struct {
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Minimal int `json:"minimal"`
|
||||
Average int `json:"average"`
|
||||
Maximal int `json:"maximal"`
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user