From b1cfdccc28d9ce8762f2e5db770a0374694f9cad Mon Sep 17 00:00:00 2001 From: shylinux Date: Wed, 24 May 2023 01:42:19 +0800 Subject: [PATCH] add procstat --- base/cli/procstat.go | 86 ++++++++++++++++++++++++++++++++++++++++++++ base/gdb/timer.go | 13 ++++--- base/web/dream.go | 4 +-- conf.go | 1 + go.sum | 1 + misc.go | 2 +- misc/git/repos.go | 32 ++++++++++++----- 7 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 base/cli/procstat.go diff --git a/base/cli/procstat.go b/base/cli/procstat.go new file mode 100644 index 00000000..ffd340f1 --- /dev/null +++ b/base/cli/procstat.go @@ -0,0 +1,86 @@ +package cli + +import ( + "strings" + "time" + + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/aaa" + "shylinux.com/x/icebergs/base/lex" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + kit "shylinux.com/x/toolkits" +) + +type procstat struct { + user int + sys int + idle int + io int + + total int + free int + available int + + rx int + tx int +} + +func newprocstat(m *ice.Message) (stat procstat) { + if ls := kit.Split(kit.Select("", strings.Split(m.Cmdx(nfs.CAT, "/proc/stat"), lex.NL), 1)); len(ls) > 0 { + stat = procstat{ + user: kit.Int(ls[1]), + sys: kit.Int(ls[3]), + idle: kit.Int(ls[4]), + io: kit.Int(ls[5]), + } + } + for _, line := range strings.Split(strings.TrimSpace(m.Cmdx(nfs.CAT, "/proc/meminfo")), lex.NL) { + switch ls := kit.Split(line, ": "); ls[0] { + case "MemTotal": + stat.total = kit.Int(ls[1]) * 1024 + case "MemFree": + stat.free = kit.Int(ls[1]) * 1024 + case "MemAvailable": + stat.available = kit.Int(ls[1]) * 1024 + } + } + for _, line := range strings.Split(strings.TrimSpace(m.Cmdx(nfs.CAT, "/proc/net/dev")), lex.NL)[2:] { + ls := kit.Split(line, ": ") + if ls[0] == "eth0" { + stat.rx = kit.Int(ls[1]) + stat.tx = kit.Int(ls[9]) + } + } + return +} + +func init() { + var last procstat + Index.MergeCommands(ice.Commands{ + "procstat": {Name: "procstat id auto page insert", Actions: ice.MergeActions(ice.Actions{ + ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { + aaa.White(m, "/proc/net/dev") + aaa.White(m, "/proc/meminfo") + aaa.White(m, "/proc/stat") + last = newprocstat(m) + }}, + mdb.INSERT: {Name: "insert", Hand: func(m *ice.Message, arg ...string) { + stat := newprocstat(m) + total := stat.user - last.user + stat.sys - last.sys + stat.idle - last.idle + stat.io - last.io + m.Cmd(mdb.INSERT, m.PrefixKey(), "", mdb.LIST, + "user", (stat.user-last.user)*1000/total, "sys", (stat.sys-last.sys)*1000/total, + "idle", (stat.idle-last.idle)*1000/total, "io", (stat.io-stat.io)*1000/total, + "free", stat.free*1000/stat.total, "available", stat.available*1000/stat.total, + "rx", (stat.rx-last.rx)*1000/10000000, "tx", (stat.tx-last.tx)*1000/10000000, + ) + last = stat + }}, + }, mdb.PageListAction(mdb.FIELD, "time,id,user,sys,idle,io,free,available,rx,tx")), Hand: func(m *ice.Message, arg ...string) { + m.OptionDefault(mdb.CACHE_LIMIT, "1000") + mdb.PageListSelect(m, arg...) + m.SortInt(mdb.ID).Display("/plugin/story/trend.js", ice.VIEW, "折线图", "min", "0", "max", "1000", mdb.FIELD, "user,sys,idle,free,available,tx,rx", COLOR, "red,yellow,green,blue,cyan,purple") + m.Status("from", m.Append(mdb.TIME), "span", kit.FmtDuration(time.Duration(kit.Time(m.Time())-kit.Time(m.Append(mdb.TIME)))), m.AppendSimple("time,user,sys,idle,free,available,tx,rx"), "cursor", "0") + }}, + }) +} diff --git a/base/gdb/timer.go b/base/gdb/timer.go index 595adafd..c5b45249 100644 --- a/base/gdb/timer.go +++ b/base/gdb/timer.go @@ -10,12 +10,15 @@ import ( func _timer_action(m *ice.Message, now time.Time, arg ...string) { mdb.HashSelects(m).Table(func(value ice.Maps) { - if value[mdb.COUNT] == "0" || value[mdb.TIME] > now.Format(ice.MOD_TIME) { + count := kit.Int(value[mdb.COUNT]) + if count == 0 || value[mdb.TIME] > now.Format(ice.MOD_TIME) { return } m.Option(ice.LOG_DISABLE, ice.FALSE) - m.Cmd(ROUTINE, mdb.CREATE, mdb.NAME, value[mdb.NAME], kit.Keycb(ROUTINE), value[ice.CMD]) - mdb.HashModify(m, mdb.HASH, value[mdb.HASH], mdb.COUNT, kit.Int(value[mdb.COUNT])-1, mdb.TIME, m.Time(value[INTERVAL])) + // m.Cmd(ROUTINE, mdb.CREATE, mdb.NAME, value[mdb.NAME], kit.Keycb(ROUTINE), value[ice.CMD]) + m.Cmd(kit.Split(value[ice.CMD])) + kit.If(count < 0, func() { count++ }) + mdb.HashModify(m, mdb.HASH, value[mdb.HASH], mdb.COUNT, count-1, mdb.TIME, m.Time(value[INTERVAL])) }) } @@ -28,11 +31,11 @@ const TIMER = "timer" func init() { Index.MergeCommands(ice.Commands{ - TIMER: {Name: "timer hash auto create prunes", Help: "定时器", Actions: ice.MergeActions(ice.Actions{ + TIMER: {Name: "timer name auto create prunes", Help: "定时器", Actions: ice.MergeActions(ice.Actions{ mdb.CREATE: {Name: "create name*=hi delay=10ms interval=10s count=3 cmd*=runtime"}, mdb.PRUNES: {Hand: func(m *ice.Message, arg ...string) { mdb.HashPrunesValue(m, mdb.COUNT, "0") }}, HAPPEN: {Hand: func(m *ice.Message, arg ...string) { _timer_action(m, time.Now(), arg...) }}, RESTART: {Name: "restart count=3", Hand: func(m *ice.Message, arg ...string) { mdb.HashModify(m, m.OptionSimple(mdb.HashShort(m)), arg) }}, - }, mdb.HashAction(mdb.FIELD, "time,hash,name,delay,interval,count,cmd", TICK, "1s"))}, + }, mdb.HashAction(mdb.SHORT, "name", mdb.FIELD, "time,hash,name,delay,interval,count,cmd", TICK, "1s"))}, }) } diff --git a/base/web/dream.go b/base/web/dream.go index 5b3e2287..4add9db2 100644 --- a/base/web/dream.go +++ b/base/web/dream.go @@ -123,9 +123,9 @@ func init() { case mdb.NAME, nfs.TEMPLATE: _dream_list(m).Cut("name,status,time") case nfs.BINARY: - m.Cmdy(nfs.DIR, ice.BIN, "path,size,hashs,time", kit.Dict(nfs.DIR_TYPE, nfs.TYPE_BIN)) + m.Cmdy(nfs.DIR, ice.BIN, "path,size,time", kit.Dict(nfs.DIR_TYPE, nfs.TYPE_BIN)) m.Cmd(nfs.DIR, ice.USR_LOCAL_WORK, kit.Dict(nfs.DIR_TYPE, nfs.TYPE_BOTH), func(value ice.Maps) { - m.Cmdy(nfs.DIR, path.Join(value[nfs.PATH], ice.BIN), "path,size,hashs,time", kit.Dict(nfs.DIR_TYPE, nfs.TYPE_BIN)) + m.Cmdy(nfs.DIR, path.Join(value[nfs.PATH], ice.BIN), "path,size,time", kit.Dict(nfs.DIR_TYPE, nfs.TYPE_BIN)) }) default: gdb.Event(m, DREAM_INPUTS, arg) diff --git a/conf.go b/conf.go index 18c47006..d81817d0 100644 --- a/conf.go +++ b/conf.go @@ -24,6 +24,7 @@ const ( HTTPS = "https" HTTP = "http" AUTO = "auto" + VIEW = "view" LIST = "list" BACK = "back" MAIN = "main" diff --git a/go.sum b/go.sum index 1b570925..84dfbbb0 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ shylinux.com/x/go-git/v5 v5.6.3 h1:TgxQSryjeDVOA33eYF8s+URzC3xdXsfJQaJAyq1liz0= shylinux.com/x/go-git/v5 v5.6.3/go.mod h1:FiJKLqM5ppFERgywelAfywwxc7UxpaYN5YH3Lhc2gys= shylinux.com/x/go-qrcode v0.0.2 h1:/c0PLj+1RT+kUPfnZVXwgbgH5m1SxBUjM2MIKXbDk+E= shylinux.com/x/go-qrcode v0.0.2/go.mod h1:TlzGBENHXy19xC3AsC6h4Vs5fx2ZuDA4TZ0U3C2OeK4= +shylinux.com/x/toolkits v0.7.5/go.mod h1:8LbYHe7oxBIqb6s4MSOD+4d28QvPdvkyCVtwB/JW7AA= shylinux.com/x/toolkits v0.7.6 h1:QwRxrS2uDHbT9MegM26gtq9j5qojmahOHVDAp1eVEms= shylinux.com/x/toolkits v0.7.6/go.mod h1:CHDJarGlDkg60kVsvMLYL/a5hAnRLEOShiEsMOuEp0Q= shylinux.com/x/websocket v0.0.2 h1:aeeZyAsh6QN7+l5oMXlO9/rlQgx6CHvDtpZyAqMCSlU= diff --git a/misc.go b/misc.go index 1c80a4be..e31aab84 100644 --- a/misc.go +++ b/misc.go @@ -107,7 +107,7 @@ func (m *Message) AppendSimple(key ...string) (res []string) { key = append(key, m.Appendv(MSG_APPEND)...) } } - kit.For(key, func(k string) { res = append(res, k, m.Append(k)) }) + kit.For(kit.Split(kit.Join(key)), func(k string) { res = append(res, k, m.Append(k)) }) return } func (m *Message) AppendTrans(cb func(value string, key string, index int) string) *Message { diff --git a/misc/git/repos.go b/misc/git/repos.go index 1584b590..a07adf67 100644 --- a/misc/git/repos.go +++ b/misc/git/repos.go @@ -30,21 +30,37 @@ func _repos_cmd(m *ice.Message, p string, arg ...string) *ice.Message { return m.Copy(_git_cmd(m.Options(cli.CMD_DIR, _repos_path(m, p)), arg...)) } func _repos_init(m *ice.Message, p string) { git.PlainInit(p, true) } +func _repos_recent(m *ice.Message, repos *git.Repository) (r *plumbing.Reference) { + max := 0 + if iter, err := repos.Tags(); err == nil { + for { + refer, err := iter.Next() + if err != nil { + break + } + ls := kit.Split(refer.Name().Short(), "v.") + if n := kit.Int(ls[0])*1000000 + kit.Int(ls[1])*1000 + kit.Int(ls[2]); n > max { + max, r = n, refer + } + } + } + return +} func _repos_insert(m *ice.Message, p string) { if repos, err := git.PlainOpen(p); err == nil { args := []string{REPOS, path.Base(p), nfs.PATH, p} - if refer, err := repos.Head(); err == nil { - args = append(args, BRANCH, refer.Name().Short()) - if commit, err := repos.CommitObject(refer.Hash()); err == nil { + if head, err := repos.Head(); err == nil { + args = append(args, BRANCH, head.Name().Short()) + if commit, err := repos.CommitObject(head.Hash()); err == nil { args = append(args, mdb.TIME, commit.Author.When.Format(ice.MOD_TIME), COMMENT, commit.Message) } } - if iter, err := repos.Tags(); err == nil { - if refer, err := iter.Next(); err == nil { - args = append(args, VERSION, refer.Name().Short()) - } + if refer := _repos_recent(m, repos); refer != nil { + args = append(args, VERSION, refer.Name().Short()) } - if remote, err := repos.Remotes(); err == nil && len(remote) > 0 { + if remote, err := repos.Remote("origin"); err == nil { + args = append(args, ORIGIN, remote.Config().URLs[0]) + } else if remote, err := repos.Remotes(); err == nil && len(remote) > 0 { args = append(args, ORIGIN, remote[0].Config().URLs[0]) } mdb.HashCreate(m.Options(mdb.TARGET, repos), args)