Skip to content

Commit

Permalink
Show upload count on community solutions (#6407)
Browse files Browse the repository at this point in the history
* Show upload count on community solutions

* Add `published_exercise_representation` to `solutions_with_includes`

* Try a different format

* Fix serialize_community_solution_test

* Fix n+1

---------

Co-authored-by: dem4ron <demaaron88@gmail.com>
  • Loading branch information
iHiD and dem4ron authored Oct 27, 2023
1 parent bad6e8e commit aa41fc8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tracks/community_solutions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def show
# We want to manually exclude (after SQL!) the solution id, and then just select 3
(os_ids - [@solution.id])[0, 3]

@other_solutions = Solution.where(id: os_ids).includes(*SerializeSolutions::NP1_INCLUDES)
@other_solutions = Solution.where(id: os_ids).includes(*SerializeSolutions::NP1_INCLUDES + [:published_exercise_representation])
@mentor_discussions = @solution.mentor_discussions.finished.not_negatively_rated.includes(:mentor)
@exercise_representation = @solution.published_exercise_representation

Expand Down
5 changes: 5 additions & 0 deletions app/images/icons/upload.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 31 additions & 11 deletions app/javascript/components/common/CommunitySolution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,43 @@ const PublishDetails = ({ solution }: { solution: CommunitySolutionProps }) => {
solution.publishedAt
)}`}</time>
<div className="--counts">
{solution.representationNumPublishedSolutions ? (
<div
className="--count"
title="Number of times someone has published a solution similar to this"
>
<GraphicalIcon icon="upload" />
<div className="--num">
{solution.representationNumPublishedSolutions}
</div>
</div>
) : null}
{solution.numLoc ? (
<div className="--count">
<Icon icon="loc" alt="Number of lines of code in the solution" />
<div
className="--count"
title="Number of lines of code in the solution"
>
<GraphicalIcon icon="loc" />
<div className="--num">{solution.numLoc}</div>
</div>
) : null}
<div className="--count">
<Icon icon="star" alt="Number of times solution has been starred" />
<div
className="--count"
title="Number of times solution has been starred"
>
<GraphicalIcon icon="star" />
<div className="--num">{solution.numStars}</div>
</div>
<div className="--count">
<Icon
icon="comment"
alt="Number of times solution has been commented on"
/>
<div className="--num">{solution.numComments}</div>
</div>
{solution.numComments &&
!solution.representationNumPublishedSolutions ? (
<div
className="--count"
title="Number of times solution has been commented on"
>
<GraphicalIcon icon="comment" />
<div className="--num">{solution.numComments}</div>
</div>
) : null}
</div>
</>
)
Expand Down
1 change: 1 addition & 0 deletions app/javascript/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export type CommunitySolution = {
numLoc?: string
numStars: string
numComments: string
representationNumPublishedSolutions: string
publishedAt: string
language: string
iterationStatus: IterationStatus
Expand Down
1 change: 0 additions & 1 deletion app/models/exercise/representation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def has_feedback?
end

def appears_frequently? = num_submissions >= APPEARS_FREQUENTLY_MIN_NUM_SUBMISSIONS

def first_submitted_at = oldest_solution.published_iterations.last.created_at
def max_reputation = prestigious_solution.user.reputation

Expand Down
1 change: 1 addition & 0 deletions app/serializers/serialize_community_solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def call
num_views: solution.num_views,
num_stars: solution.num_stars,
num_comments: solution.num_comments,
representation_num_published_solutions: solution.published_exercise_representation&.num_published_solutions,
num_iterations: solution.num_iterations,
num_loc: solution.num_loc.presence, # Currently this column is not-null in production
iteration_status: solution.iteration_status,
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/serialize_community_solutions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def call

def solutions_with_includes
solutions.to_active_relation.
includes(:exercise, :track, :user)
includes(:exercise, :track, :user, :published_exercise_representation)
end
end
1 change: 1 addition & 0 deletions test/serializers/serialize_community_solution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SerializeCommunitySolutionTest < ActiveSupport::TestCase
num_views: solution.num_views,
num_stars: solution.num_stars,
num_comments: solution.num_comments,
representation_num_published_solutions: solution.published_exercise_representation&.num_published_solutions,
num_iterations: solution.num_iterations,
num_loc: nil,
iteration_status: iteration.status.to_s.to_sym,
Expand Down

0 comments on commit aa41fc8

Please sign in to comment.