<commit_before>require 'api/lib/dbobjects'

module MojuraAPI

	class Poll < DbObject

		def initialize(id = nil, options = {})
			super('polls', id)
		end

		def load_fields
			yield :title, String, :required => true
			yield :description, String
			yield :options, Array, :default => []
			yield :active, Boolean, :default => false
			yield :votes, Array, :default => [], :hidden => true
			yield :blocked_ips, Hash, :default => {}, :hidden => true
		end

		def voteable(ip = nil)
			ip = API.remote_ip if ip.nil?
			block_expiration = Settings.get_i(:block_duration, :polls, 86400)
			blocked_ips.delete_if { | _, timestamp | timestamp < (Time.now.to_i - block_expiration) }
			return active && !blocked_ips.include?(ip.gsub('.', "\uFF0E"))
		end

		def vote(index, ip = nil)
			ip = API.remote_ip if ip.nil?
			return false unless voteable(ip)
			return false unless (index < options.size)
			votes[index] ||= 0
			votes[index] += 1
			@fields[:votes][:changed] = true
			blocked_ips[ip.gsub('.', "\uFF0E")] = Time.now.to_i
			@fields[:blocked_ips][:changed] = true
			API.log.info(JSON.pretty_generate(votes))
			save_to_db
			return true
		end

		def on_save_data(data)
			API.log.info(JSON.pretty_generate(data))
		end

		def get_votes(include_votes = true)
			result = {}
			result[:options] = []
			result[:total_votes] = votes.inject { | sum, _ | sum + 1 }
			f = 100.00 / result[:total_votes] rescue 0.00
			options.each_index { | index |
				url = API.api_url + "polls/#{self.id}/votes/?_method=put&index=#{index}"
				option = {title: options[index], index: index, vote_url: url}
				a = votes[index] || 0
				option[:votes] = { absolute: a, percentage: f * a } if include_votes
				result[:options].push(option)
			}
			return result<commit_msg>Fix for small bug in polls, which scrambled the percentages.<commit_after>require 'api/lib/dbobjects'

module MojuraAPI

	class Poll < DbObject

		def initialize(id = nil, options = {})
			super('polls', id)
		end

		def load_fields
			yield :title, String, :required => true
			yield :description, String
			yield :options, Array, :default => []
			yield :active, Boolean, :default => false
			yield :votes, Array, :default => [], :hidden => true
			yield :blocked_ips, Hash, :default => {}, :hidden => true
		end

		def voteable(ip = nil)
			ip = API.remote_ip if ip.nil?
			block_expiration = Settings.get_i(:block_duration, :polls, 86400)
			blocked_ips.delete_if { | _, timestamp | timestamp < (Time.now.to_i - block_expiration) }
			return active && !blocked_ips.include?(ip.gsub('.', "\uFF0E"))
		end

		def vote(index, ip = nil)
			ip = API.remote_ip if ip.nil?
			return false unless voteable(ip)
			return false unless (index < options.size)
			votes[index] ||= 0
			votes[index] += 1
			@fields[:votes][:changed] = true
			blocked_ips[ip.gsub('.', "\uFF0E")] = Time.now.to_i
			@fields[:blocked_ips][:changed] = true
			API.log.info(JSON.pretty_generate(votes))
			save_to_db
			return true
		end

		def get_votes(include_votes = true)
			result = {}
			result[:options] = []
			result[:total_votes] = votes.inject { | sum, i | sum + i }
			f = 100.00 / result[:total_votes] rescue 0.00
			options.each_index { | index |
				url = API.api_url + "polls/#{self.id}/votes/?_method=put&index=#{index}"
				option = {title: options[index], index: index, vote_url: url}
				a = votes[index] || 0
				option[:votes] = { absolute: a, percentage: f * a } if include_votes
				result[:options].push(option)
			}
			return result
{'fraction_non_alphanumeric': 0.1499856610266705, 'fraction_numerical': 0.01061084026383711, 'mean_word_length': 3.57742782152231, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3741064', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1222, 'n_words': 295}