Skip to content

Commit

Permalink
Merge pull request #8 from lucasvmx/dev
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
lucasvmx authored Aug 20, 2023
2 parents 2db79f4 + 2fe4f62 commit adabaf2
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 81 deletions.
14 changes: 6 additions & 8 deletions controller/gamechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/gamechat"
client "github.com/lucasvmx/WarTelemetry/network/http"
)

// GetGamechatData retrieves data about gamechat
func GetGamechatData(wg *sync.WaitGroup) {
func GetGamechatData() error {

var gc []gamechat.GameChat = []gamechat.GameChat{}
defer wg.Done()

// Sends the GET request
body, err := client.GetDataFromURL(gamechat.GetURL())
if err != nil {
logger.LogError("failed to get gamechat data: %v", err)
return
return fmt.Errorf("failed to get gamechat data: %v", err)
}

// Decode data into a struct
err = json.Unmarshal(body, &gc)
if err != nil {
logger.LogError("failed to get gamechat data: %v", err)
return
return fmt.Errorf("failed to get gamechat data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()

model.TelemetryInstance.Gamechat = gc

return nil
}
14 changes: 6 additions & 8 deletions controller/hudmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/hudmsg"
network "github.com/lucasvmx/WarTelemetry/network/http"
)

// GetHudMessagesData function retrieves all messages from HUD
func GetHudMessagesData(wg *sync.WaitGroup) {
func GetHudMessagesData() error {
var messages *hudmsg.Hudmsg
defer wg.Done()

data, err := network.GetDataFromURL(hudmsg.GetURL())
if err != nil {
logger.LogError("failed to get hud message data: %v", err)
return
return fmt.Errorf("failed to get hud message data: %v", err)
}

// Decode data into a struct
err = json.Unmarshal(data, &messages)
if err != nil {
logger.LogError("failed to get hud message data: %v", err)
return
return fmt.Errorf("failed to get hud message data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
model.TelemetryInstance.HudMessages = messages

return nil
}
14 changes: 6 additions & 8 deletions controller/indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/indicators"
network "github.com/lucasvmx/WarTelemetry/network/http"
)

// GetIndicatorsData function retrieves data about aircraft indicators
func GetIndicatorsData(wg *sync.WaitGroup) {
func GetIndicatorsData() error {
var id *indicators.Indicators = &indicators.Indicators{}
defer wg.Done()

data, err := network.GetDataFromURL(indicators.GetURL())
if err != nil {
logger.LogError("failed to get indicators data: %v", err)
return
return fmt.Errorf("failed to get indicators data: %v", err)
}

err = json.Unmarshal(data, &id)
if err != nil {
logger.LogError("failed to get indicators data: %v", err)
return
return fmt.Errorf("failed to get indicators data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
model.TelemetryInstance.Indicators = id

return nil
}
14 changes: 6 additions & 8 deletions controller/mapinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/mapinfo"
network "github.com/lucasvmx/WarTelemetry/network/http"
)

// GetMapInfoData function retrieves information about current map
func GetMapInfoData(wg *sync.WaitGroup) {
func GetMapInfoData() error {
var mi *mapinfo.MapInformation = &mapinfo.MapInformation{}
defer wg.Done()

data, err := network.GetDataFromURL(mapinfo.GetURL())
if err != nil {
logger.LogError("failed to get map information data: %v", err)
return
return fmt.Errorf("failed to get map information data: %v", err)
}

err = json.Unmarshal(data, &mi)
if err != nil {
logger.LogError("failed to get map information data: %v", err)
return
return fmt.Errorf("failed to get map information data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()

model.TelemetryInstance.MapInfo = mi

return nil
}
14 changes: 6 additions & 8 deletions controller/mapobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/mapobjects"
network "github.com/lucasvmx/WarTelemetry/network/http"
)

// GetMapObjsData function retrives data about all map objects
func GetMapObjsData(wg *sync.WaitGroup) {
func GetMapObjsData() error {
var mo []mapobjects.MapObjects = []mapobjects.MapObjects{}
defer wg.Done()

// Sends GET request
data, err := network.GetDataFromURL(mapobjects.GetURL())
if err != nil {
logger.LogError("failed to get map objects data: %v", err)
return
return fmt.Errorf("failed to get map objects data: %v", err)
}

// Decode data into a json struct
err = json.Unmarshal(data, &mo)
if err != nil {
logger.LogError("failed to get map objects data: %v", err)
return
return fmt.Errorf("failed to get map objects data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
model.TelemetryInstance.MapObjects = mo

return nil
}
13 changes: 5 additions & 8 deletions controller/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/mission"
network "github.com/lucasvmx/WarTelemetry/network/http"
)

// GetMissionData function retrieves data about running missions
func GetMissionData(wg *sync.WaitGroup) {
func GetMissionData() error {
var mi *mission.MissionInfo
defer wg.Done()

// Sends a HTTP request
data, err := network.GetDataFromURL(mission.GetURL())
if err != nil {
logger.LogError("failed to get mission data: %v", err)
return
return fmt.Errorf("failed to get mission data: %v", err)
}

// Decode JSON into a struct
err = json.Unmarshal(data, &mi)
if err != nil {
logger.LogError("failed to get mission data: %v", err)
return
return fmt.Errorf("failed to get mission data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()

model.TelemetryInstance.MissionInfo = mi
return nil
}
14 changes: 6 additions & 8 deletions controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ package controller

import (
"encoding/json"
"sync"
"fmt"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/model/state"
network "github.com/lucasvmx/WarTelemetry/network/http"
"github.com/lucasvmx/WarTelemetry/utils"
)

// GetStateData function retrieves data about running missions
func GetStateData(wg *sync.WaitGroup) {
func GetStateData() error {
var st *state.AircraftState = &state.AircraftState{}
defer wg.Done()

// Sends a HTTP request
data, err := network.GetDataFromURL(state.GetURL())
if err != nil {
logger.LogError("failed to get state data: %v", err)
return
return fmt.Errorf("failed to get state data: %v", err)
}

// Process JSON into a readable format
Expand All @@ -29,11 +26,12 @@ func GetStateData(wg *sync.WaitGroup) {
// Decode JSON into a struct
err = json.Unmarshal(data, &st)
if err != nil {
logger.LogError("failed to get state data: %v", err)
return
return fmt.Errorf("failed to get state data: %v", err)
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
model.TelemetryInstance.State = st

return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/lucasvmx/WarTelemetry

go 1.20

require golang.org/x/sync v0.3.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
36 changes: 11 additions & 25 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package telemetry

import (
"fmt"
"sync"
"time"

"github.com/lucasvmx/WarTelemetry/controller"
"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model"
"github.com/lucasvmx/WarTelemetry/utils"
"golang.org/x/sync/errgroup"
)

var wg *sync.WaitGroup
var errGroup *errgroup.Group

var (
collectors = map[string]func(*sync.WaitGroup){
collectors = map[string]func() error{
"gamechat": controller.GetGamechatData,
"indicators": controller.GetIndicatorsData,
"map_info": controller.GetMapInfoData,
Expand All @@ -34,22 +31,17 @@ func Initialize(hostname string) {
}

model.SetupTelemetry()
wg = &sync.WaitGroup{}
errGroup = new(errgroup.Group)
}

func getTelemetryData() {

start := time.Now()
func getTelemetryData() error {

for name, callback := range collectors {
logger.LogInfo("running task go get %v data", name)
wg.Add(1)
go callback(wg)
logger.LogInfo("running task to get %v data", name)
errGroup.Go(callback)
}

wg.Wait()

logger.LogInfo("fetched data in %v", time.Since(start))
return errGroup.Wait()
}

// GetTelemetryData function retrieves all telemetry data
Expand All @@ -58,7 +50,9 @@ func GetTelemetryData() (data *model.TelemetryData, err error) {
// Initialize struct
data = &model.TelemetryData{}

getTelemetryData()
if fail := getTelemetryData(); fail != nil {
return nil, fail
}

data.MapInfo = model.GetInstance().MapInfo
data.Gamechat = model.GetInstance().Gamechat
Expand All @@ -68,13 +62,5 @@ func GetTelemetryData() (data *model.TelemetryData, err error) {
data.MapObjects = model.GetInstance().MapObjects
data.MissionInfo = model.GetInstance().MissionInfo

if data.MapInfo == nil || data.Gamechat == nil ||
data.Indicators == nil || data.State == nil ||
data.HudMessages == nil || data.MapObjects == nil ||
data.MissionInfo == nil {

return nil, fmt.Errorf("invalid data returned from some (or all) endpoints")
}

return
}

0 comments on commit adabaf2

Please sign in to comment.