From 75e0639f78c9426a6c8f1d16541c21e7646fe7fc Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 17 Dec 2019 23:13:16 +0800 Subject: [PATCH] add mdb.update --- base.go | 12 ++--- base/aaa/aaa.go | 1 - base/cli/cli.go | 3 -- base/ctx/ctx.go | 5 +- base/mdb/mdb.go | 11 +++- base/shy.go | 1 + base/web/web.go | 6 ++- core/chat/chat.go | 28 ++++++++++ core/wiki/chart.go | 1 - demo/main.go | 1 + type.go | 132 ++++++++++++++++++++++++++++++++++++++++----- 11 files changed, 168 insertions(+), 33 deletions(-) diff --git a/base.go b/base.go index ffba6765..6af28972 100644 --- a/base.go +++ b/base.go @@ -26,10 +26,8 @@ func (f *Frame) Begin(m *Message, arg ...string) Server { func (f *Frame) Start(m *Message, arg ...string) bool { // 加载配置 m.Travel(func(p *Context, s *Context) { - if cmd, ok := s.Commands["_init"]; ok { - msg := m.Spawns(s) - msg.Log("_init", s.Name) - cmd.Hand(msg, s, "_init", arg...) + if _, ok := s.Commands["_init"]; ok { + m.Spawns(s).Runs("_init", "_init", arg...) } }) @@ -40,10 +38,8 @@ func (f *Frame) Start(m *Message, arg ...string) bool { func (f *Frame) Close(m *Message, arg ...string) bool { // 保存配置 m.Travel(func(p *Context, s *Context) { - if cmd, ok := s.Commands["_exit"]; ok { - msg := m.Spawns(s) - msg.Log("_exit", "some") - cmd.Hand(msg, s, "_exit", arg...) + if _, ok := s.Commands["_exit"]; ok { + m.Spawns(s).Runs("_exit", "_exit", arg...) } }) diff --git a/base/aaa/aaa.go b/base/aaa/aaa.go index 4ba5b964..3eebeaf3 100644 --- a/base/aaa/aaa.go +++ b/base/aaa/aaa.go @@ -70,7 +70,6 @@ var Index = &ice.Context{Name: "aaa", Help: "认证模块", "sess": {Name: "sess check|login", Help: "会话", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[0] { case "check": - user := m.Conf("sess", "hash."+arg[1]+".username") if user != "" { m.Confm("user", "hash."+user, func(value map[string]interface{}) { diff --git a/base/cli/cli.go b/base/cli/cli.go index 090a1287..b34ace58 100644 --- a/base/cli/cli.go +++ b/base/cli/cli.go @@ -60,9 +60,6 @@ var Index = &ice.Context{Name: "cli", Help: "命令模块", }}, "timer": {Name: "timer", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }}, - "hi": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Echo("hello %s world", c.Name) - }}, }, } diff --git a/base/ctx/ctx.go b/base/ctx/ctx.go index 9a54bef2..ca146840 100644 --- a/base/ctx/ctx.go +++ b/base/ctx/ctx.go @@ -41,10 +41,7 @@ var Index = &ice.Context{Name: "ctx", Help: "元始模块", } else { switch arg[2] { case "run": - m.Log("info", "run %s %s %v", s.Name, key, arg[3:]) - msg := m.Spawn(s) - i.Hand(msg, s, key, arg[3:]...) - m.Copy(msg) + m.Copy(m.Spawns(s).Runs(key, key, arg[3:]...)) } } } diff --git a/base/mdb/mdb.go b/base/mdb/mdb.go index 4dbddade..1ce64c59 100644 --- a/base/mdb/mdb.go +++ b/base/mdb/mdb.go @@ -2,14 +2,21 @@ package mdb import ( "github.com/shylinux/icebergs" + "github.com/shylinux/toolkits" ) var Index = &ice.Context{Name: "mdb", Help: "数据模块", Caches: map[string]*ice.Cache{}, Configs: map[string]*ice.Config{}, Commands: map[string]*ice.Command{ - "hi": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Echo("hello %s world", c.Name) + "update": {Name: "update config table index key value", Help: "修改数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + meta := m.Confm(arg[0], arg[1]+".meta") + index := kit.Int(arg[2]) - kit.Int(meta["offset"]) - 1 + + data := m.Confm(arg[0], arg[1]+".list."+kit.Format(index)) + for i := 3; i < len(arg)-1; i += 2 { + kit.Value(data, arg[i], arg[i+1]) + } }}, }, } diff --git a/base/shy.go b/base/shy.go index f86e3603..0b7b459f 100644 --- a/base/shy.go +++ b/base/shy.go @@ -6,6 +6,7 @@ import ( _ "github.com/shylinux/icebergs/base/ctx" _ "github.com/shylinux/icebergs/base/gdb" _ "github.com/shylinux/icebergs/base/log" + _ "github.com/shylinux/icebergs/base/mdb" _ "github.com/shylinux/icebergs/base/nfs" _ "github.com/shylinux/icebergs/base/web" ) diff --git a/base/web/web.go b/base/web/web.go index 528f3eb6..e8b7dad2 100644 --- a/base/web/web.go +++ b/base/web/web.go @@ -40,9 +40,11 @@ func Cookie(msg *ice.Message, sessid string) string { func (web *WEB) Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool { if msg.Options("sessid") { sub := msg.Cmd("aaa.sess", "check", msg.Option("sessid")) - msg.Log("info", "user %s %s", msg.Option("userrole", sub.Append("userrole")), + msg.Log("info", "role: %s user: %s", msg.Option("userrole", sub.Append("userrole")), msg.Option("username", sub.Append("username"))) } + + msg.Runs("_login", msg.Option("path"), kit.Simple(msg.Optionv("cmds"))...) return true } func (web *WEB) HandleWSS(m *ice.Message, safe bool, c *websocket.Conn) { @@ -270,6 +272,7 @@ func (web *WEB) Start(m *ice.Message, arg ...string) bool { port := kit.Select(m.Conf("spide", "self.port"), arg, 0) web.m = m web.Server = &http.Server{Addr: port, Handler: web} + m.Log("serve", "node %v", m.Conf("cli.runtime", "node")) m.Log("serve", "listen %s", port) m.Log("serve", "listen %s", web.Server.ListenAndServe()) return true @@ -311,6 +314,7 @@ var Index = &ice.Context{Name: "web", Help: "网页模块", }}, "serve": {Name: "hi", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Conf("cli.runtime", "node.type", "server") + m.Conf("cli.runtime", "node.name", m.Conf("cli.runtime", "boot.hostname")) m.Run(arg...) }}, "/space": &ice.Command{Name: "/space", Help: "", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/core/chat/chat.go b/core/chat/chat.go index b353baa3..3fd9510b 100644 --- a/core/chat/chat.go +++ b/core/chat/chat.go @@ -22,11 +22,29 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", m.Cmd("ctx.config", "load", "var/conf/aaa.json") m.Cmd("ctx.config", "load", "var/conf/chat.json") }}, + "_login": {Name: "_login", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if cmd != "/login" { + if !m.Options("sessid") || !m.Options("username") { + m.Option("path", "") + m.Log("warn", "not login") + m.Echo("error").Echo("not login") + return + } + } + if len(arg) > 0 && m.Confs("group", "hash."+arg[0]) { + m.Option("sess.river", arg[0]) + if len(arg) > 1 && m.Confs("group", "hash."+arg[0]+".tool.hash."+arg[1]) { + m.Option("sess.storm", arg[1]) + } + } + m.Log("info", "river: %s storm: %s", m.Option("sess.river"), m.Option("sess.storm")) + }}, "_exit": {Name: "_init", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmd("ctx.config", "save", "var/conf/chat.json", "web.chat.group") m.Cmd("ctx.config", "save", "var/conf/aaa.json", "aaa.role", "aaa.user", "aaa.sess") m.Cmd("ctx.config", "save", "var/conf/cli.json", "cli.runtime") }}, + "/login": {Name: "/login", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[0] { case "check": @@ -40,6 +58,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", "/favor": {Name: "/favor", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmdy("cli.system", arg) }}, + "/ocean": {Name: "/ocean", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) == 0 { m.Confm("aaa.user", "hash", func(key string, value map[string]interface{}) { @@ -98,6 +117,7 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", }}, "/action": {Name: "/action", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) == 2 { + m.Set("option") m.Confm("group", "hash."+arg[0]+".tool.hash."+arg[1]+".list", func(index int, value map[string]interface{}) { m.Push("river", arg[0]) m.Push("storm", arg[1]) @@ -130,6 +150,8 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", }}, "/steam": {Name: "/steam", Help: "hello", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) < 2 { + m.Push("user", m.Conf("cli.runtime", "boot.username")) + m.Push("node", m.Conf("cli.runtime", "node.name")) m.Confm("web.space", "hash", func(key string, value map[string]interface{}) { m.Push("user", m.Conf("cli.runtime", "boot.username")) m.Push("node", value["name"]) @@ -140,6 +162,9 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", case "spawn": list := []interface{}{} for i := 3; i < len(arg)-3; i += 4 { + if arg[i] == m.Conf("cli.runtime", "node.name") { + arg[i] = "" + } list = append(list, map[string]interface{}{ "pod": arg[i], "ctx": arg[i+1], @@ -157,6 +182,9 @@ var Index = &ice.Context{Name: "chat", Help: "聊天模块", m.Log("info", "steam spawn %v %v %v", arg[0], arg[2], list) default: + if arg[2] == m.Conf("cli.runtime", "node.name") { + arg[2] = "" + } m.Cmdy("web.space", arg[2], "ctx.command") } }}, diff --git a/core/wiki/chart.go b/core/wiki/chart.go index 0954474f..80364abc 100644 --- a/core/wiki/chart.go +++ b/core/wiki/chart.go @@ -95,7 +95,6 @@ func (b *Chain) Init(m *ice.Message, arg ...string) Chart { b.LineSize = kit.Int(kit.Select("12", arg, 4)) b.Padding = kit.Int(kit.Select("8", arg, 5)) b.Margin = kit.Int(kit.Select("8", arg, 6)) - m.Log("info", "data %v", kit.Formats(b.data)) // 计算尺寸 b.max = map[int]int{} diff --git a/demo/main.go b/demo/main.go index 4f378dd5..b6eb5ba3 100644 --- a/demo/main.go +++ b/demo/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/shylinux/icebergs" _ "github.com/shylinux/icebergs/core/chat" + _ "github.com/shylinux/icebergs/core/team" _ "github.com/shylinux/icebergs/core/wiki" ) diff --git a/type.go b/type.go index 0f6ab027..b688b79b 100644 --- a/type.go +++ b/type.go @@ -115,8 +115,27 @@ type Message struct { cb func(*Message) *Message } -func (m *Message) Time() string { - return m.time.Format("2006-01-02 15:04:05") +func (m *Message) Time(args ...interface{}) string { + t := m.time + if len(args) > 0 { + switch arg := args[0].(type) { + case string: + if d, e := time.ParseDuration(arg); e == nil { + t, args = t.Add(d), args[1:] + } + } + } + f := "2006-01-02 15:04:05" + if len(args) > 0 { + switch arg := args[0].(type) { + case string: + f = arg + if len(args) > 1 { + f = fmt.Sprintf(f, args[1:]...) + } + } + } + return t.Format(f) } func (m *Message) Target() *Context { return m.target @@ -129,6 +148,51 @@ func (m *Message) Format(key interface{}) string { return time.Now().Sub(m.time).String() case "meta": return kit.Format(m.meta) + case "time": + return m.Time() + case "ship": + return fmt.Sprintf("%s->%s", m.source.Name, m.target.Name) + case "prefix": + return fmt.Sprintf("%s %d %s->%s", m.Time(), m.code, m.source.Name, m.target.Name) + case "chain": + ms := []*Message{} + for msg := m; msg != nil; msg = msg.message { + ms = append(ms, msg) + } + + meta := append([]string{}, "\n\n") + for i := len(ms) - 1; i >= 0; i-- { + msg := ms[i] + + meta = append(meta, fmt.Sprintf("%s ", msg.Format("prefix"))) + if len(msg.meta["detail"]) > 0 { + meta = append(meta, fmt.Sprintf("detail:%d %v", len(msg.meta["detail"]), msg.meta["detail"])) + } + + if len(msg.meta["option"]) > 0 { + meta = append(meta, fmt.Sprintf("option:%d %v\n", len(msg.meta["option"]), msg.meta["option"])) + for _, k := range msg.meta["option"] { + if v, ok := msg.meta[k]; ok { + meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v)) + } + } + } else { + meta = append(meta, "\n") + } + + if len(msg.meta["append"]) > 0 { + meta = append(meta, fmt.Sprintf(" append:%d %v\n", len(msg.meta["append"]), msg.meta["append"])) + for _, k := range msg.meta["append"] { + if v, ok := msg.meta[k]; ok { + meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v)) + } + } + } + if len(msg.meta["result"]) > 0 { + meta = append(meta, fmt.Sprintf(" result:%d %v\n", len(msg.meta["result"]), msg.meta["result"])) + } + } + return strings.Join(meta, "") case "stack": pc := make([]uintptr, 100) pc = pc[:runtime.Callers(5, pc)] @@ -238,7 +302,23 @@ func (m *Message) Copy(msg *Message) *Message { } return m } -func (m *Message) Push(key string, value interface{}) *Message { +func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Message { + switch value := value.(type) { + case map[string]interface{}: + list := []string{} + if len(arg) > 0 { + list = kit.Simple(arg[0]) + } else { + for k := range value { + list = append(list, k) + } + sort.Strings(list) + } + for _, k := range list { + m.Add("append", k, kit.Format(value[k])) + } + return m + } return m.Add("append", key, kit.Format(value)) } func (m *Message) Echo(str string, arg ...interface{}) *Message { @@ -268,10 +348,10 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { } for msg := m; msg != nil; msg = msg.message { - if list, ok := m.meta[key]; ok { + if list, ok := msg.meta[key]; ok { return list } - if list, ok := m.data[key]; ok { + if list, ok := msg.data[key]; ok { return list } } @@ -288,7 +368,18 @@ func (m *Message) Result(arg ...interface{}) string { } func (m *Message) Log(level string, str string, arg ...interface{}) *Message { - fmt.Fprintf(os.Stderr, "%s %d %s->%s %s %s\n", time.Now().Format("2006-01-02 15:04:05"), m.code, m.source.Name, m.target.Name, level, fmt.Sprintf(str, arg...)) + prefix, suffix := "", "" + switch level { + case "cmd": + prefix, suffix = "\033[32m", "\033[0m" + case "cost": + prefix, suffix = "\033[33m", "\033[0m" + case "warn": + prefix, suffix = "\033[31m", "\033[0m" + } + fmt.Fprintf(os.Stderr, "%s %d %s->%s %s%s %s%s\n", + time.Now().Format("2006-01-02 15:04:05"), m.code, m.source.Name, m.target.Name, + prefix, level, fmt.Sprintf(str, arg...), suffix) return m } func (m *Message) Assert(arg interface{}) bool { @@ -372,6 +463,14 @@ func (m *Message) Run(arg ...string) *Message { m.target.server.Start(m, arg...) return m } +func (m *Message) Runs(key string, cmd string, arg ...string) *Message { + if s, ok := m.Target().Commands[key]; ok { + m.meta["detail"] = append([]string{cmd}, arg...) + m.Log("cmd", "%s.%s %s %v", m.Target().Name, key, cmd, arg) + s.Hand(m, m.Target(), cmd, arg...) + } + return m +} func (m *Message) Call(sync bool, cb func(*Message) *Message) *Message { if sync { wait := make(chan bool) @@ -390,7 +489,7 @@ func (m *Message) Back(sub *Message) *Message { return m } -func (m *Message) Grow(key string, args interface{}, data interface{}) interface{} { +func (m *Message) Grow(key string, args interface{}, data interface{}) map[string]interface{} { cache := m.Confm(key, args) if cache == nil { cache = map[string]interface{}{} @@ -401,7 +500,12 @@ func (m *Message) Grow(key string, args interface{}, data interface{}) interface } list, _ := cache["list"].([]interface{}) + // 添加数据 list = append(list, data) + meta["count"] = kit.Int(meta["count"]) + 1 + kit.Value(data, "id", meta["count"]) + + // 保存数据 if len(list) > kit.Int(kit.Select(m.Conf("cache", "limit"), meta["limit"])) { least := kit.Int(kit.Select(m.Conf("cache", "least"), meta["least"])) @@ -415,7 +519,7 @@ func (m *Message) Grow(key string, args interface{}, data interface{}) interface s, e := f.Stat() m.Assert(e) - // 保存数据 + // 保存表头 keys := []string{} w := csv.NewWriter(f) if s.Size() == 0 { @@ -431,7 +535,7 @@ func (m *Message) Grow(key string, args interface{}, data interface{}) interface keys, e = r.Read() } - // 保存状态 + // 保存记录 count := len(list) - least offset := kit.Int(meta["offset"]) record, _ := meta["record"].([]interface{}) @@ -467,6 +571,8 @@ func (m *Message) Grow(key string, args interface{}, data interface{}) interface list = list[:least] w.Flush() } + + // 更新数据 cache["meta"] = meta cache["list"] = list if args == nil { @@ -474,7 +580,7 @@ func (m *Message) Grow(key string, args interface{}, data interface{}) interface } else { m.Conf(key, args, cache) } - return list + return meta } func (m *Message) Grows(key string, args interface{}, cb interface{}) map[string]interface{} { cache := m.Confm(key, args) @@ -566,9 +672,8 @@ func (m *Message) Grows(key string, args interface{}, cb interface{}) map[string data = append(data, list[i]) } } - val := map[string]interface{}{"meta": meta, "list": data} - kit.Fetch(val, cb) - return val + kit.Fetch(data, cb) + return meta } func (m *Message) Cmdy(arg ...interface{}) *Message { @@ -596,6 +701,7 @@ func (m *Message) Cmd(arg ...interface{}) *Message { for c := s; c != nil; c = c.context { if cmd, ok := c.Commands[key]; ok { msg = m.Spawns(s).Log("cmd", "%s.%s %v", c.Name, key, list[1:]) + msg.meta["detail"] = list msg.TryCatch(msg, true, func(msg *Message) { cmd.Hand(msg, c, key, list[1:]...) })