diff --git a/app/assets/javascripts/app/controllers/widget/ticket_stats.coffee b/app/assets/javascripts/app/controllers/widget/ticket_stats.coffee
index 3e78ef825..7445e8294 100644
--- a/app/assets/javascripts/app/controllers/widget/ticket_stats.coffee
+++ b/app/assets/javascripts/app/controllers/widget/ticket_stats.coffee
@@ -71,63 +71,72 @@ class App.TicketStats extends App.Controller
if !data
data = @data
+ user_total = 0
+ if data.user.open_ids && data.user.closed_ids
+ user_total = data.user.open_ids.length + data.user.closed_ids.length
+ organization_total = 0
+ if data.organization.open_ids && data.organization.closed_ids
+ organization_total = data.organization.open_ids.length + data.organization.closed_ids.length
+
@html App.view('widget/ticket_stats')(
- user: @user
- user_total: data.user_tickets_open_ids.length + data.user_tickets_closed_ids.length
- organization: @organization
- org_total: data.org_tickets_open_ids.length + data.org_tickets_closed_ids.length
+ user: @user
+ user_total: user_total
+ organization: @organization
+ organization_total: organization_total
)
limit = 5
- iconClass = ''
- if data.user_tickets_open_ids.length is 0 && data.user_tickets_closed_ids.length > 0
- iconClass = 'mood icon supergood-color'
- new TicketStatsList(
- el: @$('.js-user-open-tickets')
- user: @user
- head: 'Open Tickets'
- iconClass: iconClass
- ticket_ids: data.user_tickets_open_ids
- limit: limit
- )
- new TicketStatsList(
- el: @$('.js-user-closed-tickets')
- user: @user
- head: 'Closed Tickets'
- ticket_ids: data.user_tickets_closed_ids
- limit: limit
- )
- new TicketStatsFrequency(
- el: @$('.js-user-frequency')
- user: @user
- ticket_volume_by_year: data.user_ticket_volume_by_year
- )
+ if !_.isEmpty(data.user)
+ iconClass = ''
+ if data.user.open_ids.length is 0 && data.user.closed_ids.length > 0
+ iconClass = 'mood icon supergood-color'
+ new App.TicketStatsList(
+ el: @$('.js-user-open-tickets')
+ user: @user
+ head: 'Open Tickets'
+ iconClass: iconClass
+ ticket_ids: data.user.open_ids
+ limit: limit
+ )
+ new App.TicketStatsList(
+ el: @$('.js-user-closed-tickets')
+ user: @user
+ head: 'Closed Tickets'
+ ticket_ids: data.user.closed_ids
+ limit: limit
+ )
+ new App.TicketStatsFrequency(
+ el: @$('.js-user-frequency')
+ user: @user
+ ticket_volume_by_year: data.user.volume_by_year
+ )
- iconClass = ''
- if data.org_tickets_open_ids.length is 0 && data.org_tickets_closed_ids.length > 0
- iconClass = 'mood icon supergood-color'
- new TicketStatsList(
- el: @$('.js-org-open-tickets')
- user: @user
- head: 'Open Tickets'
- iconClass: iconClass
- ticket_ids: data.org_tickets_open_ids
- limit: limit
- )
- new TicketStatsList(
- el: @$('.js-org-closed-tickets')
- user: @user
- head: 'Closed Tickets'
- ticket_ids: data.org_tickets_closed_ids
- limit: limit
- )
- new TicketStatsFrequency(
- el: @$('.js-org-frequency')
- user: @user
- ticket_volume_by_year: data.org_ticket_volume_by_year
- )
+ if !_.isEmpty(data.organization)
+ iconClass = ''
+ if data.organization.open_ids.length is 0 && data.organization.closed_ids.length > 0
+ iconClass = 'mood icon supergood-color'
+ new App.TicketStatsList(
+ el: @$('.js-org-open-tickets')
+ user: @user
+ head: 'Open Tickets'
+ iconClass: iconClass
+ ticket_ids: data.organization.open_ids
+ limit: limit
+ )
+ new App.TicketStatsList(
+ el: @$('.js-org-closed-tickets')
+ user: @user
+ head: 'Closed Tickets'
+ ticket_ids: data.organization.closed_ids
+ limit: limit
+ )
+ new App.TicketStatsFrequency(
+ el: @$('.js-org-frequency')
+ user: @user
+ ticket_volume_by_year: data.organization.volume_by_year
+ )
-class TicketStatsList extends App.Controller
+class App.TicketStatsList extends App.Controller
events:
'click .js-showAll': 'showAll'
@@ -163,7 +172,7 @@ class TicketStatsList extends App.Controller
@all = true
@render()
-class TicketStatsFrequency extends App.Controller
+class App.TicketStatsFrequency extends App.Controller
constructor: ->
super
@render()
diff --git a/app/assets/javascripts/app/views/widget/ticket_stats.jst.eco b/app/assets/javascripts/app/views/widget/ticket_stats.jst.eco
index 824583645..3ebcc82f0 100644
--- a/app/assets/javascripts/app/views/widget/ticket_stats.jst.eco
+++ b/app/assets/javascripts/app/views/widget/ticket_stats.jst.eco
@@ -1,7 +1,7 @@
<% if @user && @user.organization: %>
<%- @T('Tickets of User') %> <%- "(" + @user_total + ")" %>
-
<%- @T('Tickets of Organization') %> <%- "(" + @org_total + ")" %>
+
<%- @T('Tickets of Organization') %> <%- "(" + @organization_total + ")" %>
<% end %>
diff --git a/app/controllers/concerns/ticket_stats.rb b/app/controllers/concerns/ticket_stats.rb
new file mode 100644
index 000000000..4060dfbf3
--- /dev/null
+++ b/app/controllers/concerns/ticket_stats.rb
@@ -0,0 +1,57 @@
+module TicketStats
+ extend ActiveSupport::Concern
+
+ private
+
+ def ticket_ids_and_assets(condition, current_user, limit, assets)
+ tickets = Ticket.search(
+ limit: limit,
+ condition: condition,
+ current_user: current_user,
+ )
+ assets_of_tickets(tickets, assets)
+ end
+
+ def ticket_stats_last_year(condition, access_condition)
+ volume_by_year = []
+ now = Time.zone.now
+
+ (0..11).each { |month_back|
+ date_to_check = now - month_back.month
+ date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
+ date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
+
+ # created
+ created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end)
+ .where(access_condition)
+ .where(condition)
+ .count
+
+ # closed
+ closed = Ticket.where('close_at > ? AND close_at < ?', date_start, date_end)
+ .where(access_condition)
+ .where(condition)
+ .count
+
+ data = {
+ month: date_to_check.month,
+ year: date_to_check.year,
+ text: Date::MONTHNAMES[date_to_check.month],
+ created: created,
+ closed: closed,
+ }
+ volume_by_year.push data
+ }
+ volume_by_year
+ end
+
+ def assets_of_tickets(tickets, assets)
+ ticket_ids = []
+ tickets.each do |ticket|
+ ticket_ids.push ticket.id
+ assets = ticket.assets(assets)
+ end
+ ticket_ids
+ end
+
+end
diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb
index a4d99af88..511447c52 100644
--- a/app/controllers/tickets_controller.rb
+++ b/app/controllers/tickets_controller.rb
@@ -3,6 +3,7 @@
class TicketsController < ApplicationController
include AccessesTickets
include CreatesTicketArticles
+ include TicketStats
prepend_before_action :authentication_check
@@ -484,181 +485,101 @@ class TicketsController < ApplicationController
raise 'Need user_id or organization_id as param'
end
- # permission check
- #ticket_permission(ticket)
-
# lookup open user tickets
- limit = 100
- assets = {}
- access_condition = Ticket.access_condition(current_user)
- now = Time.zone.now
- user_tickets_open_ids = []
- user_tickets_closed_ids = []
- user_ticket_volume_by_year = []
+ limit = 100
+ assets = {}
+ access_condition = Ticket.access_condition(current_user)
+
+ user_tickets = {}
if params[:user_id]
user = User.lookup(id: params[:user_id])
if !user
raise "No such user with id #{params[:user_id]}"
end
- condition = {
- 'ticket.state_id' => {
- operator: 'is',
- value: Ticket::State.by_category(:open).pluck(:id),
+ conditions = {
+ closed_ids: {
+ 'ticket.state_id' => {
+ operator: 'is',
+ value: Ticket::State.by_category(:closed).pluck(:id),
+ },
+ 'ticket.customer_id' => {
+ operator: 'is',
+ value: user.id,
+ },
},
- 'ticket.customer_id' => {
- operator: 'is',
- value: user.id,
+ open_ids: {
+ 'ticket.state_id' => {
+ operator: 'is',
+ value: Ticket::State.by_category(:open).pluck(:id),
+ },
+ 'ticket.customer_id' => {
+ operator: 'is',
+ value: user.id,
+ },
},
}
- user_tickets_open = Ticket.search(
- limit: limit,
- condition: condition,
- current_user: current_user,
- )
- user_tickets_open_ids = assets_of_tickets(user_tickets_open, assets)
-
- # lookup closed user tickets
- condition = {
- 'ticket.state_id' => {
- operator: 'is',
- value: Ticket::State.by_category(:closed).pluck(:id),
- },
- 'ticket.customer_id' => {
- operator: 'is',
- value: user.id,
- },
+ conditions.each { |key, local_condition|
+ user_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
}
- user_tickets_closed = Ticket.search(
- limit: limit,
- condition: condition,
- current_user: current_user,
- )
- user_tickets_closed_ids = assets_of_tickets(user_tickets_closed, assets)
# generate stats by user
- (0..11).each { |month_back|
- date_to_check = now - month_back.month
- date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
- date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
-
- condition = {
- 'tickets.customer_id' => user.id,
- }
-
- # created
- created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end )
- .where(access_condition)
- .where(condition)
- .count
-
- # closed
- closed = Ticket.where('close_at > ? AND close_at < ?', date_start, date_end )
- .where(access_condition)
- .where(condition)
- .count
-
- data = {
- month: date_to_check.month,
- year: date_to_check.year,
- text: Date::MONTHNAMES[date_to_check.month],
- created: created,
- closed: closed,
- }
- user_ticket_volume_by_year.push data
+ condition = {
+ 'tickets.customer_id' => user.id,
}
+ user_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
+
end
# lookup open org tickets
- org_tickets_open_ids = []
- org_tickets_closed_ids = []
- org_ticket_volume_by_year = []
+ org_tickets = {}
if params[:organization_id] && !params[:organization_id].empty?
-
- condition = {
- 'ticket.state_id' => {
- operator: 'is',
- value: Ticket::State.by_category(:open).pluck(:id),
+ organization = Organization.lookup(id: params[:organization_id])
+ if !organization
+ raise "No such organization with id #{params[:organization_id]}"
+ end
+ conditions = {
+ closed_ids: {
+ 'ticket.state_id' => {
+ operator: 'is',
+ value: Ticket::State.by_category(:closed).pluck(:id),
+ },
+ 'ticket.organization_id' => {
+ operator: 'is',
+ value: organization.id,
+ },
},
- 'ticket.organization_id' => {
- operator: 'is',
- value: params[:organization_id],
+ open_ids: {
+ 'ticket.state_id' => {
+ operator: 'is',
+ value: Ticket::State.by_category(:open).pluck(:id),
+ },
+ 'ticket.organization_id' => {
+ operator: 'is',
+ value: organization.id,
+ },
},
}
- org_tickets_open = Ticket.search(
- limit: limit,
- condition: condition,
- current_user: current_user,
- )
- org_tickets_open_ids = assets_of_tickets(org_tickets_open, assets)
-
- # lookup closed org tickets
- condition = {
- 'ticket.state_id' => {
- operator: 'is',
- value: Ticket::State.by_category(:closed).pluck(:id),
- },
- 'ticket.organization_id' => {
- operator: 'is',
- value: params[:organization_id],
- },
+ conditions.each { |key, local_condition|
+ org_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
}
- org_tickets_closed = Ticket.search(
- limit: limit,
- condition: condition,
- current_user: current_user,
- )
- org_tickets_closed_ids = assets_of_tickets(org_tickets_closed, assets)
# generate stats by org
- (0..11).each { |month_back|
- date_to_check = now - month_back.month
- date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
- date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
-
- condition = {
- 'tickets.organization_id' => params[:organization_id],
- }
-
- # created
- created = Ticket.where('created_at > ? AND created_at < ?', date_start, date_end ).where(condition).count
-
- # closed
- closed = Ticket.where('close_at > ? AND close_at < ?', date_start, date_end ).where(condition).count
-
- data = {
- month: date_to_check.month,
- year: date_to_check.year,
- text: Date::MONTHNAMES[date_to_check.month],
- created: created,
- closed: closed,
- }
- org_ticket_volume_by_year.push data
+ condition = {
+ 'tickets.organization_id' => organization.id,
}
+ org_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
end
# return result
render json: {
- user_tickets_open_ids: user_tickets_open_ids,
- user_tickets_closed_ids: user_tickets_closed_ids,
- org_tickets_open_ids: org_tickets_open_ids,
- org_tickets_closed_ids: org_tickets_closed_ids,
- user_ticket_volume_by_year: user_ticket_volume_by_year,
- org_ticket_volume_by_year: org_ticket_volume_by_year,
+ user: user_tickets,
+ organization: org_tickets,
assets: assets,
}
end
private
- def assets_of_tickets(tickets, assets)
- ticket_ids = []
- tickets.each do |ticket|
- ticket_ids.push ticket.id
- assets = ticket.assets(assets)
- end
- ticket_ids
- end
-
def ticket_all(ticket)
# get attributes to update