remote-challenge-sync-airbus: WIP
This commit is contained in:
parent
a82defe2a7
commit
0831ea6088
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -13,14 +14,20 @@ type AirbusAPI struct {
|
|||||||
BaseURL string
|
BaseURL string
|
||||||
Token string
|
Token string
|
||||||
SessionID int64
|
SessionID int64
|
||||||
|
SessionUUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{}) error {
|
func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{}) error {
|
||||||
var reader *bytes.Reader
|
var req *http.Request
|
||||||
|
var err error
|
||||||
|
|
||||||
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
reader = bytes.NewReader(data)
|
req, err = http.NewRequest(method, a.BaseURL+endpoint, bytes.NewReader(data))
|
||||||
|
} else {
|
||||||
|
req, err = http.NewRequest(method, a.BaseURL+endpoint, nil)
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(method, a.BaseURL+endpoint, reader)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to prepare request to %q: %w", endpoint, err)
|
return fmt.Errorf("unable to prepare request to %q: %w", endpoint, err)
|
||||||
}
|
}
|
||||||
@ -61,12 +68,18 @@ func (aui AirbusUserId) String() string {
|
|||||||
return strconv.FormatInt(int64(aui), 10)
|
return strconv.FormatInt(int64(aui), 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AirbusUser struct {
|
type AirbusUserData struct {
|
||||||
Id AirbusUserId `json:"id"`
|
Data []AirbusUser `json:"data"`
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) GetUsers() (users []AirbusUser, err error) {
|
type AirbusUser struct {
|
||||||
|
Id AirbusUserId `json:"id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Nickname string `json:"nickname"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AirbusAPI) GetUsers() (users AirbusUserData, err error) {
|
||||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/users", a.SessionID), nil, &users)
|
err = a.request("GET", fmt.Sprintf("/sessions/%d/users", a.SessionID), nil, &users)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -77,7 +90,7 @@ func (a *AirbusAPI) GetUserFromName(name string) (*AirbusUser, error) {
|
|||||||
return nil, fmt.Errorf("unable to retrieve users list: %w", err)
|
return nil, fmt.Errorf("unable to retrieve users list: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, u := range users {
|
for _, u := range users.Data {
|
||||||
if u.Name == name {
|
if u.Name == name {
|
||||||
return &u, nil
|
return &u, nil
|
||||||
}
|
}
|
||||||
@ -86,19 +99,38 @@ func (a *AirbusAPI) GetUserFromName(name string) (*AirbusUser, error) {
|
|||||||
return nil, fmt.Errorf("unable to find user %q", name)
|
return nil, fmt.Errorf("unable to find user %q", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AirbusAPI) GetUserFromEmail(email string) (*AirbusUser, error) {
|
||||||
|
users, err := a.GetUsers()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to retrieve users list: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, u := range users.Data {
|
||||||
|
if u.Email == email {
|
||||||
|
return &u, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("unable to find user with email %q", email)
|
||||||
|
}
|
||||||
|
|
||||||
type AirbusChallengeId int64
|
type AirbusChallengeId int64
|
||||||
|
|
||||||
func (aci AirbusChallengeId) String() string {
|
func (aci AirbusChallengeId) String() string {
|
||||||
return strconv.FormatInt(int64(aci), 10)
|
return strconv.FormatInt(int64(aci), 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AirbusChallengeData struct {
|
||||||
|
Data []AirbusChallenge `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
type AirbusChallenge struct {
|
type AirbusChallenge struct {
|
||||||
Id AirbusChallengeId `json:"id"`
|
Id AirbusChallengeId `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) GetChallenges() (challenges []AirbusChallenge, err error) {
|
func (a *AirbusAPI) GetChallenges() (challenges AirbusChallengeData, err error) {
|
||||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/challenges", a.SessionID), nil, &challenges)
|
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/challenges", a.SessionID), nil, &challenges)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +140,7 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
|
|||||||
return nil, fmt.Errorf("unable to retrieve challenges list: %w", err)
|
return nil, fmt.Errorf("unable to retrieve challenges list: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range challenges {
|
for _, c := range challenges.Data {
|
||||||
if c.Name == name {
|
if c.Name == name {
|
||||||
return &c, nil
|
return &c, nil
|
||||||
}
|
}
|
||||||
@ -118,7 +150,7 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) ValidateChallengeFromUser(userId AirbusUserId, challengeId AirbusChallengeId) (err error) {
|
func (a *AirbusAPI) ValidateChallengeFromUser(userId AirbusUserId, challengeId AirbusChallengeId) (err error) {
|
||||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/%s/%s/validate", a.SessionID, challengeId.String(), userId.String()), nil, nil)
|
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/%s/%s/validate", a.SessionID, challengeId.String(), userId.String()), nil, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +172,7 @@ func (a *AirbusAPI) AwardUser(userId AirbusUserId, value int64, message string)
|
|||||||
return fmt.Errorf("unable to marshall JSON from awards struct: %w", err)
|
return fmt.Errorf("unable to marshall JSON from awards struct: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = a.request("POST", fmt.Sprintf("/sessions/%d/awards", a.SessionID), j, nil)
|
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), j, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gopkg.in/fsnotify.v1"
|
"gopkg.in/fsnotify.v1"
|
||||||
)
|
)
|
||||||
@ -30,7 +31,7 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
api := AirbusAPI{
|
api := AirbusAPI{
|
||||||
BaseURL: "https://portal.european-cybercup.lan/api/v1",
|
BaseURL: "https://portal.european-cybercup.lan/api",
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
|
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
|
||||||
@ -46,6 +47,9 @@ func main() {
|
|||||||
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
|
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if v, exists := os.LookupEnv("AIRBUS_SESSIONUUID"); exists {
|
||||||
|
api.SessionUUID = v
|
||||||
|
}
|
||||||
|
|
||||||
log.SetPrefix("[challenge-sync-airbus] ")
|
log.SetPrefix("[challenge-sync-airbus] ")
|
||||||
|
|
||||||
@ -79,7 +83,7 @@ func main() {
|
|||||||
|
|
||||||
if !skipInitialSync {
|
if !skipInitialSync {
|
||||||
// Iterate over teams scores
|
// Iterate over teams scores
|
||||||
err = filepath.WalkDir(TeamsDir, w.WalkScore)
|
err = filepath.WalkDir(TeamsDir, w.WalkScoreSync)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Something goes wrong during walking")
|
log.Printf("Something goes wrong during walking")
|
||||||
}
|
}
|
||||||
@ -112,7 +116,7 @@ func main() {
|
|||||||
interrupt3 := make(chan os.Signal, 1)
|
interrupt3 := make(chan os.Signal, 1)
|
||||||
signal.Notify(interrupt3, syscall.SIGUSR2)
|
signal.Notify(interrupt3, syscall.SIGUSR2)
|
||||||
|
|
||||||
c := time.NewTicker(5 * time.Second)
|
ticker := time.NewTicker(5 * time.Second)
|
||||||
|
|
||||||
watchedNotify := fsnotify.Create
|
watchedNotify := fsnotify.Create
|
||||||
|
|
||||||
@ -136,7 +140,7 @@ func main() {
|
|||||||
case <-interrupt2:
|
case <-interrupt2:
|
||||||
log.Println("SIGUSR1 received, resynching all teams")
|
log.Println("SIGUSR1 received, resynching all teams")
|
||||||
// Iterate over teams scores
|
// Iterate over teams scores
|
||||||
err = filepath.WalkDir(TeamsDir, w.WalkScore)
|
err = filepath.WalkDir(TeamsDir, w.WalkScoreSync)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Something goes wrong during walking")
|
log.Printf("Something goes wrong during walking")
|
||||||
}
|
}
|
||||||
|
@ -38,11 +38,11 @@ func (w *Walker) treat(path string) {
|
|||||||
airbusTeamId := NewAirbusUserId(w.Teams[fmt.Sprintf("%d", teammy.Id)].ExternalId)
|
airbusTeamId := NewAirbusUserId(w.Teams[fmt.Sprintf("%d", teammy.Id)].ExternalId)
|
||||||
|
|
||||||
// Treat score grid
|
// Treat score grid
|
||||||
err = w.TreatScoreGrid(path, airbusTeamId)
|
/*err = w.TreatScoreGrid(path, airbusTeamId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to treat score grid:", err)
|
log.Println("Unable to treat score grid:", err)
|
||||||
return
|
return
|
||||||
}
|
}*/
|
||||||
|
|
||||||
// Balance scores
|
// Balance scores
|
||||||
err = w.BalanceScore(int64(float64(teammy.Points)*w.Coeff), airbusTeamId)
|
err = w.BalanceScore(int64(float64(teammy.Points)*w.Coeff), airbusTeamId)
|
||||||
@ -53,6 +53,13 @@ func (w *Walker) treat(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Walker) WalkScoreSync(path string, d os.DirEntry, err error) error {
|
||||||
|
if filepath.Base(path) == "scores.json" {
|
||||||
|
w.treat(path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Walker) WalkScore(path string, d os.DirEntry, err error) error {
|
func (w *Walker) WalkScore(path string, d os.DirEntry, err error) error {
|
||||||
if filepath.Base(path) == "scores.json" {
|
if filepath.Base(path) == "scores.json" {
|
||||||
go w.treat(path)
|
go w.treat(path)
|
||||||
@ -104,7 +111,7 @@ func (w *Walker) BalanceScore(score int64, airbusTeamId AirbusUserId) error {
|
|||||||
fmt.Errorf("unable to retrieve current stats: %w", err)
|
fmt.Errorf("unable to retrieve current stats: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
my_session := stats.Data.GetSession(AirbusUUID(w.API.SessionID))
|
my_session := stats.Data.GetSession(AirbusUUID(w.API.SessionUUID))
|
||||||
if my_session == nil {
|
if my_session == nil {
|
||||||
return fmt.Errorf("session not found")
|
return fmt.Errorf("session not found")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user