Files
silk/server-go/internal/service/region_stat.go
T

45 lines
1.1 KiB
Go

package service
import "sort"
// RegionDiseaseEntry 区域发病条目(trace_records 联查 rooms 得到)
type RegionDiseaseEntry struct {
Region string
Disease string
}
// RegionStat 区域发病统计
type RegionStat struct {
Region string `json:"region"`
Total int `json:"total"`
Diseases map[string]int `json:"diseases"`
}
// AggregateRegionStats 按区域分组统计发病数与病种分布(空区域剔除,按总数降序)
func AggregateRegionStats(entries []RegionDiseaseEntry) []RegionStat {
byRegion := make(map[string]*RegionStat)
for _, e := range entries {
if e.Region == "" {
continue
}
s, ok := byRegion[e.Region]
if !ok {
s = &RegionStat{Region: e.Region, Diseases: map[string]int{}}
byRegion[e.Region] = s
}
s.Total++
s.Diseases[e.Disease]++
}
result := make([]RegionStat, 0, len(byRegion))
for _, s := range byRegion {
result = append(result, *s)
}
sort.Slice(result, func(i, j int) bool {
if result[i].Total != result[j].Total {
return result[i].Total > result[j].Total
}
return result[i].Region < result[j].Region
})
return result
}