Capstone/ui/controllers/components.go

128 lines
3.4 KiB
Go
Raw Normal View History

2023-11-04 23:43:01 -04:00
package controllers
import (
"fmt"
2023-11-04 23:43:01 -04:00
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/templates"
"github.com/gin-gonic/gin"
2023-11-23 15:07:57 -05:00
"golang.org/x/text/language"
"golang.org/x/text/message"
2023-11-04 23:43:01 -04:00
)
func GetAddActionForm(c *gin.Context) {
user := getUserFromContext(c)
if user == nil {
log.Warnf("Could not find user in context. Trying to redner Action form")
badRequest(c, "No user available in context")
return
}
2023-11-14 13:48:12 -05:00
accounts, err := mongo.FindAllVendorAccountsByUser(user.Id)
2023-11-04 23:43:01 -04:00
if err != nil {
log.WithError(err).Errorf("Failed to find vendor accounts for: %s", user.Email)
serverError(c, "No user available in context")
return
}
renderTempl(c, templates.DashboardActionModal(accounts))
}
2023-11-23 15:07:57 -05:00
type DashboardMetric struct {
Title string
PrimaryValue string
SecondaryValue string
Subtitle string
}
type dashboardMetricFunc func(c *gin.Context) *DashboardMetric
var metricFuncMap = map[string]dashboardMetricFunc{"default": defaultMetricFunction, "events_received": eventsRecievedMetricFunction, "streams_scheduled": streamsScheduledMetricFunction}
func GetMetricCard(c *gin.Context) {
user := getUserFromContext(c)
if user == nil {
log.Warnf("Could not find user in context. Trying to redner Action form")
badRequest(c, "No user available in context")
return
}
if metric, ok := c.GetQuery("metric"); ok {
if metricFunc, mok := metricFuncMap[metric]; mok {
renderDashboardMetric(c, &metricFunc)
return
}
}
//send default metric function
log.Warn("Failed to find metricfunc")
defaultFunc := metricFuncMap["default"]
renderDashboardMetric(c, &defaultFunc)
}
func defaultMetricFunction(c *gin.Context) *DashboardMetric {
return &DashboardMetric{
Title: "Err",
PrimaryValue: "0.00",
SecondaryValue: "0.00",
Subtitle: "something went wrong",
}
}
func renderDashboardMetric(c *gin.Context, metricFunc *dashboardMetricFunc) {
metric := (*metricFunc)(c)
renderTempl(c, templates.DashboardCard(metric.Title, metric.PrimaryValue, metric.SecondaryValue, metric.Subtitle))
}
func eventsRecievedMetricFunction(c *gin.Context) *DashboardMetric {
user := getUserFromContext(c)
events, err := mongo.AggregateVendorEventReport(user.Id)
if err != nil {
log.WithError(err).Errorf("Failed to find events for user: %s", user.Id.Hex())
return defaultMetricFunction(c)
}
totalEvents := 0
biggestVendor := 0
for index, event := range events {
totalEvents += event.Count
if events[biggestVendor].Count < event.Count {
biggestVendor = index
}
}
p := message.NewPrinter(language.English)
metric := &DashboardMetric{
Title: "Events Recieved",
PrimaryValue: p.Sprintf("%d", totalEvents),
}
if len(events) > 0 {
metric.Subtitle = fmt.Sprintf("Most events from: %s", events[biggestVendor].Name)
2023-11-23 15:07:57 -05:00
}
return metric
2023-11-23 15:07:57 -05:00
}
func streamsScheduledMetricFunction(c *gin.Context) *DashboardMetric {
2023-11-24 02:00:28 -05:00
user := getUserFromContext(c)
events, err := mongo.AggregateBroadcastReport(user.Id)
if err != nil {
log.WithError(err).Errorf("Failed to find broadcast report for user: %s", user.Id.Hex())
return defaultMetricFunction(c)
}
totalEvents := 0
for _, event := range events {
totalEvents += event.Count
}
p := message.NewPrinter(language.English)
return &DashboardMetric{
Title: "Broadcasts scheduled",
PrimaryValue: p.Sprintf("%d", totalEvents),
SecondaryValue: "",
Subtitle: "Scheduled to youtube",
}
2023-11-23 15:07:57 -05:00
}