[admin] statistic generation
This commit is contained in:
parent
e11cd88ee2
commit
24cbed411e
@ -85,6 +85,12 @@ func listTeam(args []string, body []byte) (interface{}, error) {
|
||||
return fic.MyJSONTeam(team, true)
|
||||
} else if args[1] == "wait.json" {
|
||||
return fic.MyJSONTeam(team, false)
|
||||
} else if args[1] == "stats.json" {
|
||||
if team != nil {
|
||||
return team.GetStats()
|
||||
} else {
|
||||
return fic.GetTeamsStats(nil)
|
||||
}
|
||||
} else if args[1] == "tries" {
|
||||
return fic.GetTries(team, nil)
|
||||
} else if team != nil && args[1] == "members" {
|
||||
|
102
libfic/team.go
102
libfic/team.go
@ -226,6 +226,108 @@ func (t Team) HasSolved(e Exercice) (bool, time.Time, int64) {
|
||||
}
|
||||
}
|
||||
|
||||
func IsSolved(e Exercice) (int, time.Time) {
|
||||
var nb *int
|
||||
var tm *time.Time
|
||||
if DBQueryRow("SELECT COUNT(id_exercice), MIN(time) FROM exercice_solved WHERE id_exercice = ?", e.Id).Scan(&nb, &tm); nb == nil || tm == nil {
|
||||
return 0, time.Time{}
|
||||
} else {
|
||||
return *nb, *tm
|
||||
}
|
||||
}
|
||||
|
||||
type statLine struct {
|
||||
Tip string `json:"tip"`
|
||||
Total int `json:"total"`
|
||||
Solved int `json:"solved"`
|
||||
Tried int `json:"tried"`
|
||||
Tries int `json:"tries"`
|
||||
}
|
||||
|
||||
type teamStats struct {
|
||||
Levels []statLine `json:"levels"`
|
||||
Themes []statLine `json:"themes"`
|
||||
}
|
||||
|
||||
func (s *teamStats) GetLevel(level int) *statLine {
|
||||
level -= 1
|
||||
|
||||
for len(s.Levels) <= level {
|
||||
s.Levels = append(s.Levels, statLine{
|
||||
fmt.Sprintf("Level %d", (len(s.Levels) + 1)),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
})
|
||||
}
|
||||
|
||||
return &s.Levels[level]
|
||||
}
|
||||
|
||||
func (t Team) GetStats() (interface{}, error) {
|
||||
return GetTeamsStats(&t)
|
||||
}
|
||||
|
||||
func GetTeamsStats(t *Team) (interface{}, error) {
|
||||
stat := teamStats{}
|
||||
|
||||
if themes, err := GetThemes(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, theme := range themes {
|
||||
total := 0
|
||||
solved := 0
|
||||
tried := 0
|
||||
tries := 0
|
||||
|
||||
if exercices, err := theme.GetExercices(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, exercice := range exercices {
|
||||
var lvl int
|
||||
if lvl, err = exercice.GetLevel(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sLvl := stat.GetLevel(lvl)
|
||||
|
||||
total += 1
|
||||
sLvl.Total += 1
|
||||
|
||||
if t != nil {
|
||||
if b, _, _ := t.HasSolved(exercice); b {
|
||||
solved += 1
|
||||
sLvl.Solved += 1
|
||||
}
|
||||
} else {
|
||||
if n, _ := IsSolved(exercice); n > 0 {
|
||||
solved += 1
|
||||
sLvl.Solved += 1
|
||||
}
|
||||
}
|
||||
|
||||
try := NbTry(t, exercice)
|
||||
if try > 0 {
|
||||
tried += 1
|
||||
tries += try
|
||||
sLvl.Tried += 1
|
||||
sLvl.Tries += try
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stat.Themes = append(stat.Themes, statLine{
|
||||
theme.Name,
|
||||
total,
|
||||
solved,
|
||||
tried,
|
||||
tries,
|
||||
})
|
||||
}
|
||||
|
||||
return stat, nil
|
||||
}
|
||||
}
|
||||
|
||||
type exportedTeam struct {
|
||||
Name string `json:"name"`
|
||||
|
Loading…
Reference in New Issue
Block a user