diff --git a/configs/scripts/run_servers.bat b/configs/scripts/run_servers.bat index 15bb71c..0cd7a9a 100644 --- a/configs/scripts/run_servers.bat +++ b/configs/scripts/run_servers.bat @@ -1,6 +1,6 @@ @echo off -set "PROJECT_ROOT_DIR=absolute-path-to-project-root-directory" +set "PROJECT_ROOT_DIR=E:\nexster-runtime\nexster" cd /d "%PROJECT_ROOT_DIR%" @@ -35,4 +35,6 @@ rem Run boarding_finder server cd ..\..\boarding_finder\cmd start "" /B go run main.go > "%PROJECT_ROOT_DIR%\logs\bdFinder_server.log" 2>&1 +cd /d "%PROJECT_ROOT_DIR%\configs\scripts" + echo --done-- diff --git a/pkgs/models/user/interface.go b/pkgs/models/user/interface.go index 67c04d3..1d04548 100644 --- a/pkgs/models/user/interface.go +++ b/pkgs/models/user/interface.go @@ -18,6 +18,8 @@ type Interface interface { DeleteUser(ctx context.Context, key string) error CreateDocument(ctx context.Context, doc *UserCreateInfo) (string, error) ListFacDepOfAllUsers(ctx context.Context) ([]*map[string]string, error) + // CountOfAllUsers(ctx context.Context) (int, error) + CountOfAllUsers(ctx context.Context) (totalUsers, maleUsers, femaleUsers int, err error) } // TODO: diff --git a/pkgs/models/user/query.go b/pkgs/models/user/query.go index 53a9c36..f1f1996 100644 --- a/pkgs/models/user/query.go +++ b/pkgs/models/user/query.go @@ -2,3 +2,18 @@ package user const listFacDepOfAllUsersQry string = `FOR doc IN users RETURN { key: doc._key, faculty: doc.faculty, field: doc.field }` + +const getUserSignupsQry string = `RETURN LENGTH (FOR user IN users + FILTER user.createdAt >= @fromDate && user.createdAt <=@toDate + RETURN user._key)` + +//haritha mihimal all users +//const getAllUsersCountQry string = `RETURN LENGTH(FOR user IN users +// RETURN user._key)` +// + +const getAllUsersCountQry string = ` +LET totalUsers = LENGTH(FOR user IN users RETURN user._key) +LET maleUsers = LENGTH(FOR user IN users FILTER user.gender == "male" RETURN user._key) +LET femaleUsers = LENGTH(FOR user IN users FILTER user.gender == "female" RETURN user._key) +RETURN { totalUsers, maleUsers, femaleUsers }` diff --git a/pkgs/models/user/user.go b/pkgs/models/user/user.go index df01479..0255385 100644 --- a/pkgs/models/user/user.go +++ b/pkgs/models/user/user.go @@ -182,3 +182,30 @@ func (uc *userCtrler) CreateDocument(ctx context.Context, doc *UserCreateInfo) ( func (uc *userCtrler) ListFacDepOfAllUsers(ctx context.Context) ([]*map[string]string, error) { return uc.ListUsersV2(ctx, listFacDepOfAllUsersQry, map[string]interface{}{}) } +func (uc *userCtrler) CountOfAllUsers(ctx context.Context) (totalUsers, maleUsers, femaleUsers int, err error) { + cursor, err := uc.argClient.Db.Query(ctx, getAllUsersCountQry, nil) + if err != nil { + return + } + defer cursor.Close() + + if !cursor.HasMore() { + err = fmt.Errorf("cursor has no more results") + return + } + + var result struct { + TotalUsers int `json:"totalUsers"` + MaleUsers int `json:"maleUsers"` + FemaleUsers int `json:"femaleUsers"` + } + _, err = cursor.ReadDocument(ctx, &result) + if err != nil { + return + } + totalUsers = result.TotalUsers + maleUsers = result.MaleUsers + femaleUsers = result.FemaleUsers + err = nil + return +} diff --git a/usrmgmt/cmd/main.go b/usrmgmt/cmd/main.go index e94d740..183e1cb 100644 --- a/usrmgmt/cmd/main.go +++ b/usrmgmt/cmd/main.go @@ -107,6 +107,8 @@ func main() { w.Write([]byte("You are in usrmgmt/test page...!")) }) + + router.GET("/usrmgmt/insights/users/all",srv.GetAllUsers) // get all user count router.GET("/usrmgmt/all/friends", srv.ListFriendInfo) router.GET("/usrmgmt/friends/:user_id/count", srv.GetFriendsCount) diff --git a/usrmgmt/pkg/server/interface.go b/usrmgmt/pkg/server/interface.go index 690fcfb..01ae675 100644 --- a/usrmgmt/pkg/server/interface.go +++ b/usrmgmt/pkg/server/interface.go @@ -33,6 +33,7 @@ type Interface interface { EmailAccountCreationLink(w http.ResponseWriter, r *http.Request, _ httprouter.Params) CreateUserAccount(w http.ResponseWriter, r *http.Request, _ httprouter.Params) ValidatePasswordResetLink(w http.ResponseWriter, r *http.Request, _ httprouter.Params) + GetAllUsers(w http.ResponseWriter, r *http.Request, _ httprouter.Params) // all user } type FriendRequest struct { @@ -63,4 +64,5 @@ type ServerConfig struct { DefaultUserRoles []string `yaml:"defaultUserRoles"` RegLinkExpireTime int `yaml:"regLinkExpireTime"` PasswdResetLinkExpireTime int `yaml:"passwdResetLinkExpireTime"` + } diff --git a/usrmgmt/pkg/server/server.go b/usrmgmt/pkg/server/server.go index 3877b73..42f5492 100644 --- a/usrmgmt/pkg/server/server.go +++ b/usrmgmt/pkg/server/server.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + //"os/user" "strconv" "time" @@ -65,7 +66,7 @@ type server struct { tokenGen gjwt.Interface } -var _ Interface = (*server)(nil) +var _ Interface = (*server)(nil) func New(cfg *ServerConfig, sgrInterface socigr.Interface, logger *lg.Logger, mailIntfce umail.Interface, jwtTokenGen gjwt.Interface) *server { return &server{ @@ -76,6 +77,33 @@ func New(cfg *ServerConfig, sgrInterface socigr.Interface, logger *lg.Logger, ma tokenGen: jwtTokenGen, } } +// add a comment +func (s *server) GetAllUsers(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + respBody := map[string]interface{}{ + "state": failed, + "data": map[string]int{}, + } + jwtUserKey, ok := r.Context().Value(jwt.JwtUserKey).(string) + if !ok { + s.logger.Infof("failed to count all users: unsupported user_key type in JWT token: unauthorized request: user_key=%v", r.Context().Value(jwt.JwtUserKey)) + s.sendRespDefault(w, http.StatusUnauthorized, respBody) + return + } + totalUsers, maleUsers, femaleUsers, err := s.scGraph.GetAllUsers(r.Context(), jwtUserKey) //change the function name here + if err != nil { + s.logger.Errorf("failed to count all users: %v: userKey=%s", err, jwtUserKey) + s.sendRespDefault(w, http.StatusInternalServerError, respBody) + return + } + respBody["state"] = success + respBody["data"] = map[string]int{ + "totalUsers": totalUsers, + "maleUsers": maleUsers, + "femaleUsers": femaleUsers, + } + s.sendRespDefault(w, http.StatusOK, respBody) +} + func (s *server) ListFriendReqs(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { headers := map[string]string{ diff --git a/usrmgmt/pkg/social_graph/graph.go b/usrmgmt/pkg/social_graph/graph.go index 57f8083..5f37ddd 100644 --- a/usrmgmt/pkg/social_graph/graph.go +++ b/usrmgmt/pkg/social_graph/graph.go @@ -118,6 +118,10 @@ func NewGrphCtrler(frIntfce freq.Interface, frndIntfce frnd.Interface, usrIntfce interestsInCtrler: interestsInIntfce, } } +// added discription about the retun values +func (sgr *socialGraph) GetAllUsers(ctx context.Context, userKey string) (int, int, int, error) { //remove userKey + return sgr.usrCtrler.CountOfAllUsers(ctx) // change the function name +} func (sgr *socialGraph) ListFriendReqs(ctx context.Context, userKey string, offset, count int) ([]*map[string]string, error) { friendReqs, err := sgr.fReqCtrler.ListStringValueJson(ctx, listFriendReqs, map[string]interface{}{ diff --git a/usrmgmt/pkg/social_graph/interface.go b/usrmgmt/pkg/social_graph/interface.go index 0335472..60a6c08 100644 --- a/usrmgmt/pkg/social_graph/interface.go +++ b/usrmgmt/pkg/social_graph/interface.go @@ -29,4 +29,5 @@ type Interface interface { ExistUserForIndexEmail(ctx context.Context, indexNo, email string) (bool, error) ExistUserForEmail(ctx context.Context, email string) (bool, error) ForgotPasswordReset(ctx context.Context, email, newPasswd string) error + GetAllUsers(ctx context.Context, userKey string) (int,int,int, error) }