-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only give role nickname to the POC, not to assistants / team members
- Loading branch information
1 parent
307f2c1
commit 9a8253c
Showing
4 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
) | ||
|
||
type FacilityData struct { | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
AirTrafficManagerCID uint64 `json:"atm"` | ||
DeputyAirTrafficManagerCID uint64 `json:"datm"` | ||
TrainingAdministratorCID uint64 `json:"ta"` | ||
EventCoordinatorCID uint64 `json:"ec"` | ||
FacilityEngineerCID uint64 `json:"fe"` | ||
WebMasterCID uint64 `json:"wm"` | ||
} | ||
|
||
type FacilityListWrapper struct { | ||
Data []FacilityData `json:"data"` | ||
} | ||
|
||
func GetFacilities() ([]FacilityData, error) { | ||
response, err := Get("/facility/") | ||
if err != nil { | ||
return nil, err | ||
} | ||
if response.StatusCode == 404 { | ||
return nil, nil | ||
} | ||
if response.StatusCode != 200 { | ||
return nil, errors.New("HTTP Error when fetching facility data") | ||
} | ||
responseData, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var wrapper FacilityListWrapper | ||
err = json.Unmarshal(responseData, &wrapper) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
out := make([]FacilityData, len(wrapper.Data)) | ||
for _, value := range wrapper.Data { | ||
out = append(out, value) | ||
} | ||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package bot | ||
|
||
import ( | ||
"github.com/VATUSA/discord-bot-v3/internal/api" | ||
"log" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var FacilityDataByIdMap = make(map[string]api.FacilityData) | ||
var LastFacilityDataLoad *time.Time = nil | ||
|
||
const FacilityDataCacheDuration = 5 * time.Minute | ||
|
||
func LoadFacilityData() { | ||
facilityData, err := api.GetFacilities() | ||
if err != nil { | ||
log.Printf("Error fetching facility data: %v", err) | ||
return | ||
} | ||
for _, facility := range facilityData { | ||
FacilityDataByIdMap[strings.ToUpper(facility.Id)] = facility | ||
} | ||
} | ||
|
||
func GetFacilityData(facilityId string) api.FacilityData { | ||
if LastFacilityDataLoad == nil || time.Now().After(LastFacilityDataLoad.Add(FacilityDataCacheDuration)) { | ||
LoadFacilityData() | ||
} | ||
facility := FacilityDataByIdMap[facilityId] | ||
return facility | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters