@@ -698,72 +698,97 @@ defmodule Algora.Workspace do
698
698
end
699
699
end
700
700
701
- def fetch_top_contributions ( token , provider_login ) do
702
- with { :ok , contributions } <- Algora.Cloud . top_contributions ( provider_login ) ,
703
- { :ok , user } <- ensure_user ( token , provider_login ) ,
704
- :ok <- add_contributions ( token , user . id , contributions ) do
701
+ def fetch_top_contributions ( token , provider_logins ) when is_list ( provider_logins ) do
702
+ with { :ok , contributions } <- Algora.Cloud . top_contributions ( provider_logins ) ,
703
+ { :ok , users } <- ensure_users ( token , provider_logins ) ,
704
+ :ok <- add_contributions ( token , users , contributions ) do
705
705
{ :ok , contributions }
706
706
else
707
707
{ :error , reason } ->
708
- Logger . error ( "Failed to fetch contributions for #{ provider_login } : #{ inspect ( reason ) } " )
708
+ Logger . error ( "Failed to fetch contributions for #{ inspect ( provider_logins ) } : #{ inspect ( reason ) } " )
709
709
{ :error , reason }
710
710
end
711
711
end
712
712
713
- def fetch_top_contributions_async ( token , provider_login ) do
714
- with { :ok , contributions } <- Algora.Cloud . top_contributions ( provider_login ) ,
715
- { :ok , user } <- ensure_user ( token , provider_login ) ,
716
- { :ok , _ } <- add_contributions_async ( token , user . id , contributions ) do
717
- { :ok , user }
713
+ def fetch_top_contributions_async ( token , provider_logins ) when is_list ( provider_logins ) do
714
+ with { :ok , contributions } <- Algora.Cloud . top_contributions ( provider_logins ) ,
715
+ { :ok , users } <- ensure_users ( token , provider_logins ) ,
716
+ :ok <- add_contributions_async ( token , users , contributions ) do
717
+ { :ok , users }
718
718
else
719
719
{ :error , reason } ->
720
- Logger . error ( "Failed to fetch contributions for #{ provider_login } : #{ inspect ( reason ) } " )
720
+ Logger . error ( "Failed to fetch contributions for #{ inspect ( provider_logins ) } : #{ inspect ( reason ) } " )
721
721
{ :error , reason }
722
722
end
723
723
end
724
724
725
- def add_contributions_async ( _token , user_id , opts ) do
726
- Repo . transact ( fn ->
727
- Enum . reduce_while ( opts , :ok , fn contribution , _ ->
728
- case % {
729
- "user_id" => user_id ,
730
- "repo_full_name" => contribution . repo_name ,
731
- "contribution_count" => contribution . contribution_count
732
- }
733
- |> Jobs.SyncContribution . new ( )
734
- |> Oban . insert ( ) do
735
- { :ok , _job } -> { :cont , :ok }
736
- error -> { :halt , error }
725
+ defp add_contributions_async ( _token , users , contributions ) do
726
+ users_map = Enum . group_by ( users , & & 1 . provider_login )
727
+
728
+ results =
729
+ Enum . map ( contributions , fn contribution ->
730
+ case users_map [ contribution . provider_login ] do
731
+ [ user ] ->
732
+ % {
733
+ "user_id" => user . id ,
734
+ "repo_full_name" => contribution . repo_name ,
735
+ "contribution_count" => contribution . contribution_count
736
+ }
737
+ |> Jobs.SyncContribution . new ( )
738
+ |> Oban . insert ( )
739
+
740
+ _ ->
741
+ { :error , :user_not_found }
737
742
end
738
743
end )
739
- end )
744
+
745
+ if Enum . any? ( results , & match? ( { :ok , _ } , & 1 ) ) , do: :ok , else: { :error , :failed }
740
746
end
741
747
742
- def add_contributions ( token , user_id , opts ) do
748
+ defp add_contributions ( token , users , contributions ) do
749
+ users_map = Enum . group_by ( users , & & 1 . provider_login )
750
+
743
751
results =
744
- Enum . map ( opts , fn % { repo_name: repo_name , contribution_count: contribution_count } ->
745
- add_contribution ( % {
746
- token: token ,
747
- user_id: user_id ,
748
- repo_full_name: repo_name ,
749
- contribution_count: contribution_count
750
- } )
752
+ Enum . map ( contributions , fn contribution ->
753
+ case users_map [ contribution . provider_login ] do
754
+ [ user ] ->
755
+ add_contribution ( % {
756
+ token: token ,
757
+ user_id: user . id ,
758
+ repo_full_name: contribution . repo_name ,
759
+ contribution_count: contribution . contribution_count
760
+ } )
761
+
762
+ _ ->
763
+ { :error , :user_not_found }
764
+ end
751
765
end )
752
766
753
767
if Enum . any? ( results , fn result -> result == :ok end ) , do: :ok , else: { :error , :failed }
754
768
end
755
769
756
- def add_contribution ( % {
757
- token: token ,
758
- user_id: user_id ,
759
- repo_full_name: repo_full_name ,
760
- contribution_count: contribution_count
761
- } ) do
770
+ defp add_contribution ( % {
771
+ token: token ,
772
+ user_id: user_id ,
773
+ repo_full_name: repo_full_name ,
774
+ contribution_count: contribution_count
775
+ } ) do
762
776
with [ repo_owner , repo_name ] <- String . split ( repo_full_name , "/" ) ,
763
777
{ :ok , repo } <- ensure_repository ( token , repo_owner , repo_name ) ,
764
778
{ :ok , _tech_stack } <- ensure_repo_tech_stack ( token , repo ) ,
765
779
{ :ok , _contribution } <- upsert_user_contribution ( user_id , repo . id , contribution_count ) do
766
780
:ok
767
781
end
768
782
end
783
+
784
+ def ensure_users ( token , provider_logins ) do
785
+ Repo . transact ( fn ->
786
+ provider_logins
787
+ |> Enum . map ( & ensure_user ( token , & 1 ) )
788
+ |> Enum . reduce_while ( { :ok , [ ] } , fn
789
+ { :ok , user } , { :ok , users } -> { :cont , { :ok , [ user | users ] } }
790
+ { :error , reason } , _ -> { :halt , { :error , reason } }
791
+ end )
792
+ end )
793
+ end
769
794
end
0 commit comments