base.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. class Report::Base
  2. # :object
  3. # :type created|updated
  4. # :attribute
  5. # :value_from
  6. # :value_to
  7. # :start
  8. # :end
  9. # :selector
  10. def self.history_count(params)
  11. history_object = History::Object.lookup( name: params[:object] )
  12. query, bind_params, tables = Ticket.selector2sql(params[:selector])
  13. # created
  14. if params[:type] == 'created'
  15. history_type = History::Type.lookup( name: 'created' )
  16. return History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  17. .where(
  18. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ?', params[:start], params[:end], history_object.id, history_type.id
  19. )
  20. .where(query, *bind_params).joins(tables).count
  21. end
  22. # updated
  23. if params[:type] == 'updated'
  24. history_type = History::Type.lookup( name: 'updated' )
  25. history_attribute = History::Attribute.lookup( name: params[:attribute] )
  26. result = nil
  27. if !history_attribute || !history_type
  28. result = 0
  29. elsif params[:id_not_from] && params[:id_to]
  30. result = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  31. .where(query, *bind_params).joins(tables)
  32. .where(
  33. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.id_from NOT IN (?) AND histories.id_to IN (?)',
  34. params[:start],
  35. params[:end],
  36. history_object.id,
  37. history_type.id,
  38. history_attribute.id,
  39. params[:id_not_from],
  40. params[:id_to],
  41. ).count
  42. elsif params[:id_from] && params[:id_not_to]
  43. result = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  44. .where(query, *bind_params).joins(tables)
  45. .where(
  46. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.id_from IN (?) AND histories.id_to NOT IN (?)',
  47. params[:start],
  48. params[:end],
  49. history_object.id,
  50. history_type.id,
  51. history_attribute.id,
  52. params[:id_from],
  53. params[:id_not_to],
  54. ).count
  55. elsif params[:value_from] && params[:value_not_to]
  56. result = History.joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  57. .where(query, *bind_params).joins(tables)
  58. .where(
  59. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.value_from IN (?) AND histories.value_to NOT IN (?)',
  60. params[:start],
  61. params[:end],
  62. history_object.id,
  63. history_type.id,
  64. history_attribute.id,
  65. params[:value_from],
  66. params[:value_not_to],
  67. ).count
  68. elsif params[:value_to]
  69. result = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  70. .where(query, *bind_params).joins(tables)
  71. .where(
  72. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.value_to IN (?)',
  73. params[:start],
  74. params[:end],
  75. history_object.id,
  76. history_type.id,
  77. history_attribute.id,
  78. params[:value_to],
  79. ).count
  80. elsif params[:id_to]
  81. result = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  82. .where(query, *bind_params).joins(tables)
  83. .where(
  84. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.id_to IN (?)',
  85. params[:start],
  86. params[:end],
  87. history_object.id,
  88. history_type.id,
  89. history_attribute.id,
  90. params[:id_to],
  91. ).count
  92. end
  93. return result if !result.nil?
  94. raise "UNKOWN params (#{params.inspect})!"
  95. end
  96. raise "UNKOWN :type (#{params[:type]})!"
  97. end
  98. # :object
  99. # :type created|updated
  100. # :attribute
  101. # :value_from
  102. # :value_to
  103. # :start
  104. # :end
  105. # :condition
  106. def self.history(data)
  107. history_object = History::Object.lookup( name: data[:object] )
  108. query, bind_params, tables = Ticket.selector2sql(data[:selector])
  109. count = 0
  110. ticket_ids = []
  111. # created
  112. if data[:type] == 'created'
  113. history_type = History::Type.lookup( name: 'created' )
  114. histories = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  115. .where(
  116. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ?', data[:start], data[:end], history_object.id, history_type.id
  117. )
  118. .where(query, *bind_params).joins(tables)
  119. histories.each do |history|
  120. count += 1
  121. ticket_ids.push history.o_id
  122. end
  123. return {
  124. count: count,
  125. ticket_ids: ticket_ids,
  126. }
  127. end
  128. # updated
  129. if data[:type] == 'updated'
  130. history_type = History::Type.lookup( name: 'updated' )
  131. history_attribute = History::Attribute.lookup( name: data[:attribute] )
  132. if !history_attribute || !history_type
  133. count = 0
  134. else
  135. if data[:id_not_from] && data[:id_to]
  136. histories = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  137. .where(query, *bind_params).joins(tables)
  138. .where(
  139. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.id_from NOT IN (?) AND histories.id_to IN (?)',
  140. data[:start],
  141. data[:end],
  142. history_object.id,
  143. history_type.id,
  144. history_attribute.id,
  145. data[:id_not_from],
  146. data[:id_to],
  147. )
  148. elsif data[:id_from] && data[:id_not_to]
  149. histories = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  150. .where(query, *bind_params).joins(tables)
  151. .where(
  152. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.id_from IN (?) AND histories.id_to NOT IN (?)',
  153. data[:start],
  154. data[:end],
  155. history_object.id,
  156. history_type.id,
  157. history_attribute.id,
  158. data[:id_from],
  159. data[:id_not_to],
  160. )
  161. elsif data[:value_from] && data[:value_not_to]
  162. histories = History.joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  163. .where(query, *bind_params).joins(tables)
  164. .where(
  165. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.value_from IN (?) AND histories.value_to NOT IN (?)',
  166. data[:start],
  167. data[:end],
  168. history_object.id,
  169. history_type.id,
  170. history_attribute.id,
  171. data[:value_from],
  172. data[:value_not_to],
  173. )
  174. elsif data[:value_to]
  175. histories = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  176. .where(query, *bind_params).joins(tables)
  177. .where(
  178. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.value_to IN (?)',
  179. data[:start],
  180. data[:end],
  181. history_object.id,
  182. history_type.id,
  183. history_attribute.id,
  184. data[:value_to],
  185. )
  186. elsif data[:id_to]
  187. histories = History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  188. .where(query, *bind_params).joins(tables)
  189. .where(
  190. 'histories.created_at >= ? AND histories.created_at <= ? AND histories.history_object_id = ? AND histories.history_type_id = ? AND histories.history_attribute_id IN (?) AND histories.id_to IN (?)',
  191. data[:start],
  192. data[:end],
  193. history_object.id,
  194. history_type.id,
  195. history_attribute.id,
  196. data[:id_to],
  197. )
  198. end
  199. histories.each do |history|
  200. count += 1
  201. ticket_ids.push history.o_id
  202. end
  203. end
  204. return {
  205. count: count,
  206. ticket_ids: ticket_ids,
  207. }
  208. end
  209. raise "UNKOWN :type (#{data[:type]})!"
  210. end
  211. # :type
  212. # :start
  213. # :end
  214. # :condition
  215. def self.time_average(data)
  216. query, bind_params, tables = Ticket.selector2sql(data[:condition])
  217. ticket_list = Ticket.where( 'tickets.created_at >= ? AND tickets.created_at <= ?', data[:start], data[:end] )
  218. .where(query, *bind_params).joins(tables)
  219. tickets = 0
  220. time_total = 0
  221. ticket_list.each do |ticket|
  222. timestamp = ticket[ data[:type].to_sym ]
  223. next if !timestamp
  224. # puts 'FR:' + first_response.to_s
  225. # puts 'CT:' + ticket.created_at.to_s
  226. diff = timestamp - ticket.created_at
  227. #puts 'DIFF:' + diff.to_s
  228. time_total = time_total + diff
  229. tickets += 1
  230. end
  231. if time_total.zero? || tickets.zero?
  232. tickets = -0.001
  233. else
  234. tickets = time_total / tickets / 60
  235. tickets = tickets.to_i
  236. end
  237. {
  238. count: tickets,
  239. }
  240. end
  241. # :type
  242. # :start
  243. # :end
  244. # :condition
  245. def self.time_min(data)
  246. query, bind_params, tables = Ticket.selector2sql(data[:condition])
  247. ticket_list = Ticket.where( 'tickets.created_at >= ? AND tickets.created_at <= ?', data[:start], data[:end] )
  248. .where(query, *bind_params).joins(tables)
  249. tickets = 0
  250. time_min = 0
  251. ticket_ids = []
  252. ticket_list.each do |ticket|
  253. timestamp = ticket[ data[:type].to_sym ]
  254. next if !timestamp
  255. ticket_ids.push ticket.id
  256. # puts 'FR:' + first_response.to_s
  257. # puts 'CT:' + ticket.created_at.to_s
  258. diff = timestamp - ticket.created_at
  259. #puts 'DIFF:' + diff.to_s
  260. if !time_min
  261. time_min = diff
  262. end
  263. if diff < time_min
  264. time_min = diff
  265. end
  266. end
  267. tickets = if time_min.zero?
  268. -0.001
  269. else
  270. (time_min / 60).to_i
  271. end
  272. {
  273. count: tickets,
  274. ticket_ids: ticket_ids,
  275. }
  276. end
  277. # :type
  278. # :start
  279. # :end
  280. # :condition
  281. def self.time_max(data)
  282. query, bind_params, tables = Ticket.selector2sql(data[:condition])
  283. ticket_list = Ticket.where( 'tickets.created_at >= ? AND tickets.created_at <= ?', data[:start], data[:end] )
  284. .where(query, *bind_params).joins(tables)
  285. tickets = 0
  286. time_max = 0
  287. ticket_ids = []
  288. ticket_list.each do |ticket|
  289. timestamp = ticket[ data[:type].to_sym ]
  290. next if !timestamp
  291. ticket_ids.push ticket.id
  292. # puts "#{data[:type].to_s} - #{timestamp} - #{ticket.inspect}"
  293. # puts 'FR:' + ticket.first_response.to_s
  294. # puts 'CT:' + ticket.created_at.to_s
  295. diff = timestamp - ticket.created_at
  296. #puts 'DIFF:' + diff.to_s
  297. if !time_max
  298. time_max = diff
  299. end
  300. if diff > time_max
  301. time_max = diff
  302. end
  303. end
  304. tickets = if time_max.zero?
  305. -0.001
  306. else
  307. (time_max / 60).to_i
  308. end
  309. {
  310. count: tickets,
  311. ticket_ids: ticket_ids,
  312. }
  313. end
  314. def self.ticket_condition(ticket_id, condition)
  315. ticket = Ticket.lookup( id: ticket_id )
  316. match = true
  317. condition.each do |key, value|
  318. if ticket[key.to_sym] != value
  319. return false
  320. end
  321. end
  322. true
  323. end
  324. end