• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

benwbrum / fromthepage / 18198125262

02 Oct 2025 03:43PM UTC coverage: 65.028%. Remained the same
18198125262

push

github

web-flow
Rubocop lint remove array whitespaces (#4950)

1848 of 3359 branches covered (55.02%)

Branch coverage included in aggregate %.

154 of 201 new or added lines in 62 files covered. (76.62%)

8087 of 11919 relevant lines covered (67.85%)

109.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.83
/app/models/quality_sampling.rb
1
# == Schema Information
2
#
3
# Table name: quality_samplings
4
#
5
#  id            :integer          not null, primary key
6
#  sample_set    :text(16777215)
7
#  created_at    :datetime         not null
8
#  updated_at    :datetime         not null
9
#  collection_id :integer          not null
10
#  user_id       :integer          not null
11
#
12
# Indexes
13
#
14
#  index_quality_samplings_on_collection_id  (collection_id)
15
#  index_quality_samplings_on_user_id        (user_id)
16
#
17
# Foreign Keys
18
#
19
#  fk_rails_...  (collection_id => collections.id)
20
#  fk_rails_...  (user_id => users.id)
21
#
22
class QualitySampling < ApplicationRecord
1✔
23
  belongs_to :user
1✔
24
  belongs_to :collection
1✔
25
  before_create :calculate_set
1✔
26

27
  MINIMUM_SAMPLE_SIZE = 5
1✔
28

29
  # attr_writer :additional_pages
30

31
  # module SamplingType
32
  #   EACH_USER='each_user'
33
  #   EACH_WORK='each_work'
34
  #   TOGETHER='together'
35
  #   PERMUTATIONS='permutations'
36

37
  #   ALL=[EACH_WORK,EACH_USER,TOGETHER,PERMUTATIONS]
38
  # end
39

40
  def current_field
1✔
41
    self.collection.pages.where(status: :needs_review)
×
42
  end
43

44
  def calculate_set
1✔
45
    working_set = self.sample_set || []
×
46

47
    all_triples = self.collection.pages.pluck(:work_id, :last_editor_user_id, 'pages.id')
×
48
    all_triples_by_work = all_triples.group_by { |triple| triple[0] } # work_id
×
49
    all_triples_by_user = all_triples.group_by { |triple| triple[1] } # user_id
×
50

51
    # look for unique works/users in the current field (pages needing review)
52
    review_triples = current_field.pluck(:work_id, :last_editor_user_id, 'pages.id')
×
53
    review_triples_by_work = review_triples.group_by { |triple| triple[0] } # work_id
×
54
    review_triples_by_user = review_triples.select { |triple| !triple[1].nil? }.group_by { |triple| triple[1] }# user_id
×
55

56
    # for each user, add the relevant pages to the sample
57
    review_triples_by_user.sort.each do |user_id, review_triples_for_user|
×
58
      # how many of this user's pages are in the set?
59
      user_page_ids = all_triples_by_user[user_id].map { |user_triple| user_triple[2] }
×
60
      user_pages_in_set = working_set & user_page_ids
×
61
      if user_pages_in_set.size < MINIMUM_SAMPLE_SIZE
×
62
        # append target pages
×
63
        user_review_page_ids = review_triples_for_user.map { |review_triple| review_triple[2] }
×
64
        user_review_page_ids_not_in_set = user_review_page_ids - working_set
×
65
        working_set += user_review_page_ids_not_in_set.sample(MINIMUM_SAMPLE_SIZE - user_pages_in_set.size)
×
66
      end
67
    end
68

69
    # do the same for works
70
    review_triples_by_work.sort.each do |work_id, review_triples_for_work|
×
71
      # how many of this work's pages are in the set?
72
      work_page_ids = all_triples_by_work[work_id].map { |work_triple| work_triple[2] }
×
73
      work_pages_in_set = working_set & work_page_ids
×
74
      if work_pages_in_set.size < MINIMUM_SAMPLE_SIZE
×
75
        # append target pages
×
76
        work_review_page_ids = review_triples_for_work.map { |review_triple| review_triple[2] }
×
77
        work_review_page_ids_not_in_set = work_review_page_ids - working_set
×
78
        working_set += work_review_page_ids_not_in_set.sample(MINIMUM_SAMPLE_SIZE - work_pages_in_set.size)
×
79
      end
80
    end
81

82
    self.sample_set = working_set
×
83
  end
84

85

86

87

88
  def total_field_size
1✔
89
    current_field.size
×
90
  end
91

92
  def needs_review_pages
1✔
93
    Page.where(status: :needs_review).where(id: sample_set)
×
94
  end
95

96
  def next_unsampled_page
1✔
97
    candidate_ids = needs_review_pages.pluck(:id)
×
98
    next_unsampled_page_id = sample_set.detect do |id|
×
99
      candidate_ids.include? id # TODO add check for current editing
×
100
    end
101
    Page.where(id: next_unsampled_page_id).first
×
102
  end
103

104
  def index_within_sample(page)
1✔
105
    self.sample_set.index(page.id)
×
106
  end
107

108
  def sample_page_count
1✔
109
    self.sample_set.size
×
110
  end
111

112
  def sampled?
1✔
113
    !needs_review_pages.present?
×
114
  end
115

116
  def sample_set
1✔
117
    if self[:sample_set].blank?
×
118
      []
×
119
    else
×
120
      JSON.parse(self[:sample_set])
×
121
    end
122
  end
123

124
  def sample_set=(array)
1✔
125
    self[:sample_set]=array.to_json
×
126
  end
127

128
  def max_approval_delta
1✔
129
    Page.where(id: sample_set).where.not(approval_delta: nil).maximum(:approval_delta)
×
130
  end
131

132
  def sampling_objects
1✔
133
    work_hash = {}
×
134
    user_hash = {}
×
135

136
    ## this is wrong!  we should display the stats for the whole collection, not just the set
137
    # for users:
138
    ## total_page_count should be all pages the user has worked on (pr just been last editor on)
139
    ## approval_delta should be the total for the pages the user has been last_editor on.
140
    ##    (note that we may need to calcuate the average completely differently than we do now)
141
    ## corrected_page_count should be the pages that had an approval count > 0
142
    # replace reviewed page count with pages needing review
143

144

145
    # for works:
146
    ## total page count should be the total pages in the work
147
    ## approval delta should be the total/average for the pages in the work that have one and
148
    ##    are in a completed state (since reviewed pages can be opened again)
149
    ## corrected_page_count should be pages in completed state with approval delta > 0
150
    #  replace reviewed_page_count with pages needing review.
151
    ##
152
    Page.where(id: sample_set).each do |page|
×
153
      work_sampling = work_hash[page.work_id] ||= PageSampling.new
×
154
      user_sampling = user_hash[page.last_editor_user_id] ||= PageSampling.new
×
155

156
      work_sampling.total_page_count += 1
×
157
      user_sampling.total_page_count += 1
×
158

159
      if page.approval_delta # unreviewed pages will have no delta
×
160
        work_sampling.approval_delta_sum += page.approval_delta
×
161
        user_sampling.approval_delta_sum += page.approval_delta
×
162

163
        if page.approval_delta > 0
×
164
          work_sampling.corrected_page_count += 1
×
165
          user_sampling.corrected_page_count += 1
×
166
        end
167
      end
168

169
      if Page::COMPLETED_STATUSES.include? page.status
×
170
        work_sampling.reviewed_page_count += 1
×
171
        user_sampling.reviewed_page_count += 1
×
172
      end
173
    end
174

NEW
175
    [work_hash, user_hash]
×
176
  end
177

178
  class PageSampling
1✔
179
    attr_accessor :reviewed_page_count, :total_page_count, :approval_delta_sum, :corrected_page_count
1✔
180
    def mean_approval_delta
1✔
181
      approval_delta_sum.to_f / reviewed_page_count.to_f
×
182
    end
183

184
    def initialize
1✔
185
      @reviewed_page_count = 0
×
186
      @total_page_count = 0
×
187
      @approval_delta_sum = 0.0
×
188
      @corrected_page_count = 0
×
189
    end
190
  end
191
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc