mirror of
https://shylinux.com/x/icebergs
synced 2025-05-05 12:47:03 +08:00
opt mdb
This commit is contained in:
parent
47b38186b6
commit
46759d6795
63
base/mdb/lbs.go
Normal file
63
base/mdb/lbs.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package mdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/shylinux/icebergs"
|
||||||
|
"github.com/shylinux/toolkits"
|
||||||
|
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func distance(lat1, long1, lat2, long2 float64) float64 {
|
||||||
|
lat1 = lat1 * math.Pi / 180
|
||||||
|
long1 = long1 * math.Pi / 180
|
||||||
|
lat2 = lat2 * math.Pi / 180
|
||||||
|
long2 = long2 * math.Pi / 180
|
||||||
|
return 2 * 6371 * math.Asin(math.Sqrt(math.Pow(math.Sin(math.Abs(lat1-lat2)/2), 2)+math.Cos(lat1)*math.Cos(lat2)*math.Pow(math.Sin(math.Abs(long1-long2)/2), 2)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Index.Merge(&ice.Context{
|
||||||
|
Configs: map[string]*ice.Config{
|
||||||
|
"location": {Name: "location", Help: "定位", Value: kit.Data(kit.MDB_SHORT, "name")},
|
||||||
|
},
|
||||||
|
Commands: map[string]*ice.Command{
|
||||||
|
"location": {Name: "location", Help: "location", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
if len(arg) == 0 {
|
||||||
|
m.Grows("location", nil, "", "", func(index int, value map[string]interface{}) {
|
||||||
|
m.Push("", value)
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(arg) == 1 {
|
||||||
|
m.Richs("location", nil, arg[0], func(key string, value map[string]interface{}) {
|
||||||
|
m.Info("what %v", value)
|
||||||
|
m.Push("detail", value)
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(arg) == 2 {
|
||||||
|
m.Richs("aaa.location", nil, "*", func(key string, value map[string]interface{}) {
|
||||||
|
m.Push("name", value["name"])
|
||||||
|
m.Push("distance", kit.Int(distance(
|
||||||
|
float64(kit.Int(arg[0]))/100000,
|
||||||
|
float64(kit.Int(arg[1]))/100000,
|
||||||
|
float64(kit.Int(value["latitude"]))/100000,
|
||||||
|
float64(kit.Int(value["longitude"]))/100000,
|
||||||
|
)*1000))
|
||||||
|
})
|
||||||
|
m.Sort("distance", "int")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := m.Richs("location", nil, arg[0], nil)
|
||||||
|
if data != nil {
|
||||||
|
data["count"] = kit.Int(data["count"]) + 1
|
||||||
|
} else {
|
||||||
|
data = kit.Dict("name", arg[0], "address", arg[1], "latitude", arg[2], "longitude", arg[3], "count", 1)
|
||||||
|
m.Rich("location", nil, data)
|
||||||
|
}
|
||||||
|
m.Grow("location", nil, data)
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}, nil)
|
||||||
|
}
|
141
base/mdb/mdb.go
141
base/mdb/mdb.go
@ -5,28 +5,21 @@ import (
|
|||||||
"github.com/shylinux/icebergs/base/web"
|
"github.com/shylinux/icebergs/base/web"
|
||||||
"github.com/shylinux/toolkits"
|
"github.com/shylinux/toolkits"
|
||||||
|
|
||||||
"bytes"
|
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func _list_import(m *ice.Message, prefix, key, file, text string) {
|
func _list_import(m *ice.Message, prefix, key, file string) {
|
||||||
buf := bytes.NewBufferString(text)
|
f, e := os.Open(file)
|
||||||
r := csv.NewReader(buf)
|
m.Assert(e)
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
if file != "" {
|
r := csv.NewReader(f)
|
||||||
if f, e := os.Open(file); m.Assert(e) {
|
|
||||||
defer f.Close()
|
|
||||||
r = csv.NewReader(f)
|
|
||||||
// 导入文件
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
count := 0
|
||||||
head, _ := r.Read()
|
head, _ := r.Read()
|
||||||
for {
|
for {
|
||||||
line, e := r.Read()
|
line, e := r.Read()
|
||||||
@ -44,12 +37,13 @@ func _list_import(m *ice.Message, prefix, key, file, text string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 导入数据
|
// 导入数据
|
||||||
n := m.Grow(prefix, key, data)
|
m.Grow(prefix, key, data)
|
||||||
m.Log_INSERT(kit.MDB_KEY, kit.Keys(prefix, key), kit.MDB_ID, n)
|
count++
|
||||||
}
|
}
|
||||||
|
m.Log_IMPORT(kit.MDB_KEY, kit.Keys(prefix, key), kit.MDB_COUNT, count)
|
||||||
}
|
}
|
||||||
func _list_export(m *ice.Message, prefix, key, file string) {
|
func _list_export(m *ice.Message, prefix, key, file string) {
|
||||||
f, p, e := kit.Create(path.Join("usr/export", kit.Keys(file, "csv")))
|
f, p, e := kit.Create(kit.Keys(file, "csv"))
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
defer m.Cmdy(web.STORY, "catch", "csv", p)
|
defer m.Cmdy(web.STORY, "catch", "csv", p)
|
||||||
@ -57,6 +51,7 @@ func _list_export(m *ice.Message, prefix, key, file string) {
|
|||||||
w := csv.NewWriter(f)
|
w := csv.NewWriter(f)
|
||||||
defer w.Flush()
|
defer w.Flush()
|
||||||
|
|
||||||
|
count := 0
|
||||||
head := []string{}
|
head := []string{}
|
||||||
m.Grows(prefix, key, "", "", func(index int, value map[string]interface{}) {
|
m.Grows(prefix, key, "", "", func(index int, value map[string]interface{}) {
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
@ -74,54 +69,80 @@ func _list_export(m *ice.Message, prefix, key, file string) {
|
|||||||
data = append(data, kit.Format(value[k]))
|
data = append(data, kit.Format(value[k]))
|
||||||
}
|
}
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
count++
|
||||||
})
|
})
|
||||||
|
m.Log_EXPORT(kit.MDB_FILE, p, kit.MDB_COUNT, count)
|
||||||
}
|
}
|
||||||
func _hash_import(m *ice.Message, prefix, key, file, text string) {
|
func _hash_import(m *ice.Message, prefix, key, file string) {
|
||||||
data := kit.Parse(nil, "", kit.Split(text, "\t: ,\n")...).(map[string]interface{})
|
f, e := os.Open(file)
|
||||||
|
m.Assert(e)
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
defer m.Log_IMPORT(kit.MDB_FILE, file)
|
||||||
|
|
||||||
|
data := map[string]interface{}{}
|
||||||
|
de := json.NewDecoder(f)
|
||||||
|
de.Decode(&data)
|
||||||
|
|
||||||
for k, v := range data {
|
for k, v := range data {
|
||||||
m.Log_MODIFY(kit.MDB_KEY, kit.Keys(prefix, key), "k", k, "v", v)
|
m.Log_MODIFY(kit.MDB_KEY, kit.Keys(prefix, key, kit.MDB_HASH), "k", k, "v", v)
|
||||||
m.Conf(prefix, kit.Keys(key, k), v)
|
m.Conf(prefix, kit.Keys(key, k), v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func _hash_export(m *ice.Message, prefix, key, file string) {
|
func _hash_export(m *ice.Message, prefix, key, file string) {
|
||||||
f, p, e := kit.Create(path.Join("usr/export", kit.Keys(file, "json")))
|
f, p, e := kit.Create(kit.Keys(file, "json"))
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
defer m.Cmdy(web.STORY, "catch", "json", p)
|
defer m.Cmdy(web.STORY, "catch", "json", p)
|
||||||
|
|
||||||
en := json.NewEncoder(f)
|
en := json.NewEncoder(f)
|
||||||
en.Encode(m.Confv(prefix, key))
|
en.SetIndent("", " ")
|
||||||
|
en.Encode(m.Confv(prefix, kit.Keys(key, kit.MDB_HASH)))
|
||||||
|
m.Log_EXPORT(kit.MDB_FILE, p)
|
||||||
}
|
}
|
||||||
|
func _dict_import(m *ice.Message, prefix, key, file string) {
|
||||||
|
f, e := os.Open(file)
|
||||||
|
m.Assert(e)
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
func distance(lat1, long1, lat2, long2 float64) float64 {
|
defer m.Log_IMPORT(kit.MDB_FILE, file)
|
||||||
lat1 = lat1 * math.Pi / 180
|
|
||||||
long1 = long1 * math.Pi / 180
|
data := map[string]interface{}{}
|
||||||
lat2 = lat2 * math.Pi / 180
|
de := json.NewDecoder(f)
|
||||||
long2 = long2 * math.Pi / 180
|
de.Decode(&data)
|
||||||
return 2 * 6371 * math.Asin(math.Sqrt(math.Pow(math.Sin(math.Abs(lat1-lat2)/2), 2)+math.Cos(lat1)*math.Cos(lat2)*math.Pow(math.Sin(math.Abs(long1-long2)/2), 2)))
|
|
||||||
|
for k, v := range data {
|
||||||
|
m.Log_MODIFY(kit.MDB_KEY, kit.Keys(prefix, key), "k", k, "v", v)
|
||||||
|
m.Conf(prefix, kit.Keys(key, k), v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func _dict_export(m *ice.Message, prefix, key, file string) {
|
||||||
|
f, p, e := kit.Create(kit.Keys(file, "json"))
|
||||||
|
m.Assert(e)
|
||||||
|
defer f.Close()
|
||||||
|
defer m.Cmdy(web.STORY, "catch", "json", p)
|
||||||
|
|
||||||
|
en := json.NewEncoder(f)
|
||||||
|
en.SetIndent("", " ")
|
||||||
|
en.Encode(m.Confv(prefix, kit.Keys(key)))
|
||||||
|
m.Log_EXPORT(kit.MDB_FILE, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
const IMPORT = "import"
|
const IMPORT = "import"
|
||||||
const EXPORT = "export"
|
const EXPORT = "export"
|
||||||
|
|
||||||
var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
||||||
Caches: map[string]*ice.Cache{},
|
|
||||||
Configs: map[string]*ice.Config{
|
|
||||||
"location": {Name: "location", Help: "定位", Value: kit.Data(kit.MDB_SHORT, "name")},
|
|
||||||
},
|
|
||||||
Commands: map[string]*ice.Command{
|
Commands: map[string]*ice.Command{
|
||||||
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
|
||||||
m.Load()
|
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}},
|
||||||
}},
|
|
||||||
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
m.Save(m.Prefix("location"))
|
|
||||||
}},
|
|
||||||
kit.MDB_IMPORT: {Name: "import conf key type file", Help: "导入数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
kit.MDB_IMPORT: {Name: "import conf key type file", Help: "导入数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
switch msg := m.Cmd(ice.WEB_STORY, "index", arg[3]); arg[2] {
|
switch arg[2] {
|
||||||
case kit.MDB_LIST:
|
case kit.MDB_LIST:
|
||||||
_list_import(m, arg[0], arg[1], msg.Append(kit.MDB_FILE), msg.Append(kit.MDB_TEXT))
|
_list_import(m, arg[0], arg[1], arg[3])
|
||||||
case kit.MDB_HASH:
|
case kit.MDB_HASH:
|
||||||
_hash_import(m, arg[0], arg[1], msg.Append(kit.MDB_FILE), msg.Append(kit.MDB_TEXT))
|
_hash_import(m, arg[0], arg[1], arg[3])
|
||||||
|
case kit.MDB_DICT:
|
||||||
|
_dict_import(m, arg[0], arg[1], arg[3])
|
||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
kit.MDB_EXPORT: {Name: "export conf key type [name]", Help: "导出数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
kit.MDB_EXPORT: {Name: "export conf key type [name]", Help: "导出数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
@ -130,47 +151,11 @@ var Index = &ice.Context{Name: "mdb", Help: "数据模块",
|
|||||||
_list_export(m, arg[0], arg[1], file)
|
_list_export(m, arg[0], arg[1], file)
|
||||||
case kit.MDB_HASH:
|
case kit.MDB_HASH:
|
||||||
_hash_export(m, arg[0], arg[1], file)
|
_hash_export(m, arg[0], arg[1], file)
|
||||||
|
case kit.MDB_DICT:
|
||||||
|
_dict_export(m, arg[0], arg[1], file)
|
||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
|
|
||||||
"location": {Name: "location", Help: "location", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
|
||||||
if len(arg) == 0 {
|
|
||||||
m.Grows("location", nil, "", "", func(index int, value map[string]interface{}) {
|
|
||||||
m.Push("", value)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(arg) == 1 {
|
|
||||||
m.Richs("location", nil, arg[0], func(key string, value map[string]interface{}) {
|
|
||||||
m.Info("what %v", value)
|
|
||||||
m.Push("detail", value)
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(arg) == 2 {
|
|
||||||
m.Richs("aaa.location", nil, "*", func(key string, value map[string]interface{}) {
|
|
||||||
m.Push("name", value["name"])
|
|
||||||
m.Push("distance", kit.Int(distance(
|
|
||||||
float64(kit.Int(arg[0]))/100000,
|
|
||||||
float64(kit.Int(arg[1]))/100000,
|
|
||||||
float64(kit.Int(value["latitude"]))/100000,
|
|
||||||
float64(kit.Int(value["longitude"]))/100000,
|
|
||||||
)*1000))
|
|
||||||
})
|
|
||||||
m.Sort("distance", "int")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data := m.Richs("location", nil, arg[0], nil)
|
|
||||||
if data != nil {
|
|
||||||
data["count"] = kit.Int(data["count"]) + 1
|
|
||||||
} else {
|
|
||||||
data = kit.Dict("name", arg[0], "address", arg[1], "latitude", arg[2], "longitude", arg[3], "count", 1)
|
|
||||||
m.Rich("location", nil, data)
|
|
||||||
}
|
|
||||||
m.Grow("location", nil, data)
|
|
||||||
}},
|
|
||||||
|
|
||||||
"update": {Name: "update config table index key value", Help: "修改数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
"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")
|
meta := m.Confm(arg[0], arg[1]+".meta")
|
||||||
index := kit.Int(arg[2]) - kit.Int(meta["offset"]) - 1
|
index := kit.Int(arg[2]) - kit.Int(meta["offset"]) - 1
|
||||||
|
@ -263,16 +263,16 @@ func _story_add(m *ice.Message, arg ...string) {
|
|||||||
m.Echo(list)
|
m.Echo(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分发数据
|
// // 分发数据
|
||||||
for _, k := range []string{"you", "pod"} {
|
// for _, k := range []string{"you", "pod"} {
|
||||||
if p := m.Option(k); p != "" {
|
// if p := m.Option(k); p != "" {
|
||||||
m.Option(k, "")
|
// m.Option(k, "")
|
||||||
m.Cmd(ice.WEB_PROXY, p, STORY, ice.STORY_PULL, arg[2], "dev", arg[2])
|
// m.Cmd(ice.WEB_PROXY, p, STORY, ice.STORY_PULL, arg[2], "dev", arg[2])
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
m.Cmd(ice.WEB_PROXY, m.Conf(ice.WEB_FAVOR, "meta.proxy"),
|
// m.Cmd(ice.WEB_PROXY, m.Conf(ice.WEB_FAVOR, "meta.proxy"),
|
||||||
STORY, ice.STORY_PULL, arg[2], "dev", arg[2])
|
// STORY, ice.STORY_PULL, arg[2], "dev", arg[2])
|
||||||
}
|
}
|
||||||
func _story_trash(m *ice.Message, arg ...string) {
|
func _story_trash(m *ice.Message, arg ...string) {
|
||||||
bak := kit.Select(kit.Keys(arg[1], "bak"), arg, 2)
|
bak := kit.Select(kit.Keys(arg[1], "bak"), arg, 2)
|
||||||
|
@ -66,7 +66,7 @@ var Index = &ice.Context{Name: "lark", Help: "机器人",
|
|||||||
Commands: map[string]*ice.Command{
|
Commands: map[string]*ice.Command{
|
||||||
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
m.Load()
|
m.Load()
|
||||||
web.SpideCreate(m, LARK, m.Conf(APP, "meta.lark"))
|
m.Cmd(web.SPIDE, "add", LARK, m.Conf(APP, "meta.lark"))
|
||||||
m.Cmd(DUTY, "boot", m.Conf(ice.CLI_RUNTIME, "boot.hostname"), m.Time())
|
m.Cmd(DUTY, "boot", m.Conf(ice.CLI_RUNTIME, "boot.hostname"), m.Time())
|
||||||
}},
|
}},
|
||||||
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user