Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.68 KB | None | 0 0
  1. # ---------- Fakers
  2. # rake fake:cluster - generates the whole cluster data
  3. # rake fake:cluster_without_evaluations - generates fake cluster data but without evaluations (no templates or template_answers)
  4. # rake fake:evaluations - generates evaluations against the existed templates
  5.  
  6. # ---------- Documentation
  7. # Before you try this faker, it is better to create another db and use it only when u need to benchmark something or check the performance.
  8. # The main rake creates a proxy, and couple of servers, couple of roles and groups, create supervisors and dozens of agents, assign access for each object, and creates templates of questions.
  9. # for each agent it generates a handful of calls, randomly evaluate these calls with double templates, calibration or single evaluation. also it generates random call notes on some random calls.
  10. # all the numbers are at the beginning of the rake, u can edit them as u like, two sets of numbers are in the code, one generates 225,000 calls and the other generates 22,500.
  11. # it takes time so don't go crazy with numbers.
  12.  
  13. if defined?(Faker)
  14. require 'ffaker'
  15. require 'populator'
  16.  
  17. desc 'Load fake data for development/testing.'
  18. task :clean_db => ['db:drop', 'db:create', 'db:migrate', 'db:seed']
  19. task :faker => ['fake:cluster_without_evaluations', 'fake:templates', 'fake:full_evaluations']
  20. task :large_faker => ['fake:cluster_without_evaluations', 'fake:templates', 'fake:full_evaluations_optimized']
  21. task :mediaum_faker => ['fake:cluster_without_evaluations', 'fake:templates', 'fake:evaluations_optimized']
  22. task :clean_data => ['clean_db', 'faker']
  23. task :clean_large_data => ['clean_db', 'large_faker']
  24.  
  25. debug = true
  26.  
  27. namespace :fake do
  28.  
  29. desc 'Create full cluster data'
  30. task :cluster => :environment do
  31.  
  32. user_password = '123123123'
  33.  
  34. departments = ['Technical', 'Relations', 'Marketing', 'Manners', 'Duration']
  35. num_general_templates = departments.length
  36.  
  37. extension_start_value = 1000
  38. agent_id_start_value = 3000
  39.  
  40. percentage_calibration = 1
  41. percentage_double_evaluation = 1
  42. percentage_evaluation = 1
  43.  
  44. date_span = 1 # in years
  45.  
  46. # 150,000 calls
  47. # 800 users
  48. num_max_questions = 1
  49. num_servers = 5
  50. num_supervisors_per_group = 10
  51. num_agents_per_supervisor = 15
  52. num_calls_per_agent = 200
  53.  
  54. # 225,000 calls
  55. # 800 users
  56. # num_max_questions = 1
  57. # num_servers = 5
  58. # num_supervisors_per_group = 10
  59. # num_agents_per_supervisor = 15
  60. # num_calls_per_agent = 300
  61.  
  62. # 22,500 calls
  63. # 800 users
  64. # num_max_questions = 1
  65. # num_servers = 5
  66. # num_supervisors_per_group = 10
  67. # num_agents_per_supervisor = 15
  68. # num_calls_per_agent = 30
  69.  
  70. # 3,000,000 calls
  71. # 2100 users
  72. # num_max_questions = 1
  73. # num_servers = 5
  74. # num_supervisors_per_group = 20
  75. # num_agents_per_supervisor = 20
  76. # num_calls_per_agent = 1500
  77.  
  78.  
  79. # refernece call to copy its media file data
  80. refernece_call = {mp4_file: 'DEMO/DEMO.MP4', audio_file: 'DEMO/DEMO.MP3', media_type: 3}
  81.  
  82. # retrieving super user data
  83. super_user = User.super_user
  84. super_role = Role.super_role
  85.  
  86. # creating the cluster
  87. proxy_prefix, proxy = create_cluster(debug)
  88.  
  89. # creating general templates
  90. puts "creating general templates".colorize(:green)
  91. general_templates = []
  92. (1..num_general_templates).each do |index|
  93. # creating the template
  94. general_template = Template.create!( {user_id: super_user.id, title: "#{proxy_prefix} #{departments[index-1]}"} )
  95. # creating the questions section
  96. questions_section = general_template.questions_sections.create!( {title: 'Section 1', rank: 100} )
  97.  
  98. # creating random number of yes/no questions
  99. (1..rand(num_max_questions)+2).each do |question_index|
  100. QuestionYesNo.create!( {template_id: general_template.id, questions_section_id: questions_section.id, text: "Question #{question_index}: #{Populator.words(3..5)} ?", coach_hint: Populator.sentences(0..1), default_response: QuestionYesNo.list_possible_answers.sample, weight: rand(5)*10, passing: rand(5)*10, rank: question_index} )
  101. end
  102.  
  103. # assign super user access to it
  104. RoleAccess.create!( {role_id: super_role.id, accessable_type: 'Template', accessable_id: general_template.id} )
  105.  
  106. general_templates << general_template
  107. end # of creating generate template
  108.  
  109. agent_id_counter = agent_id_start_value
  110. # creating servers and their dependant data
  111. (1..num_servers).each do |servers_index|
  112. puts "creating server #{servers_index}".colorize(:blue)
  113.  
  114. # creating the server
  115. server_prefix, server = create_server(debug, proxy)
  116. extension_counter = extension_start_value
  117.  
  118. # creating management role and group
  119. puts "creating management role and group".colorize(:green)
  120. management_role = Role.create!({name: "#{server_prefix} Management", supervisable: true})
  121. management_group = Group.create!({name: "#{server_prefix} Management", role_ids: management_role.id})
  122.  
  123. # creating agents role and group
  124. puts "creating agents role and group".colorize(:green)
  125. agent_role = Role.create!({name: "#{server_prefix} Agent", access_own_data: true})
  126. agent_group = Group.create!({name: "#{server_prefix} Agents", role_ids: agent_role.id})
  127. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'Group', accessable_id: agent_group.id} )
  128.  
  129. # creating server template
  130. puts "creating server template".colorize(:green)
  131. server_template = Template.create!( {user_id: super_user.id, title: "#{server_prefix} Template"} )
  132. questions_section = server_template.questions_sections.create!( {title: 'Section 1', rank: 100} )
  133. (1..rand(num_max_questions)+2).each do |question_index|
  134. QuestionYesNo.create!( {template_id: server_template.id, questions_section_id: questions_section.id, text: "Question #{question_index}: #{Populator.words(3..5)} ?", coach_hint: Populator.sentences(0..1), default_response: QuestionYesNo.list_possible_answers.sample, weight: rand(5)*10, passing: rand(5)*10, rank: question_index} )
  135. end
  136. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'Template', accessable_id: server_template.id} )
  137.  
  138. # creating supervisors
  139. puts "creating #{num_supervisors_per_group} supervisors".colorize(:green)
  140. supervisors = []
  141. (1..num_supervisors_per_group).each do |supervisor_index|
  142. puts "creating supervisor #{supervisor_index} of server #{servers_index}".colorize(:cyan) if debug
  143.  
  144. # incrementing the agent_id counter
  145. agent_id_counter += 1
  146.  
  147. # department data
  148. department_index = supervisor_index % num_general_templates
  149. department = departments[department_index]
  150. department_template = general_templates[department_index]
  151.  
  152. # create supervisor
  153. full_name = Faker::Name.name
  154. supervisor = User.create!( {
  155. full_name: full_name,
  156. first_name: full_name.split.first,
  157. last_name: full_name.split.last,
  158. group_id: management_group.id,
  159. password: user_password,
  160. password_confirmation: user_password,
  161. email: Faker::Internet.email,
  162. department: department,
  163. inherit_role: true,
  164. inherit_group_access: true,
  165. inherit_role_templates: true,
  166. phone: Faker::PhoneNumber.phone_number,
  167. agent_id: agent_id_counter.to_s,
  168. created_at: date_span.year.ago - rand(100).day
  169. } )
  170. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'User', accessable_id: supervisor.id} )
  171. supervisors << supervisor
  172.  
  173. # create subordinates for the supervisor
  174. (1..num_agents_per_supervisor).each do |agent_index|
  175. puts "creating agent(#{agent_index}) for supervisor(#{supervisor_index}), server (#{servers_index})".colorize(:red) if debug
  176.  
  177. # incrementing the agent_id counter
  178. agent_id_counter += 1
  179.  
  180. # create agent's extension
  181. extension = server.extensions.create!( {extension_id: extension_counter} )
  182. extension_counter += 1
  183.  
  184. # create agent
  185. full_name = Faker::Name.name
  186. agent = User.create!( {
  187. full_name: full_name,
  188. first_name: full_name.split.first,
  189. last_name: full_name.split.last,
  190. group_id: agent_group.id,
  191. supervisor_id: supervisor.id,
  192. extension_id: extension.id,
  193. password: user_password,
  194. password_confirmation: user_password,
  195. email: Faker::Internet.email,
  196. department: department,
  197. inherit_role: true,
  198. inherit_group_access: true,
  199. inherit_role_templates: true,
  200. phone: Faker::PhoneNumber.phone_number,
  201. agent_id: agent_id_counter.to_s,
  202. created_at: date_span.year.ago - rand(50).day
  203. } )
  204. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'User', accessable_id: agent.id} )
  205.  
  206. # populate call data
  207. call_index = -1
  208. Call.populate num_calls_per_agent do |call|
  209. call_index += 1
  210. # puts "creating call (#{call_index}) for agent(#{agent_index})" if debug
  211.  
  212. call.user_id = agent.id
  213. call.agent_id = agent.agent_id
  214. call.server_id = server.id
  215. call.extension_id = extension.id
  216. call.call_guid = "#{server_prefix}:#{agent.id}:#{call_index}"
  217. call.caller_id = '3216973800'
  218. call.call_id = 11111111..99999999
  219. call.call_direction = ['Inbound', 'Outbound']
  220. call.call_duration = 30..700
  221. call.media_file_size = 500..10000
  222. call.mp4_file = refernece_call[:mp4_file]
  223. call.audio_file = refernece_call[:audio_file]
  224. call.media_type = refernece_call[:media_type]
  225. call.caller_id = Faker::PhoneNumber.phone_number
  226. call.state = 'completed'
  227. call.audio_recording_time = date_span.year.ago + rand(date_span*360).day
  228. call.created_at = call.audio_recording_time + rand(360).minute
  229.  
  230. # create max two call notes per call
  231. call.call_notes_count = 0
  232. CallNote.populate rand(2) do |call_note|
  233. call_note.user_id = [supervisor.id, agent.id]
  234. call_note.call_id = call.id
  235. call_note.note = Populator.words(4..10)
  236. call_note.created_at = call.created_at..Time.now
  237. call.call_notes_count += 1
  238. end
  239.  
  240. # flip coins to create calibration
  241. if supervisor_index > 1 && rand(100 / percentage_calibration) == 1
  242. puts "creating calibration".colorize(:green) if debug
  243. template = [department_template, server_template].sample
  244. other_supervisor = supervisors[rand(supervisor_index-1)]
  245.  
  246. template_index = -1
  247. TemplateAnswer.populate 2 do |template_answer|
  248. template_index += 1
  249. template_answer.user_id = [other_supervisor.id, supervisor.id][template_index]
  250. template_answer.call_id = call.id
  251. template_answer.template_id = template.id
  252. template_answer.for_calibration = true
  253. template_answer.created_at = call.created_at..Time.now
  254.  
  255. # answer one random question only
  256. question = template.questions.sample
  257. QuestionAnswer.populate 1 do |question_answer|
  258. question_answer.user_id = [other_supervisor.id, supervisor.id][template_index]
  259. question_answer.question_id = question.id
  260. question_answer.template_answer_id = template_answer.id
  261. question_answer.answer = question.list_possible_answers.sample
  262. question_answer.weight = question.weigh_answer(question_answer)
  263. question_answer.created_at = template_answer.created_at
  264. template_answer.total_weight = question.weight
  265. template_answer.answer_weight = question_answer.weight
  266.  
  267. template_answer.score_percentage = template_answer.total_weight == 0 ? 0 : (template_answer.answer_weight.to_f / template_answer.total_weight.to_f * 100)
  268. end # of question answer
  269. end # of template answer
  270.  
  271. # flip coins to create evaluation
  272. elsif rand(100 / percentage_evaluation) == 1
  273. puts "creating evaluation".colorize(:green) if debug
  274. template = [department_template, server_template].sample
  275.  
  276. TemplateAnswer.populate 1 do |template_answer|
  277. template_answer.user_id = supervisor.id
  278. template_answer.call_id = call.id
  279. template_answer.template_id = template.id
  280. template_answer.for_calibration = false
  281. template_answer.created_at = call.created_at..Time.now
  282.  
  283. question = template.questions.sample
  284. QuestionAnswer.populate 1 do |question_answer|
  285. question_answer.user_id = supervisor.id
  286. question_answer.question_id = question.id
  287. question_answer.template_answer_id = template_answer.id
  288. question_answer.answer = question.list_possible_answers.sample
  289. question_answer.weight = question.weigh_answer(question_answer)
  290. question_answer.created_at = template_answer.created_at
  291. template_answer.total_weight = question.weight
  292. template_answer.answer_weight = question_answer.weight
  293.  
  294. template_answer.score_percentage = template_answer.total_weight == 0 ? 0 : (template_answer.answer_weight.to_f / template_answer.total_weight.to_f * 100)
  295. end # of question answer
  296. end # of template answer
  297.  
  298. # flip coins to create double evaluations
  299. elsif rand(100 / percentage_double_evaluation) == 1
  300. puts "creating double evaluations".colorize(:green) if debug
  301. template = [department_template, server_template].sample
  302. other_supervisor = supervisors[rand(supervisor_index)]
  303.  
  304. template_index = -1
  305. TemplateAnswer.populate 2 do |template_answer|
  306. template_index += 1
  307.  
  308. template_answer.user_id = [other_supervisor.id, supervisor.id][template_index]
  309. template_answer.template_id = [department_template, server_template][template_index]
  310. template_answer.call_id = call.id
  311. template_answer.for_calibration = false
  312. template_answer.created_at = call.created_at..Time.now
  313.  
  314. # answer one random question only
  315. question = template.questions.sample
  316. QuestionAnswer.populate 1 do |question_answer|
  317. question_answer.user_id = [other_supervisor.id, supervisor.id][template_index]
  318. question_answer.question_id = question.id
  319. question_answer.template_answer_id = template_answer.id
  320. question_answer.answer = question.list_possible_answers.sample
  321. question_answer.weight = question.weigh_answer(question_answer)
  322. question_answer.created_at = template_answer.created_at
  323. template_answer.total_weight = question.weight
  324. template_answer.answer_weight = question_answer.weight
  325.  
  326. template_answer.score_percentage = template_answer.total_weight == 0 ? 0 : (template_answer.answer_weight.to_f / template_answer.total_weight.to_f * 100)
  327. end # of question answer
  328. end # of template answer
  329.  
  330. end # of calibration and evaluation
  331.  
  332. end # of calls
  333. end # of agent
  334.  
  335. end # of supervisor
  336.  
  337. # calculate server evaluations score
  338. # puts "calculate #{server_template.template_answers.count} server evaluations score".colorize(:green)
  339. # server_template.template_answers.each do |template_answer|
  340. # template_answer.calculate_score
  341. # template_answer.save
  342. # end # of calculating this server evaluations score
  343. end # of server
  344.  
  345. # calculate general templates evaluations score
  346. # puts "calculate general templates evaluations score".colorize(:green) if debug
  347. # general_templates.each do |general_template|
  348. # puts "calculate #{general_template.template_answers.count} general evaluations score".colorize(:green)
  349. # general_template.template_answers.each do |template_answer|
  350. # template_answer.calculate_score
  351. # template_answer.save
  352. # end
  353. # end # of calculating general templates evaluations score
  354.  
  355. # reset caches
  356. # Call.reset_column_information
  357.  
  358. end # of faking cluster
  359.  
  360. desc 'Create cluster data without evaluations'
  361. task :cluster_without_evaluations => :environment do
  362.  
  363. user_password = '123123123'
  364.  
  365. departments = ['Technical', 'Relations', 'Marketing', 'Manners', 'Duration']
  366. num_general_templates = departments.length
  367.  
  368. extension_start_value = 1000
  369. agent_id_start_value = 3000
  370.  
  371. percentage_calibration = 1
  372. percentage_double_evaluation = 1
  373. percentage_evaluation = 1
  374.  
  375. date_span = 2 # in years
  376.  
  377. # 450,000 calls
  378. # 1200 users
  379. # num_max_questions = 1
  380. # num_servers = 1
  381. # num_supervisors_per_group = 1
  382. # num_agents_per_supervisor = 10
  383. # num_calls_per_agent = 20
  384.  
  385. # 5,000,000 calls
  386. # 200 users
  387. # num_max_questions = 1
  388. # num_servers = 5
  389. # num_supervisors_per_group = 5
  390. # num_agents_per_supervisor = 8
  391. # num_calls_per_agent = 25000
  392.  
  393. # 150,000 calls
  394. # 800 users
  395. # num_max_questions = 1
  396. # num_servers = 5
  397. # num_supervisors_per_group = 10
  398. # num_agents_per_supervisor = 15
  399. # num_calls_per_agent = 200
  400.  
  401. # 225,000 calls
  402. # num_max_questions = 1
  403. # num_servers = 5
  404. # num_supervisors_per_group = 10
  405. # num_agents_per_supervisor = 15
  406. # num_calls_per_agent = 300
  407.  
  408. # 22,500 calls
  409. # 800 users
  410. num_max_questions = 1
  411. num_servers = 3
  412. num_supervisors_per_group = 10
  413. num_agents_per_supervisor = 15
  414. num_calls_per_agent = 30
  415.  
  416. # 3,000,000 calls
  417. # 2100 users
  418. # num_max_questions = 1
  419. # num_servers = 2
  420. # num_supervisors_per_group = 20
  421. # num_agents_per_supervisor = 20
  422. # num_calls_per_agent = 1500
  423.  
  424. # refernece call to copy its media file data
  425. refernece_call = {mp4_file: 'DEMO/DEMO.MP4', audio_file: 'DEMO/DEMO.MP3', media_type: 3}
  426.  
  427. # retrieving super user data
  428. super_user = User.super_user
  429. super_role = Role.super_role
  430.  
  431. # creating the cluster
  432. puts "creating the cluster".colorize(:green)
  433. proxy_name = Faker::Company.name.split(/[,;-]|\s/).reject{|s| s == '' or s == 'and'}[0..1].join(' ')
  434. proxy_prefix = proxy_name.split(/[,;]|\s/).first
  435. proxy = Proxy.create!( {name: proxy_name, qc_primary_ip: '172.16.20.110', qc_primary_port: '3010', proxy_primary_ip: '172.16.20.108', proxy_primary_port: '8000'} )
  436.  
  437. agent_id_counter = agent_id_start_value
  438. # creating servers and their dependant data
  439. (1..num_servers).each do |servers_index|
  440. puts "creating server #{servers_index}".colorize(:blue)
  441.  
  442. # creating the server
  443. server_name = Faker::Company.name.split(/[,;-]|\s/).reject{|s| s == '' or s == 'and'}[0..1].join(' ')
  444. server_prefix = server_name.split(/[,;]|\s/).first
  445. server = proxy.servers.create!( {name: server_name} )
  446. extension_counter = extension_start_value
  447.  
  448. # creating management role and group
  449. puts "creating management role and group".colorize(:green)
  450. management_role = Role.create!({name: "#{server_prefix} Management", supervisable: true})
  451. management_group = Group.create!({name: "#{server_prefix} Management", role_ids: management_role.id})
  452.  
  453. # creating agents role and group
  454. puts "creating agents role and group".colorize(:green)
  455. agent_role = Role.create!({name: "#{server_prefix} Agent", access_own_data: true})
  456. agent_group = Group.create!({name: "#{server_prefix} Agents", role_ids: agent_role.id})
  457. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'Group', accessable_id: agent_group.id} )
  458.  
  459. # creating supervisors
  460. puts "creating #{num_supervisors_per_group} supervisors".colorize(:green)
  461. supervisors = []
  462. (1..num_supervisors_per_group).each do |supervisor_index|
  463. puts "creating supervisor #{supervisor_index}".colorize(:cyan) if debug
  464.  
  465. # incrementing the agent_id counter
  466. agent_id_counter += 1
  467.  
  468. # department data
  469. department_index = supervisor_index % num_general_templates
  470. department = departments[department_index]
  471.  
  472. # create supervisor
  473. full_name = Faker::Name.name
  474. supervisor = User.create!( {
  475. full_name: full_name,
  476. first_name: full_name.split.first,
  477. last_name: full_name.split.last,
  478. group_id: management_group.id,
  479. password: user_password,
  480. password_confirmation: user_password,
  481. email: Faker::Internet.email,
  482. department: department,
  483. inherit_role: true,
  484. inherit_group_access: true,
  485. inherit_role_templates: true,
  486. phone: Faker::PhoneNumber.phone_number,
  487. agent_id: agent_id_counter.to_s,
  488. created_at: date_span.year.ago - rand(100).day
  489. } )
  490.  
  491. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'User', accessable_id: supervisor.id} )
  492. supervisors << supervisor
  493.  
  494. # create subordinates for the supervisor
  495. (1..num_agents_per_supervisor).each do |agent_index|
  496. puts "creating agent(#{agent_index}) for supervisor(#{supervisor_index}), server (#{servers_index})".colorize(:red) if debug
  497.  
  498. # incrementing the agent_id counter
  499. agent_id_counter += 1
  500.  
  501. # create agent's extension
  502. extension = server.extensions.create!( {extension_id: extension_counter} )
  503. extension_counter += 1
  504.  
  505. # create agent
  506. full_name = Faker::Name.name
  507. agent = User.create!( {
  508. full_name: full_name,
  509. first_name: full_name.split.first,
  510. last_name: full_name.split.last,
  511. group_id: agent_group.id,
  512. supervisor_id: supervisor.id,
  513. extension_id: extension.id,
  514. password: user_password,
  515. password_confirmation: user_password,
  516. email: Faker::Internet.email,
  517. department: department,
  518. inherit_role: true,
  519. inherit_group_access: true,
  520. inherit_role_templates: true,
  521. phone: Faker::PhoneNumber.phone_number,
  522. agent_id: agent_id_counter.to_s,
  523. created_at: date_span.year.ago - rand(50).day
  524. } )
  525. RoleAccess.create!( {role_id: management_role.id, accessable_type: 'User', accessable_id: agent.id} )
  526.  
  527. # populate call data
  528. call_index = -1
  529. Call.populate num_calls_per_agent do |call|
  530. call_index += 1
  531. # puts "creating call (#{call_index}) for agent(#{agent_index})" if debug
  532.  
  533. call.user_id = agent.id
  534. call.agent_id = agent.agent_id
  535. call.server_id = server.id
  536. call.extension_id = extension.id
  537. call.call_guid = "#{server_prefix}:#{agent.id}:#{call_index}"
  538. call.caller_id = '3216973800'
  539. call.call_id = 11111111..99999999
  540. call.call_direction = ['Inbound', 'Outbound']
  541. call.call_duration = 30..700
  542. call.media_file_size = 500..10000
  543. call.mp4_file = refernece_call[:mp4_file]
  544. call.audio_file = refernece_call[:audio_file]
  545. call.media_type = refernece_call[:media_type]
  546. call.caller_id = Faker::PhoneNumber.phone_number
  547. call.state = 'completed'
  548. call.audio_recording_time = date_span.year.ago + rand(date_span*360).day
  549. call.created_at = call.audio_recording_time + rand(360).minute
  550.  
  551. # create max two call notes per call
  552. call.call_notes_count = 0
  553. CallNote.populate rand(2) do |call_note|
  554. call_note.user_id = [supervisor.id, agent.id]
  555. call_note.call_id = call.id
  556. call_note.note = Populator.words(4..10)
  557. call_note.created_at = call.created_at..Time.now
  558. call.call_notes_count += 1
  559. end
  560.  
  561. end # of calls
  562. end # of agent
  563.  
  564. end # of supervisor
  565.  
  566. end # of server
  567.  
  568. # reset caches
  569. # Call.reset_column_information
  570.  
  571. end # of faking cluster without evaluations
  572.  
  573. desc 'Fake evaluations'
  574. task :evaluations => :environment do
  575.  
  576. # As max. expected numbers are 200k evaluations for 5M calls ~4%
  577. # Double this number as lots of evaluation would be cut out to mimic the average score percentage to around 85-90%
  578. num_evaluations = (Call.count * 0.04 * 1.5).to_i
  579.  
  580. min_call_id = Call.minimum('id')
  581. max_call_id = Call.maximum('id')
  582.  
  583. templates = Template.all
  584.  
  585. TemplateAnswer.populate num_evaluations do |template_answer|
  586. # pick a random call
  587. begin
  588. random_call_id = min_call_id + rand(max_call_id)
  589. call = Call.find_by_id(random_call_id)
  590. end while call.blank? || call.user.supervisor.blank?
  591.  
  592. # generate a type of evaluation: calibration, double evaluations, or single evaluation
  593. template_answer = create_type_of_evaluation(debug, template_answer, call, templates)
  594. end # end of populating evaluations
  595.  
  596. # adjusting the average percentage
  597. adjust_average_score(debug)
  598.  
  599. end # of faking evaluations
  600.  
  601. desc 'Fake evaluation for each call'
  602. task :full_evaluations => :environment do
  603.  
  604. num_evaluations_per_call = 1
  605. templates = Template.all
  606.  
  607. # looping on all calls and evaluating them
  608. Call.find_each do |call|
  609. TemplateAnswer.populate num_evaluations_per_call do |template_answer|
  610. # generate a type of evaluation: calibration, double evaluations, or single evaluation
  611. template_answer = create_type_of_evaluation(debug, template_answer, call, templates)
  612. end # of populating evaluations
  613. end # of looping on calls
  614.  
  615. # adjusting the average percentage
  616. adjust_average_score(debug)
  617.  
  618. end # of faking full_evaluations
  619.  
  620. desc 'Fake evaluation for percentage of calls'
  621. task :evaluations_optimized => :environment do
  622.  
  623. evaluations_percentage = 20
  624. num_evaluations = Call.count * evaluations_percentage / 100
  625. call_step = Call.count / num_evaluations
  626.  
  627. templates = Template.all
  628.  
  629. percentage_na_questions = 10
  630. percentage_random_answer = 25
  631.  
  632. # looping on all calls and evaluating them
  633. call_index = 1
  634. evals_count = 0
  635. TemplateAnswer.populate num_evaluations do |template_answer|
  636. evals_count += 1
  637. call = Call.find(call_index)
  638. call_index += call_step
  639.  
  640. # choose the call's user supervisor or pick random supervisors
  641. supervisor = call.user.supervisor
  642.  
  643. # pick a random template, their number is limited
  644. template = templates.sample
  645.  
  646. # print status each 5000 call
  647. puts "creating evaluation #{evals_count}".colorize(:green) if debug && (evals_count % 5000 == 0)
  648.  
  649. # create first evaluation
  650. template_answer.user_id = supervisor.id
  651. template_answer.call_id = call.id
  652. template_answer.template_id = template.id
  653. template_answer.for_calibration = true
  654. template_answer.created_at = call.created_at + rand(3).day # after creating the call by max. 3 days
  655. template_answer.total_weight = 0
  656. template_answer.answer_weight = 0
  657.  
  658. # pick the questions to be answered
  659. questions = template.parent_questions.select{ |q| q unless (rand(100 / percentage_na_questions) == 1)}
  660. questions_index = -1
  661. QuestionAnswer.populate questions.count do |question_answer|
  662. questions_index += 1
  663. question = questions[questions_index]
  664.  
  665. question_answer.user_id = supervisor.id
  666. question_answer.question_id = question.id
  667. question_answer.template_answer_id = template_answer.id
  668. question_answer.created_at = template_answer.created_at
  669.  
  670. # giving weight to highest weight answers
  671. if (rand(100 / percentage_random_answer) != 1)
  672. h = question.list_possible_answers_with_weights
  673. answer = Hash[h.sort_by{ |_, v| -v }].to_a[rand(1)].first
  674. else
  675. answer = question.list_possible_answers.sample
  676. end
  677. question_answer.answer = answer
  678.  
  679. # calculating answer weight
  680. question_answer.weight = question.weigh_answer(question_answer)
  681. template_answer.total_weight += question.weight
  682. template_answer.answer_weight += question_answer.weight
  683. end # of question answer
  684. template_answer.score_percentage = template_answer.total_weight == 0 ? 0 : (template_answer.answer_weight.to_f / template_answer.total_weight.to_f * 100)
  685. end # of populating evaluations
  686.  
  687. # adjusting the average percentage
  688. adjust_average_score(debug)
  689.  
  690. puts "adding initial values of the template answers, this would take a while for large database"
  691. Template.reset_column_information
  692. Template.pluck(:id).each do |template_id|
  693. # template_answers_count is in readonly list, thus needs the special method reset_counters
  694. Template.reset_counters(template_id, :template_answers)
  695. end
  696.  
  697. puts "adding initial values of the question answers, this would take a while for large database"
  698. Question.reset_column_information
  699. total_questions = Question.count
  700. Question.pluck(:id).each do |question_id|
  701. # question_answers_count is in readonly list, thus needs the special method reset_counters
  702. puts "Question #{question_id}/#{total_questions}"
  703. Question.reset_counters(question_id, :question_answers)
  704. end
  705.  
  706. end # of faking evaluations_optimized
  707.  
  708. desc 'Fake evaluation for each call'
  709. task :full_evaluations_optimized => :environment do
  710.  
  711. num_evaluations_per_call = 1
  712. templates = Template.all
  713.  
  714. percentage_na_questions = 10
  715. percentage_random_answer = 25
  716.  
  717. # looping on all calls and evaluating them
  718. call_index = 0
  719. TemplateAnswer.populate Call.count do |template_answer|
  720. call_index += 1
  721. call = Call.find(call_index)
  722.  
  723. # choose the call's user supervisor or pick random supervisors
  724. supervisor = call.user.supervisor
  725.  
  726. # pick a random template, their number is limited
  727. template = templates.sample
  728.  
  729. # print status each 1000 call
  730. puts "creating evaluation #{call_index}".colorize(:green) if debug && (call_index % 5000 == 0)
  731.  
  732. # create first evaluation
  733. template_answer.user_id = supervisor.id
  734. template_answer.call_id = call.id
  735. template_answer.template_id = template.id
  736. template_answer.for_calibration = true
  737. template_answer.created_at = call.created_at + rand(3).day # after creating the call by max. 3 days
  738. template_answer.total_weight = 0
  739. template_answer.answer_weight = 0
  740.  
  741. # pick the questions to be answered
  742. questions = template.parent_questions.select{ |q| q unless (rand(100 / percentage_na_questions) == 1)}
  743. questions_index = -1
  744. QuestionAnswer.populate questions.count do |question_answer|
  745. questions_index += 1
  746. question = questions[questions_index]
  747.  
  748. question_answer.user_id = supervisor.id
  749. question_answer.question_id = question.id
  750. question_answer.template_answer_id = template_answer.id
  751. question_answer.created_at = template_answer.created_at
  752.  
  753. # giving weight to highest weight answers
  754. if (rand(100 / percentage_random_answer) != 1)
  755. h = question.list_possible_answers_with_weights
  756. answer = Hash[h.sort_by{ |_, v| -v }].to_a[rand(1)].first
  757. else
  758. answer = question.list_possible_answers.sample
  759. end
  760. question_answer.answer = answer
  761.  
  762. # calculating answer weight
  763. question_answer.weight = question.weigh_answer(question_answer)
  764. template_answer.total_weight += question.weight
  765. template_answer.answer_weight += question_answer.weight
  766. end # of question answer
  767. template_answer.score_percentage = template_answer.total_weight == 0 ? 0 : (template_answer.answer_weight.to_f / template_answer.total_weight.to_f * 100)
  768. end # of populating evaluations
  769.  
  770. # adjusting the average percentage
  771. adjust_average_score(debug)
  772.  
  773. puts "adding initial values of the template answers, this would take a while for large database"
  774. Template.reset_column_information
  775. Template.pluck(:id).each do |template_id|
  776. # template_answers_count is in readonly list, thus needs the special method reset_counters
  777. Template.reset_counters(template_id, :template_answers)
  778. end
  779.  
  780. puts "adding initial values of the question answers, this would take a while for large database"
  781. Question.reset_column_information
  782. total_questions = Question.count
  783. Question.pluck(:id).each do |question_id|
  784. # question_answers_count is in readonly list, thus needs the special method reset_counters
  785. puts "Question #{question_id}/#{total_questions}"
  786. Question.reset_counters(question_id, :question_answers)
  787. end
  788.  
  789. end # of faking full_evaluations_optimized
  790.  
  791. desc 'Fake templates'
  792. task :templates => :environment do
  793. # import the templates saved in 'lib/tasks/faker_templates'
  794. files = Dir.glob("lib/tasks/faker_templates/*.xls")
  795. files.each do |template_file|
  796. Template.import!(File.new(template_file), User.super_user)
  797. end
  798. puts "Imported #{files.count} templates successfully!".colorize(:cyan)
  799. end # of faking templates
  800.  
  801. #-------------- Utility Functions
  802.  
  803. def create_cluster(debug)
  804. puts "creating the cluster".colorize(:green)
  805. proxy_name = Faker::Company.name.split(/[,;-]|\s/).reject{|s| s == '' or s == 'and'}[0..1].join(' ')
  806. proxy_prefix = proxy_name.split(/[,;]|\s/).first
  807. proxy = Proxy.create!( {name: proxy_name, qc_primary_ip: '172.16.20.110', qc_primary_port: '3010', proxy_primary_ip: '172.16.20.108', proxy_primary_port: '8000'} )
  808. [proxy_prefix, proxy]
  809. end
  810.  
  811. def create_server(debug, proxy)
  812. server_name = Faker::Company.name.split(/[,;-]|\s/).reject{|s| s == '' or s == 'and'}[0..1].join(' ')
  813. server_prefix = server_name.split(/[,;]|\s/).first
  814. server = proxy.servers.create!( {name: server_name} )
  815. [server_prefix, server]
  816. end
  817.  
  818. def create_evaluation(debug, template_answer, call, template, audtior)
  819. percentage_na_questions = 10
  820. percentage_random_answer = 25
  821.  
  822. # create first evaluation
  823. template_answer.user_id = audtior.id
  824. template_answer.call_id = call.id
  825. template_answer.template_id = template.id
  826. template_answer.for_calibration = true
  827. template_answer.created_at = call.created_at + rand(3).day # after creating the call by max. 3 days
  828. template_answer.total_weight = 0
  829. template_answer.answer_weight = 0
  830.  
  831. # pick the questions to be answered
  832. questions = template.parent_questions.select{ |q| q unless (rand(100 / percentage_na_questions) == 1)}
  833. questions_index = -1
  834. QuestionAnswer.populate questions.count do |question_answer|
  835. questions_index += 1
  836. question = questions[questions_index]
  837.  
  838. question_answer.user_id = audtior.id
  839. question_answer.question_id = question.id
  840. question_answer.template_answer_id = template_answer.id
  841. question_answer.created_at = template_answer.created_at
  842.  
  843. # giving weight to highest weight answers
  844. if (rand(100 / percentage_random_answer) != 1)
  845. h = question.list_possible_answers_with_weights
  846. answer = Hash[h.sort_by{ |_, v| -v }].to_a[rand(1)].first
  847. else
  848. answer = question.list_possible_answers.sample
  849. end
  850. question_answer.answer = answer
  851.  
  852. # calculating answer weight
  853. question_answer.weight = question.weigh_answer(question_answer)
  854. template_answer.total_weight += question.weight
  855. template_answer.answer_weight += question_answer.weight
  856. end # of question answer
  857. template_answer.score_percentage = template_answer.total_weight == 0 ? 0 : (template_answer.answer_weight.to_f / template_answer.total_weight.to_f * 100)
  858. end
  859.  
  860. def create_calibration(debug, template_answer, call, template, audtior, second_auditor)
  861. # create first evaluation
  862. template_answer = create_evaluation(debug, template_answer, call, template, audtior)
  863.  
  864. # create second evaluation
  865. TemplateAnswer.populate 1 do |other_template_answer|
  866. other_template_answer = create_evaluation(debug, other_template_answer, call, template, second_auditor)
  867. end
  868.  
  869. return template_answer
  870. end
  871.  
  872. def create_double_evaluations(debug, template_answer, call, template, audtior, second_template, second_auditor)
  873. # create first evaluation
  874. template_answer = create_evaluation(debug, template_answer, call, template, audtior)
  875.  
  876. # create second evaluation
  877. TemplateAnswer.populate 1 do |other_template_answer|
  878. other_template_answer = create_evaluation(debug, other_template_answer, call, second_template, second_auditor)
  879. end
  880.  
  881. return template_answer
  882. end
  883.  
  884. def create_type_of_evaluation(debug, template_answer, call, templates)
  885. percentage_calibration = 10
  886. percentage_double_evaluation = 50
  887.  
  888. # choose the call's user supervisor or pick random supervisors
  889. supervisor = call.user.supervisor
  890.  
  891. # pick a random template, their number is limited
  892. template = templates.sample
  893.  
  894. # flip coins to create calibration
  895. template_answer = if supervisor.group.users.count > 1 && rand(100 / percentage_calibration) == 1
  896. puts "creating calibration".colorize(:green) if debug
  897. puts "supervisor(#{supervisor.id}) template(#{template.id})".colorize(:cyan) if debug
  898.  
  899. second_supervisor = supervisor.group.users.where("users.id not in ('#{supervisor.id}')").all.sample
  900. puts "2nd supervisor(#{second_supervisor.id})".colorize(:cyan) if debug
  901.  
  902. create_calibration(debug, template_answer, call, template, supervisor, second_supervisor)
  903.  
  904. # end of calibration
  905.  
  906. # flip coins to create double evaluations
  907. elsif rand(100 / percentage_double_evaluation) == 1
  908. puts "creating double evaluations".colorize(:green) if debug
  909. puts "supervisor(#{supervisor.id}) template(#{template.id})".colorize(:cyan) if debug
  910.  
  911. # pick random supervisors
  912. second_supervisor = User.supervisors.where("users.id not in ('#{supervisor.id}')").all.sample
  913.  
  914. # pick a random template, their number is limited
  915. second_template = templates.select{|t| t.id != template.id}.sample
  916.  
  917. puts "2nd supervisor(#{second_supervisor.id}) 2nd template(#{second_template.try(:id)})".colorize(:cyan) if debug
  918.  
  919. create_double_evaluations(debug, template_answer, call, template, supervisor, second_template, second_supervisor)
  920.  
  921. # end of doeble evaluations
  922.  
  923. # then create a single evaluations
  924. else
  925. puts "creating evaluation".colorize(:green) if debug
  926. puts "supervisor(#{supervisor.id}) template(#{template.id})".colorize(:cyan) if debug
  927.  
  928. create_evaluation(debug, template_answer, call, template, supervisor)
  929.  
  930. end # of calibration and evaluation
  931.  
  932. end
  933.  
  934. def adjust_average_score(debug)
  935. min_score_percentage = 40
  936. avg_score_percentage = 80
  937.  
  938. puts "Created #{TemplateAnswer.count} evaluations".colorize(:red) if debug
  939. puts "Adjusting the average percentage with score average to #{TemplateAnswer.average(:score_percentage)}".colorize(:green) if debug
  940. puts "#{TemplateAnswer.where('score_percentage < 50').count} evals < 50%, #{TemplateAnswer.where('score_percentage > 50').count} evals > 50%, #{TemplateAnswer.where('score_percentage > 80').count} evals > 80%".colorize(:green) if debug
  941.  
  942. # git rid of the low score evaluations
  943. TemplateAnswer.where("score_percentage < #{min_score_percentage}").destroy_all
  944.  
  945. # delete randomly from each percentile
  946. iterations = (avg_score_percentage - min_score_percentage) / 10
  947. low = min_score_percentage - 10
  948. (1..iterations).each do |index|
  949. low += 10
  950. high = low + 10
  951. evals = TemplateAnswer.where("score_percentage > #{low} and score_percentage < #{high}")
  952. # remove heavily from low percentages
  953. evals.limit(evals.count/10*(9-index+1)).destroy_all
  954. end
  955.  
  956. puts "Remaining #{TemplateAnswer.count} evaluations after adjusting score average to #{TemplateAnswer.average(:score_percentage)}".colorize(:cyan) if debug
  957. end
  958.  
  959. end # of namespace fake
  960. end # of checking on Faker existence
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement