base.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. count = 0
  14. ticket_ids = []
  15. # created
  16. if params[:type] == 'created'
  17. history_type = History::Type.lookup( name: 'created' )
  18. return History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  19. .where(
  20. '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
  21. )
  22. .where(query, *bind_params).joins(tables).count
  23. end
  24. # updated
  25. if params[:type] == 'updated'
  26. history_type = History::Type.lookup( name: 'updated' )
  27. history_attribute = History::Attribute.lookup( name: params[:attribute] )
  28. if !history_attribute || !history_type
  29. count = 0
  30. elsif params[:id_not_from] && params[:id_to]
  31. return History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  32. .where(query, *bind_params).joins(tables)
  33. .where(
  34. '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 (?)',
  35. params[:start],
  36. params[:end],
  37. history_object.id,
  38. history_type.id,
  39. history_attribute.id,
  40. params[:id_not_from],
  41. params[:id_to],
  42. ).count
  43. elsif params[:id_from] && params[:id_not_to]
  44. return History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  45. .where(query, *bind_params).joins(tables)
  46. .where(
  47. '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 (?)',
  48. params[:start],
  49. params[:end],
  50. history_object.id,
  51. history_type.id,
  52. history_attribute.id,
  53. params[:id_from],
  54. params[:id_not_to],
  55. ).count
  56. elsif params[:value_from] && params[:value_not_to]
  57. return History.joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  58. .where(query, *bind_params).joins(tables)
  59. .where(
  60. '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 (?)',
  61. params[:start],
  62. params[:end],
  63. history_object.id,
  64. history_type.id,
  65. history_attribute.id,
  66. params[:value_from],
  67. params[:value_not_to],
  68. ).count
  69. elsif params[:value_to]
  70. return History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  71. .where(query, *bind_params).joins(tables)
  72. .where(
  73. '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 (?)',
  74. params[:start],
  75. params[:end],
  76. history_object.id,
  77. history_type.id,
  78. history_attribute.id,
  79. params[:value_to],
  80. ).count
  81. elsif params[:id_to]
  82. return History.select('histories.o_id').joins('INNER JOIN tickets ON tickets.id = histories.o_id')
  83. .where(query, *bind_params).joins(tables)
  84. .where(
  85. '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 (?)',
  86. params[:start],
  87. params[:end],
  88. history_object.id,
  89. history_type.id,
  90. history_attribute.id,
  91. params[:id_to],
  92. ).count
  93. end
  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 { |history|
  120. count += 1
  121. ticket_ids.push history.o_id
  122. }
  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 { |history|
  200. count += 1
  201. ticket_ids.push history.o_id
  202. }
  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 { |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. }
  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 { |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. }
  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 { |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. }
  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 { |key, value|
  318. if ticket[key.to_sym] != value
  319. return false
  320. end
  321. }
  322. true
  323. end
  324. end