challenge-sync-airbus: Ready for 2024

This commit is contained in:
nemunaire 2024-03-19 11:49:39 +01:00
parent 5a6d9047c2
commit ac966f9023
5 changed files with 275 additions and 117 deletions

View File

@ -1,22 +1,21 @@
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
type AirbusAPI struct {
BaseURL string
Token string
SessionID int64
SessionUUID string
BaseURL string
Token string
SessionID int64
InsecureSkipVerify bool
}
func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interface{}) error {
@ -25,7 +24,7 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSClientConfig: &tls.Config{InsecureSkipVerify: a.InsecureSkipVerify},
},
}
@ -39,7 +38,8 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
}
req.Header.Add("Authorization", "Bearer "+a.Token)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
@ -179,68 +179,21 @@ func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (er
Value: value,
}
log.Printf("AwardUser: %v", awards)
var marshalled []byte
marshalled, err = json.Marshal(awards)
if err != nil {
return
}
log.Printf("AwardUser: %s", marshalled)
if dryRun {
return
}
data := url.Values{}
data.Set("gaming_user_id", fmt.Sprintf("%d", awards.UserId))
data.Set("name", awards.Message)
data.Set("value", fmt.Sprintf("%d", awards.Value))
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), strings.NewReader(data.Encode()), nil)
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), bytes.NewReader(marshalled), nil)
if err != nil {
return err
}
return
}
type AirbusStats struct {
Data AirbusStatsData `json:"data"`
}
type AirbusStatsData struct {
BySessions []AirbusStatsSession `json:"by_sessions"`
}
func (s AirbusStatsData) GetSession(sessionId AirbusUUID) *AirbusStatsSession {
for _, session := range s.BySessions {
if session.UUID == sessionId {
return &session
}
}
return nil
}
type AirbusUUID string
type AirbusStatsSession struct {
UUID AirbusUUID `json:"uuid"`
Name string `json:"name"`
Duration string `json:"duration"`
TeamStats []AirbusTeamStats `json:"team_stats"`
}
func (s AirbusStatsSession) GetTeam(teamId AirbusUUID) *AirbusTeamStats {
for _, team := range s.TeamStats {
if team.UUID == teamId {
return &team
}
}
return nil
}
type AirbusTeamStats struct {
UUID AirbusUUID `json:"uuid"`
Name string `json:"name"`
Score int64 `json:"score"`
}
func (a *AirbusAPI) GetCurrentStats() (stats AirbusStats, err error) {
err = a.request("GET", "/stats", nil, &stats)
return
}

View File

