Refactoring ticket stats of user and org profile.

This commit is contained in:
Martin Edenhofer 2017-04-10 10:06:11 +02:00
parent 5c94682caf
commit 058bd93fcb
4 changed files with 181 additions and 194 deletions

View file

@ -71,63 +71,72 @@ class App.TicketStats extends App.Controller
if !data if !data
data = @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')( @html App.view('widget/ticket_stats')(
user: @user user: @user
user_total: data.user_tickets_open_ids.length + data.user_tickets_closed_ids.length user_total: user_total
organization: @organization organization: @organization
org_total: data.org_tickets_open_ids.length + data.org_tickets_closed_ids.length organization_total: organization_total
) )
limit = 5 limit = 5
iconClass = '' if !_.isEmpty(data.user)
if data.user_tickets_open_ids.length is 0 && data.user_tickets_closed_ids.length > 0 iconClass = ''
iconClass = 'mood icon supergood-color' if data.user.open_ids.length is 0 && data.user.closed_ids.length > 0
new TicketStatsList( iconClass = 'mood icon supergood-color'
el: @$('.js-user-open-tickets') new App.TicketStatsList(
user: @user el: @$('.js-user-open-tickets')
head: 'Open Tickets' user: @user
iconClass: iconClass head: 'Open Tickets'
ticket_ids: data.user_tickets_open_ids iconClass: iconClass
limit: limit ticket_ids: data.user.open_ids
) limit: limit
new TicketStatsList( )
el: @$('.js-user-closed-tickets') new App.TicketStatsList(
user: @user el: @$('.js-user-closed-tickets')
head: 'Closed Tickets' user: @user
ticket_ids: data.user_tickets_closed_ids head: 'Closed Tickets'
limit: limit ticket_ids: data.user.closed_ids
) limit: limit
new TicketStatsFrequency( )
el: @$('.js-user-frequency') new App.TicketStatsFrequency(
user: @user el: @$('.js-user-frequency')
ticket_volume_by_year: data.user_ticket_volume_by_year user: @user
) ticket_volume_by_year: data.user.volume_by_year
)
iconClass = '' if !_.isEmpty(data.organization)
if data.org_tickets_open_ids.length is 0 && data.org_tickets_closed_ids.length > 0 iconClass = ''
iconClass = 'mood icon supergood-color' if data.organization.open_ids.length is 0 && data.organization.closed_ids.length > 0
new TicketStatsList( iconClass = 'mood icon supergood-color'
el: @$('.js-org-open-tickets') new App.TicketStatsList(
user: @user el: @$('.js-org-open-tickets')
head: 'Open Tickets' user: @user
iconClass: iconClass head: 'Open Tickets'
ticket_ids: data.org_tickets_open_ids iconClass: iconClass
limit: limit ticket_ids: data.organization.open_ids
) limit: limit
new TicketStatsList( )
el: @$('.js-org-closed-tickets') new App.TicketStatsList(
user: @user el: @$('.js-org-closed-tickets')
head: 'Closed Tickets' user: @user
ticket_ids: data.org_tickets_closed_ids head: 'Closed Tickets'
limit: limit ticket_ids: data.organization.closed_ids
) limit: limit
new TicketStatsFrequency( )
el: @$('.js-org-frequency') new App.TicketStatsFrequency(
user: @user el: @$('.js-org-frequency')
ticket_volume_by_year: data.org_ticket_volume_by_year user: @user
) ticket_volume_by_year: data.organization.volume_by_year
)
class TicketStatsList extends App.Controller class App.TicketStatsList extends App.Controller
events: events:
'click .js-showAll': 'showAll' 'click .js-showAll': 'showAll'
@ -163,7 +172,7 @@ class TicketStatsList extends App.Controller
@all = true @all = true
@render() @render()
class TicketStatsFrequency extends App.Controller class App.TicketStatsFrequency extends App.Controller
constructor: -> constructor: ->
super super
@render() @render()

View file

