report_spec.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Report', searchindex: true, type: :request 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. travel_to today.midday
  45. Ticket.destroy_all
  46. create(:ticket, title: 'ticket for report #1', created_at: today.midday)
  47. create(:ticket, title: 'ticket for report #2', created_at: today.midday + 2.hours)
  48. create(:ticket, title: 'ticket for report #3', created_at: today.midday + 2.hours)
  49. create(:ticket, title: 'ticket for report #4', created_at: today.midday + 10.hours, state: Ticket::State.lookup(name: 'closed'))
  50. create(:ticket, title: 'ticket for report #5', created_at: today.midday + 11.hours)
  51. create(:ticket, title: 'ticket for report #6', created_at: today.midday - 11.hours)
  52. create(:ticket, title: 'ticket for report #7', created_at: Time.zone.parse('2019-02-28T23:30:00Z'))
  53. create(:ticket, title: 'ticket for report #8', created_at: Time.zone.parse('2019-03-01T00:30:00Z'))
  54. create(:ticket, title: 'ticket for report #9', created_at: Time.zone.parse('2019-03-31T23:30:00Z'))
  55. create(:ticket, title: 'ticket for report #10', created_at: Time.zone.parse('2019-04-01T00:30:00Z'))
  56. searchindex_model_reload([Ticket, User])
  57. end
  58. describe 'request handling' do
  59. it 'does report example - admin access' do
  60. authenticated_as(admin)
  61. 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
  62. expect(response).to have_http_status(:ok)
  63. assert(response['Content-Disposition'])
  64. expect(response['Content-Disposition']).to eq('attachment; filename="tickets--all--Created.xlsx"; filename*=UTF-8\'\'tickets--all--Created.xlsx')
  65. expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
  66. end
  67. it 'does report example - deliver result' do
  68. skip('No ES configured') if !SearchIndexBackend.enabled?
  69. authenticated_as(admin)
  70. # 2019-03-15 - day interval
  71. params = {
  72. metric: 'count',
  73. year: today.year,
  74. month: today.month,
  75. day: today.day,
  76. timeRange: 'day',
  77. profiles: {
  78. 1 => true
  79. },
  80. backends: backends
  81. }
  82. post '/api/v1/reports/generate', params: params, as: :json
  83. expect(response).to have_http_status(:ok)
  84. 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])
  85. 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])
  86. 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])
  87. Setting.set('timezone_default', 'Europe/Berlin')
  88. post '/api/v1/reports/generate', params: params, as: :json
  89. expect(response).to have_http_status(:ok)
  90. 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])
  91. 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])
  92. 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])
  93. Setting.set('timezone_default', 'America/Chicago')
  94. post '/api/v1/reports/generate', params: params, as: :json
  95. expect(response).to have_http_status(:ok)
  96. 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])
  97. 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])
  98. 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])
  99. Setting.set('timezone_default', 'Australia/Melbourne')
  100. post '/api/v1/reports/generate', params: params, as: :json
  101. expect(response).to have_http_status(:ok)
  102. 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])
  103. 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])
  104. 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])
  105. # 2019-03 - month interval
  106. Setting.set('timezone_default', 'UTC')
  107. params = {
  108. metric: 'count',
  109. year: today.year,
  110. month: today.month,
  111. timeRange: 'month',
  112. profiles: {
  113. 1 => true
  114. },
  115. backends: backends
  116. }
  117. post '/api/v1/reports/generate', params: params, as: :json
  118. expect(response).to have_http_status(:ok)
  119. 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])
  120. 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])
  121. 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])
  122. Setting.set('timezone_default', 'Europe/Berlin')
  123. post '/api/v1/reports/generate', params: params, as: :json
  124. expect(response).to have_http_status(:ok)
  125. 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])
  126. 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])
  127. 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])
  128. Setting.set('timezone_default', 'America/Chicago')
  129. post '/api/v1/reports/generate', params: params, as: :json
  130. expect(response).to have_http_status(:ok)
  131. 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])
  132. 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])
  133. 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])
  134. Setting.set('timezone_default', 'Australia/Melbourne')
  135. post '/api/v1/reports/generate', params: params, as: :json
  136. 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])
  137. 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])
  138. 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])
  139. # 2019-02 - month interval
  140. Setting.set('timezone_default', 'UTC')
  141. params = {
  142. metric: 'count',
  143. year: today.year,
  144. month: today.month - 1,
  145. timeRange: 'month',
  146. profiles: {
  147. 1 => true
  148. },
  149. backends: backends
  150. }
  151. post '/api/v1/reports/generate', params: params, as: :json
  152. expect(response).to have_http_status(:ok)
  153. 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])
  154. 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])
  155. 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])
  156. Setting.set('timezone_default', 'Europe/Berlin')
  157. post '/api/v1/reports/generate', params: params, as: :json
  158. 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])
  159. 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])
  160. 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])
  161. Setting.set('timezone_default', 'America/Chicago')
  162. post '/api/v1/reports/generate', params: params, as: :json
  163. expect(response).to have_http_status(:ok)
  164. 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])
  165. 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])
  166. 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])
  167. Setting.set('timezone_default', 'Australia/Melbourne')
  168. post '/api/v1/reports/generate', params: params, as: :json
  169. 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])
  170. 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])
  171. 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])
  172. # 2019-04 - month interval
  173. Setting.set('timezone_default', 'UTC')
  174. params = {
  175. metric: 'count',
  176. year: today.year,
  177. month: today.month + 1,
  178. timeRange: 'month',
  179. profiles: {
  180. 1 => true
  181. },
  182. backends: backends
  183. }
  184. post '/api/v1/reports/generate', params: params, as: :json
  185. expect(response).to have_http_status(:ok)
  186. 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])
  187. 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])
  188. 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])
  189. Setting.set('timezone_default', 'Europe/Berlin')
  190. post '/api/v1/reports/generate', params: params, as: :json
  191. 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])
  192. 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])
  193. 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])
  194. Setting.set('timezone_default', 'America/Chicago')
  195. post '/api/v1/reports/generate', params: params, as: :json
  196. expect(response).to have_http_status(:ok)
  197. 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])
  198. 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])
  199. 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])
  200. Setting.set('timezone_default', 'Australia/Melbourne')
  201. post '/api/v1/reports/generate', params: params, as: :json
  202. 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])
  203. 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])
  204. 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])
  205. # 2019 - year interval
  206. Setting.set('timezone_default', 'UTC')
  207. params = {
  208. metric: 'count',
  209. year: today.year,
  210. timeRange: 'year',
  211. profiles: {
  212. 1 => true
  213. },
  214. backends: backends
  215. }
  216. post '/api/v1/reports/generate', params: params, as: :json
  217. expect(response).to have_http_status(:ok)
  218. expect(json_response['data']['count::created']).to eq([0, 1, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  219. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  220. expect(json_response['data']['count::backlog']).to eq([0, 1, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  221. Setting.set('timezone_default', 'Europe/Berlin')
  222. post '/api/v1/reports/generate', params: params, as: :json
  223. expect(response).to have_http_status(:ok)
  224. expect(json_response['data']['count::created']).to eq([0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  225. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  226. expect(json_response['data']['count::backlog']).to eq([0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  227. Setting.set('timezone_default', 'America/Chicago')
  228. post '/api/v1/reports/generate', params: params, as: :json
  229. expect(response).to have_http_status(:ok)
  230. expect(json_response['data']['count::created']).to eq([0, 2, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  231. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  232. expect(json_response['data']['count::backlog']).to eq([0, 2, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  233. Setting.set('timezone_default', 'Australia/Melbourne')
  234. post '/api/v1/reports/generate', params: params, as: :json
  235. expect(response).to have_http_status(:ok)
  236. expect(json_response['data']['count::created']).to eq([0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  237. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  238. expect(json_response['data']['count::backlog']).to eq([0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  239. end
  240. end
  241. end