From b8d2b1f6c242317abd42755de189d64cc743fce8 Mon Sep 17 00:00:00 2001 From: elraphty Date: Fri, 3 May 2024 11:28:04 +0100 Subject: [PATCH] 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)