Skip to content

Commit

Permalink
Merge pull request stakwork#1627 from stakwork/feat/admin_workspace
Browse files Browse the repository at this point in the history
Added workspace to admin
  • Loading branch information
elraphty authored May 3, 2024
2 parents fe70a56 + 1438e44 commit e6c08b3
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 176 deletions.
21 changes: 11 additions & 10 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ 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
TotalAssignedBounties(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
Expand Down
167 changes: 125 additions & 42 deletions db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -52,43 +70,81 @@ 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
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) TotalAssignedBounties(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("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) 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))
Expand All @@ -97,47 +153,63 @@ 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))
}
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))
Expand All @@ -162,9 +234,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")

orderQuery := ""
limitQuery := ""
workspaceQuery := ""

var statusConditions []string

Expand Down Expand Up @@ -193,6 +267,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 {
Expand All @@ -201,10 +278,11 @@ 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)

return b
}

Expand All @@ -214,6 +292,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")

var statusConditions []string

Expand All @@ -239,11 +318,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
}
Expand Down
1 change: 1 addition & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,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"`
Expand Down
9 changes: 7 additions & 2 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit e6c08b3

Please sign in to comment.