added platform count and transmissions table

main
Nilo Roberto C Paim 2025-08-13 10:15:21 -03:00
parent 5707a7eb4d
commit e7b34caeb4
4 changed files with 48 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package controllers
import ( import (
"api/globals" "api/globals"
"log"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -27,7 +28,9 @@ func GetTransmissionByChannel(c *fiber.Ctx) error {
} }
func WatchersCount(c *fiber.Ctx) error { func WatchersCount(c *fiber.Ctx) error {
channel := c.Params("channel") channel := c.Params("channel")
if channel == "" { if channel == "" {
return fiber.ErrBadRequest return fiber.ErrBadRequest
} }
@ -43,13 +46,22 @@ func WatchersCount(c *fiber.Ctx) error {
func Watch(c *fiber.Ctx) error { func Watch(c *fiber.Ctx) error {
channel := c.Params("channel") channel := c.Params("channel")
platform := c.Params("platform")
if channel == "" { if channel == "" {
return fiber.ErrBadRequest return fiber.ErrBadRequest
} }
log.Printf("User started watching channel on %s: %s", platform, channel)
if transmission, exists := globals.Transmissions[channel]; exists { if transmission, exists := globals.Transmissions[channel]; exists {
transmission.Watchers = transmission.Watchers + 1 transmission.Watchers = transmission.Watchers + 1
switch platform {
case "desktop":
transmission.Desktop = transmission.Desktop + 1
case "mobile":
transmission.Mobile = transmission.Mobile + 1
}
return c.JSON(transmission.Watchers) return c.JSON(transmission.Watchers)
} }
@ -60,16 +72,25 @@ func Watch(c *fiber.Ctx) error {
func Leave(c *fiber.Ctx) error { func Leave(c *fiber.Ctx) error {
channel := c.Params("channel") channel := c.Params("channel")
platform := c.Params("platform")
if channel == "" { if channel == "" {
return fiber.ErrBadRequest return fiber.ErrBadRequest
} }
log.Printf("User stopped watching channel: %s", channel)
if transmission, exists := globals.Transmissions[channel]; exists { if transmission, exists := globals.Transmissions[channel]; exists {
transmission.Watchers = transmission.Watchers - 1 transmission.Watchers = transmission.Watchers - 1
if transmission.Watchers < 0 { if transmission.Watchers < 0 {
transmission.Watchers = 0 transmission.Watchers = 0
} }
switch platform {
case "desktop":
transmission.Desktop = transmission.Desktop - 1
case "mobile":
transmission.Mobile = transmission.Mobile - 1
}
return c.JSON(transmission.Watchers) return c.JSON(transmission.Watchers)
} }

View File

@ -121,7 +121,7 @@ func OnPubStop(c *fiber.Ctx) error {
return c.SendString("On_Pub_Stop: " + string(c.Body())) return c.SendString("On_Pub_Stop: " + string(c.Body()))
} }
log.Printf("======================== Stop StreamName %s - Duration %d\n", p.StreamName, duration) log.Printf("======================== Transmissão encerrada do canal %s - Duration %d\n", p.StreamName, duration)
// TODO: Updates the transmission on database for future calculation of remaining daily time // TODO: Updates the transmission on database for future calculation of remaining daily time
@ -129,9 +129,13 @@ func OnPubStop(c *fiber.Ctx) error {
minutes = 1 minutes = 1
} }
msg := fmt.Sprintf("Transmissão encerrada. Duração %d minutos.\n", int(minutes)) // msg := fmt.Sprintf("Transmissão encerrada. Duração %d minutos.\n", int(minutes))
services.AddTransmissionlog(p.StreamName, msg) // services.AddTransmissionlog(p.StreamName, msg)
// Saves the transmission on the database
transm.Duration = minutes
globals.DB.Save(&transm)
delete(globals.Transmissions, p.StreamName) delete(globals.Transmissions, p.StreamName)
return c.SendString("On_Pub_Stop: " + string(c.Body())) return c.SendString("On_Pub_Stop: " + string(c.Body()))

View File

@ -229,13 +229,23 @@ func (TransmissionLog) TableName() string {
return "transmissionlog" return "transmissionlog"
} }
// Available only on RAM // Available on RAM during the execution
// CurrentTransmission represents the transmissions table in the database
type CurrentTransmission struct { type CurrentTransmission struct {
Channel string ID uint `gorm:"primaryKey;column:id;autoIncrement"`
StartTime time.Time Channel string `gorm:"column:channel;type:text;not null"`
Limit time.Time StartTime time.Time `gorm:"column:starttime;type:timestamp;not null"`
SessionID string Duration int `gorm:"column:duration;not null"`
PlayerKey string Limit time.Time `gorm:"-"`
PlanDailyLimit int SessionID string `gorm:"-"`
Watchers int PlayerKey string `gorm:"-"`
PlanDailyLimit int `gorm:"-"`
Watchers int `gorm:"column:watchers;not null"`
Desktop int `gorm:"column:desktop;not null"`
Mobile int `gorm:"column:mobile;not null"`
}
// TableName overrides the table name
func (CurrentTransmission) TableName() string {
return "transmissions"
} }

View File

@ -34,6 +34,6 @@ func Setup(app *fiber.App) {
// Interfaces to player // Interfaces to player
app.Get("/c/:channel", controllers.WatchersCount) app.Get("/c/:channel", controllers.WatchersCount)
app.Get("/w/:channel", controllers.Watch) app.Get("/w/:channel/:platform", controllers.Watch)
app.Get("/l/:channel", controllers.Leave) app.Get("/l/:channel/:platform", controllers.Leave)
} }