2024-05-18 10:59:54 +00:00
|
|
|
package server
|
2024-05-17 17:56:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2024-05-18 10:59:54 +00:00
|
|
|
"xmr-remote-nodes/cmd"
|
2024-05-17 17:56:13 +00:00
|
|
|
"xmr-remote-nodes/internal/database"
|
|
|
|
"xmr-remote-nodes/internal/repo"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var probersCmd = &cobra.Command{
|
2024-05-17 18:42:31 +00:00
|
|
|
Use: "probers",
|
|
|
|
Short: "[server] Add, edit, delete, and show registered probers",
|
|
|
|
Long: `Command to administer prober machines.
|
|
|
|
|
|
|
|
This command should only be run on the server which directly connect to the MySQL database.
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
|
|
cmd.Help()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var listProbersCmd = &cobra.Command{
|
|
|
|
Use: "list [search]",
|
|
|
|
Short: "Print registered probers",
|
|
|
|
Long: `Print list of registered prober machines.
|
2024-05-17 17:56:13 +00:00
|
|
|
|
|
|
|
Use [search] args to filter results by name or api key.
|
|
|
|
|
|
|
|
"sort-by" flag can be "id" or "last_submit_ts"
|
|
|
|
"sort-dir" flag can be "asc" or "desc"`,
|
|
|
|
Example: `# To sort probers by last submit time in ascending order that contains "sin1":
|
2024-05-17 18:42:31 +00:00
|
|
|
xmr-nodes probers list -s last_submit_ts -d asc sin1`,
|
2024-05-17 17:56:13 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if err := database.ConnectDB(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
sortBy, _ := cmd.Flags().GetString("sort-by")
|
|
|
|
sortDir, _ := cmd.Flags().GetString("sort-dir")
|
|
|
|
|
|
|
|
probersRepo := repo.NewProberRepo(database.GetDB())
|
|
|
|
probers, err := probersRepo.Probers(repo.ProbersQueryParams{
|
|
|
|
Search: strings.Join(args, " "),
|
|
|
|
SortBy: sortBy,
|
|
|
|
SortDirection: sortDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(probers) == 0 {
|
|
|
|
fmt.Println("No probers found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
|
|
|
|
fmt.Fprintf(w, "ID\t| Name\t| Last Submit\t| API Key\n")
|
|
|
|
for _, prober := range probers {
|
|
|
|
fmt.Fprintf(w, "%d\t| %s\t| %s\t| %s\n",
|
|
|
|
prober.Id,
|
|
|
|
prober.Name,
|
|
|
|
time.Unix(prober.LastSubmitTs, 0).Format(time.RFC3339),
|
|
|
|
prober.ApiKey,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-17 18:42:31 +00:00
|
|
|
var addProbersCmd = &cobra.Command{
|
|
|
|
Use: "add [name]",
|
|
|
|
Short: "[server] Add new prober",
|
|
|
|
Long: `Add new prober machine.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Println("TODO: Add new prober")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:56:13 +00:00
|
|
|
func init() {
|
2024-05-18 10:59:54 +00:00
|
|
|
cmd.Root.AddCommand(probersCmd)
|
2024-05-17 18:42:31 +00:00
|
|
|
probersCmd.AddCommand(listProbersCmd)
|
|
|
|
probersCmd.AddCommand(addProbersCmd)
|
|
|
|
listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts")
|
|
|
|
listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")
|
2024-05-17 17:56:13 +00:00
|
|
|
}
|