The Sessions page polls its list and its stats every 15s, the Usage page its clients, and each asked agent.Clients() only for each agent's id, name and icon. Clients() builds every agent (30+ constructors): 2.5 ms, 378 KB and 3411 allocations a call on the Windows box with discovery's real 2s look window, mostly os.Stat, filepath.Join and Getenv. On the real-account run (t69, 4 min) agent.All under the sessions handler was 22 MB of 210 MB allocated. agent.Looks keeps that id/name/shared-name/icon list for 30s, built again at once when the plugins' agents change (agentplug's set) or the Agents page's refresh (Rescan). A new omp or QwenPaw profile shows by its id for up to 30s. Nothing is kept in a test binary unless a test says, so tests that add an agent read it at once. The GUI's sessions list, stats, one session, markdown export, trash, manage and reads views, and usageapi.Clients, read Looks. Tests: TestLooksBuiltOnceAWhile — six asks build once, the list equals Clients(), a plugin agent switched on and off shows at once, Rescan builds again. Broken by hand (memo check made false): "six asks built the agents 6 times; want once". Run on the Windows box: go test ./internal/agent ./internal/gui ./internal/usageapi at the pre-rebase base0cc91af3d: usageapi ok, agent and gui with 38 reds. Those tests, run at8aae124b0with and without this change: the same 37 fail on both (#6068). TestLooks/TestPlugged/TestRescan ok and go vet ok at8aae124b0. mote (macOS) refused ssh all session, so no macOS run and no real-account measurement yet. Rebased on156861b3aafter the runs; its new commits touch library, plugin and site only.
91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"slices"
|
|
"sort"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Which agents' session history magpie may read (RustCats on X): the
|
|
// Sessions page asks once on a fresh install, and Settings changes it per
|
|
// agent later. sessions.Reads is the gate every reader asks.
|
|
|
|
type readAgentJSON struct {
|
|
Agent string `json:"agent"`
|
|
Name string `json:"name"`
|
|
Icon string `json:"icon"`
|
|
On bool `json:"on"`
|
|
}
|
|
|
|
type readsJSON struct {
|
|
// Asked: the user said which agents may be read (or the install read
|
|
// them all before there was a choice)
|
|
Asked bool `json:"asked"`
|
|
// All: every agent, those found later too, unless one is turned off
|
|
All bool `json:"all"`
|
|
// Found: the agents with session history on this computer, and those
|
|
// the user named
|
|
Found []readAgentJSON `json:"found"`
|
|
// Gateway: gateway mode, which reads no agent's files here whatever
|
|
// the choice (ownSessionsOff), so nothing is asked and none is found
|
|
Gateway bool `json:"gateway,omitempty"`
|
|
}
|
|
|
|
func readsNow() readsJSON {
|
|
r := sessions.SettleReads()
|
|
if s := settings.Shared().SessionReads; s != nil {
|
|
r = s
|
|
}
|
|
out := readsJSON{Found: []readAgentJSON{}, Gateway: ownSessionsOff()}
|
|
if r != nil {
|
|
out.Asked, out.All = r.Asked, r.All
|
|
}
|
|
agents := sessions.Found()
|
|
if r != nil {
|
|
for a := range r.Agents {
|
|
if !slices.Contains(agents, a) {
|
|
agents = append(agents, a)
|
|
}
|
|
}
|
|
}
|
|
looks := agentLooks()
|
|
for _, a := range agents {
|
|
j := readAgentJSON{Agent: a, Name: a, Icon: "generic", On: r.Reads(a)}
|
|
if l, ok := looks[a]; ok {
|
|
j.Name, j.Icon = l.Shared, l.Icon
|
|
}
|
|
out.Found = append(out.Found, j)
|
|
}
|
|
sort.SliceStable(out.Found, func(i, j int) bool { return out.Found[i].Name < out.Found[j].Name })
|
|
return out
|
|
}
|
|
|
|
func sessionReadsRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/sessions/reads", func(rw http.ResponseWriter, r *http.Request) {
|
|
writeJSON(rw, readsNow())
|
|
})
|
|
// {all, agents: {id: on}}: all, when sent, is whether an agent not
|
|
// named is read (allow all sends true, allow selected false), and each
|
|
// agent named is read or not; either way the choice is made (asked).
|
|
mux.HandleFunc("POST /api/sessions/reads", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
All *bool `json:"all"`
|
|
Agents map[string]bool `json:"agents"`
|
|
}
|
|
if err := json.NewDecoder(http.MaxBytesReader(rw, r.Body, 16384)).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if err := sessions.SetReads(in.All, in.Agents); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
forgetStats()
|
|
writeJSON(rw, readsNow())
|
|
})
|
|
}
|