This commit is contained in:
illustris committed 2026-10-28 08:00:16 +05:30
commit 625becd03e
24 files changed
+3822

No files matched your search

+307
View File
@@ -0,0 +1,307 @@
// Package filter parses the query language shared by the TUI and web UI.
//
// A query is a whitespace separated list of terms, all of which must match:
//
// foo substring of link, target, via or command (case-insensitive)
// /re/ regular expression over the same fields (no spaces; use \s)
// -term negates any term
// kind:K indirect, profile, direct, dangling, runtime (comma separated)
// owner:U owner name or uid; "me" is the current user
// under:/path link starts with /path
// older:30d modified more than 30 days ago (s, m, h, d, w, y)
// newer:2h modified less than 2 hours ago
// size>500M closure size bigger than 500 MiB (K, M, G, T; also <)
// is:protected protected roots; is:current for current generations
// is:hidden nix cache roots (~/.cache/nix/...), shown only when asked for
package filter
import (
"fmt"
"os/user"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/illustris/i-am-gcroot/internal/roots"
)
type pred func(r *roots.Root, now time.Time) bool
type Query struct {
Raw string
preds []pred
// ShowHidden is set when the query mentions is:hidden, which would
// otherwise never match because hidden roots are dropped first.
ShowHidden bool
}
func Parse(q string) (*Query, error) {
out := &Query{Raw: q}
for _, tok := range strings.Fields(q) {
neg := false
if strings.HasPrefix(tok, "-") && len(tok) > 1 {
neg, tok = true, tok[1:]
}
p, err := parseTerm(tok)
if err != nil {
return nil, err
}
if tok == "is:hidden" {
out.ShowHidden = true
}
if neg {
inner := p
p = func(r *roots.Root, now time.Time) bool { return !inner(r, now) }
}
out.preds = append(out.preds, p)
}
return out, nil
}
func haystack(r *roots.Root) string {
return r.Link + "\x00" + r.Target + "\x00" + r.Via + "\x00" + r.Command
}
func parseTerm(tok string) (pred, error) {
if isRegex(tok) {
re, err := regexp.Compile("(?i)" + tok[1:len(tok)-1])
if err != nil {
return nil, fmt.Errorf("bad regex %s: %w", tok, err)
}
return func(r *roots.Root, _ time.Time) bool { return re.MatchString(haystack(r)) }, nil
}
if strings.HasPrefix(tok, "size>") || strings.HasPrefix(tok, "size<") {
n, err := ParseSize(tok[5:])
if err != nil {
return nil, err
}
if tok[4] == '>' {
return func(r *roots.Root, _ time.Time) bool { return r.ClosureSize > n }, nil
}
return func(r *roots.Root, _ time.Time) bool { return r.ClosureSize >= 0 && r.ClosureSize < n }, nil
}
key, val, ok := strings.Cut(tok, ":")
if ok && val != "" {
switch key {
case "kind", "k":
kinds := map[roots.Kind]bool{}
for _, k := range strings.Split(val, ",") {
kk, err := ParseKind(k)
if err != nil {
return nil, err
}
kinds[kk] = true
}
return func(r *roots.Root, _ time.Time) bool { return kinds[r.Kind] }, nil
case "owner", "user":
if val == "me" {
if u, err := user.Current(); err == nil {
val = u.Username
}
}
return func(r *roots.Root, _ time.Time) bool {
return r.Owner == val || strconv.Itoa(r.UID) == val
}, nil
case "under", "in":
return func(r *roots.Root, _ time.Time) bool { return strings.HasPrefix(r.Link, val) }, nil
case "older", "newer":
d, err := ParseDuration(val)
if err != nil {
return nil, err
}
if key == "older" {
return func(r *roots.Root, now time.Time) bool { return !r.ModTime.IsZero() && now.Sub(r.ModTime) > d }, nil
}
return func(r *roots.Root, now time.Time) bool { return now.Sub(r.ModTime) < d }, nil
case "is":
switch val {
case "protected":
return func(r *roots.Root, _ time.Time) bool { return r.Protected }, nil
case "current":
return func(r *roots.Root, _ time.Time) bool { return r.Current }, nil
case "dangling":
return func(r *roots.Root, _ time.Time) bool { return r.Kind == roots.KindDangling }, nil
case "hidden":
return func(r *roots.Root, _ time.Time) bool { return r.Hidden }, nil
}
return nil, fmt.Errorf("unknown is:%s (protected, current, dangling, hidden)", val)
}
}
lower := strings.ToLower(tok)
return func(r *roots.Root, _ time.Time) bool {
return strings.Contains(strings.ToLower(haystack(r)), lower)
}, nil
}
// isRegex distinguishes /re/ from a plain path like /tmp/foo, which is
// matched as a substring.
func isRegex(tok string) bool {
return len(tok) > 2 && tok[0] == '/' && tok[len(tok)-1] == '/' && strings.Count(tok, "/") == 2
}
func ParseKind(s string) (roots.Kind, error) {
for _, k := range roots.AllKinds {
if strings.HasPrefix(string(k), s) && s != "" {
return k, nil
}
}
return "", fmt.Errorf("unknown kind %q", s)
}
func ParseDuration(s string) (time.Duration, error) {
if len(s) < 2 {
return 0, fmt.Errorf("bad duration %q", s)
}
n, err := strconv.ParseFloat(s[:len(s)-1], 64)
if err != nil {
return 0, fmt.Errorf("bad duration %q", s)
}
unit := map[byte]time.Duration{
's': time.Second, 'm': time.Minute, 'h': time.Hour,
'd': 24 * time.Hour, 'w': 7 * 24 * time.Hour, 'y': 365 * 24 * time.Hour,
}[s[len(s)-1]]
if unit == 0 {
return 0, fmt.Errorf("bad duration unit in %q (s, m, h, d, w, y)", s)
}
return time.Duration(n * float64(unit)), nil
}
func ParseSize(s string) (int64, error) {
s = strings.TrimSuffix(strings.TrimSuffix(strings.ToUpper(s), "B"), "I")
mult := int64(1)
if s != "" {
switch s[len(s)-1] {
case 'K':
mult = 1 << 10
case 'M':
mult = 1 << 20
case 'G':
mult = 1 << 30
case 'T':
mult = 1 << 40
}
if mult > 1 {
s = s[:len(s)-1]
}
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, fmt.Errorf("bad size %q", s)
}
return int64(n * float64(mult)), nil
}
func (q *Query) Match(r *roots.Root, now time.Time) bool {
for _, p := range q.preds {
if !p(r, now) {
return false
}
}
return true
}
type SortKey int
const (
SortLink SortKey = iota
SortAge
SortSize
SortTarget
SortKind
)
var SortNames = []string{"link", "age", "size", "target", "kind"}
func (k SortKey) String() string { return SortNames[k] }
func ParseSort(s string) SortKey {
for i, n := range SortNames {
if n == s {
return SortKey(i)
}
}
return SortLink
}
// Apply filters by query and kind set (nil means all kinds) and sorts the
// result. Hidden roots are dropped unless showHidden or the query asks for
// them.
func Apply(rs []roots.Root, q *Query, kinds map[roots.Kind]bool, showHidden bool, key SortKey, desc bool) []roots.Root {
showHidden = showHidden || (q != nil && q.ShowHidden)
now := time.Now()
out := make([]roots.Root, 0, len(rs))
for i := range rs {
r := &rs[i]
if kinds != nil && !kinds[r.Kind] {
continue
}
if r.Hidden && !showHidden {
continue
}
if q != nil && !q.Match(r, now) {
continue
}
out = append(out, *r)
}
less := func(a, b *roots.Root) bool {
switch key {
case SortAge:
// "ascending" age means newest first
return a.ModTime.After(b.ModTime)
case SortSize:
return a.ClosureSize < b.ClosureSize
case SortTarget:
return a.TargetName() < b.TargetName()
case SortKind:
if a.Kind != b.Kind {
return a.Kind < b.Kind
}
}
return a.Link < b.Link
}
sort.SliceStable(out, func(i, j int) bool {
if desc {
return less(&out[j], &out[i])
}
return less(&out[i], &out[j])
})
return out
}
func HumanSize(n int64) string {
switch {
case n == -1:
return "..."
case n < 0:
return "-"
case n < 1<<10:
return fmt.Sprintf("%dB", n)
case n < 1<<20:
return fmt.Sprintf("%.1fK", float64(n)/(1<<10))
case n < 1<<30:
return fmt.Sprintf("%.1fM", float64(n)/(1<<20))
case n < 1<<40:
return fmt.Sprintf("%.1fG", float64(n)/(1<<30))
}
return fmt.Sprintf("%.1fT", float64(n)/(1<<40))
}
func HumanAge(t time.Time, now time.Time) string {
if t.IsZero() {
return "-"
}
d := now.Sub(t)
switch {
case d < time.Minute:
return fmt.Sprintf("%ds", int(d.Seconds()))
case d < time.Hour:
return fmt.Sprintf("%dm", int(d.Minutes()))
case d < 48*time.Hour:
return fmt.Sprintf("%dh", int(d.Hours()))
case d < 365*24*time.Hour:
return fmt.Sprintf("%dd", int(d.Hours()/24))
}
return fmt.Sprintf("%.1fy", d.Hours()/24/365)
}
+105
View File
@@ -0,0 +1,105 @@
package filter
import (
"testing"
"time"
"github.com/illustris/i-am-gcroot/internal/roots"
)
func TestQuery(t *testing.T) {
now := time.Now()
r := roots.Root{
Link: "/tmp/claude-1000/scratch/result",
Target: "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello-2.12",
Kind: roots.KindIndirect,
Owner: "alice",
UID: 1000,
ModTime: now.Add(-40 * 24 * time.Hour),
ClosureSize: 2 << 30,
}
cases := map[string]bool{
"": true,
"hello": true,
"HELLO": true,
"-hello": false,
"hello world": false,
"/hel+o-2/": true,
"/^/tmp/": false, // more than two slashes: plain substring
"/tmp/claude-": true, // substring
"under:/tmp/claude-": true,
"under:/home": false,
"kind:indirect": true,
"kind:prof,ind": true,
"kind:profile": false,
"owner:alice": true,
"owner:1000": true,
"owner:bob": false,
"older:30d": true,
"older:50d": false,
"newer:1w": false,
"size>1G": true,
"size>3G": false,
"size<3G": true,
"is:protected": false,
"-is:protected kind:in": true,
}
for q, want := range cases {
p, err := Parse(q)
if err != nil {
t.Errorf("%q: %v", q, err)
continue
}
if got := p.Match(&r, now); got != want {
t.Errorf("%q: got %v want %v", q, got, want)
}
}
for _, bad := range []string{"kind:nope", "older:3x", "size>abc", "/[/", "is:what"} {
if _, err := Parse(bad); err == nil {
t.Errorf("%q should fail to parse", bad)
}
}
}
func TestApplySort(t *testing.T) {
now := time.Now()
rs := []roots.Root{
{Link: "/b", Kind: roots.KindIndirect, ClosureSize: 5, ModTime: now.Add(-time.Hour)},
{Link: "/a", Kind: roots.KindIndirect, ClosureSize: 10, ModTime: now.Add(-2 * time.Hour)},
{Link: "/c", Kind: roots.KindRuntime, ClosureSize: 1},
}
all := map[roots.Kind]bool{roots.KindIndirect: true}
out := Apply(rs, nil, all, false, SortLink, false)
if len(out) != 2 || out[0].Link != "/a" {
t.Errorf("link sort: %+v", out)
}
out = Apply(rs, nil, all, false, SortSize, true)
if out[0].Link != "/a" {
t.Errorf("size desc: %+v", out)
}
out = Apply(rs, nil, all, false, SortAge, false)
if out[0].Link != "/b" {
t.Errorf("age: newest first: %+v", out)
}
rs[0].Hidden = true
if out = Apply(rs, nil, all, false, SortLink, false); len(out) != 1 {
t.Errorf("hidden root should be dropped: %+v", out)
}
if out = Apply(rs, nil, all, true, SortLink, false); len(out) != 2 {
t.Errorf("showHidden should keep it: %+v", out)
}
q, _ := Parse("is:hidden")
if out = Apply(rs, q, all, false, SortLink, false); len(out) != 1 || out[0].Link != "/b" {
t.Errorf("is:hidden should show only hidden: %+v", out)
}
}
func TestParseSize(t *testing.T) {
for in, want := range map[string]int64{"1K": 1024, "1.5M": 1572864, "2GiB": 2 << 30, "100": 100} {
got, err := ParseSize(in)
if err != nil || got != want {
t.Errorf("%s: got %d %v want %d", in, got, err, want)
}
}
}