@@ -771,6 +771,8 @@ defmodule Algora.Bounties do
771
771
end
772
772
773
773
def fetch_stats ( org_id \\ nil ) do
774
+ zero_money = Money . zero ( :USD , no_fraction_if_integer: true )
775
+
774
776
open_bounties_query =
775
777
from b in Bounty ,
776
778
join: u in assoc ( b , :owner ) ,
@@ -783,27 +785,26 @@ defmodule Algora.Bounties do
783
785
where: u . id == ^ org_id ,
784
786
where: b . status == :paid
785
787
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
788
797
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'" ) )
791
800
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
793
805
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 )
807
808
members_count = Repo . aggregate ( members_query , :count , :id )
808
809
809
810
% {
@@ -812,7 +813,7 @@ defmodule Algora.Bounties do
812
813
total_awarded: total_awarded ,
813
814
completed_bounties_count: completed_bounties ,
814
815
solvers_count: solvers_count ,
815
- solvers_diff: solvers_diff ,
816
+ solvers_diff: solvers_count - solvers_count_last_month ,
816
817
members_count: members_count ,
817
818
# TODO
818
819
reviews_count: 4
0 commit comments