report_spec.rb 15 KB

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