@ -1,7 +1,7 @@
<% if @user && @user.organization: %> <% if @user && @user.organization: %>
<div class="tabs tabs--big"> <div class="tabs tabs--big">
<div class="tab js-userTab active"><%- @T('Tickets of User') %> <%- "(" + @user_total + ")" %></div> <div class="tab js-userTab active"><%- @T('Tickets of User') %> <%- "(" + @user_total + ")" %></div>
<div class="tab js-orgTab"><%- @T('Tickets of Organization') %> <%- "(" + @org_total + ")" %></div> <div class="tab js-orgTab"><%- @T('Tickets of Organization') %> <%- "(" + @organization_total + ")" %></div>
</div> </div>
<% end %> <% end %>

View file

@ -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

View file

@ -3,6 +3,7 @@
class TicketsController < ApplicationController class TicketsController < ApplicationController
include AccessesTickets include AccessesTickets
include CreatesTicketArticles include CreatesTicketArticles
include TicketStats
prepend_before_action :authentication_check prepend_before_action :authentication_check
@ -484,181 +485,101 @@ class TicketsController < ApplicationController
raise 'Need user_id or organization_id as param' raise 'Need user_id or organization_id as param'
end end
# permission check
#ticket_permission(ticket)
# lookup open user tickets # lookup open user tickets
limit = 100 limit = 100
assets = {} assets = {}
access_condition = Ticket.access_condition(current_user) access_condition = Ticket.access_condition(current_user)
now = Time.zone.now
user_tickets_open_ids = [] user_tickets = {}
user_tickets_closed_ids = []
user_ticket_volume_by_year = []
if params[:user_id] if params[:user_id]
user = User.lookup(id: params[:user_id]) user = User.lookup(id: params[:user_id])
if !user if !user
raise "No such user with id #{params[:user_id]}" raise "No such user with id #{params[:user_id]}"
end end
condition = { conditions = {
'ticket.state_id' => { closed_ids: {
operator: 'is', 'ticket.state_id' => {
value: Ticket::State.by_category(:open).pluck(:id), operator: 'is',
value: Ticket::State.by_category(:closed).pluck(:id),
},
'ticket.customer_id' => {
operator: 'is',
value: user.id,
},
}, },
'ticket.customer_id' => { open_ids: {
operator: 'is', 'ticket.state_id' => {
value: user.id, operator: 'is',
value: Ticket::State.by_category(:open).pluck(:id),
},
'ticket.customer_id' => {
operator: 'is',
value: user.id,
},
}, },
} }
user_tickets_open = Ticket.search( conditions.each { |key, local_condition|
limit: limit, user_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
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,
},
} }
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 # generate stats by user
(0..11).each { |month_back| condition = {
date_to_check = now - month_back.month 'tickets.customer_id' => user.id,
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
} }
user_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
end end
# lookup open org tickets # lookup open org tickets
org_tickets_open_ids = [] org_tickets = {}
org_tickets_closed_ids = []
org_ticket_volume_by_year = []
if params[:organization_id] && !params[:organization_id].empty? if params[:organization_id] && !params[:organization_id].empty?
organization = Organization.lookup(id: params[:organization_id])
condition = { if !organization
'ticket.state_id' => { raise "No such organization with id #{params[:organization_id]}"
operator: 'is', end
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.organization_id' => {
operator: 'is',
value: organization.id,
},
}, },
'ticket.organization_id' => { open_ids: {
operator: 'is', 'ticket.state_id' => {
value: params[:organization_id], operator: 'is',
value: Ticket::State.by_category(:open).pluck(:id),
},
'ticket.organization_id' => {
operator: 'is',
value: organization.id,
},
}, },
} }
org_tickets_open = Ticket.search( conditions.each { |key, local_condition|
limit: limit, org_tickets[key] = ticket_ids_and_assets(local_condition, current_user, limit, assets)
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],
},
} }
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 # generate stats by org
(0..11).each { |month_back| condition = {
date_to_check = now - month_back.month 'tickets.organization_id' => organization.id,
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
} }
org_tickets[:volume_by_year] = ticket_stats_last_year(condition, access_condition)
end end
# return result # return result
render json: { render json: {
user_tickets_open_ids: user_tickets_open_ids, user: user_tickets,
user_tickets_closed_ids: user_tickets_closed_ids, organization: org_tickets,
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,
assets: assets, assets: assets,
} }
end end
private 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) def ticket_all(ticket)
# get attributes to update # get attributes to update