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

View File

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