From 1b2902c6320ef7577c562cd49b985e3481a37454 Mon Sep 17 00:00:00 2001 From: soukainna Date: Mon, 6 Mar 2023 10:23:49 +0100 Subject: [PATCH] add functions to display statistiques --- handlers/application/administration.go | 199 +++++++++++++++++++++++-- 1 file changed, 183 insertions(+), 16 deletions(-) diff --git a/handlers/application/administration.go b/handlers/application/administration.go index 052d8cb..63ca02f 100644 --- a/handlers/application/administration.go +++ b/handlers/application/administration.go @@ -10,19 +10,20 @@ import ( "sort" "time" + "git.coopgo.io/coopgo-apps/parcoursmob/utils/identification" "git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting" agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi" agendastorage "git.coopgo.io/coopgo-platform/agenda/storage" fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi" - "git.coopgo.io/coopgo-platform/fleets/storage" + fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage" groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" + "git.coopgo.io/coopgo-platform/groups-management/storage" groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage" accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage" "github.com/google/uuid" "github.com/gorilla/mux" "google.golang.org/protobuf/types/known/structpb" - "google.golang.org/protobuf/types/known/timestamppb" ) func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) { @@ -70,7 +71,6 @@ func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Reque ////////////////////////////////////add event//////////////////////////////////////////// rresp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{ Namespaces: []string{"parcoursmob_dispositifs"}, - Mindate: timestamppb.New(time.Now().Add(-24 * time.Hour)), }) if err != nil { @@ -368,30 +368,125 @@ func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWrit return } +func filteVehicle(r *http.Request, v *fleets.Vehicle) bool { + g := r.Context().Value(identification.GroupKey) + if g == nil { + return false + } + + group := g.(storage.Group) + + for _, n := range v.Administrators { + if n == group.ID { + return true + } + } + + return false +} + func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Request) { - request := &fleets.GetBookingsRequest{} - resp, err := h.services.GRPC.Fleets.GetBookings(context.TODO(), request) + // request := &fleets.GetVehiclesRequest{ + // Namespaces: []string{"parcoursmob"}, + // } + // resp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), request) + // if err != nil { + // fmt.Println(err) + // w.WriteHeader(http.StatusInternalServerError) + // } + + //vehicles := []fleetsstorage.Vehicle{} + bookings := []fleetsstorage.Booking{} + //vehicles_map := map[string]fleetsstorage.Vehicle{} + + // for _, vehicle := range resp.Vehicles { + // if filteVehicle(r, vehicle) { + // v := vehicle.ToStorageType() + // vehicles = append(vehicles, v) + // vehicles_map[v.ID] = v + // for _, b := range v.Bookings { + // if b.Status() != fleetsstorage.StatusOld { + // bookings = append(bookings, b) + // } + // } + + // } + // } + administrators := []string{} + reequest := &fleets.GetVehiclesRequest{ + Namespaces: []string{"parcoursmob"}, + } + reesp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), reequest) if err != nil { fmt.Println(err) - w.WriteHeader(http.StatusNotFound) - return + w.WriteHeader(http.StatusInternalServerError) } - bookings := []storage.Booking{} + vehicles := []fleetsstorage.Vehicle{} + for _, vehiicle := range reesp.Vehicles { + + v := vehiicle.ToStorageType() + adminfound := false + for _, a := range administrators { + if a == v.Administrators[0] { + adminfound = true + break + } + } + if !adminfound { + administrators = append(administrators, v.Administrators[0]) + } + + vehicles = append(vehicles, v) - for _, b := range resp.Bookings { - booking := b.ToStorageType() - bookings = append(bookings, booking) } + groups := map[string]any{} - sort.Sort(sorting.BookingsByStartdate(bookings)) + if len(administrators) > 0 { + admingroups, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{ + Groupids: administrators, + }) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } - vehicles, _ := h.services.GetVehiclesMap() - groups, _ := h.services.GetGroupsMap() + for _, g := range admingroups.Groups { + groups[g.Id] = g.ToStorageType() + } + + } + //fmt.Println(groups) + //fmt.Println("****************************************************************") // fmt.Println(vehicles) - // fmt.Println("////////////////////////////////////////:") - h.Renderer.VehicleBookingsList(w, r, bookings, vehicles, groups) + // fmt.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") + // id := "" + // for _, v := range vehicles { + // id = v.ID + // for _, vv := range v.Bookings { + + // i := 1 + // // fmt.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") + // // fmt.Println(vv.Vehicleid) + // // fmt.Println("*********************************************************") + // if id == vv.Vehicleid { + // i++ + // fmt.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") + // fmt.Println(vv.Vehicleid) + // fmt.Println("*********************************************************") + // } + // fmt.Println(i) + + // } + // fmt.Println(v.Status) + // fmt.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") + //} + // fmt.Println(bookings) + sort.Sort(sorting.VehiclesByLicencePlate(vehicles)) + sort.Sort(sorting.BookingsByStartdate(bookings)) + h.Renderer.AdminStatVehicles(w, r, vehicles, bookings, groups) } func (h *ApplicationHandler) members() ([]*accounts.Account, error) { @@ -431,3 +526,75 @@ func (h *ApplicationHandler) groupmembers(groupid string) (groupmembers []mobili return groupmembers, admins, err } + +////////////////////////available //////////////////////////////////// +// func (h ApplicationHandler) AdminStatAvailableVehicles(w http.ResponseWriter, r *http.Request) { +// request := &fleets.GetVehiclesRequest{ +// Namespaces: []string{"parcoursmob"}, +// } +// resp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), request) +// if err != nil { +// fmt.Println(err) +// w.WriteHeader(http.StatusInternalServerError) +// } + +// vehicles := []fleetsstorage.Vehicle{} +// for i, vehiicle := range resp.Vehicles { +// if len(resp.Vehicles[i].Bookings) == 0 { +// v := vehiicle.ToStorageType() +// vehicles = append(vehicles, v) +// } +// } +// fmt.Println(vehicles) +// fmt.Println("////////////////////////////////////////:") +// h.Renderer.UnbookingVehicles(w, r, vehicles) +// } + +//////////////////////////////beneificiare list/////////// +func (h ApplicationHandler) AdminStatBeneficaires(w http.ResponseWriter, r *http.Request) { + beneficiaries, err := h.services.GetBeneficiaries() + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + cacheid := uuid.NewString() + h.cache.PutWithTTL(cacheid, beneficiaries, 1*time.Hour) + fmt.Println(beneficiaries) + h.Renderer.AdminStatBeneficaires(w, r, beneficiaries, cacheid) +} + +//////////////////////////////event list/////////// +func (h ApplicationHandler) AdminStatEvents(w http.ResponseWriter, r *http.Request) { + resp, 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 resp.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, + }) + groups := map[string]any{} + + if err == nil { + for _, g := range groupsresp.Groups { + groups[g.Id] = g.ToStorageType() + } + } + h.Renderer.AdminStatEvents(w, r, responses, groups) +}