-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_bots.go
54 lines (47 loc) · 1.47 KB
/
list_bots.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package backend
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/stellar/kelp/gui/model2"
"github.com/stellar/kelp/support/kelpos"
)
func (s *APIServer) listBots(w http.ResponseWriter, r *http.Request) {
log.Printf("listing bots\n")
resultBytes, e := s.kos.Blocking("ls", fmt.Sprintf("ls %s | sort", s.botConfigsPath.Unix()))
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("error when listing bots: %s\n", e))
return
}
configFiles := string(resultBytes)
files := strings.Split(configFiles, "\n")
bots := []model2.Bot{}
// run till one less than length of files because the last name will end in a newline
for i := 0; i < len(files)-1; i += 2 {
bot := model2.FromFilenames(files[i+1], files[i])
bots = append(bots, *bot)
}
log.Printf("bots available: %v", bots)
for _, bot := range bots {
botState, e := s.kos.QueryBotState(bot.Name)
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("unable to query bot state for bot '%s': %s\n", bot.Name, e))
return
}
log.Printf("found bot '%s' with state '%s'\n", bot.Name, botState)
// if page is reloaded then bot would already be registered, which is ok -- but we upsert here so it doesn't matter
if botState != kelpos.InitState() {
s.kos.RegisterBotWithStateUpsert(&bot, botState)
}
}
// serialize and return
botsJson, e := json.Marshal(bots)
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("unable to serialize bots: %s\n", e))
return
}
w.WriteHeader(http.StatusOK)
w.Write(botsJson)
}