HeheLi29136651 asked for the Day card's weekly, monthly and total bill. The
read only answered one day, so the plugin had nothing to draw. usage:day
now takes {from: "YYYY-MM-DD" | "all"} beside date: the days from there to
date, with the same totals, agents, models and days list (usageapi.SpanOf;
DayOf is SpanOf with no from). "all" starts at the first day with calls:
the ledger keeps old calls in wider buckets that start before the first
call, so From is taken from the first day the heatmap lists. A from after
date, or a date after today, is an error. Answers carry `from`, which is how
a plugin tells a magpie that has spans from one that ignores the param.
Run, on mote:
- TestSpanOf (week, month, all, all up to an earlier date, day alone,
bad inputs) with calls 300 days back, where the bucket starts before the
first call: red when the From correction is removed (From 2026-01-12 for
a first call on 01-15), green with it.
- TestPluginPageReadsADay gains from "2000-01-01", from "all" and a from
after the day.
- go vet (darwin, windows, linux -tags nogui), usageapi and internal/plugin
tests ok.
- The plugin side (page-day-card 0.3.0) reads it: seen in the sandbox web
instance in chromium and webkit, empty ledger only.
Not tried: a ledger with real calls end to end in the page (the plugin's
test covers the card from a made-up read), and a magpie from before this
change opening the 0.3.0 page (the code path throws "usage:day from" when
there is no `from`, and says to update).
117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
"github.com/yetone/magpie/internal/usageapi"
|
|
)
|
|
|
|
// The usage:day read (plugin.PageReads): a day of usage for a plugin's
|
|
// page, by agent and by model, with the logos the Usage page draws for
|
|
// them. A page loads nothing but its own files and data: pictures
|
|
// (pagePolicy), so each logo comes as a data: URL, once for each name.
|
|
|
|
// pageIcon is a logo as a page draws it: Mono a one-colour mark the page
|
|
// fills with its text colour, as the window's mask icons are (app.js's
|
|
// icon), else a picture drawn as it is.
|
|
type pageIcon struct {
|
|
Src string `json:"src"`
|
|
Mono bool `json:"mono,omitempty"`
|
|
}
|
|
|
|
// maxPageIcon is the largest logo a read carries: a picture of the user's
|
|
// own past it is drawn as the generic one.
|
|
const maxPageIcon = 256 << 10
|
|
|
|
// pageIconOf is the logo named name, as app.js's icon finds it: a picture
|
|
// the user gave a provider (file:…), else the bundled SVG or PNG; ok is
|
|
// false for "generic", "" and a name with no file.
|
|
func pageIconOf(name string) (pageIcon, bool) {
|
|
if name == "" || name == "generic" {
|
|
return pageIcon{}, false
|
|
}
|
|
if own, ok := strings.CutPrefix(name, "file:"); ok {
|
|
f, _ := provider.IconServed(own)
|
|
if f == "" {
|
|
return pageIcon{}, false
|
|
}
|
|
b, err := os.ReadFile(f)
|
|
if err != nil || len(b) > maxPageIcon {
|
|
return pageIcon{}, false
|
|
}
|
|
return pageIcon{Src: dataURL(own, b)}, true
|
|
}
|
|
// a URL or a path is no bundled name (app.js draws it as generic)
|
|
if strings.ContainsAny(name, ":/\\") {
|
|
return pageIcon{}, false
|
|
}
|
|
for _, ext := range []string{".svg", ".png"} {
|
|
b, err := fs.ReadFile(assets, "assets/icons/"+name+ext)
|
|
if err != nil || len(b) > maxPageIcon {
|
|
continue
|
|
}
|
|
return pageIcon{Src: dataURL(name+ext, b), Mono: ext == ".svg" && !strings.HasSuffix(name, "-color")}, true
|
|
}
|
|
return pageIcon{}, false
|
|
}
|
|
|
|
func dataURL(name string, b []byte) string {
|
|
ctype := "image/svg+xml"
|
|
switch strings.ToLower(path.Ext(name)) {
|
|
case ".png":
|
|
ctype = "image/png"
|
|
case ".jpg", ".jpeg":
|
|
ctype = "image/jpeg"
|
|
case ".webp":
|
|
ctype = "image/webp"
|
|
}
|
|
return "data:" + ctype + ";base64," + base64.StdEncoding.EncodeToString(b)
|
|
}
|
|
|
|
// pageDay is the usage:day read: params {date: "YYYY-MM-DD", from:
|
|
// "YYYY-MM-DD" or "all", tz: an IANA zone}, all optional (today, the day
|
|
// alone, this computer's zone): with a from it is the days from there to
|
|
// date (usageapi.SpanOf), a week's or a month's or all time's.
|
|
func pageDay(params map[string]any) (any, error) {
|
|
date, _ := params["date"].(string)
|
|
from, _ := params["from"].(string)
|
|
tz, _ := params["tz"].(string)
|
|
d, err := usageapi.SpanOf(from, date, usage.Zone(tz))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
icons := map[string]pageIcon{}
|
|
want := func(name string) {
|
|
if _, ok := icons[name]; ok {
|
|
return
|
|
}
|
|
if ic, ok := pageIconOf(name); ok {
|
|
icons[name] = ic
|
|
}
|
|
}
|
|
for _, a := range d.Agents {
|
|
want(a.Icon)
|
|
for _, m := range a.Models {
|
|
want(m.Icon)
|
|
}
|
|
}
|
|
// a cost as Settings shows it: in USD, or in CNY at the rate known now
|
|
cur := settings.Load().Currency
|
|
if cur == "" {
|
|
cur = "usd"
|
|
}
|
|
return struct {
|
|
usageapi.Day
|
|
Icons map[string]pageIcon `json:"icons"`
|
|
Currency string `json:"currency"`
|
|
Rate float64 `json:"rate"` // CNY per USD, 0 when unknown
|
|
}{d, icons, cur, currentFX().Rate}, nil
|
|
}
|