Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #220 from JustHumanz/rework_prediction
Browse files Browse the repository at this point in the history
Remove prediction & slash command logs
  • Loading branch information
JustHumanz authored Oct 8, 2021
2 parents 87b7ab9 + 13f6c74 commit dd07ccf
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 144 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/stretchr/testify v1.7.0 // indirect
github.com/top-gg/go-dbl v0.0.0-20201116001615-e844586b1159
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210908191846-a5e095526f91 // indirect
golang.org/x/net v0.0.0-20210908191846-a5e095526f91
golang.org/x/sys v0.0.0-20210908160347-a851e7ddeee0 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type ConfigFile struct {
DonationLink string `toml:"DonationLink"`
TopGG string `toml:"TOPGG"`
Domain string `toml:"Domain"`
PrometheusURL string `toml:"PrometheusURL"`
LowResources bool `toml:"LowResources"` //Disable update like fanart & set wait every 5 counter
Metric bool `toml:"Metric"`
Twitch struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/generaldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (Member Member) GetSubsCount() (*MemberSubs, error) {
)

val, err := GeneralCache.Get(context.Background(), Key).Result()
if len(val) == 0 || err == redis.Nil {
if val == "" || err == redis.Nil {
rows, err := DB.Query(`SELECT * FROM Subscriber WHERE VtuberMember_id=?`, Member.ID)
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion pkg/engine/initSlash.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,13 @@ func InitSlash(Bot *discordgo.Session, GroupsPayload []database.Group, NewGuild
Type: discordgo.ApplicationCommandOptionString,
Required: true,
Name: "vtuber-name",
Description: "select vtuber name",
Description: "input vtuber name",
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Required: false,
Name: "prediction-count",
Description: "imput prediction-count(days)\nexample,1 is tomorrow or 7 is next week",
},
},
},
Expand Down
44 changes: 44 additions & 0 deletions pkg/engine/prediction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package engine

import (
"encoding/json"
"strconv"

"github.com/JustHumanz/Go-Simp/pkg/config"
"github.com/JustHumanz/Go-Simp/pkg/database"
"github.com/JustHumanz/Go-Simp/pkg/network"
)

func Prediction(vtuber database.Member, state string, tar int) (int, error) {

type PromeMetric struct {
Status string `json:"status"`
Data struct {
Resulttype string `json:"resultType"`
Result []struct {
Metric interface{} `json:"metric"`
Value []interface{} `json:"value"`
} `json:"result"`
} `json:"data"`
}

var Data PromeMetric

RawQuery := "/api/v1/query?query=%0Apredict_linear(get_subscriber{state%3D\"" + state + "\"%2C+vtuber%3D\"" + vtuber.Name + "\"}+[356d]%2C86400*" + strconv.Itoa(tar) + ")"

RawData, err := network.Curl(config.GoSimpConf.PrometheusURL+RawQuery, nil)
if err != nil {
return 0, nil
}
err = json.Unmarshal(RawData, &Data)
if err != nil {
return 0, nil
}
val := Data.Data.Result[0].Value[1].(string)
valInt, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, nil
}
return int(valInt), nil

}
11 changes: 6 additions & 5 deletions service/frontend/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"math/rand"
"reflect"
"regexp"
"strconv"
Expand All @@ -14,7 +15,6 @@ import (
engine "github.com/JustHumanz/Go-Simp/pkg/engine"
network "github.com/JustHumanz/Go-Simp/pkg/network"
pilot "github.com/JustHumanz/Go-Simp/service/pilot/grpc"
"github.com/JustHumanz/Go-Simp/service/prediction"
"github.com/JustHumanz/Go-Simp/service/utility/runfunc"
"github.com/bwmarrin/discordgo"
"github.com/olekukonko/tablewriter"
Expand All @@ -32,7 +32,6 @@ var (
GroupsPayload *[]database.Group
configfile config.ConfigFile
Bot *discordgo.Session
PredictionConn prediction.PredictionClient
VtuberGroupChoices []*discordgo.ApplicationCommandOptionChoice
)

Expand Down Expand Up @@ -67,7 +66,6 @@ const (

func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableColors: true})
PredictionConn = prediction.NewPredictionClient(network.InitgRPC(config.Prediction))
}

//StartInit running the fe
Expand Down Expand Up @@ -164,7 +162,6 @@ func main() {
Bot.AddHandler(BiliBiliSpace)
Bot.AddHandler(YoutubeMessage)
Bot.AddHandler(TwitchMessage)
Bot.AddHandler(SubsMessage)
Bot.AddHandler(Lewd)
Bot.AddHandler(StartRegister)
c.Stop()
Expand All @@ -186,7 +183,7 @@ func main() {
}

go pilot.RunHeartBeat(gRCPconn, "Frontend")
go engine.InitSlash(Bot, *GroupsPayload, nil)
//go engine.InitSlash(Bot, *GroupsPayload, nil)

Bot.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
Expand Down Expand Up @@ -624,3 +621,7 @@ func Clear(v interface{}) {
p := reflect.ValueOf(v).Elem()
p.Set(reflect.Zero(p.Type()))
}

func gacha() bool {
return rand.Float32() < 0.5
}
Loading

0 comments on commit dd07ccf

Please sign in to comment.