Skip to content

Commit

Permalink
Fixes #4924 - No possibility to export merged tickets in reports.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas committed May 26, 2024
1 parent bf20565 commit d85e573
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 8 deletions.
7 changes: 7 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def self.config
adapter: Report::TicketMoved,
params: { type: 'out' }
},
{
name: 'merged',
display: __('Merged'),
selected: false,
dataDownload: true,
adapter: Report::TicketMerged,
},
]
config[:metric][:count][:backend] = backend

Expand Down
4 changes: 4 additions & 0 deletions i18n/zammad.pot
Original file line number Diff line number Diff line change
Expand Up @@ -7778,6 +7778,10 @@ msgstr ""
msgid "Merge to Ticket#"
msgstr ""

#: app/models/report.rb
msgid "Merged"
msgstr ""

#: app/assets/javascripts/app/controllers/_channel/sms.coffee
#: app/assets/javascripts/app/views/maintenance.jst.eco
#: public/assets/form/form.js
Expand Down
2 changes: 1 addition & 1 deletion lib/report/article_by_type_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Report::ArticleByTypeSender < Report::Base
=end

def self.aggs(params_origin)
params = params_origin.dup
params = params_origin.deep_dup

result = []
case params[:interval]
Expand Down
18 changes: 17 additions & 1 deletion lib/report/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Report::Base
# :end
# :selector
def self.history_count(params)

history_object = History::Object.lookup(name: params[:object])

query, bind_params, tables = Ticket.selector2sql(params[:selector])
Expand Down Expand Up @@ -342,4 +341,21 @@ def self.ticket_condition(ticket_id, condition)
true
end

INTERVAL_LENGTH = {
month: 12,
week: 7,
# day: 31, Day is counted bellow by given month/year
hour: 24,
minute: 60,
}.with_indifferent_access

def self.interval_length(params)
interval = params[:interval]

if interval == 'day'
return Time.days_in_month params[:range_start].month, params[:range_start].year
end

INTERVAL_LENGTH[interval]
end
end
2 changes: 1 addition & 1 deletion lib/report/ticket_backlog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Report::TicketBacklog < Report::Base
=end

def self.aggs(params_origin)
params = params_origin.dup
params = params_origin.deep_dup

local_params = params.clone
local_params[:params] = {}
Expand Down
2 changes: 1 addition & 1 deletion lib/report/ticket_first_solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Report::TicketFirstSolution
=end

def self.aggs(params_origin)
params = params_origin.dup
params = params_origin.deep_dup

result = []
case params[:interval]
Expand Down
2 changes: 1 addition & 1 deletion lib/report/ticket_generic_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Report::TicketGenericTime
=end

def self.aggs(params_origin)
params = params_origin.dup
params = params_origin.deep_dup
interval_es = params[:interval]
if params[:interval] == 'week'
interval_es = 'day'
Expand Down
82 changes: 82 additions & 0 deletions lib/report/ticket_merged.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/

class Report::TicketMerged < Report::Base

# Returns amount of merged tickets in a given time range
# sliced by given time interval.
#
# @example
#
# result = Report::TicketMerged.aggs(
# range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
# range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
# interval: 'month', # month, week, day, hour, minute, second
# selector: selector, # ticket selector to get only a collection of tickets
# timezone: 'Europe/Berlin',
# )
# #=> [4,5,1,5,0,51,5,56,7,4]
#
# @return [Array<Integer>]
def self.aggs(params_origin)
params = params_origin.deep_dup

Array.new(interval_length(params)) do |_counter|
case params[:interval]
when 'month'
params[:range_end] = params[:range_start].next_month
when 'week', 'day'
params[:range_end] = params[:range_start].next_day
when 'hour'
params[:range_end] = params[:range_start] + 1.hour
when 'minute'
params[:range_end] = params[:range_start] + 1.minute
end

count = history_count(query_params(params))

params[:range_start] = params[:range_end]

count
end
end

# Returns merged tickets in a given time range matching the selector
#
# @example
#
# result = Report::TicketMerged.items(
# range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
# range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
# selector: selector, # ticket selector to get only a collection of tickets
# )
#
# #=> {
# count: 123,
# ticket_ids: [4,5,1,5,0,51,5,56,7,4],
# assets: assets,
# }
#
# @return [Hash]
def self.items(params)
result = history(query_params(params))

if params[:sheet].blank?
result[:assets] = ApplicationModel::CanAssets
.reduce(Ticket.where(id: result[:ticket_ids]), {})
end

result
end

def self.query_params(params)
{
object: 'Ticket',
type: 'updated',
attribute: 'state',
start: params[:range_start],
end: params[:range_end],
selector: params[:selector],
id_to: Ticket::State.lookup(name: 'merged').id,
}
end
end
2 changes: 1 addition & 1 deletion lib/report/ticket_moved.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Report::TicketMoved < Report::Base
=end

def self.aggs(params_origin)
params = params_origin.dup
params = params_origin.deep_dup

selector = params[:selector]['ticket.group_id']

Expand Down
2 changes: 1 addition & 1 deletion lib/report/ticket_reopened.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Report::TicketReopened < Report::Base
=end

def self.aggs(params_origin)
params = params_origin.dup
params = params_origin.deep_dup
ticket_state_ids = ticket_ids

result = []
Expand Down
47 changes: 47 additions & 0 deletions spec/lib/report/ticket_merged_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'lib/report_examples'

RSpec.describe Report::TicketMerged, searchindex: true do
include_examples 'with report examples'

describe '.aggs' do
it 'gets monthly aggregated results in merged state' do
result = described_class.aggs(
range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
interval: 'month',
selector: {},
)

expect(result).to eq [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
end

it 'gets daily aggregated results in merged state' do
result = described_class.aggs(
range_start: Time.zone.parse('2015-11-01T00:00:00Z'),
range_end: Time.zone.parse('2015-12-01T00:00:00Z'),
interval: 'day',
selector: {},
)

expected = Array.new(30, 0) # 30 days in November
expected[1] = 1 # ticket exists on November 2nd

expect(result).to eq expected
end
end

describe '.items' do
it 'gets items in year range in merged state' do
result = described_class.items(
range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
selector: {},
)

expect(result).to match_tickets ticket_8
end
end
end
10 changes: 9 additions & 1 deletion spec/lib/report_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,18 @@
ticket = create(:ticket,
group: group_1,
customer: customer,
state_name: 'merged',
state_name: 'open',
priority_name: '2 normal',
close_at: Time.zone.now)

create(:ticket_article,
:inbound_email,
ticket: ticket)

travel 10.minutes

ticket.update!(state: Ticket::State.lookup(name: 'merged'))

travel_back
ticket
end
Expand All @@ -177,6 +181,10 @@
:inbound_email,
ticket: ticket)

travel 10.minutes

ticket.update!(state: Ticket::State.lookup(name: 'merged'))

travel_back

ticket
Expand Down

0 comments on commit d85e573

Please sign in to comment.