diff --git a/db/metrics.go b/db/metrics.go index 777412de6..997de887a 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -5,6 +5,7 @@ import ( "math" "net/http" "strings" + "time" "github.com/stakwork/sphinx-tribes/utils" ) @@ -19,18 +20,33 @@ func (db database) TotalPeopleByDateRange(r PaymentDateRange) int64 { func (db database) TotalPeopleByPeriod(r PaymentDateRange) int64 { var count int64 - db.db.Model(&Person{}).Where("created < ?", r.StartDate).Count(&count) + + // convert timestamp string to int64 + timestamp, err := utils.ConvertStringToInt(r.StartDate) + if err != nil { + fmt.Println("Error parsing date:", err) + } + + // Convert the timestamp to a time.Time object + t := time.Unix(int64(timestamp), 0) + + // Format the time as a human-readable string + formattedTime := t.Format("2006-01-02 15:04:05") + + db.db.Model(&Person{}).Where("created < ?", formattedTime).Count(&count) return count } func (db database) GetNewHunters(r PaymentDateRange) int64 { var totalCount int64 - var newHunters int64 + var newHunters int64 = 0 var huntersByPeriod int64 = db.TotalPeopleByPeriod(r) db.db.Model(&Person{}).Count(&totalCount) - newHunters = totalCount - huntersByPeriod + if huntersByPeriod > 0 && totalCount > huntersByPeriod { + newHunters = totalCount - huntersByPeriod + } return newHunters } diff --git a/routes/index.go b/routes/index.go index 27d4931c8..4b727df43 100644 --- a/routes/index.go +++ b/routes/index.go @@ -142,35 +142,35 @@ func getFromAuth(path string) (*extractResponse, error) { } // Middleware to handle InternalServerError -func internalServerErrorHandler(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - rr := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK} - next.ServeHTTP(rr, r) - - if rr.statusCode == http.StatusInternalServerError { - fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - } - }) -} +// func internalServerErrorHandler(next http.Handler) http.Handler { +// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { +// rr := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK} +// next.ServeHTTP(rr, r) + +// if rr.statusCode == http.StatusInternalServerError { +// fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) +// http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) +// } +// }) +// } // Custom ResponseWriter to capture status codes -type responseRecorder struct { - http.ResponseWriter - statusCode int -} +// type responseRecorder struct { +// http.ResponseWriter +// statusCode int +// } -func (rr *responseRecorder) WriteHeader(code int) { - rr.statusCode = code - rr.ResponseWriter.WriteHeader(code) -} +// func (rr *responseRecorder) WriteHeader(code int) { +// rr.statusCode = code +// rr.ResponseWriter.WriteHeader(code) +// } func initChi() *chi.Mux { r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.Logger) r.Use(middleware.Recoverer) - r.Use(internalServerErrorHandler) + // r.Use(internalServerErrorHandler) cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},