add state history
This commit is contained in:
@@ -8,22 +8,25 @@ import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ServerService struct {
|
||||
repository *repository.ServerRepository
|
||||
stateHistoryRepo *repository.StateHistoryRepository
|
||||
apiService *ApiService
|
||||
instances sync.Map
|
||||
configService *ConfigService
|
||||
}
|
||||
|
||||
func NewServerService(repository *repository.ServerRepository, apiService *ApiService, configService *ConfigService) *ServerService {
|
||||
func NewServerService(repository *repository.ServerRepository, stateHistoryRepo *repository.StateHistoryRepository, apiService *ApiService, configService *ConfigService) *ServerService {
|
||||
service := &ServerService{
|
||||
repository: repository,
|
||||
apiService: apiService,
|
||||
configService: configService,
|
||||
stateHistoryRepo: stateHistoryRepo,
|
||||
}
|
||||
servers := repository.GetAll(context.Background())
|
||||
for _, server := range *servers {
|
||||
@@ -40,10 +43,13 @@ func NewServerService(repository *repository.ServerRepository, apiService *ApiSe
|
||||
|
||||
func (s *ServerService) StartAccServerRuntime(server *model.Server) {
|
||||
s.instances.Delete(server.ID)
|
||||
instance := tracking.NewAccServerInstance(server, func(states ...tracking.StateChange) {
|
||||
for _, state := range states {
|
||||
log.Println(tracking.StateChanges[state])
|
||||
}
|
||||
instance := tracking.NewAccServerInstance(server, func(state *model.ServerState, states ...tracking.StateChange) {
|
||||
s.stateHistoryRepo.Insert(context.Background(), &model.StateHistory{
|
||||
ServerID: server.ID,
|
||||
Session: state.Session,
|
||||
PlayerCount: state.PlayerCount,
|
||||
DateCreated: time.Now().UTC(),
|
||||
})
|
||||
})
|
||||
config, _ := DecodeFileName(ConfigurationJson)(server.ConfigPath)
|
||||
cfg := config.(model.Configuration)
|
||||
|
||||
@@ -16,6 +16,7 @@ func InitializeServices(c *dig.Container) {
|
||||
repository.InitializeRepositories(c)
|
||||
|
||||
c.Provide(NewServerService)
|
||||
c.Provide(NewStateHistoryService)
|
||||
c.Provide(NewApiService)
|
||||
c.Provide(NewConfigService)
|
||||
c.Provide(NewLookupService)
|
||||
|
||||
29
local/service/stateHistory.go
Normal file
29
local/service/stateHistory.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/repository"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type StateHistoryService struct {
|
||||
repository *repository.StateHistoryRepository
|
||||
}
|
||||
|
||||
func NewStateHistoryService(repository *repository.StateHistoryRepository) *StateHistoryService {
|
||||
return &StateHistoryService{
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
// GetAll
|
||||
// Gets All rows from StateHistory table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// string: Application version
|
||||
func (as StateHistoryService) GetAll(ctx *fiber.Ctx, id int) *[]model.StateHistory {
|
||||
return as.repository.GetAll(ctx.UserContext(), id)
|
||||
}
|
||||
Reference in New Issue
Block a user