|
156 | 156 | end |
157 | 157 | end |
158 | 158 | Rails.logger.info '..done!' |
| 159 | + |
| 160 | + Rails.logger.info 'Creating invitation logs...' |
| 161 | + |
| 162 | + # Get organizers as initiators (or create one if none exist) |
| 163 | + organisers = Member.joins(:roles).where(roles: { name: 'organiser' }).to_a |
| 164 | + if organisers.empty? |
| 165 | + organisers << Fabricate(:member, name: 'Chapter', surname: 'Organiser') |
| 166 | + end |
| 167 | + |
| 168 | + realistic_failure_reasons = [ |
| 169 | + 'SMTP connection timeout', |
| 170 | + 'Invalid email format: malformed address', |
| 171 | + 'Rate limit exceeded: too many recipients', |
| 172 | + 'Recipient mailbox full', |
| 173 | + 'Connection refused by mail server', |
| 174 | + 'DNS lookup failed for recipient domain' |
| 175 | + ].freeze |
| 176 | + |
| 177 | + # Filter to recent past workshops (last 3 months) |
| 178 | + recent_past_workshops = past_workshops.select do |w| |
| 179 | + w.date_and_time > 3.months.ago |
| 180 | + end |
| 181 | + |
| 182 | + # Create logs for ~25 recent workshops |
| 183 | + recent_past_workshops.sample(25).each do |workshop| |
| 184 | + initiator = organisers.sample |
| 185 | + audience = %w[students coaches everyone].sample |
| 186 | + |
| 187 | + # Determine invitee pool based on audience |
| 188 | + potential_invitees = case audience |
| 189 | + when 'students' then students |
| 190 | + when 'coaches' then coaches |
| 191 | + else students + coaches |
| 192 | + end.sample(50) |
| 193 | + |
| 194 | + # Simulate realistic distribution |
| 195 | + total_to_invite = rand(15..potential_invitees.length) |
| 196 | + skipped_count = rand(0..(total_to_invite * 0.1).to_i) |
| 197 | + actual_sent = total_to_invite - skipped_count |
| 198 | + success_count = rand((actual_sent * 0.85).to_i..actual_sent) |
| 199 | + failure_count = actual_sent - success_count |
| 200 | + |
| 201 | + log = InvitationLog.create!( |
| 202 | + loggable: workshop, |
| 203 | + initiator: initiator, |
| 204 | + chapter: workshop.chapter, |
| 205 | + audience: audience, |
| 206 | + action: 'invite', |
| 207 | + status: 'completed', |
| 208 | + total_invitees: total_to_invite, |
| 209 | + success_count: success_count, |
| 210 | + failure_count: failure_count, |
| 211 | + skipped_count: skipped_count, |
| 212 | + started_at: workshop.date_and_time - 2.hours, |
| 213 | + completed_at: workshop.date_and_time - 1.hour + rand(0..30).minutes, |
| 214 | + expires_at: 180.days.from_now |
| 215 | + ) |
| 216 | + |
| 217 | + # Get actual members for entries |
| 218 | + invitees = potential_invitees.sample(total_to_invite) |
| 219 | + skipped_members = invitees.shift(skipped_count) |
| 220 | + sent_members = invitees |
| 221 | + |
| 222 | + # Create skipped entries |
| 223 | + skipped_members.each do |member| |
| 224 | + InvitationLogEntry.create!( |
| 225 | + invitation_log: log, |
| 226 | + member: member, |
| 227 | + status: 'skipped', |
| 228 | + failure_reason: 'Invitation already exists', |
| 229 | + processed_at: log.started_at + rand(1..10).seconds |
| 230 | + ) |
| 231 | + end |
| 232 | + |
| 233 | + # Create success entries |
| 234 | + success_members = sent_members.sample(success_count) |
| 235 | + success_members.each do |member| |
| 236 | + invitation = WorkshopInvitation.where(workshop: workshop, member: member).first |
| 237 | + InvitationLogEntry.create!( |
| 238 | + invitation_log: log, |
| 239 | + member: member, |
| 240 | + invitation: invitation, |
| 241 | + status: 'success', |
| 242 | + processed_at: log.started_at + rand(10..120).seconds |
| 243 | + ) |
| 244 | + end |
| 245 | + |
| 246 | + # Create failure entries |
| 247 | + failure_members = sent_members - success_members |
| 248 | + failure_members.each do |member| |
| 249 | + InvitationLogEntry.create!( |
| 250 | + invitation_log: log, |
| 251 | + member: member, |
| 252 | + status: 'failed', |
| 253 | + failure_reason: realistic_failure_reasons.sample, |
| 254 | + processed_at: log.started_at + rand(10..120).seconds |
| 255 | + ) |
| 256 | + end |
| 257 | + end |
| 258 | + |
| 259 | + # Create 2 running (in-progress) logs for future workshops |
| 260 | + future_workshops.sample(2).each do |workshop| |
| 261 | + initiator = organisers.sample |
| 262 | + audience = %w[students coaches everyone].sample |
| 263 | + |
| 264 | + log = InvitationLog.create!( |
| 265 | + loggable: workshop, |
| 266 | + initiator: initiator, |
| 267 | + chapter: workshop.chapter, |
| 268 | + audience: audience, |
| 269 | + action: 'invite', |
| 270 | + status: 'running', |
| 271 | + total_invitees: 0, |
| 272 | + success_count: 0, |
| 273 | + failure_count: 0, |
| 274 | + skipped_count: 0, |
| 275 | + started_at: Time.current - rand(5..30).minutes, |
| 276 | + expires_at: 180.days.from_now |
| 277 | + ) |
| 278 | + |
| 279 | + # Create a few in-progress entries |
| 280 | + potential_invitees = case audience |
| 281 | + when 'students' then students |
| 282 | + when 'coaches' then coaches |
| 283 | + else students + coaches |
| 284 | + end.sample(20) |
| 285 | + |
| 286 | + potential_invitees.sample(rand(3..8)).each do |member| |
| 287 | + InvitationLogEntry.create!( |
| 288 | + invitation_log: log, |
| 289 | + member: member, |
| 290 | + status: 'success', |
| 291 | + processed_at: log.started_at + rand(60..300).seconds |
| 292 | + ) |
| 293 | + log.update_column(:success_count, log.success_count + 1) |
| 294 | + end |
| 295 | + end |
| 296 | + |
| 297 | + Rails.logger.info '..done creating invitation logs!' |
159 | 298 | rescue Exception => e |
160 | 299 | Rails.logger.error 'Something went wrong. Try running `bundle exec rake db:drop db:create db:migrate` first' |
161 | 300 | Rails.logger.error e.message |
|
0 commit comments