Skip to content

Commit 74d24cb

Browse files
committed
pkg/fms: Add web-based team mapping
1 parent 3db7634 commit 74d24cb

File tree

9 files changed

+283
-8
lines changed

9 files changed

+283
-8
lines changed

pkg/fms/fms.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package fms
22

33
import (
44
"context"
5+
"embed"
56
"fmt"
67
"io/fs"
78
nhttp "net/http"
89
"os"
910
"strings"
1011
"sync"
1112
"time"
12-
"embed"
1313

14-
"github.com/flosch/pongo2/v5"
14+
"github.com/flosch/pongo2/v6"
1515
"github.com/go-chi/chi/v5"
1616
"github.com/hashicorp/go-hclog"
1717

@@ -62,6 +62,8 @@ func New(opts ...Option) (*FMS, error) {
6262
}
6363

6464
pongo2.RegisterFilter("valueok", x.filterValueOK)
65+
pongo2.RegisterFilter("split", x.filterSplit)
66+
pongo2.RegisterFilter("teamName", x.filterTeamName)
6567

6668
sfs, _ := fs.Sub(uifs, "ui")
6769
r.Handle("/static/*", nhttp.FileServer(nhttp.FS(sfs)))
@@ -83,6 +85,32 @@ func New(opts ...Option) (*FMS, error) {
8385
hr.Get("/", x.fieldHUD)
8486
})
8587
})
88+
89+
r.Route("/api", func(r chi.Router) {
90+
r.Route("/field", func(r chi.Router) {
91+
r.Get("/configured-quads", x.configuredQuads)
92+
})
93+
r.Route("/map", func(r chi.Router) {
94+
r.Get("/current", x.apiGetCurrentMap)
95+
r.Get("/stage", x.apiGetStageMap)
96+
r.Post("/stage", x.apiUpdateStageMap)
97+
r.Post("/commit-stage", x.apiCommitStageMap)
98+
})
99+
})
100+
101+
r.Route("/ui", func(r chi.Router) {
102+
r.Route("/admin", func(r chi.Router) {
103+
r.Get("/", x.uiViewAdminLanding)
104+
105+
r.Route("/map", func(r chi.Router) {
106+
r.Get("/current", x.uiViewCurrentMap)
107+
r.Get("/stage", x.uiViewStageMap)
108+
r.Post("/stage", x.uiViewUpdateStageMap)
109+
r.Post("/commit-stage", x.uiViewCommitStageMap)
110+
})
111+
})
112+
})
113+
86114
r.Get("/metrics-sd", x.promSD)
87115

88116
x.s.Mount("/", r)
@@ -123,7 +151,7 @@ func (f *FMS) doTemplate(w nhttp.ResponseWriter, r *nhttp.Request, tmpl string,
123151
}
124152
}
125153

