Skip to content

Commit

Permalink
Merge pull request #408 from euanwm/feature/throughput_logging
Browse files Browse the repository at this point in the history
feature/throughput logging
  • Loading branch information
euanwm authored Oct 6, 2024
2 parents 22ce939 + 45681fa commit 596ab87
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
11 changes: 9 additions & 2 deletions backend/discordbot/discordbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import "github.com/bwmarrin/discordgo"

// DiscordBot is the main struct for the discord bot
type DiscordBot struct {
Session *discordgo.Session
Channel string
Session *discordgo.Session
Channel string
PlatformChannel string
// todo: add mutex synchronization
}

// New creates a new DiscordBot
Expand All @@ -31,3 +33,8 @@ func (d *DiscordBot) CloseConnection() error {
func (d *DiscordBot) PostMessage(message string) (*discordgo.Message, error) {
return d.Session.ChannelMessageSend(d.Channel, message)
}

// PostPlatformData because I haven't bothered to create a better way of handling multiple channels yet
func (d *DiscordBot) PostPlatformData(message string) (*discordgo.Message, error) {
return d.Session.ChannelMessageSend(d.PlatformChannel, message)
}
3 changes: 3 additions & 0 deletions backend/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var QueryCache dbtools.QueryCache
// EventsData is a global variable that is used to hold the event metadata.
var EventsData structs.EventsMetaData

// TheBank is a glorified byte counter, but it can be expanded to do more stuff
var TheBank structs.BeanCounter

// ServerTime godoc
//
// @Summary Checking the servers localtime
Expand Down
21 changes: 21 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package main
import (
"backend/dbtools"
"backend/discordbot"
"backend/middleware"
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"log"
"os"
"os/signal"
"syscall"
)

func CORSConfig() cors.Config {
Expand All @@ -32,8 +36,10 @@ func setupDiscordBot(bot *discordbot.DiscordBot) {
log.Println("Failed to open discord connection")
}
bot.Channel = os.Getenv("ISSUES_CHANNEL")
bot.PlatformChannel = os.Getenv("PLATFORM_CHANNEL")
if err != nil {
log.Println("Failed to post message to discord")
return
}
log.Println("Discord bot started")
}
Expand All @@ -44,6 +50,7 @@ func buildServer() *gin.Engine {
r := gin.Default()
r.Use(cors.New(CORSConfig()))
r.Use(gzip.Gzip(gzip.DefaultCompression))
r.Use(middleware.PayloadSizer(&TheBank))
r.GET("time", ServerTime)
r.GET("leaderboard", Leaderboard)
r.GET("search", SearchName)
Expand All @@ -67,6 +74,19 @@ func CacheMeOutsideHowBoutDat() {
log.Println("Caching complete")
}

func RestartHandler(bot *discordbot.DiscordBot) {
// This could likely be moved into middleware
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
// post an update into Discord with how many bytes of data were handler
_, _ = bot.PostPlatformData(fmt.Sprintf("Total backend data handled today: %s", TheBank.UnitToString()))
err := bot.CloseConnection()
if err != nil {
log.Printf("Error when closing discord connection: %s", err)
}
}

// @title OpenWeightlifting API
// @description This is the API for OpenWeightlifting.org
// @BasePath /
Expand All @@ -79,6 +99,7 @@ func main() {
setupDiscordBot(&DiscoKaren)
apiServer := buildServer()
go CacheMeOutsideHowBoutDat()
go RestartHandler(&DiscoKaren)
err := apiServer.Run() // listen and serve on
if err != nil {
log.Fatal("Failed to run server")
Expand Down
31 changes: 31 additions & 0 deletions backend/middleware/payloadsizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package middleware

import (
"backend/structs"
"github.com/gin-gonic/gin"
)

type responseWriter struct {
gin.ResponseWriter
bytes int
bank *structs.BeanCounter
}

func (rw *responseWriter) Write(data []byte) (int, error) {
size, err := rw.ResponseWriter.Write(data)
rw.bytes += size
rw.bank.AddBytes(uint64(size))
return size, err
}

func PayloadSizer(theBank *structs.BeanCounter) gin.HandlerFunc {
return func(c *gin.Context) {
rw := &responseWriter{
ResponseWriter: c.Writer,
bytes: 0,
bank: theBank,
}
c.Writer = rw
c.Next()
}
}
29 changes: 29 additions & 0 deletions backend/structs/struct_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,32 @@ func (e EventsMetaData) FetchEventWithinDate(startDate, endDate string) (events
}
return
}

func (e *BeanCounter) AddBytes(bytes uint64) {
e.Bytes += bytes
}

func (e *BeanCounter) ByteCount() uint64 {
return e.Bytes
}

func (e *BeanCounter) UnitToString() string {
const (
Byte = 1 << (10 * iota)
KB
MB
GB
// extend if needed
)

switch {
case e.Bytes >= GB:
return fmt.Sprintf("%.2f GB", float32(e.Bytes)/GB)
case e.Bytes >= MB:
return fmt.Sprintf("%.2f MB", float32(e.Bytes)/MB)
case e.Bytes >= KB:
return fmt.Sprintf("%.2f KB", float32(e.Bytes)/KB)
default:
return fmt.Sprintf("%d bytes", e.Bytes)
}
}
4 changes: 4 additions & 0 deletions backend/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ type LiftReport struct {
ReportedLift Entry `json:"lift"`
Comments string `json:"comments"`
}

type BeanCounter struct {
Bytes uint64
}

0 comments on commit 596ab87

Please sign in to comment.