From 377c27f6ca0de5699b12c212a78315927c02b18c Mon Sep 17 00:00:00 2001 From: elraphty Date: Wed, 1 May 2024 16:11:22 +0100 Subject: [PATCH 1/6] added workspace to admin --- db/metrics.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/db/metrics.go b/db/metrics.go index b2a91bc94..666bf8d57 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -162,9 +162,11 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) assingned := keys.Get("Assigned") paid := keys.Get("Paid") providers := keys.Get("provider") + workspace := keys.Get("workspace_uuid") orderQuery := "" limitQuery := "" + workspaceQuery := "" var statusConditions []string @@ -193,6 +195,9 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) if limit > 1 { limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset) } + if workspace != "" { + workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + } providerCondition := "" if len(providers) > 0 { @@ -201,7 +206,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) } query := `SELECT * FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'` + providerCondition - allQuery := query + " " + statusQuery + " " + orderQuery + " " + limitQuery + allQuery := query + " " + workspaceQuery + " " + statusQuery + " " + orderQuery + " " + limitQuery b := []NewBounty{} db.db.Raw(allQuery).Find(&b) @@ -214,6 +219,7 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ assingned := keys.Get("Assigned") paid := keys.Get("Paid") providers := keys.Get("provider") + workspace := keys.Get("workspace_uuid") var statusConditions []string @@ -239,11 +245,15 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ providerSlice := strings.Split(providers, ",") providerCondition = " AND owner_id IN ('" + strings.Join(providerSlice, "','") + "')" } + var workspaceQuery string + if workspace != "" { + workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + } var count int64 query := `SELECT COUNT(*) FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'` + providerCondition - allQuery := query + " " + statusQuery + allQuery := query + " " + workspaceQuery + " " + statusQuery db.db.Raw(allQuery).Scan(&count) return count } From 4dac8d016f6f93f30e2a5039f6a5c77ce674a9c9 Mon Sep 17 00:00:00 2001 From: elraphty Date: Wed, 1 May 2024 18:56:22 +0100 Subject: [PATCH 2/6] added workspace for payment and bounty metrics --- db/interface.go | 20 ++-- db/metrics.go | 144 +++++++++++++++++++-------- handlers/metrics.go | 26 +++-- handlers/metrics_test.go | 24 +++-- mocks/Database.go | 210 ++++++++++++++++++++------------------- 5 files changed, 250 insertions(+), 174 deletions(-) diff --git a/db/interface.go b/db/interface.go index 9a8b88112..c1a714a0e 100644 --- a/db/interface.go +++ b/db/interface.go @@ -122,16 +122,16 @@ type Database interface { DeleteAllUsersFromWorkspace(uuid string) error GetFilterStatusCount() FilterStattuCount UserHasManageBountyRoles(pubKeyFromAuth string, uuid string) bool - BountiesPaidPercentage(r PaymentDateRange) uint - TotalSatsPosted(r PaymentDateRange) uint - TotalSatsPaid(r PaymentDateRange) uint - SatsPaidPercentage(r PaymentDateRange) uint - AveragePaidTime(r PaymentDateRange) uint - AverageCompletedTime(r PaymentDateRange) uint - TotalBountiesPosted(r PaymentDateRange) int64 - TotalPaidBounties(r PaymentDateRange) int64 - NewHuntersPaid(r PaymentDateRange) int64 - TotalHuntersPaid(r PaymentDateRange) int64 + BountiesPaidPercentage(r PaymentDateRange, workspace string) uint + TotalSatsPosted(r PaymentDateRange, workspace string) uint + TotalSatsPaid(r PaymentDateRange, workspace string) uint + SatsPaidPercentage(r PaymentDateRange, workspace string) uint + AveragePaidTime(r PaymentDateRange, workspace string) uint + AverageCompletedTime(r PaymentDateRange, workspace string) uint + TotalBountiesPosted(r PaymentDateRange, workspace string) int64 + TotalPaidBounties(r PaymentDateRange, workspace string) int64 + NewHuntersPaid(r PaymentDateRange, workspace string) int64 + TotalHuntersPaid(r PaymentDateRange, workspace string) int64 GetPersonByPubkey(pubkey string) Person GetBountiesByDateRange(r PaymentDateRange, re *http.Request) []NewBounty GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Request) int64 diff --git a/db/metrics.go b/db/metrics.go index 666bf8d57..f0914fd91 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -23,27 +23,45 @@ func (db database) TotalWorkspacesByDateRange(r PaymentDateRange) int64 { return count } -func (db database) TotalPaymentsByDateRange(r PaymentDateRange) uint { +func (db database) TotalPaymentsByDateRange(r PaymentDateRange, workspace string) uint { var sum uint - db.db.Model(&PaymentHistory{}).Where("payment_type = ?", r.PaymentType).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate).Select("SUM(amount)").Row().Scan(&sum) + query := db.db.Model(&NewPaymentHistory{}).Where("payment_type = ?", r.PaymentType).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Select("SUM(amount)").Row().Scan(&sum) return sum } -func (db database) TotalSatsPosted(r PaymentDateRange) uint { +func (db database) TotalSatsPosted(r PaymentDateRange, workspace string) uint { var sum uint - db.db.Model(&Bounty{}).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate).Select("SUM(price)").Row().Scan(&sum) + query := db.db.Model(&NewBounty{}).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Select("SUM(price)").Row().Scan(&sum) return sum } -func (db database) TotalSatsPaid(r PaymentDateRange) uint { +func (db database) TotalSatsPaid(r PaymentDateRange, workspace string) uint { var sum uint - db.db.Model(&Bounty{}).Where("paid = ?", true).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate).Select("SUM(price)").Row().Scan(&sum) + query := db.db.Model(&NewBounty{}).Where("paid = ?", true).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Select("SUM(price)").Row().Scan(&sum) return sum } -func (db database) SatsPaidPercentage(r PaymentDateRange) uint { - satsPosted := DB.TotalSatsPosted(r) - satsPaid := DB.TotalSatsPaid(r) +func (db database) SatsPaidPercentage(r PaymentDateRange, workspace string) uint { + satsPosted := DB.TotalSatsPosted(r, workspace) + satsPaid := DB.TotalSatsPaid(r, workspace) if satsPaid != 0 && satsPosted != 0 { value := (satsPaid * 100) / satsPosted paidPercentage := math.Round(float64(value)) @@ -52,43 +70,69 @@ func (db database) SatsPaidPercentage(r PaymentDateRange) uint { return 0 } -func (db database) TotalPaidBounties(r PaymentDateRange) int64 { +func (db database) TotalPaidBounties(r PaymentDateRange, workspace string) int64 { var count int64 - db.db.Model(&Bounty{}).Where("paid = ?", true).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate).Count(&count) + query := db.db.Model(&NewBounty{}).Where("paid = ?", true).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Count(&count) return count } -func (db database) TotalHuntersPaid(r PaymentDateRange) int64 { +func (db database) TotalHuntersPaid(r PaymentDateRange, workspace string) int64 { var count int64 query := fmt.Sprintf(`SELECT COUNT(DISTINCT assignee) FROM bounty WHERE assignee !='' AND paid=true AND created >= %s AND created <= %s`, r.StartDate, r.EndDate) - db.db.Raw(query).Count(&count) + var workspaceQuery string + if workspace != "" { + workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + } + + allQuery := query + " " + workspaceQuery + db.db.Raw(allQuery).Count(&count) return count } -func (db database) NewHuntersPaid(r PaymentDateRange) int64 { +func (db database) NewHuntersPaid(r PaymentDateRange, workspace string) int64 { var count int64 - db.db.Model(&Bounty{}). + + query := db.db.Model(&NewBounty{}). Select("DISTINCT assignee"). Where("paid = true"). - Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate). - Not("assignee IN (?)", db.db.Model(&Bounty{}). - Select("assignee"). - Where("paid = true"). - Where("created < ?", r.StartDate), - ).Count(&count) + Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Not("assignee IN (?)", db.db.Model(&NewBounty{}). + Select("assignee"). + Where("paid = true"). + Where("created < ?", r.StartDate), + ) + + query.Count(&count) return count } -func (db database) TotalBountiesPosted(r PaymentDateRange) int64 { +func (db database) TotalBountiesPosted(r PaymentDateRange, workspace string) int64 { var count int64 - db.db.Model(&Bounty{}).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate).Count(&count) + query := db.db.Model(&Bounty{}).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Count(&count) return count } -func (db database) BountiesPaidPercentage(r PaymentDateRange) uint { - bountiesPosted := DB.TotalBountiesPosted(r) - bountiesPaid := DB.TotalPaidBounties(r) +func (db database) BountiesPaidPercentage(r PaymentDateRange, workspace string) uint { + bountiesPosted := DB.TotalBountiesPosted(r, workspace) + bountiesPaid := DB.TotalPaidBounties(r, workspace) if bountiesPaid != 0 && bountiesPosted != 0 { value := bountiesPaid * 100 / bountiesPosted paidPercentage := math.Round(float64(value)) @@ -97,23 +141,31 @@ func (db database) BountiesPaidPercentage(r PaymentDateRange) uint { return 0 } -func (db database) PaidDifference(r PaymentDateRange) []DateDifference { +func (db database) PaidDifference(r PaymentDateRange, workspace string) []DateDifference { ms := []DateDifference{} - db.db.Raw(`SELECT EXTRACT(EPOCH FROM (paid_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE paid_date IS NOT NULL AND created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `' `).Find(&ms) + query := fmt.Sprintf("SELECT EXTRACT(EPOCH FROM (paid_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE paid_date IS NOT NULL AND created >= %s AND created <= %s", "`"+r.StartDate+"`", "`"+r.EndDate+"`") + + var workspaceQuery string + if workspace != "" { + workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + } + + allQuery := query + " " + workspaceQuery + db.db.Raw(allQuery).Find(&ms) return ms } -func (db database) PaidDifferenceCount(r PaymentDateRange) int64 { +func (db database) PaidDifferenceCount(r PaymentDateRange, workspace string) int64 { var count int64 - list := db.PaidDifference(r) + list := db.PaidDifference(r, workspace) count = int64(len(list)) return count } -func (db database) AveragePaidTime(r PaymentDateRange) uint { - paidList := DB.PaidDifference(r) - paidCount := DB.PaidDifferenceCount(r) +func (db database) AveragePaidTime(r PaymentDateRange, workspace string) uint { + paidList := DB.PaidDifference(r, workspace) + paidCount := DB.PaidDifferenceCount(r, workspace) var paidSum uint for _, diff := range paidList { paidSum = uint(math.Round(diff.Diff)) @@ -121,23 +173,31 @@ func (db database) AveragePaidTime(r PaymentDateRange) uint { return CalculateAverageDays(paidCount, paidSum) } -func (db database) CompletedDifference(r PaymentDateRange) []DateDifference { +func (db database) CompletedDifference(r PaymentDateRange, workspace string) []DateDifference { ms := []DateDifference{} - db.db.Raw(`SELECT EXTRACT(EPOCH FROM (completion_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE completion_date IS NOT NULL AND created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `' `).Find(&ms) + query := fmt.Sprintf("SELECT EXTRACT(EPOCH FROM (completion_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE completion_date IS NOT NULL AND created >= %s AND created <= %s ", "`"+r.StartDate+"`", "`"+r.EndDate+"`") + + var workspaceQuery string + if workspace != "" { + workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + } + + allQuery := query + " " + workspaceQuery + db.db.Raw(allQuery).Find(&ms) return ms } -func (db database) CompletedDifferenceCount(r PaymentDateRange) int64 { +func (db database) CompletedDifferenceCount(r PaymentDateRange, workspace string) int64 { var count int64 - list := db.CompletedDifference(r) + list := db.CompletedDifference(r, workspace) count = int64(len(list)) return count } -func (db database) AverageCompletedTime(r PaymentDateRange) uint { - paidList := DB.CompletedDifference(r) - paidCount := DB.CompletedDifferenceCount(r) +func (db database) AverageCompletedTime(r PaymentDateRange, workspace string) uint { + paidList := DB.CompletedDifference(r, workspace) + paidCount := DB.CompletedDifferenceCount(r, workspace) var paidSum uint for _, diff := range paidList { paidSum = uint(math.Round(diff.Diff)) @@ -162,7 +222,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) assingned := keys.Get("Assigned") paid := keys.Get("Paid") providers := keys.Get("provider") - workspace := keys.Get("workspace_uuid") + workspace := keys.Get("workspace") orderQuery := "" limitQuery := "" @@ -219,7 +279,7 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ assingned := keys.Get("Assigned") paid := keys.Get("Paid") providers := keys.Get("provider") - workspace := keys.Get("workspace_uuid") + workspace := keys.Get("workspace") var statusConditions []string diff --git a/handlers/metrics.go b/handlers/metrics.go index 431ceda55..df7e99513 100644 --- a/handlers/metrics.go +++ b/handlers/metrics.go @@ -33,6 +33,8 @@ func NewMetricHandler(db db.Database) *metricHandler { func PaymentMetrics(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + keys := r.URL.Query() + workspace := keys.Get("workspace") if pubKeyFromAuth == "" { fmt.Println("no pubkey from auth") @@ -51,7 +53,7 @@ func PaymentMetrics(w http.ResponseWriter, r *http.Request) { return } - sumAmount := db.DB.TotalPaymentsByDateRange(request) + sumAmount := db.DB.TotalPaymentsByDateRange(request, workspace) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(sumAmount) @@ -114,6 +116,8 @@ func PeopleMetrics(w http.ResponseWriter, r *http.Request) { func (mh *metricHandler) BountyMetrics(w http.ResponseWriter, r *http.Request) { ctx := r.Context() pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + keys := r.URL.Query() + workspace := keys.Get("workspace") if pubKeyFromAuth == "" { fmt.Println("no pubkey from auth") @@ -146,16 +150,16 @@ func (mh *metricHandler) BountyMetrics(w http.ResponseWriter, r *http.Request) { } } - totalBountiesPosted := mh.db.TotalBountiesPosted(request) - totalBountiesPaid := mh.db.TotalPaidBounties(request) - bountiesPaidPercentage := mh.db.BountiesPaidPercentage(request) - totalSatsPosted := mh.db.TotalSatsPosted(request) - totalSatsPaid := mh.db.TotalSatsPaid(request) - satsPaidPercentage := mh.db.SatsPaidPercentage(request) - avgPaidDays := mh.db.AveragePaidTime(request) - avgCompletedDays := mh.db.AverageCompletedTime(request) - uniqueHuntersPaid := mh.db.TotalHuntersPaid(request) - newHuntersPaid := mh.db.NewHuntersPaid(request) + totalBountiesPosted := mh.db.TotalBountiesPosted(request, workspace) + totalBountiesPaid := mh.db.TotalPaidBounties(request, workspace) + bountiesPaidPercentage := mh.db.BountiesPaidPercentage(request, workspace) + totalSatsPosted := mh.db.TotalSatsPosted(request, workspace) + totalSatsPaid := mh.db.TotalSatsPaid(request, workspace) + satsPaidPercentage := mh.db.SatsPaidPercentage(request, workspace) + avgPaidDays := mh.db.AveragePaidTime(request, workspace) + avgCompletedDays := mh.db.AverageCompletedTime(request, workspace) + uniqueHuntersPaid := mh.db.TotalHuntersPaid(request, workspace) + newHuntersPaid := mh.db.NewHuntersPaid(request, workspace) bountyMetrics := db.BountyMetrics{ BountiesPosted: totalBountiesPosted, diff --git a/handlers/metrics_test.go b/handlers/metrics_test.go index 8b8a2d1d7..f9a2ee619 100644 --- a/handlers/metrics_test.go +++ b/handlers/metrics_test.go @@ -58,25 +58,27 @@ func TestBountyMetrics(t *testing.T) { db.RedisError = errors.New("redis not initialized") rr := httptest.NewRecorder() handler := http.HandlerFunc(mh.BountyMetrics) + workspace := "test-workspace" + dateRange := db.PaymentDateRange{ StartDate: "1111", EndDate: "2222", } body, _ := json.Marshal(dateRange) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/bounty_stats", bytes.NewReader(body)) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/bounty_stats?workspace="+workspace, bytes.NewReader(body)) if err != nil { t.Fatal(err) } - mockDb.On("TotalBountiesPosted", dateRange).Return(int64(1)).Once() - mockDb.On("TotalPaidBounties", dateRange).Return(int64(1)).Once() - mockDb.On("BountiesPaidPercentage", dateRange).Return(uint(1)).Once() - mockDb.On("TotalSatsPosted", dateRange).Return(uint(1)).Once() - mockDb.On("TotalSatsPaid", dateRange).Return(uint(1)).Once() - mockDb.On("SatsPaidPercentage", dateRange).Return(uint(1)).Once() - mockDb.On("AveragePaidTime", dateRange).Return(uint(1)).Once() - mockDb.On("AverageCompletedTime", dateRange).Return(uint(1)).Once() - mockDb.On("TotalHuntersPaid", dateRange).Return(int64(1)).Once() - mockDb.On("NewHuntersPaid", dateRange).Return(int64(1)).Once() + mockDb.On("TotalBountiesPosted", dateRange, workspace).Return(int64(1)).Once() + mockDb.On("TotalPaidBounties", dateRange, workspace).Return(int64(1)).Once() + mockDb.On("BountiesPaidPercentage", dateRange, workspace).Return(uint(1)).Once() + mockDb.On("TotalSatsPosted", dateRange, workspace).Return(uint(1)).Once() + mockDb.On("TotalSatsPaid", dateRange, workspace).Return(uint(1)).Once() + mockDb.On("SatsPaidPercentage", dateRange, workspace).Return(uint(1)).Once() + mockDb.On("AveragePaidTime", dateRange, workspace).Return(uint(1)).Once() + mockDb.On("AverageCompletedTime", dateRange, workspace).Return(uint(1)).Once() + mockDb.On("TotalHuntersPaid", dateRange, workspace).Return(int64(1)).Once() + mockDb.On("NewHuntersPaid", dateRange, workspace).Return(int64(1)).Once() handler.ServeHTTP(rr, req) expectedMetricRes := db.BountyMetrics{ diff --git a/mocks/Database.go b/mocks/Database.go index 7b3961287..152ef1159 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -311,17 +311,17 @@ func (_c *Database_AddUserInvoiceData_Call) RunAndReturn(run func(db.UserInvoice return _c } -// AverageCompletedTime provides a mock function with given fields: r -func (_m *Database) AverageCompletedTime(r db.PaymentDateRange) uint { - ret := _m.Called(r) +// AverageCompletedTime provides a mock function with given fields: r, workspace +func (_m *Database) AverageCompletedTime(r db.PaymentDateRange, workspace string) uint { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for AverageCompletedTime") } var r0 uint - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) uint); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) uint); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(uint) } @@ -336,13 +336,14 @@ type Database_AverageCompletedTime_Call struct { // AverageCompletedTime is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) AverageCompletedTime(r interface{}) *Database_AverageCompletedTime_Call { - return &Database_AverageCompletedTime_Call{Call: _e.mock.On("AverageCompletedTime", r)} +// - workspace string +func (_e *Database_Expecter) AverageCompletedTime(r interface{}, workspace interface{}) *Database_AverageCompletedTime_Call { + return &Database_AverageCompletedTime_Call{Call: _e.mock.On("AverageCompletedTime", r, workspace)} } -func (_c *Database_AverageCompletedTime_Call) Run(run func(r db.PaymentDateRange)) *Database_AverageCompletedTime_Call { +func (_c *Database_AverageCompletedTime_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_AverageCompletedTime_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -352,22 +353,22 @@ func (_c *Database_AverageCompletedTime_Call) Return(_a0 uint) *Database_Average return _c } -func (_c *Database_AverageCompletedTime_Call) RunAndReturn(run func(db.PaymentDateRange) uint) *Database_AverageCompletedTime_Call { +func (_c *Database_AverageCompletedTime_Call) RunAndReturn(run func(db.PaymentDateRange, string) uint) *Database_AverageCompletedTime_Call { _c.Call.Return(run) return _c } -// AveragePaidTime provides a mock function with given fields: r -func (_m *Database) AveragePaidTime(r db.PaymentDateRange) uint { - ret := _m.Called(r) +// AveragePaidTime provides a mock function with given fields: r, workspace +func (_m *Database) AveragePaidTime(r db.PaymentDateRange, workspace string) uint { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for AveragePaidTime") } var r0 uint - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) uint); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) uint); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(uint) } @@ -382,13 +383,14 @@ type Database_AveragePaidTime_Call struct { // AveragePaidTime is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) AveragePaidTime(r interface{}) *Database_AveragePaidTime_Call { - return &Database_AveragePaidTime_Call{Call: _e.mock.On("AveragePaidTime", r)} +// - workspace string +func (_e *Database_Expecter) AveragePaidTime(r interface{}, workspace interface{}) *Database_AveragePaidTime_Call { + return &Database_AveragePaidTime_Call{Call: _e.mock.On("AveragePaidTime", r, workspace)} } -func (_c *Database_AveragePaidTime_Call) Run(run func(r db.PaymentDateRange)) *Database_AveragePaidTime_Call { +func (_c *Database_AveragePaidTime_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_AveragePaidTime_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -398,22 +400,22 @@ func (_c *Database_AveragePaidTime_Call) Return(_a0 uint) *Database_AveragePaidT return _c } -func (_c *Database_AveragePaidTime_Call) RunAndReturn(run func(db.PaymentDateRange) uint) *Database_AveragePaidTime_Call { +func (_c *Database_AveragePaidTime_Call) RunAndReturn(run func(db.PaymentDateRange, string) uint) *Database_AveragePaidTime_Call { _c.Call.Return(run) return _c } -// BountiesPaidPercentage provides a mock function with given fields: r -func (_m *Database) BountiesPaidPercentage(r db.PaymentDateRange) uint { - ret := _m.Called(r) +// BountiesPaidPercentage provides a mock function with given fields: r, workspace +func (_m *Database) BountiesPaidPercentage(r db.PaymentDateRange, workspace string) uint { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for BountiesPaidPercentage") } var r0 uint - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) uint); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) uint); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(uint) } @@ -428,13 +430,14 @@ type Database_BountiesPaidPercentage_Call struct { // BountiesPaidPercentage is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) BountiesPaidPercentage(r interface{}) *Database_BountiesPaidPercentage_Call { - return &Database_BountiesPaidPercentage_Call{Call: _e.mock.On("BountiesPaidPercentage", r)} +// - workspace string +func (_e *Database_Expecter) BountiesPaidPercentage(r interface{}, workspace interface{}) *Database_BountiesPaidPercentage_Call { + return &Database_BountiesPaidPercentage_Call{Call: _e.mock.On("BountiesPaidPercentage", r, workspace)} } -func (_c *Database_BountiesPaidPercentage_Call) Run(run func(r db.PaymentDateRange)) *Database_BountiesPaidPercentage_Call { +func (_c *Database_BountiesPaidPercentage_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_BountiesPaidPercentage_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -444,7 +447,7 @@ func (_c *Database_BountiesPaidPercentage_Call) Return(_a0 uint) *Database_Bount return _c } -func (_c *Database_BountiesPaidPercentage_Call) RunAndReturn(run func(db.PaymentDateRange) uint) *Database_BountiesPaidPercentage_Call { +func (_c *Database_BountiesPaidPercentage_Call) RunAndReturn(run func(db.PaymentDateRange, string) uint) *Database_BountiesPaidPercentage_Call { _c.Call.Return(run) return _c } @@ -5035,17 +5038,17 @@ func (_c *Database_GetWorkspacesCount_Call) RunAndReturn(run func() int64) *Data return _c } -// NewHuntersPaid provides a mock function with given fields: r -func (_m *Database) NewHuntersPaid(r db.PaymentDateRange) int64 { - ret := _m.Called(r) +// NewHuntersPaid provides a mock function with given fields: r, workspace +func (_m *Database) NewHuntersPaid(r db.PaymentDateRange, workspace string) int64 { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for NewHuntersPaid") } var r0 int64 - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) int64); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) int64); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(int64) } @@ -5060,13 +5063,14 @@ type Database_NewHuntersPaid_Call struct { // NewHuntersPaid is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) NewHuntersPaid(r interface{}) *Database_NewHuntersPaid_Call { - return &Database_NewHuntersPaid_Call{Call: _e.mock.On("NewHuntersPaid", r)} +// - workspace string +func (_e *Database_Expecter) NewHuntersPaid(r interface{}, workspace interface{}) *Database_NewHuntersPaid_Call { + return &Database_NewHuntersPaid_Call{Call: _e.mock.On("NewHuntersPaid", r, workspace)} } -func (_c *Database_NewHuntersPaid_Call) Run(run func(r db.PaymentDateRange)) *Database_NewHuntersPaid_Call { +func (_c *Database_NewHuntersPaid_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_NewHuntersPaid_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5076,7 +5080,7 @@ func (_c *Database_NewHuntersPaid_Call) Return(_a0 int64) *Database_NewHuntersPa return _c } -func (_c *Database_NewHuntersPaid_Call) RunAndReturn(run func(db.PaymentDateRange) int64) *Database_NewHuntersPaid_Call { +func (_c *Database_NewHuntersPaid_Call) RunAndReturn(run func(db.PaymentDateRange, string) int64) *Database_NewHuntersPaid_Call { _c.Call.Return(run) return _c } @@ -5170,17 +5174,17 @@ func (_c *Database_ProcessAlerts_Call) RunAndReturn(run func(db.Person)) *Databa return _c } -// SatsPaidPercentage provides a mock function with given fields: r -func (_m *Database) SatsPaidPercentage(r db.PaymentDateRange) uint { - ret := _m.Called(r) +// SatsPaidPercentage provides a mock function with given fields: r, workspace +func (_m *Database) SatsPaidPercentage(r db.PaymentDateRange, workspace string) uint { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for SatsPaidPercentage") } var r0 uint - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) uint); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) uint); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(uint) } @@ -5195,13 +5199,14 @@ type Database_SatsPaidPercentage_Call struct { // SatsPaidPercentage is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) SatsPaidPercentage(r interface{}) *Database_SatsPaidPercentage_Call { - return &Database_SatsPaidPercentage_Call{Call: _e.mock.On("SatsPaidPercentage", r)} +// - workspace string +func (_e *Database_Expecter) SatsPaidPercentage(r interface{}, workspace interface{}) *Database_SatsPaidPercentage_Call { + return &Database_SatsPaidPercentage_Call{Call: _e.mock.On("SatsPaidPercentage", r, workspace)} } -func (_c *Database_SatsPaidPercentage_Call) Run(run func(r db.PaymentDateRange)) *Database_SatsPaidPercentage_Call { +func (_c *Database_SatsPaidPercentage_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_SatsPaidPercentage_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5211,7 +5216,7 @@ func (_c *Database_SatsPaidPercentage_Call) Return(_a0 uint) *Database_SatsPaidP return _c } -func (_c *Database_SatsPaidPercentage_Call) RunAndReturn(run func(db.PaymentDateRange) uint) *Database_SatsPaidPercentage_Call { +func (_c *Database_SatsPaidPercentage_Call) RunAndReturn(run func(db.PaymentDateRange, string) uint) *Database_SatsPaidPercentage_Call { _c.Call.Return(run) return _c } @@ -5364,17 +5369,17 @@ func (_c *Database_SearchTribes_Call) RunAndReturn(run func(string) []db.Tribe) return _c } -// TotalBountiesPosted provides a mock function with given fields: r -func (_m *Database) TotalBountiesPosted(r db.PaymentDateRange) int64 { - ret := _m.Called(r) +// TotalBountiesPosted provides a mock function with given fields: r, workspace +func (_m *Database) TotalBountiesPosted(r db.PaymentDateRange, workspace string) int64 { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for TotalBountiesPosted") } var r0 int64 - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) int64); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) int64); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(int64) } @@ -5389,13 +5394,14 @@ type Database_TotalBountiesPosted_Call struct { // TotalBountiesPosted is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) TotalBountiesPosted(r interface{}) *Database_TotalBountiesPosted_Call { - return &Database_TotalBountiesPosted_Call{Call: _e.mock.On("TotalBountiesPosted", r)} +// - workspace string +func (_e *Database_Expecter) TotalBountiesPosted(r interface{}, workspace interface{}) *Database_TotalBountiesPosted_Call { + return &Database_TotalBountiesPosted_Call{Call: _e.mock.On("TotalBountiesPosted", r, workspace)} } -func (_c *Database_TotalBountiesPosted_Call) Run(run func(r db.PaymentDateRange)) *Database_TotalBountiesPosted_Call { +func (_c *Database_TotalBountiesPosted_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_TotalBountiesPosted_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5405,22 +5411,22 @@ func (_c *Database_TotalBountiesPosted_Call) Return(_a0 int64) *Database_TotalBo return _c } -func (_c *Database_TotalBountiesPosted_Call) RunAndReturn(run func(db.PaymentDateRange) int64) *Database_TotalBountiesPosted_Call { +func (_c *Database_TotalBountiesPosted_Call) RunAndReturn(run func(db.PaymentDateRange, string) int64) *Database_TotalBountiesPosted_Call { _c.Call.Return(run) return _c } -// TotalHuntersPaid provides a mock function with given fields: r -func (_m *Database) TotalHuntersPaid(r db.PaymentDateRange) int64 { - ret := _m.Called(r) +// TotalHuntersPaid provides a mock function with given fields: r, workspace +func (_m *Database) TotalHuntersPaid(r db.PaymentDateRange, workspace string) int64 { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for TotalHuntersPaid") } var r0 int64 - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) int64); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) int64); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(int64) } @@ -5435,13 +5441,14 @@ type Database_TotalHuntersPaid_Call struct { // TotalHuntersPaid is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) TotalHuntersPaid(r interface{}) *Database_TotalHuntersPaid_Call { - return &Database_TotalHuntersPaid_Call{Call: _e.mock.On("TotalHuntersPaid", r)} +// - workspace string +func (_e *Database_Expecter) TotalHuntersPaid(r interface{}, workspace interface{}) *Database_TotalHuntersPaid_Call { + return &Database_TotalHuntersPaid_Call{Call: _e.mock.On("TotalHuntersPaid", r, workspace)} } -func (_c *Database_TotalHuntersPaid_Call) Run(run func(r db.PaymentDateRange)) *Database_TotalHuntersPaid_Call { +func (_c *Database_TotalHuntersPaid_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_TotalHuntersPaid_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5451,22 +5458,22 @@ func (_c *Database_TotalHuntersPaid_Call) Return(_a0 int64) *Database_TotalHunte return _c } -func (_c *Database_TotalHuntersPaid_Call) RunAndReturn(run func(db.PaymentDateRange) int64) *Database_TotalHuntersPaid_Call { +func (_c *Database_TotalHuntersPaid_Call) RunAndReturn(run func(db.PaymentDateRange, string) int64) *Database_TotalHuntersPaid_Call { _c.Call.Return(run) return _c } -// TotalPaidBounties provides a mock function with given fields: r -func (_m *Database) TotalPaidBounties(r db.PaymentDateRange) int64 { - ret := _m.Called(r) +// TotalPaidBounties provides a mock function with given fields: r, workspace +func (_m *Database) TotalPaidBounties(r db.PaymentDateRange, workspace string) int64 { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for TotalPaidBounties") } var r0 int64 - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) int64); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) int64); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(int64) } @@ -5481,13 +5488,14 @@ type Database_TotalPaidBounties_Call struct { // TotalPaidBounties is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) TotalPaidBounties(r interface{}) *Database_TotalPaidBounties_Call { - return &Database_TotalPaidBounties_Call{Call: _e.mock.On("TotalPaidBounties", r)} +// - workspace string +func (_e *Database_Expecter) TotalPaidBounties(r interface{}, workspace interface{}) *Database_TotalPaidBounties_Call { + return &Database_TotalPaidBounties_Call{Call: _e.mock.On("TotalPaidBounties", r, workspace)} } -func (_c *Database_TotalPaidBounties_Call) Run(run func(r db.PaymentDateRange)) *Database_TotalPaidBounties_Call { +func (_c *Database_TotalPaidBounties_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_TotalPaidBounties_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5497,22 +5505,22 @@ func (_c *Database_TotalPaidBounties_Call) Return(_a0 int64) *Database_TotalPaid return _c } -func (_c *Database_TotalPaidBounties_Call) RunAndReturn(run func(db.PaymentDateRange) int64) *Database_TotalPaidBounties_Call { +func (_c *Database_TotalPaidBounties_Call) RunAndReturn(run func(db.PaymentDateRange, string) int64) *Database_TotalPaidBounties_Call { _c.Call.Return(run) return _c } -// TotalSatsPaid provides a mock function with given fields: r -func (_m *Database) TotalSatsPaid(r db.PaymentDateRange) uint { - ret := _m.Called(r) +// TotalSatsPaid provides a mock function with given fields: r, workspace +func (_m *Database) TotalSatsPaid(r db.PaymentDateRange, workspace string) uint { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for TotalSatsPaid") } var r0 uint - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) uint); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) uint); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(uint) } @@ -5527,13 +5535,14 @@ type Database_TotalSatsPaid_Call struct { // TotalSatsPaid is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) TotalSatsPaid(r interface{}) *Database_TotalSatsPaid_Call { - return &Database_TotalSatsPaid_Call{Call: _e.mock.On("TotalSatsPaid", r)} +// - workspace string +func (_e *Database_Expecter) TotalSatsPaid(r interface{}, workspace interface{}) *Database_TotalSatsPaid_Call { + return &Database_TotalSatsPaid_Call{Call: _e.mock.On("TotalSatsPaid", r, workspace)} } -func (_c *Database_TotalSatsPaid_Call) Run(run func(r db.PaymentDateRange)) *Database_TotalSatsPaid_Call { +func (_c *Database_TotalSatsPaid_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_TotalSatsPaid_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5543,22 +5552,22 @@ func (_c *Database_TotalSatsPaid_Call) Return(_a0 uint) *Database_TotalSatsPaid_ return _c } -func (_c *Database_TotalSatsPaid_Call) RunAndReturn(run func(db.PaymentDateRange) uint) *Database_TotalSatsPaid_Call { +func (_c *Database_TotalSatsPaid_Call) RunAndReturn(run func(db.PaymentDateRange, string) uint) *Database_TotalSatsPaid_Call { _c.Call.Return(run) return _c } -// TotalSatsPosted provides a mock function with given fields: r -func (_m *Database) TotalSatsPosted(r db.PaymentDateRange) uint { - ret := _m.Called(r) +// TotalSatsPosted provides a mock function with given fields: r, workspace +func (_m *Database) TotalSatsPosted(r db.PaymentDateRange, workspace string) uint { + ret := _m.Called(r, workspace) if len(ret) == 0 { panic("no return value specified for TotalSatsPosted") } var r0 uint - if rf, ok := ret.Get(0).(func(db.PaymentDateRange) uint); ok { - r0 = rf(r) + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) uint); ok { + r0 = rf(r, workspace) } else { r0 = ret.Get(0).(uint) } @@ -5573,13 +5582,14 @@ type Database_TotalSatsPosted_Call struct { // TotalSatsPosted is a helper method to define mock.On call // - r db.PaymentDateRange -func (_e *Database_Expecter) TotalSatsPosted(r interface{}) *Database_TotalSatsPosted_Call { - return &Database_TotalSatsPosted_Call{Call: _e.mock.On("TotalSatsPosted", r)} +// - workspace string +func (_e *Database_Expecter) TotalSatsPosted(r interface{}, workspace interface{}) *Database_TotalSatsPosted_Call { + return &Database_TotalSatsPosted_Call{Call: _e.mock.On("TotalSatsPosted", r, workspace)} } -func (_c *Database_TotalSatsPosted_Call) Run(run func(r db.PaymentDateRange)) *Database_TotalSatsPosted_Call { +func (_c *Database_TotalSatsPosted_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_TotalSatsPosted_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(db.PaymentDateRange)) + run(args[0].(db.PaymentDateRange), args[1].(string)) }) return _c } @@ -5589,7 +5599,7 @@ func (_c *Database_TotalSatsPosted_Call) Return(_a0 uint) *Database_TotalSatsPos return _c } -func (_c *Database_TotalSatsPosted_Call) RunAndReturn(run func(db.PaymentDateRange) uint) *Database_TotalSatsPosted_Call { +func (_c *Database_TotalSatsPosted_Call) RunAndReturn(run func(db.PaymentDateRange, string) uint) *Database_TotalSatsPosted_Call { _c.Call.Return(run) return _c } From ff72560bfdd6def8113ef1470cfc2ba7bef928df Mon Sep 17 00:00:00 2001 From: elraphty Date: Thu, 2 May 2024 16:22:45 +0100 Subject: [PATCH 3/6] updated workspace for admin --- db/workspaces.go | 9 +++++++-- handlers/metrics.go | 15 +++++++++++++++ routes/metrics.go | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/db/workspaces.go b/db/workspaces.go index f737bdd31..be9bb403c 100644 --- a/db/workspaces.go +++ b/db/workspaces.go @@ -13,8 +13,13 @@ func (db database) GetWorkspaces(r *http.Request) []Workspace { ms := []Workspace{} offset, limit, sortBy, direction, search := utils.GetPaginationParams(r) - // return if like owner_alias, unique_name, or equals pubkey - db.db.Offset(offset).Limit(limit).Order(sortBy+" "+direction+" ").Where("LOWER(name) LIKE ?", "%"+search+"%").Where("deleted != ?", false).Find(&ms) + query := db.db.Model(&ms).Where("LOWER(name) LIKE ?", "%"+search+"%").Where("deleted != ?", true) + + if limit > 1 { + query.Offset(offset).Limit(limit).Order(sortBy + " " + direction + " ") + } + + query.Find(&ms) return ms } diff --git a/handlers/metrics.go b/handlers/metrics.go index df7e99513..46f2b3c44 100644 --- a/handlers/metrics.go +++ b/handlers/metrics.go @@ -304,6 +304,21 @@ func MetricsCsv(w http.ResponseWriter, r *http.Request) { } } +func GetAdminWorkspaces(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + + if pubKeyFromAuth == "" { + fmt.Println("no pubkey from auth") + w.WriteHeader(http.StatusUnauthorized) + return + } + + workspaces := db.DB.GetWorkspaces(r) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(workspaces) +} + func (mh *metricHandler) GetMetricsBountiesData(metricBounties []db.NewBounty) []db.BountyData { var metricBountiesData []db.BountyData for _, bounty := range metricBounties { diff --git a/routes/metrics.go b/routes/metrics.go index e24a1386d..19065e175 100644 --- a/routes/metrics.go +++ b/routes/metrics.go @@ -13,6 +13,8 @@ func MetricsRoutes() chi.Router { r.Group(func(r chi.Router) { r.Use(auth.PubKeyContextSuperAdmin) + r.Get("/workspaces", handlers.GetAdminWorkspaces) + r.Post("/payment", handlers.PaymentMetrics) r.Post("/people", handlers.PeopleMetrics) r.Post("/organization", handlers.WorkspacetMetrics) From 1861bf53a6c363d2f463a4b2effc20938eb29cc4 Mon Sep 17 00:00:00 2001 From: elraphty Date: Thu, 2 May 2024 19:10:40 +0100 Subject: [PATCH 4/6] changes --- db/metrics.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/db/metrics.go b/db/metrics.go index f0914fd91..1de6350ee 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -88,7 +88,7 @@ func (db database) TotalHuntersPaid(r PaymentDateRange, workspace string) int64 var workspaceQuery string if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = '%s'", workspace) } allQuery := query + " " + workspaceQuery @@ -148,7 +148,7 @@ func (db database) PaidDifference(r PaymentDateRange, workspace string) []DateDi var workspaceQuery string if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = `%s`", workspace) } allQuery := query + " " + workspaceQuery @@ -180,7 +180,7 @@ func (db database) CompletedDifference(r PaymentDateRange, workspace string) []D var workspaceQuery string if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = `%s`", workspace) } allQuery := query + " " + workspaceQuery @@ -224,6 +224,8 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) providers := keys.Get("provider") workspace := keys.Get("workspace") + fmt.Println("WORSKPACE === workspace", workspace) + orderQuery := "" limitQuery := "" workspaceQuery := "" @@ -256,7 +258,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset) } if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = '%s'", workspace) } providerCondition := "" @@ -270,6 +272,11 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) b := []NewBounty{} db.db.Raw(allQuery).Find(&b) + + if workspace != "" { + fmt.Println("Bounties1 ===", b) + fmt.Println("Query1 ===", allQuery) + } return b } @@ -307,7 +314,7 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ } var workspaceQuery string if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = %s", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = '%s'", workspace) } var count int64 From b8d2b1f6c242317abd42755de189d64cc743fce8 Mon Sep 17 00:00:00 2001 From: elraphty Date: Fri, 3 May 2024 11:28:04 +0100 Subject: [PATCH 5/6] added assignee stats --- db/interface.go | 1 + db/metrics.go | 26 +++++++++++++++---------- db/structs.go | 1 + handlers/metrics.go | 2 ++ mocks/Database.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 10 deletions(-) diff --git a/db/interface.go b/db/interface.go index c1a714a0e..05d328654 100644 --- a/db/interface.go +++ b/db/interface.go @@ -130,6 +130,7 @@ type Database interface { AverageCompletedTime(r PaymentDateRange, workspace string) uint TotalBountiesPosted(r PaymentDateRange, workspace string) int64 TotalPaidBounties(r PaymentDateRange, workspace string) int64 + TotalAssignedBounties(r PaymentDateRange, workspace string) int64 NewHuntersPaid(r PaymentDateRange, workspace string) int64 TotalHuntersPaid(r PaymentDateRange, workspace string) int64 GetPersonByPubkey(pubkey string) Person diff --git a/db/metrics.go b/db/metrics.go index 1de6350ee..0fd676c2b 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -82,6 +82,18 @@ func (db database) TotalPaidBounties(r PaymentDateRange, workspace string) int64 return count } +func (db database) TotalAssignedBounties(r PaymentDateRange, workspace string) int64 { + var count int64 + query := db.db.Model(&NewBounty{}).Where("assignee != ''").Where("paid = ?", false).Where("created >= ?", r.StartDate).Where("created <= ?", r.EndDate) + + if workspace != "" { + query.Where("workspace_uuid", workspace) + } + + query.Count(&count) + return count +} + func (db database) TotalHuntersPaid(r PaymentDateRange, workspace string) int64 { var count int64 query := fmt.Sprintf(`SELECT COUNT(DISTINCT assignee) FROM bounty WHERE assignee !='' AND paid=true AND created >= %s AND created <= %s`, r.StartDate, r.EndDate) @@ -144,11 +156,11 @@ func (db database) BountiesPaidPercentage(r PaymentDateRange, workspace string) func (db database) PaidDifference(r PaymentDateRange, workspace string) []DateDifference { ms := []DateDifference{} - query := fmt.Sprintf("SELECT EXTRACT(EPOCH FROM (paid_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE paid_date IS NOT NULL AND created >= %s AND created <= %s", "`"+r.StartDate+"`", "`"+r.EndDate+"`") + query := fmt.Sprintf("SELECT EXTRACT(EPOCH FROM (paid_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE paid_date IS NOT NULL AND created >= %s AND created <= %s", r.StartDate, r.EndDate) var workspaceQuery string if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = `%s`", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = '%s'", workspace) } allQuery := query + " " + workspaceQuery @@ -176,11 +188,11 @@ func (db database) AveragePaidTime(r PaymentDateRange, workspace string) uint { func (db database) CompletedDifference(r PaymentDateRange, workspace string) []DateDifference { ms := []DateDifference{} - query := fmt.Sprintf("SELECT EXTRACT(EPOCH FROM (completion_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE completion_date IS NOT NULL AND created >= %s AND created <= %s ", "`"+r.StartDate+"`", "`"+r.EndDate+"`") + query := fmt.Sprintf("SELECT EXTRACT(EPOCH FROM (completion_date - TO_TIMESTAMP(created))) as diff FROM public.bounty WHERE completion_date IS NOT NULL AND created >= %s AND created <= %s", r.StartDate, r.EndDate) var workspaceQuery string if workspace != "" { - workspaceQuery = fmt.Sprintf("AND workspace_uuid = `%s`", workspace) + workspaceQuery = fmt.Sprintf("AND workspace_uuid = '%s'", workspace) } allQuery := query + " " + workspaceQuery @@ -224,8 +236,6 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) providers := keys.Get("provider") workspace := keys.Get("workspace") - fmt.Println("WORSKPACE === workspace", workspace) - orderQuery := "" limitQuery := "" workspaceQuery := "" @@ -273,10 +283,6 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request) b := []NewBounty{} db.db.Raw(allQuery).Find(&b) - if workspace != "" { - fmt.Println("Bounties1 ===", b) - fmt.Println("Query1 ===", allQuery) - } return b } diff --git a/db/structs.go b/db/structs.go index 637e9c295..ebcfe0e2e 100644 --- a/db/structs.go +++ b/db/structs.go @@ -800,6 +800,7 @@ type DateDifference struct { type BountyMetrics struct { BountiesPosted int64 `json:"bounties_posted"` BountiesPaid int64 `json:"bounties_paid"` + BountiesAssigned int64 `json:"bounties_assigned"` BountiesPaidPercentage uint `json:"bounties_paid_average"` SatsPosted uint `json:"sats_posted"` SatsPaid uint `json:"sats_paid"` diff --git a/handlers/metrics.go b/handlers/metrics.go index 46f2b3c44..27f1378b7 100644 --- a/handlers/metrics.go +++ b/handlers/metrics.go @@ -152,6 +152,7 @@ func (mh *metricHandler) BountyMetrics(w http.ResponseWriter, r *http.Request) { totalBountiesPosted := mh.db.TotalBountiesPosted(request, workspace) totalBountiesPaid := mh.db.TotalPaidBounties(request, workspace) + totalBountiesAssigned := mh.db.TotalAssignedBounties(request, workspace) bountiesPaidPercentage := mh.db.BountiesPaidPercentage(request, workspace) totalSatsPosted := mh.db.TotalSatsPosted(request, workspace) totalSatsPaid := mh.db.TotalSatsPaid(request, workspace) @@ -164,6 +165,7 @@ func (mh *metricHandler) BountyMetrics(w http.ResponseWriter, r *http.Request) { bountyMetrics := db.BountyMetrics{ BountiesPosted: totalBountiesPosted, BountiesPaid: totalBountiesPaid, + BountiesAssigned: totalBountiesAssigned, BountiesPaidPercentage: bountiesPaidPercentage, SatsPosted: totalSatsPosted, SatsPaid: totalSatsPaid, diff --git a/mocks/Database.go b/mocks/Database.go index 152ef1159..8c727b024 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -5369,6 +5369,53 @@ func (_c *Database_SearchTribes_Call) RunAndReturn(run func(string) []db.Tribe) return _c } +// TotalAssignedBounties provides a mock function with given fields: r, workspace +func (_m *Database) TotalAssignedBounties(r db.PaymentDateRange, workspace string) int64 { + ret := _m.Called(r, workspace) + + if len(ret) == 0 { + panic("no return value specified for TotalAssignedBounties") + } + + var r0 int64 + if rf, ok := ret.Get(0).(func(db.PaymentDateRange, string) int64); ok { + r0 = rf(r, workspace) + } else { + r0 = ret.Get(0).(int64) + } + + return r0 +} + +// Database_TotalAssignedBounties_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'TotalAssignedBounties' +type Database_TotalAssignedBounties_Call struct { + *mock.Call +} + +// TotalAssignedBounties is a helper method to define mock.On call +// - r db.PaymentDateRange +// - workspace string +func (_e *Database_Expecter) TotalAssignedBounties(r interface{}, workspace interface{}) *Database_TotalAssignedBounties_Call { + return &Database_TotalAssignedBounties_Call{Call: _e.mock.On("TotalAssignedBounties", r, workspace)} +} + +func (_c *Database_TotalAssignedBounties_Call) Run(run func(r db.PaymentDateRange, workspace string)) *Database_TotalAssignedBounties_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(db.PaymentDateRange), args[1].(string)) + }) + return _c +} + +func (_c *Database_TotalAssignedBounties_Call) Return(_a0 int64) *Database_TotalAssignedBounties_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Database_TotalAssignedBounties_Call) RunAndReturn(run func(db.PaymentDateRange, string) int64) *Database_TotalAssignedBounties_Call { + _c.Call.Return(run) + return _c +} + // TotalBountiesPosted provides a mock function with given fields: r, workspace func (_m *Database) TotalBountiesPosted(r db.PaymentDateRange, workspace string) int64 { ret := _m.Called(r, workspace) From 1438e4475cbc3597c763e02d83bbd10e42c9cead Mon Sep 17 00:00:00 2001 From: elraphty Date: Fri, 3 May 2024 13:26:01 +0100 Subject: [PATCH 6/6] fixed tests --- handlers/metrics_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/handlers/metrics_test.go b/handlers/metrics_test.go index f9a2ee619..d278831a4 100644 --- a/handlers/metrics_test.go +++ b/handlers/metrics_test.go @@ -71,6 +71,7 @@ func TestBountyMetrics(t *testing.T) { } mockDb.On("TotalBountiesPosted", dateRange, workspace).Return(int64(1)).Once() mockDb.On("TotalPaidBounties", dateRange, workspace).Return(int64(1)).Once() + mockDb.On("TotalAssignedBounties", dateRange, workspace).Return(int64(2)).Once() mockDb.On("BountiesPaidPercentage", dateRange, workspace).Return(uint(1)).Once() mockDb.On("TotalSatsPosted", dateRange, workspace).Return(uint(1)).Once() mockDb.On("TotalSatsPaid", dateRange, workspace).Return(uint(1)).Once() @@ -84,6 +85,7 @@ func TestBountyMetrics(t *testing.T) { expectedMetricRes := db.BountyMetrics{ BountiesPosted: 1, BountiesPaid: 1, + BountiesAssigned: 2, BountiesPaidPercentage: 1, SatsPosted: 1, SatsPaid: 1,