report_spec.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. require 'rails_helper'
  2. RSpec.describe 'Report', type: :request, searchindex: true do
  3. let!(:admin_user) do
  4. create(:admin_user)
  5. end
  6. let!(:year) do
  7. DateTime.now.utc.year
  8. end
  9. let!(:month) do
  10. DateTime.now.utc.month
  11. end
  12. let!(:week) do
  13. DateTime.now.utc.strftime('%U').to_i
  14. end
  15. let!(:day) do
  16. DateTime.now.utc.day
  17. end
  18. let!(:today) do
  19. Time.zone.parse('2019-03-15T08:00:00Z')
  20. end
  21. let!(:backends) do
  22. {
  23. 'count::created': true,
  24. 'count::closed': true,
  25. 'count::backlog': true,
  26. 'create_channels::phone_in': true,
  27. 'create_channels::phone_out': true,
  28. 'create_channels::email_in': true,
  29. 'create_channels::email_out': true,
  30. 'create_channels::web_in': true,
  31. 'create_channels::twitter_in': true,
  32. 'create_channels::twitter_out': true,
  33. 'communication::phone_in': true,
  34. 'communication::phone_out': true,
  35. 'communication::email_in': true,
  36. 'communication::email_out': true,
  37. 'communication::web_in': true,
  38. 'communication::twitter_in': true,
  39. 'communication::twitter_out': true
  40. }
  41. end
  42. before do
  43. configure_elasticsearch do
  44. travel 1.minute
  45. travel_to today.midday
  46. Ticket.destroy_all
  47. create(:ticket, title: 'ticket for report #1', created_at: today.midday)
  48. create(:ticket, title: 'ticket for report #2', created_at: today.midday + 2.hours)
  49. create(:ticket, title: 'ticket for report #3', created_at: today.midday + 2.hours)
  50. create(:ticket, title: 'ticket for report #4', created_at: today.midday + 10.hours, state: Ticket::State.lookup(name: 'closed') )
  51. create(:ticket, title: 'ticket for report #5', created_at: today.midday + 11.hours)
  52. create(:ticket, title: 'ticket for report #6', created_at: today.midday - 11.hours)
  53. create(:ticket, title: 'ticket for report #7', created_at: Time.zone.parse('2019-02-28T23:30:00Z'))
  54. create(:ticket, title: 'ticket for report #8', created_at: Time.zone.parse('2019-03-01T00:30:00Z'))
  55. create(:ticket, title: 'ticket for report #9', created_at: Time.zone.parse('2019-03-31T23:30:00Z'))
  56. create(:ticket, title: 'ticket for report #10', created_at: Time.zone.parse('2019-04-01T00:30:00Z'))
  57. rebuild_searchindex
  58. # execute background jobs
  59. Scheduler.worker(true)
  60. sleep 6
  61. end
  62. end
  63. describe 'request handling' do
  64. it 'does report example - admin access' do
  65. authenticated_as(admin_user)
  66. get "/api/v1/reports/sets?sheet=true;metric=count;year=#{year};month=#{month};week=#{week};day=#{day};timeRange=year;profile_id=1;downloadBackendSelected=count::created", params: {}, as: :json
  67. expect(response).to have_http_status(:ok)
  68. assert(@response['Content-Disposition'])
  69. expect(@response['Content-Disposition']).to eq('attachment; filename="tickets--all--Created.xls"')
  70. expect(@response['Content-Type']).to eq('application/vnd.ms-excel')
  71. end
  72. it 'does convert UTC timestamp to local system based timestamp' do
  73. expect(ReportsController.new.time_in_localtime_for_excel(Time.parse('2019-08-08T01:00:05Z').in_time_zone, 'Europe/Berlin')).to eq('2019-08-08 03:00:05')
  74. end
  75. it 'does report example - deliver result' do
  76. skip('No ES configured') if !SearchIndexBackend.enabled?
  77. authenticated_as(admin_user)
  78. # 2019-03-15 - day interval
  79. params = {
  80. metric: 'count',
  81. year: today.year,
  82. month: today.month,
  83. day: today.day,
  84. timeRange: 'day',
  85. profiles: {
  86. 1 => true
  87. },
  88. backends: backends
  89. }
  90. post '/api/v1/reports/generate', params: params, as: :json
  91. expect(response).to have_http_status(:ok)
  92. expect(json_response['data']['count::created']).to eq([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1])
  93. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  94. expect(json_response['data']['count::backlog']).to eq([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1])
  95. Setting.set('timezone_default', 'Europe/Berlin')
  96. post '/api/v1/reports/generate', params: params, as: :json
  97. expect(response).to have_http_status(:ok)
  98. expect(json_response['data']['count::created']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1])
  99. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  100. expect(json_response['data']['count::backlog']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1])
  101. Setting.set('timezone_default', 'America/Chicago')
  102. post '/api/v1/reports/generate', params: params, as: :json
  103. expect(response).to have_http_status(:ok)
  104. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0])
  105. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  106. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0])
  107. Setting.set('timezone_default', 'Australia/Melbourne')
  108. post '/api/v1/reports/generate', params: params, as: :json
  109. expect(response).to have_http_status(:ok)
  110. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  111. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  112. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  113. # 2019-03 - month interval
  114. Setting.set('timezone_default', '')
  115. params = {
  116. metric: 'count',
  117. year: today.year,
  118. month: today.month,
  119. timeRange: 'month',
  120. profiles: {
  121. 1 => true
  122. },
  123. backends: backends
  124. }
  125. post '/api/v1/reports/generate', params: params, as: :json
  126. expect(response).to have_http_status(:ok)
  127. expect(json_response['data']['count::created']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  128. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  129. expect(json_response['data']['count::backlog']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  130. Setting.set('timezone_default', 'Europe/Berlin')
  131. post '/api/v1/reports/generate', params: params, as: :json
  132. expect(response).to have_http_status(:ok)
  133. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  134. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  135. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  136. Setting.set('timezone_default', 'America/Chicago')
  137. post '/api/v1/reports/generate', params: params, as: :json
  138. expect(response).to have_http_status(:ok)
  139. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  140. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  141. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  142. Setting.set('timezone_default', 'Australia/Melbourne')
  143. post '/api/v1/reports/generate', params: params, as: :json
  144. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  145. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  146. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  147. # 2019-02 - month interval
  148. Setting.set('timezone_default', '')
  149. params = {
  150. metric: 'count',
  151. year: today.year,
  152. month: today.month - 1,
  153. timeRange: 'month',
  154. profiles: {
  155. 1 => true
  156. },
  157. backends: backends
  158. }
  159. post '/api/v1/reports/generate', params: params, as: :json
  160. expect(response).to have_http_status(:ok)
  161. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  162. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  163. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  164. Setting.set('timezone_default', 'Europe/Berlin')
  165. post '/api/v1/reports/generate', params: params, as: :json
  166. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  167. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  168. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  169. Setting.set('timezone_default', 'America/Chicago')
  170. post '/api/v1/reports/generate', params: params, as: :json
  171. expect(response).to have_http_status(:ok)
  172. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  173. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  174. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  175. Setting.set('timezone_default', 'Australia/Melbourne')
  176. post '/api/v1/reports/generate', params: params, as: :json
  177. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  178. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  179. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  180. # 2019-04 - month interval
  181. Setting.set('timezone_default', '')
  182. params = {
  183. metric: 'count',
  184. year: today.year,
  185. month: today.month + 1,
  186. timeRange: 'month',
  187. profiles: {
  188. 1 => true
  189. },
  190. backends: backends
  191. }
  192. post '/api/v1/reports/generate', params: params, as: :json
  193. expect(response).to have_http_status(:ok)
  194. expect(json_response['data']['count::created']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  195. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  196. expect(json_response['data']['count::backlog']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  197. Setting.set('timezone_default', 'Europe/Berlin')
  198. post '/api/v1/reports/generate', params: params, as: :json
  199. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  200. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  201. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  202. Setting.set('timezone_default', 'America/Chicago')
  203. post '/api/v1/reports/generate', params: params, as: :json
  204. expect(response).to have_http_status(:ok)
  205. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  206. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  207. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  208. Setting.set('timezone_default', 'Australia/Melbourne')
  209. post '/api/v1/reports/generate', params: params, as: :json
  210. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  211. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  212. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  213. # 2019 - year interval
  214. Setting.set('timezone_default', '')
  215. params = {
  216. metric: 'count',
  217. year: today.year,
  218. timeRange: 'year',
  219. profiles: {
  220. 1 => true
  221. },
  222. backends: backends
  223. }
  224. post '/api/v1/reports/generate', params: params, as: :json
  225. expect(response).to have_http_status(:ok)
  226. expect(json_response['data']['count::created']).to eq([0, 1, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  227. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  228. expect(json_response['data']['count::backlog']).to eq([0, 1, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  229. Setting.set('timezone_default', 'Europe/Berlin')
  230. post '/api/v1/reports/generate', params: params, as: :json
  231. expect(response).to have_http_status(:ok)
  232. expect(json_response['data']['count::created']).to eq([0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  233. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  234. expect(json_response['data']['count::backlog']).to eq([0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  235. Setting.set('timezone_default', 'America/Chicago')
  236. post '/api/v1/reports/generate', params: params, as: :json
  237. expect(response).to have_http_status(:ok)
  238. expect(json_response['data']['count::created']).to eq([0, 2, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  239. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  240. expect(json_response['data']['count::backlog']).to eq([0, 2, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  241. Setting.set('timezone_default', 'Australia/Melbourne')
  242. post '/api/v1/reports/generate', params: params, as: :json
  243. expect(response).to have_http_status(:ok)
  244. expect(json_response['data']['count::created']).to eq([0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  245. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  246. expect(json_response['data']['count::backlog']).to eq([0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  247. end
  248. end
  249. end