@@ -771,6 +771,8 @@ defmodule Algora.Bounties do
771771 end
772772
773773 def fetch_stats ( org_id \\ nil ) do
774+ zero_money = Money . zero ( :USD , no_fraction_if_integer: true )
775+
774776 open_bounties_query =
775777 from b in Bounty ,
776778 join: u in assoc ( b , :owner ) ,
@@ -783,27 +785,26 @@ defmodule Algora.Bounties do
783785 where: u . id == ^ org_id ,
784786 where: b . status == :paid
785787
786- rewarded_claims_query = Claim . filter_by_org_id ( Claim . rewarded ( ) , org_id )
787- members_query = Member . filter_by_org_id ( Member , org_id )
788+ rewarded_users_query =
789+ from t in Transaction ,
790+ where: t . type == :credit ,
791+ where: t . status == :succeeded ,
792+ join: lt in assoc ( t , :linked_transaction ) ,
793+ where: lt . type == :debit ,
794+ where: lt . status == :succeeded ,
795+ where: lt . user_id == ^ org_id ,
796+ distinct: :user_id
788797
789- open_bounties = Repo . aggregate ( open_bounties_query , :count , :id )
790- open_bounties_amount = Repo . aggregate ( open_bounties_query , :sum , :amount ) || Money . zero ( :USD )
798+ rewarded_users_last_month_query =
799+ where ( rewarded_users_query , [ t ] , t . succeeded_at >= fragment ( "NOW() - INTERVAL '1 month'" ) )
791800
792- total_awarded = Repo . aggregate ( rewarded_bounties_query , :sum , :amount ) || Money . zero ( :USD )
801+ members_query = Member . filter_by_org_id ( Member , org_id )
802+ open_bounties = Repo . aggregate ( open_bounties_query , :count , :id )
803+ open_bounties_amount = Repo . aggregate ( open_bounties_query , :sum , :amount ) || zero_money
804+ total_awarded = Repo . aggregate ( rewarded_bounties_query , :sum , :amount ) || zero_money
793805 completed_bounties = Repo . aggregate ( rewarded_bounties_query , :count , :id )
794-
795- solvers_count_last_month =
796- rewarded_claims_query
797- |> where ( [ c ] , c . inserted_at >= fragment ( "NOW() - INTERVAL '1 month'" ) )
798- |> Repo . aggregate (
799- :count ,
800- :user_id ,
801- distinct: true
802- )
803-
804- solvers_count = Repo . aggregate ( rewarded_claims_query , :count , :user_id , distinct: true )
805- solvers_diff = solvers_count - solvers_count_last_month
806-
806+ solvers_count_last_month = Repo . aggregate ( rewarded_users_last_month_query , :count , :user_id )
807+ solvers_count = Repo . aggregate ( rewarded_users_query , :count , :user_id )
807808 members_count = Repo . aggregate ( members_query , :count , :id )
808809
809810 % {
@@ -812,7 +813,7 @@ defmodule Algora.Bounties do
812813 total_awarded: total_awarded ,
813814 completed_bounties_count: completed_bounties ,
814815 solvers_count: solvers_count ,
815- solvers_diff: solvers_diff ,
816+ solvers_diff: solvers_count - solvers_count_last_month ,
816817 members_count: members_count ,
817818 # TODO
818819 reviews_count: 4
0 commit comments