diff --git a/script/check_merit_actions_vs_points.rb b/script/check_merit_actions_vs_points.rb index 78a7245..0f1af09 100644 --- a/script/check_merit_actions_vs_points.rb +++ b/script/check_merit_actions_vs_points.rb @@ -36,8 +36,8 @@ def create_action(obj, index, count) end def recreate_actions person, objects, category - puts "Recreating actions for #{person.identifier} on model #{objects.first.class.base_class.name}" - actions = Merit::Action.where(target_id: objects, target_model: objects.first.class.base_class.name.downcase, action_method: 'create') + puts "Recreating actions for #{person.identifier} on model #{objects.name}" + actions = Merit::Action.where(target_id: objects, target_model: objects.name.downcase, action_method: 'create') Merit::Score::Point.where(action_id: actions).destroy_all actions.destroy_all # erase remaining points if any (can be wrong on destroy cases ?) @@ -65,11 +65,20 @@ class Article < ActiveRecord::Base end end -puts "Creaning up points from actions which don't exist" +puts "Cleaning up points from actions which don't exist" Merit::Score::Point.includes(:action).find_each(batch_size: 100) do |point| point.destroy if point.action.nil? end +# erase the badges spreadsheet +CSV.open( "gamification_wrong_badges.csv", 'w' ) do |csv| + csv << ['identifier', 'missing badges', 'exceeding badges'] +end +# erase the points spreadsheet +CSV.open( "gamification_points_out_expectation.csv", 'w' ) do |csv| + csv << ['identifier', 'name', 'action', 'profile', 'category id', 'category type', 'should have', 'have'] +end + Environment.all.each do |environment| puts "Process environment #{environment.name}" @@ -82,12 +91,12 @@ Environment.all.each do |environment| conditions = group_control.nil? ? {} : {:identifier => group_control.map{|k,v| v['profiles']}.flatten} people_count = environment.people.where(conditions).count person_index = 0 - remaining_wrong_points = [] puts "Analising environment people" environment.people.find_each(:conditions => conditions) do |person| person_index += 1 profile_ids = GamificationPlugin::PointsCategorization.uniq.pluck(:profile_id) profile_ids.keep_if { |item| group_control.keys.include?(item) } unless group_control.nil? + profile_ids.delete nil # avoid loosing time with generic for now profile_ids.each do |profile_id| profile = Profile.where(id: profile_id).first if profile.nil? @@ -112,8 +121,34 @@ Environment.all.each do |environment| follows_received = ArticleFollower.where(:article_id => person_articles) puts "#{person_index}/#{people_count} - Analising points for #{person.identifier} on #{profile_name}" - puts "Proposed #{person_articles.count} times, Commented #{comments.count} times, Voted #{votes.count} times, Followed #{follows.count} times" - puts "Received #{votes_received.count} votes, #{comments_received.count} comments, #{follows_received.count} follows\n" + #puts "Proposed #{person_articles.count} times, Commented #{comments.count} times, Voted #{votes.count} times, Followed #{follows.count} times" + #puts "Received #{votes_received.count} votes, #{comments_received.count} comments, #{follows_received.count} follows\n" + + scope_by_badge_action = { + "articlefollower#create" => follows, "comment#create" => comments, "article#create" => person_articles, "vote#create" => votes + } + + # ignoring user badges out of environment badges + should_and_doesnt_have = [] + should_not_have = [] + should_have = true + environment.gamification_plugin_badges.each do |badge| + (badge.custom_fields || {}).each do |action, config| + break if scope_by_badge_action[action].nil? or config[:threshold].nil? + should_have &= scope_by_badge_action[action].count >= config[:threshold].to_i + end + have = person.badges.include? badge + if should_have && !have + should_and_doesnt_have << "#{badge.title} #{badge.level}" + elsif should_have && !have + should_not_have << "#{badge.title} #{badge.level}" + end + end + if should_and_doesnt_have.size > 0 || should_not_have.size > 0 + CSV.open( "gamification_points_out_expectation.csv", 'a' ) do |csv| + [person.identifier, should_and_doesnt_have.join(' | '), should_not_have.join(' | ')] + end + end scope_by_type = { article_author: person_articles, comment_author: comments, vote_voter: votes, follower: follows, @@ -128,8 +163,15 @@ Environment.all.each do |environment| if points != person.points(category: c.id.to_s) recreate_actions person, scope, c.id.to_s points = calc_points c, scope - puts "after recreating points the person has: #{person.reload.points(category: c.id.to_s)} and should have #{points}" - remaining_wrong_points << [person.identifier, person.name, scope.first.class.base_class.name, profile_name, c.id, c.point_type.name, scope.count*c.weight, person.points(category: c.id.to_s)] if points != person.points(category: c.id.to_s) + if points != person.reload.points(category: c.id.to_s) + puts "after recreating points the person has: #{person.reload.points(category: c.id.to_s)} and should have #{points}" + # write to the spreadsheet the person points that couldn't regulate + CSV.open( "gamification_points_out_expectation.csv", 'a' ) do |csv| + [person.identifier, person.name, scope.first.class.base_class.name, profile_name, c.id, c.point_type.name, scope.count*c.weight, person.points(category: c.id.to_s)] + end + else + puts "points fixed for #{c.point_type.name}!" + end end end puts @@ -142,20 +184,4 @@ Environment.all.each do |environment| puts "Updating #{person.identifier} level\n" person.update_attribute(:level, person.gamification_plugin_calculate_level) end - - # write to the spreadsheet the person points that couldn't regulate - unless remaining_wrong_points.blank? - CSV.open( "gamification_points_out_expectation.csv", 'w' ) do |csv| - csv << ['identifier', 'name', 'action', 'profile', 'category id', 'category type', 'should have', 'have'] - remaining_wrong_points.each do |line| - csv << line - end - end - end - - if remaining_wrong_points.count - puts "Finished. There was #{remaining_wrong_points.count} people/pontuation types with errors after check and fix. Please check the created spreadsheet." - else - puts "Finished. There was no errors after checking. \o/ Pontuation seems to be ok!" - end end -- libgit2 0.21.2