handlers/administration goroutines

This commit is contained in:
root 2023-04-20 19:14:00 +03:00
parent c34d08ff70
commit b54961b619
2 changed files with 77 additions and 72 deletions

View File

@ -8,6 +8,7 @@ import (
"io" "io"
"net/http" "net/http"
"sort" "sort"
"sync"
"time" "time"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification" "git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
@ -27,79 +28,83 @@ import (
) )
func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) { func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
var (
accounts, err := h.services.GetAccounts() wg sync.WaitGroup
if err != nil { accounts, beneficiaries []mobilityaccountsstorage.Account
fmt.Println(err) bookings []fleetsstorage.Booking
w.WriteHeader(http.StatusInternalServerError) accountsErr, beneficiariesErr, bookingsErr, groupsResponseErr, eventsResponseErr, groupsBatchErr error
return groups = []groupstorage.Group{}
} responses = []agendastorage.Event{}
groupsResponse *groupsmanagement.GetGroupsResponse
beneficiaries, err := h.services.GetBeneficiaries() eventsResponse *agenda.GetEventsResponse
if err != nil { groupids = []string{}
fmt.Println(err) groupsBatchResponse *groupsmanagement.GetGroupsBatchResponse
w.WriteHeader(http.StatusInternalServerError) )
return // Retrieve accounts in a goroutine
} wg.Add(1)
go func() {
bookings, err := h.services.GetBookings() defer wg.Done()
if err != nil { accounts, accountsErr = h.services.GetAccounts()
fmt.Println(err) }()
w.WriteHeader(http.StatusInternalServerError) // Retrieve beneficiaries in a goroutine
return wg.Add(1)
} go func() {
defer wg.Done()
beneficiaries, beneficiariesErr = h.services.GetBeneficiaries()
}()
// Retrieve bookings in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
bookings, bookingsErr = h.services.GetBookings()
}()
// Retrieve groupsRequest in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
request := &groupsmanagement.GetGroupsRequest{ request := &groupsmanagement.GetGroupsRequest{
Namespaces: []string{"parcoursmob_organizations"}, Namespaces: []string{"parcoursmob_organizations"},
} }
groupsResponse, groupsResponseErr = h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request)
resp, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request) for _, group := range groupsResponse.Groups {
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var groups = []groupstorage.Group{}
for _, group := range resp.Groups {
g := group.ToStorageType() g := group.ToStorageType()
groups = append(groups, g) groups = append(groups, g)
} }
sort.Sort(sorting.GroupsByName(groups)) sort.Sort(sorting.GroupsByName(groups))
////////////////////////////////////add event//////////////////////////////////////////// }()
rresp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{ // Retrieve Events in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
eventsResponse, eventsResponseErr = h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"}, Namespaces: []string{"parcoursmob_dispositifs"},
}) })
for _, e := range eventsResponse.Events {
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
responses := []agendastorage.Event{}
groupids := []string{}
for _, e := range rresp.Events {
groupids = append(groupids, e.Owners...) groupids = append(groupids, e.Owners...)
responses = append(responses, e.ToStorageType()) responses = append(responses, e.ToStorageType())
} }
sort.Sort(sorting.EventsByStartdate(responses)) sort.Sort(sorting.EventsByStartdate(responses))
}()
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{ wg.Add(1)
// Retrieve groupsBatch in a goroutine
go func() {
defer wg.Done()
groupsBatchResponse, groupsBatchErr = h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids, Groupids: groupids,
}) })
groupps := map[string]any{} groupps := map[string]any{}
if groupsBatchErr == nil {
if err == nil { for _, g := range groupsBatchResponse.Groups {
for _, g := range groupsresp.Groups {
groupps[g.Id] = g.ToStorageType() groupps[g.Id] = g.ToStorageType()
} }
} }
}()
wg.Wait()
if accountsErr != nil || beneficiariesErr != nil || bookingsErr != nil || groupsResponseErr != nil || eventsResponseErr != nil {
fmt.Println(accountsErr, beneficiariesErr, bookingsErr, groupsResponseErr, eventsResponseErr, groupsBatchErr)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.Administration(w, r, accounts, beneficiaries, groups, bookings, responses) h.Renderer.Administration(w, r, accounts, beneficiaries, groups, bookings, responses)
} }

View File

@ -26,7 +26,7 @@ import (
func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Request) { func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
fmt.Println("invoked")
var beneficiary mobilityaccountsstorage.Account var beneficiary mobilityaccountsstorage.Account
beneficiarydocuments := []filestorage.FileInfo{} beneficiarydocuments := []filestorage.FileInfo{}