126-
func (f *FMS) filterValueOK(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
154+
func (f *FMS) filterValueOK(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
127155
list := strings.Split(param.String(), ",")
128156
for _, val := range list {
129157
if strings.TrimSpace(in.String()) == strings.TrimSpace(val) {
@@ -133,6 +161,20 @@ func (f *FMS) filterValueOK(in *pongo2.Value, param *pongo2.Value) (*pongo2.Valu
133161
return pongo2.AsValue(false), nil
134162
}
135163

164+
func (f *FMS) filterSplit(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
165+
list := strings.Split(in.String(), param.String())
166+
return pongo2.AsValue(list), nil
167+
}
168+
169+
func (f *FMS) filterTeamName(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
170+
t, ok := in.Interface().(*Team)
171+
if !ok {
172+
f.l.Error("Something that wasn't a team got passed to the teamName filter", "in", in.Interface())
173+
return pongo2.AsValue(""), &pongo2.Error{Sender: "filter:teamName"}
174+
}
175+
return pongo2.AsValue(t.Name), nil
176+
}
177+
136178
func (f *FMS) populateHUDVersions() {
137179
fw := os.Getenv("GIZMO_HUD_FWVERSIONS")
138180
if fw != "" {

pkg/fms/hud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strconv"
66
"strings"
77

8-
"github.com/flosch/pongo2/v5"
8+
"github.com/flosch/pongo2/v6"
99

1010
"github.com/gizmo-platform/gizmo/pkg/config"
1111
)

pkg/fms/types.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
package fms
55

66
import (
7+
"sort"
78
"sync"
89
"time"
910

10-
"github.com/flosch/pongo2/v5"
11+
"github.com/flosch/pongo2/v6"
1112
"github.com/hashicorp/go-hclog"
1213

1314
"github.com/gizmo-platform/gizmo/pkg/config"
@@ -36,6 +37,10 @@ type TeamLocationMapper interface {
3637
GetFieldForTeam(int) (string, error)
3738
GetCurrentMapping() (map[int]string, error)
3839
InsertOnDemandMap(map[int]string) error
40+
41+
GetStageMapping() (map[int]string, error)
42+
InsertStageMapping(map[int]string) error
43+
CommitStagedMap() error
3944
}
4045

4146
type hudVersions struct {
@@ -81,6 +86,7 @@ type IntegrationSlice []Integration
8186
// FMS
8287
type Team struct {
8388
Name string
89+
Number int
8490
SSID string
8591
PSK string
8692
VLAN int
@@ -152,3 +158,18 @@ type Config struct {
152158
AdvancedBGPPeerIP string
153159
AdvancedBGPVLAN int
154160
}
161+
162+
// SortedTeams returns a list of teams that are sorted by the team
163+
// number. This makes it easy to visually scan a team list and know
164+
// roughly where they should be.
165+
func (c *Config) SortedTeams() []*Team {
166+
out := []*Team{}
167+
for n, t := range c.Teams {
168+
t.Number = n
169+
out = append(out, t)
170+
}
171+
sort.Slice(out, func(i, j int) bool {
172+
return out[i].Number < out[j].Number
173+
})
174+
return out
175+
}

pkg/fms/ui/p2/base.p2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
{% endblock %}
1313
</head>
1414
<body class="{% block bodystyle %}background{% endblock %}">
15-
{% if shownav %}
1615
{% include "fragments/nav.p2" %}
17-
{% endif %}
1816
<div id="content">
1917
{% block content %}
2018
{% endblock %}

pkg/fms/ui/p2/fragments/nav.p2

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
<div class="nav-container">
99
<div class="nav-header">Setup</div>
1010
<div class="nav-dropdown">
11-
<a class="nav-item" href="/admin/setup/oobe/">Out of Box</a>
11+
<a class="nav-item" href="/ui/admin/setup/oobe/">Out of Box</a>
12+
</div>
13+
</div>
14+
<div class="nav-container">
15+
<div class="nav-header">Operate</div>
16+
<div class="nav-dropdown">
17+
<a class="nav-item" href="/ui/admin/map/current">Current Mapping</a>
18+
<a class="nav-item" href="/ui/admin/map/stage">Stage Mapping</a>
1219
</div>
1320
</div>
1421
</nav>

pkg/fms/ui/p2/views/map/current.p2

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends "../../base.p2" %}
2+
3+
{% block title %}Current Field Mapping | Gizmo FMS{% endblock %}
4+
5+
{% block content %}
6+
<div class="flex-container flex-row flex-center">
7+
<div class="flex-item flex-max foreground box">
8+
<table>
9+
<tr>
10+
<th>Field</th>
11+
<th>Position</th>
12+
<th>Current</th>
13+
</tr>
14+
{% for q in quads %}
15+
<tr>
16+
<td>{{ q|split:":"|first|cut:"field" }}</td>
17+
<td>{{ q|split:":"|last|capfirst }}</td>
18+
<td>
19+
{% if active[q] %}
20+
{{ active[q] }} ({{ teams[active[q]]|teamName }})
21+
{% else %}
22+
No Team
23+
{% endif %}
24+
</td>
25+
{% endfor %}
26+
</table>
27+
</div>
28+
</div>
29+
{% endblock %}

pkg/fms/ui/p2/views/map/stage.p2

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% extends "../../base.p2" %}
2+
3+
{% block title %}Stage Field Mapping | Gizmo FMS{% endblock %}
4+
5+
{% block content %}
6+
<div class="flex-container flex-row flex-center">
7+
<div class="flex-item flex-max foreground box">
8+
<form id="stageform" method="post">
9+
<table>
10+
<tr>
11+
<th>Field</th>
12+
<th>Position</th>
13+
<th>Current</th>
14+
<th>Staged</th>
15+
</tr>
16+
{% for q in quads %}
17+
<tr>
18+
<td>{{ q|split:":"|first|cut:"field" }}</td>
19+
<td>{{ q|split:":"|last|capfirst }}</td>
20+
<td>
21+
{% if active[q] %}
22+
{{ active[q] }} ({{ teams[active[q]]|teamName }})
23+
{% else %}
24+
No Team
25+
{% endif %}
26+
</td>
27+
<td>
28+
<select name="{{ q }}">
29+
<option value="0" {% if not stage[q] %}selected{% endif %}>Empty</option>
30+
{% for team in roster %}
31+
<option value="{{ team.Number }}" {% if team.Number == stage[q] %}selected{% endif %}>{{ team.Number }} ({{ team.Name }})</option>
32+
{% endfor %}
33+
</select>
34+
</td>
35+
</tr>
36+
{% endfor %}
37+
</table>
38+
</form>
39+
<form id="commitform" method="post" action="/ui/admin/map/commit-stage"></form>
40+
<div class="flex-container flex-row flex-center">
41+
<input form="stageform" type="submit" value="Save Stage Map" class="flex-item button" />
42+
<input form="commitform" type="submit" value="Commit Staging Map" class="flex-item button" />
43+
</div>
44+
</div>
45+
</div>
46+
{% endblock %}

pkg/fms/ui/static/css/theme.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ p {
7777
height: auto;
7878
}
7979

80+
.button {
81+
margin: 0.25em;
82+
padding: 1em;
83+
}
84+
8085
.center {
8186
text-align: center;
8287
}

0 commit comments

Comments
 (0)