Web Variante in Go V0.1
This commit is contained in:
1 parent
f9e3e94629
commit
692b9b6ef8
60 files changed
+13140
No files matched your search
@@ -0,0 +1,204 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StatisticsCalculator struct{}
|
||||
|
||||
func ComputeStatistics(
|
||||
r DayRange,
|
||||
services []ServiceOccurrence,
|
||||
assignments []Assignment,
|
||||
people []Person,
|
||||
churches []Church,
|
||||
absences []Absence,
|
||||
) RangeStatistics {
|
||||
servicesByID := map[string]ServiceOccurrence{}
|
||||
for _, s := range services {
|
||||
servicesByID[s.ID] = s
|
||||
}
|
||||
peopleByID := map[string]Person{}
|
||||
for _, p := range people {
|
||||
peopleByID[p.ID] = p
|
||||
}
|
||||
assignmentsByPerson := map[string][]Assignment{}
|
||||
for _, a := range assignments {
|
||||
assignmentsByPerson[a.PersonID] = append(assignmentsByPerson[a.PersonID], a)
|
||||
}
|
||||
assignmentsByService := map[string][]Assignment{}
|
||||
for _, a := range assignments {
|
||||
assignmentsByService[a.ServiceID] = append(assignmentsByService[a.ServiceID], a)
|
||||
}
|
||||
|
||||
personStats := make([]PersonStat, 0, len(people))
|
||||
for _, person := range people {
|
||||
own := assignmentsByPerson[person.ID]
|
||||
lectors, helpers := 0, 0
|
||||
churchCounts := map[string]int{}
|
||||
preferredHits, sundays := 0, 0
|
||||
var lastDate *time.Time
|
||||
for _, a := range own {
|
||||
service, ok := servicesByID[a.ServiceID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if a.Role == RoleLector {
|
||||
lectors++
|
||||
} else {
|
||||
helpers++
|
||||
}
|
||||
churchCounts[service.ChurchID]++
|
||||
if IsSunday(service.Date) {
|
||||
sundays++
|
||||
}
|
||||
if lastDate == nil || service.Date.After(*lastDate) {
|
||||
d := service.Date
|
||||
lastDate = &d
|
||||
}
|
||||
if PreferenceHit(person, service) {
|
||||
preferredHits++
|
||||
}
|
||||
}
|
||||
stat := PersonStat{
|
||||
Person: person, Total: len(own), Lectors: lectors, Helpers: helpers,
|
||||
ChurchCounts: churchCounts, PreferredHits: preferredHits, SundayCount: sundays,
|
||||
}
|
||||
if lastDate != nil {
|
||||
stat.LastAssignment = lastDate
|
||||
}
|
||||
personStats = append(personStats, stat)
|
||||
}
|
||||
sort.SliceStable(personStats, func(i, j int) bool {
|
||||
if personStats[i].Total != personStats[j].Total {
|
||||
return personStats[i].Total > personStats[j].Total
|
||||
}
|
||||
return personStats[i].Person.LastName < personStats[j].Person.LastName
|
||||
})
|
||||
|
||||
churchStats := make([]ChurchStat, 0, len(churches))
|
||||
for _, church := range churches {
|
||||
var churchServices []ServiceOccurrence
|
||||
serviceIDs := map[string]bool{}
|
||||
for _, s := range services {
|
||||
if s.ChurchID == church.ID {
|
||||
churchServices = append(churchServices, s)
|
||||
serviceIDs[s.ID] = true
|
||||
}
|
||||
}
|
||||
churchAssignments := 0
|
||||
personIDs := map[string]bool{}
|
||||
for _, a := range assignments {
|
||||
if serviceIDs[a.ServiceID] {
|
||||
churchAssignments++
|
||||
personIDs[a.PersonID] = true
|
||||
}
|
||||
}
|
||||
open := 0
|
||||
for _, s := range churchServices {
|
||||
remaining := s.RequiredSlots() - len(assignmentsByService[s.ID])
|
||||
if remaining > 0 {
|
||||
open += remaining
|
||||
}
|
||||
}
|
||||
churchStats = append(churchStats, ChurchStat{
|
||||
Church: church, Assignments: churchAssignments, Services: len(churchServices),
|
||||
OpenSlots: open, PersonCount: len(personIDs),
|
||||
})
|
||||
}
|
||||
sort.SliceStable(churchStats, func(i, j int) bool {
|
||||
return churchStats[i].Church.SortIndex < churchStats[j].Church.SortIndex
|
||||
})
|
||||
|
||||
monthly := map[string]int{}
|
||||
for _, a := range assignments {
|
||||
s, ok := servicesByID[a.ServiceID]
|
||||
if ok {
|
||||
monthly[MonthKey(s.Date)]++
|
||||
}
|
||||
}
|
||||
|
||||
sundayServiceCount := 0
|
||||
sundayIDs := map[string]bool{}
|
||||
for _, s := range services {
|
||||
if IsSunday(s.Date) {
|
||||
sundayServiceCount++
|
||||
sundayIDs[s.ID] = true
|
||||
}
|
||||
}
|
||||
sundayFilled := 0
|
||||
for _, a := range assignments {
|
||||
if sundayIDs[a.ServiceID] {
|
||||
sundayFilled++
|
||||
}
|
||||
}
|
||||
requiredSlots := 0
|
||||
for _, s := range services {
|
||||
requiredSlots += s.RequiredSlots()
|
||||
}
|
||||
openSlots := requiredSlots - len(assignments)
|
||||
if openSlots < 0 {
|
||||
openSlots = 0
|
||||
}
|
||||
|
||||
preferenceHits, preferenceEligible := 0, 0
|
||||
for _, a := range assignments {
|
||||
s, ok := servicesByID[a.ServiceID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
person, ok := peopleByID[a.PersonID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if person.HasPreference() {
|
||||
preferenceEligible++
|
||||
if PreferenceHit(person, s) {
|
||||
preferenceHits++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
active := map[string]bool{}
|
||||
for _, p := range people {
|
||||
if p.Active {
|
||||
active[p.ID] = true
|
||||
}
|
||||
}
|
||||
absenceDays := 0
|
||||
for _, a := range absences {
|
||||
if !active[a.PersonID] {
|
||||
continue
|
||||
}
|
||||
clamped := a.Range().Clamped(r)
|
||||
if !clamped.Start.After(clamped.End) {
|
||||
absenceDays += clamped.DayCount()
|
||||
}
|
||||
}
|
||||
|
||||
return RangeStatistics{
|
||||
Range: r, ServiceCount: len(services), AssignmentCount: len(assignments),
|
||||
OpenSlots: openSlots, RequiredSlots: requiredSlots, PersonStats: personStats,
|
||||
ChurchStats: churchStats, MonthlyCounts: monthly, SundayServiceCount: sundayServiceCount,
|
||||
SundayFilled: sundayFilled, PreferenceHits: preferenceHits,
|
||||
PreferenceEligible: preferenceEligible, AbsenceDays: absenceDays,
|
||||
}
|
||||
}
|
||||
|
||||
func PreferenceHit(person Person, service ServiceOccurrence) bool {
|
||||
if StringContains(person.PreferredChurchIDs, service.ChurchID) {
|
||||
return true
|
||||
}
|
||||
if IntContains(person.PreferredWeekdays, Weekday(service.Date)) {
|
||||
return true
|
||||
}
|
||||
title := strings.ToLower(service.Title)
|
||||
for _, preference := range person.PreferredServiceTitles {
|
||||
if preference != "" && strings.Contains(title, strings.ToLower(preference)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user