@ -2,13 +2,16 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
"time"
@ -27,6 +30,8 @@ func main() {
flag.BoolVar(&dryRun, "dry-run", dryRun, "Don't perform any write action, just display")
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
flag.BoolVar(&noValidateChallenge, "no-validate-challenge", noValidateChallenge, "Consider challenge validation as a standard award (if each exercice hasn't been imported on their side)")
skipVerify := flag.Bool("skip-tls-verify", false, "Allow not verified certificates (INSECURE!)")
tsFromFile := flag.Bool("timestamp-from-file", false, "Load timestamp matching for score from filesystem instead of the API")
daemon := flag.Bool("watch", false, "Enable daemon mode by watching the directory")
tspath := flag.String("timestamp-file", "./REMOTE/timestamp", "Path to the file storing the last timestamp")
exercicespath := flag.String("exercices-file", "./REMOTE/exercices-bindings.json", "Path to the file containing the ID bindings")
@ -34,12 +39,20 @@ func main() {
flag.Parse()
api := AirbusAPI{
BaseURL: "https://portal.european-cybercup.lan/api",
BaseURL: "https://portal.european-cybercup.lan/api",
InsecureSkipVerify: *skipVerify,
}
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
api.BaseURL = v
}
if v, exists := os.LookupEnv("AIRBUS_SKIP_TLS_VERIFY"); exists {
var err error
api.InsecureSkipVerify, err = strconv.ParseBool(v)
if err != nil {
log.Fatal("Unable to parse boolean value in AIRBUS_SKIP_TLS_VERIFY:", err)
}
}
if v, exists := os.LookupEnv("AIRBUS_TOKEN"); exists {
api.Token = v
}
@ -68,39 +81,156 @@ func main() {
} else {
log.Println("Session ID discovered: ", api.SessionID)
}
}
} else {
sessions, err := api.GetSessions()
if err != nil {
log.Fatal("Unable to retrieve session (check your credentials!): ", err)
}
if v, exists := os.LookupEnv("AIRBUS_SESSIONUUID"); exists {
api.SessionUUID = v
log.Println("Please define your AIRBUS_SESSIONID or AIRBUS_SESSION_NAME.")
log.Println("Existing sessions are:")
for _, session := range sessions {
log.Printf(" - %d: %q", session.ID, session.Name)
}
os.Exit(1)
}
log.SetPrefix("[challenge-sync-airbus] ")
if flag.NArg() > 0 {
args := flag.Args()
switch args[0] {
case "list":
teams, err := api.GetTeams()
if err != nil {
log.Println("Unable to retrieve teams:", err)
os.Exit(1)
}
fmt.Println("## Airbus' registered teams:")
fmt.Println("-------------------------------------------")
fmt.Println(" ID | Name | Nb. | Score | Rank")
fmt.Println("-------------------------------------------")
for _, team := range teams {
fmt.Printf("% 2d | % 15s | % 3d | % 5d | % 3d\n", team.ID, team.Name, len(team.Members), team.Score, team.Rank)
}
case "rank":
teams, err := api.GetTeams()
if err != nil {
log.Println("Unable to retrieve teams:", err)
os.Exit(1)
}
ranking := []*AirbusTeam{}
for _, team := range teams {
tmp := team
ranking = append(ranking, &tmp)
}
sort.Sort(sort.Reverse(ByScore(ranking)))
fmt.Println("## Airbus' ranking:")
fmt.Println("-------------------------------------")
fmt.Println(" Rank | Name | ID | Score")
fmt.Println("-------------------------------------")
for _, team := range ranking {
fmt.Printf("% 5d | % 15s |% 3d | % 5d\n", team.Rank, team.Name, team.ID, team.Score)
}
case "get":
teams, err := api.GetTeams()
if err != nil {
log.Println("Unable to retrieve teams:", err)
os.Exit(1)
}
teamid, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
log.Println(err)
os.Exit(1)
}
for _, team := range teams {
if team.ID == teamid {
fmt.Printf("## Airbus' registered team %d:\n\nID: %d\nName: %s\nScore: %d\nRank: %d\nMembers:\n", teamid, team.ID, team.Name, team.Score, team.Rank)
for _, member := range team.Members {
fmt.Printf(" - ID: %d\n Name: %s\n Nickname: %s\n E-mail: %s\n", member.ID, member.Name, member.Nickname, member.EMail)
}
os.Exit(0)
}
}
fmt.Printf("Team %d not found. Use 'list' to view all existing teams\n", teamid)
case "award":
if len(args) < 3 {
fmt.Println("award <TEAM_ID> <VALUE> <MESSAGE>")
os.Exit(1)
}
teams, err := api.GetTeams()
if err != nil {
log.Println("Unable to retrieve teams:", err)
os.Exit(1)
}
teamid, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
log.Println("Invalid team id", err)
os.Exit(1)
}
value, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
log.Println("Invalid award value:", err)
os.Exit(1)
}
for _, team := range teams {
if team.ID == teamid {
err = api.AwardUser(&team, value, strings.Join(args[3:], " "))
if err != nil {
log.Println("Unable to award team:", err)
os.Exit(1)
}
fmt.Println("Team awarded")
os.Exit(0)
}
}
fmt.Printf("Team %d not found. Use 'list' to view all existing teams\n", teamid)
}
os.Exit(0)
}
var err error
TeamsDir = path.Clean(TeamsDir)
// Load the timestamp
ts, err := loadTS(*tspath)
if err != nil {
log.Fatal("Unable to open timestamp file: ", err.Error())
w := Walker{
API: api,
Coeff: *coeff,
}
// Load teams.json
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
w.Teams, err = getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Fatal("Unable to open teams bindings file: ", err.Error())
}
log.Println("Team bindings loaded: ", len(teamsbindings))
log.Println("Team bindings loaded: ", len(w.Teams))
w := Walker{
LastSync: ts,
Teams: teamsbindings,
API: api,
Coeff: *coeff,
}
// Fetch teams from Airbus API
if err = w.fetchTeams(); err != nil {
log.Fatal("Unable to fetch Airbus teams: ", err.Error())
}
// Load the timestamp
if *tsFromFile {
w.LastSync, err = loadTS(*tspath)
} else {
w.LastSync, err = loadTSFromAPI(w.TeamBindings)
}
if err != nil {
log.Fatal("Unable to open timestamp file: ", err.Error())
}
if !noValidateChallenge {
// Load exercices bindings
w.Exercices, err = ReadExercicesBindings(*exercicespath)
@ -110,6 +240,7 @@ func main() {
}
if !skipInitialSync {
log.Println("Doing initial score balance")
err = w.BalanceScores()
if err != nil {
log.Println("Something goes wrong during score balance: ", err.Error())
@ -120,9 +251,9 @@ func main() {
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
}
log.Println("initial sync done")
log.Println("initial sync done")
}
if daemon != nil && *daemon {
// Watch teams.json and scores.json
@ -145,7 +276,12 @@ func main() {
interrupt3 := make(chan os.Signal, 1)
signal.Notify(interrupt3, syscall.SIGUSR2)
ticker := time.NewTicker(5 * time.Second)
var ticker *time.Ticker
if *tsFromFile {
ticker = time.NewTicker(5 * time.Second)
} else {
ticker = time.NewTicker(5 * time.Minute)
}
watchedNotify := fsnotify.Create
@ -156,17 +292,27 @@ func main() {
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Println("Unable to open teams bindings file: ", err.Error())
return
} else {
w.Teams = teamsbindings
}
w.Teams = teamsbindings
if err = w.fetchTeams(); err != nil {
log.Fatal("Unable to fetch teams: ", err.Error())
log.Println("Unable to fetch teams: ", err.Error())
}
if !*tsFromFile {
ts, err := loadTSFromAPI(w.TeamBindings)
if err != nil {
log.Println("Unable to refresh timestamp: ", err.Error())
} else {
w.LastSync = ts
}
}
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
log.Println("Unable to save timestamp file: ", err.Error())
}
log.Println("SIGHUP treated.")
case <-interrupt2:
@ -180,7 +326,7 @@ func main() {
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
log.Println("Unable to save timestamp file: ", err.Error())
}
log.Println("SIGUSR1 treated.")
case <-interrupt3:
@ -189,17 +335,40 @@ func main() {
if err != nil {
log.Println("Unable to open teams bindings file: ", err.Error())
return
}
w.Teams = teamsbindings
if err = w.fetchTeams(); err != nil {
log.Fatal("Unable to fetch teams: ", err.Error())
} else {
w.Teams = teamsbindings
}
// FIXME
if err = w.fetchTeams(); err != nil {
log.Println("Unable to fetch teams: ", err.Error())
return
}
if !*tsFromFile {
ts, err := loadTSFromAPI(w.TeamBindings)
if err != nil {
log.Println("Unable to refresh timestamp: ", err.Error())
return
} else {
w.LastSync = ts
}
}
err = w.BalanceScores()
if err != nil {
log.Println("Unable to balance scores: ", err.Error())
}
log.Println("SIGUSR2 treated.")
case ev := <-watcher.Events:
if d, err := os.Lstat(ev.Name); err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
d, err := os.Lstat(ev.Name)
if err == nil && watchedNotify == fsnotify.Create && ev.Op&fsnotify.Write == fsnotify.Write {
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
watchedNotify = fsnotify.Write
}
if err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
// Register new subdirectory
if err := watchsubdir(watcher, ev.Name); err != nil {
log.Println(err)
@ -208,22 +377,24 @@ func main() {
if *debugINotify {
log.Println("Treating event:", ev, "for", ev.Name)
}
if watchedNotify == fsnotify.Write {
time.Sleep(100 * time.Millisecond)
}
if filepath.Base(ev.Name) == "scores.json" {
go w.treat(ev.Name)
} else if filepath.Base(ev.Name) == "teams.json" {
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Println("Unable to open teams bindings file: ", err.Error())
return
} else {
w.Teams = teamsbindings
}
w.Teams = teamsbindings
if err = w.fetchTeams(); err != nil {
log.Fatal("Unable to fetch teams: ", err.Error())
log.Println("Unable to fetch teams: ", err.Error())
}
}
} else if err == nil && ev.Op&fsnotify.Write == fsnotify.Write {
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
watchedNotify = fsnotify.Write
} else if err == nil && *debugINotify {
log.Println("Skipped event:", ev, "for", ev.Name)
}
@ -233,7 +404,7 @@ func main() {
// save current timestamp for teams
err = saveTS(*tspath, w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
log.Println("Unable to save timestamp file: ", err.Error())
}
}
}

View File

@ -8,6 +8,8 @@ type AirbusTeam struct {
ID int64 `json:"id"`
Members []TeamMember `json:"members"`
Name string `json:"name"`
Score int64 `json:"score"`
Rank int `json:"rank"`
}
type TeamMember struct {
@ -30,3 +32,15 @@ func (a *AirbusAPI) GetTeams() ([]AirbusTeam, error) {
return data.Data, nil
}
}
type ByRank []*AirbusTeam
func (a ByRank) Len() int { return len(a) }
func (a ByRank) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRank) Less(i, j int) bool { return a[i].Rank < a[j].Rank }
type ByScore []*AirbusTeam
func (a ByScore) Len() int { return len(a) }
func (a ByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByScore) Less(i, j int) bool { return a[i].Score < a[j].Score }

View File

@ -32,6 +32,20 @@ func loadTS(tspath string) (timestamp map[string]*TSValue, err error) {
}
}
func loadTSFromAPI(teams map[string]*AirbusTeam) (timestamp map[string]*TSValue, err error) {
now := time.Now()
timestamp = map[string]*TSValue{}
for _, team := range teams {
timestamp[team.Name] = &TSValue{
Time: now,
Score: team.Score,
}
}
return
}
func saveTS(tspath string, ts map[string]*TSValue) error {
if dryRun {
tmp := map[string]TSValue{}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"math"
"os"
"path/filepath"
"time"
@ -154,17 +155,17 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
}
// Found all new entries
maxts := &TSValue{
maxts := TSValue{
Time: time.Time{},
}
if ts, ok := w.LastSync[airbusTeam.Name]; ok {
maxts = ts
ts, ok := w.LastSync[airbusTeam.Name]
if ok {
maxts = *ts
} else {
ts = &TSValue{Time: time.Time{}}
}
for _, row := range teamscores {
if row.Time.After(maxts.Time) {
maxts.Time = row.Time
}
if ts, ok := w.LastSync[airbusTeam.Name]; !ok || row.Time.After(ts.Time) {
if row.Time.After(ts.Time) {
if !noValidateChallenge && row.Reason == "Validation" {
err = w.API.ValidateChallengeFromUser(airbusTeam, w.Exercices[row.IdExercice])
} else {
@ -177,20 +178,18 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
maxts.Score += int64(row.Points * row.Coeff * w.Coeff)
}
if row.Time.After(maxts.Time) {
maxts.Time = row.Time
}
}
w.LastSync[airbusTeam.Name] = maxts
w.LastSync[airbusTeam.Name] = &maxts
return nil
}
func (w *Walker) BalanceScores() error {
for team, ts := range w.LastSync {
team_id, ok := w.RevTeams[team]
if !ok {
continue
}
for team_id, team := range w.Teams {
myteam, err := w.loadMyFile(filepath.Join(TeamsDir, team_id, "my.json"))
if err != nil {
return fmt.Errorf("Unable to open %s/my.json: %w", team_id, err)
@ -198,13 +197,20 @@ func (w *Walker) BalanceScores() error {
airbusTeam := w.TeamBindings[fmt.Sprintf("%d", myteam.Id)]
if ts.Score != myteam.Points*int64(w.Coeff) {
err := w.API.AwardUser(airbusTeam, myteam.Points*int64(w.Coeff)-ts.Score, "Équilibrage")
expected_score := int64(math.Floor(float64(myteam.Points) * w.Coeff))
if airbusTeam == nil {
log.Printf("Skip team %q (tid=%d): no binding found", myteam.Name, myteam.Id)
} else if airbusTeam.Score != expected_score {
err := w.API.AwardUser(airbusTeam, expected_score-airbusTeam.Score, "Équilibrage")
if err != nil {
return fmt.Errorf("Unable to open %s/my.json: %w", team, err)
}
w.LastSync[airbusTeam.Name].Score = myteam.Points * int64(w.Coeff)
if _, ok := w.LastSync[airbusTeam.Name]; !ok {
w.LastSync[airbusTeam.Name] = &TSValue{}
}
w.LastSync[airbusTeam.Name].Score = expected_score
w.LastSync[airbusTeam.Name].Time = time.Now()
}
}