fix: code readability?
This commit is contained in:
parent
e9cef78d54
commit
3768bb162e
1 changed files with 45 additions and 82 deletions
|
@ -9,17 +9,18 @@ import (
|
||||||
"git.wh64.net/muffin/goMuffin/utils"
|
"git.wh64.net/muffin/goMuffin/utils"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type chStruct struct {
|
type chStruct struct {
|
||||||
name lenType
|
name dataType
|
||||||
length int
|
length int
|
||||||
}
|
}
|
||||||
|
|
||||||
type lenType int
|
type dataType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
text lenType = iota
|
text dataType = iota
|
||||||
muffin
|
muffin
|
||||||
nsfw
|
nsfw
|
||||||
learn
|
learn
|
||||||
|
@ -37,6 +38,41 @@ var DataLengthCommand *Command = &Command{
|
||||||
Usage: "머핀아 학습데이터량",
|
Usage: "머핀아 학습데이터량",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
var ch chan chStruct = make(chan chStruct)
|
||||||
|
|
||||||
|
func getLength(data dataType, userId string) {
|
||||||
|
var err error
|
||||||
|
var cur *mongo.Cursor
|
||||||
|
var datas []bson.M
|
||||||
|
|
||||||
|
switch data {
|
||||||
|
case text:
|
||||||
|
cur, err = databases.Texts.Find(context.TODO(), bson.D{{}})
|
||||||
|
case muffin:
|
||||||
|
cur, err = databases.Texts.Find(context.TODO(), bson.D{{Key: "persona", Value: "muffin"}})
|
||||||
|
case nsfw:
|
||||||
|
cur, err = databases.Texts.Find(context.TODO(), bson.D{
|
||||||
|
{
|
||||||
|
Key: "persona",
|
||||||
|
Value: bson.M{
|
||||||
|
"$regex": "^user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
case learn:
|
||||||
|
cur, err = databases.Learns.Find(context.TODO(), bson.D{{}})
|
||||||
|
case userLearn:
|
||||||
|
cur, err = databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer cur.Close(context.TODO())
|
||||||
|
|
||||||
|
cur.All(context.TODO(), &datas)
|
||||||
|
ch <- chStruct{name: data, length: len(datas)}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
||||||
var i *discordgo.Interaction
|
var i *discordgo.Interaction
|
||||||
|
@ -48,8 +84,6 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
||||||
learnLength,
|
learnLength,
|
||||||
userLearnLength int
|
userLearnLength int
|
||||||
|
|
||||||
ch := make(chan chStruct)
|
|
||||||
|
|
||||||
switch m := m.(type) {
|
switch m := m.(type) {
|
||||||
case *discordgo.MessageCreate:
|
case *discordgo.MessageCreate:
|
||||||
username = m.Author.Username
|
username = m.Author.Username
|
||||||
|
@ -70,86 +104,15 @@ func (c *Command) dataLengthRun(s *discordgo.Session, m interface{}) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go getLength(text, "")
|
||||||
var datas []databases.Text
|
go getLength(muffin, "")
|
||||||
|
go getLength(nsfw, "")
|
||||||
cur, err := databases.Texts.Find(context.TODO(), bson.D{{}})
|
go getLength(learn, "")
|
||||||
if err != nil {
|
go getLength(userLearn, userId)
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cur.Close(context.TODO())
|
|
||||||
|
|
||||||
cur.All(context.TODO(), &datas)
|
|
||||||
ch <- chStruct{name: text, length: len(datas)}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
var datas []databases.Text
|
|
||||||
|
|
||||||
cur, err := databases.Texts.Find(context.TODO(), bson.D{{Key: "persona", Value: "muffin"}})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cur.Close(context.TODO())
|
|
||||||
|
|
||||||
cur.All(context.TODO(), &datas)
|
|
||||||
ch <- chStruct{name: muffin, length: len(datas)}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
var datas []databases.Text
|
|
||||||
|
|
||||||
cur, err := databases.Texts.Find(context.TODO(), bson.D{
|
|
||||||
{
|
|
||||||
Key: "persona",
|
|
||||||
Value: bson.M{
|
|
||||||
"$regex": "^user",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cur.Close(context.TODO())
|
|
||||||
|
|
||||||
cur.All(context.TODO(), &datas)
|
|
||||||
ch <- chStruct{name: nsfw, length: len(datas)}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
var datas []databases.Learn
|
|
||||||
|
|
||||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{}})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cur.Close(context.TODO())
|
|
||||||
|
|
||||||
cur.All(context.TODO(), &datas)
|
|
||||||
ch <- chStruct{name: learn, length: len(datas)}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
var datas []databases.Learn
|
|
||||||
|
|
||||||
cur, err := databases.Learns.Find(context.TODO(), bson.D{{Key: "user_id", Value: userId}})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cur.Close(context.TODO())
|
|
||||||
|
|
||||||
cur.All(context.TODO(), &datas)
|
|
||||||
ch <- chStruct{name: userLearn, length: len(datas)}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
resp := <-ch
|
resp := <-ch
|
||||||
switch lenType(resp.name) {
|
switch dataType(resp.name) {
|
||||||
case text:
|
case text:
|
||||||
textLength = resp.length
|
textLength = resp.length
|
||||||
case muffin:
|
case muffin:
|
||||||
|
|
Loading…
Reference in a new issue