From b170312d8e8cf6530ce1207d7489d866b0c687d1 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 9 Sep 2015 08:49:08 +0200 Subject: [PATCH] Added reopen dashboard stats. --- app/models/observer/ticket/stats_reopen.rb | 22 ++++++++++++ app/models/stats_store.rb | 2 +- config/application.rb | 1 + lib/stats/ticket_reopen.rb | 41 +++++++++++++++------- 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 app/models/observer/ticket/stats_reopen.rb diff --git a/app/models/observer/ticket/stats_reopen.rb b/app/models/observer/ticket/stats_reopen.rb new file mode 100644 index 000000000..d08af23ed --- /dev/null +++ b/app/models/observer/ticket/stats_reopen.rb @@ -0,0 +1,22 @@ +# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ +require 'stats/ticket_reopen' +class Observer::Ticket::StatsReopen < ActiveRecord::Observer + observe 'ticket' + + def after_create(record) + _check(record) + end + + def after_update(record) + _check(record) + end + + private + + def _check(record) + + # return if we run import mode + return if Setting.get('import_mode') + Stats::TicketReopen.log('Ticket', record.id, record.changes, record.updated_by_id) + end +end diff --git a/app/models/stats_store.rb b/app/models/stats_store.rb index 992664a89..cd3363d3d 100644 --- a/app/models/stats_store.rb +++ b/app/models/stats_store.rb @@ -23,7 +23,7 @@ class StatsStore < ApplicationModel end StatsStore.where(stats_store_object_id: object_id, o_id: data[:o_id], key: data[:key]) - .where('created_at < ? AND created_at > ?', data[:start], data[:end]).count + .where('created_at > ? AND created_at < ?', data[:start], data[:end]).count end =begin diff --git a/config/application.rb b/config/application.rb index a6086ae7e..59384439b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -39,6 +39,7 @@ module Zammad 'observer::_ticket::_escalation_calculation', 'observer::_ticket::_ref_object_touch', 'observer::_ticket::_online_notification_seen', + 'observer::_ticket::_stats_reopen', 'observer::_tag::_ticket_history', 'observer::_user::_ref_object_touch', 'observer::_user::_ticket_organization', diff --git a/lib/stats/ticket_reopen.rb b/lib/stats/ticket_reopen.rb index a75e16b96..395d9e178 100644 --- a/lib/stats/ticket_reopen.rb +++ b/lib/stats/ticket_reopen.rb @@ -3,6 +3,14 @@ class Stats::TicketReopen def self.generate(user) + + # get my closed tickets + total = Ticket.select('id').where( + 'owner_id = ? AND close_time > ?', + user.id, Time.zone.now - 7.days + ).count + + # get count of repoens count = StatsStore.count_by_search( object: 'User', o_id: user.id, @@ -10,14 +18,22 @@ class Stats::TicketReopen start: Time.zone.now - 7.days, end: Time.zone.now, ) + + if count > total + total = count + end + + reopen_in_precent = 0 + if total != 0 + reopen_in_precent = ( count.to_f / (total.to_f / 100) ).round(3) + end { - used_for_average: 0, + used_for_average: reopen_in_precent, + percent: reopen_in_precent, average_per_agent: '-', state: 'good', - own: count, - total: 0, - percent: 0, - its_me: true, + count: count, + total: total, } end @@ -25,24 +41,25 @@ class Stats::TicketReopen return result if !result.key?(:used_for_average) - if result[:total] < 1 + if result[:total] < 1 || result[:average_per_agent] == 0.0 result[:state] = 'supergood' return result end - in_percent = ( result[:used_for_average].to_f / (result[:total].to_f/100) ).round(1) + #in_percent = ( result[:used_for_average].to_f / (result[:average_per_agent].to_f / 100) ).round(1) + #result[:average_per_agent_in_percent] = in_percent + in_percent = ( result[:count].to_f / (result[:total].to_f / 100) ).round(1) if in_percent >= 90 - result[:state] = 'supergood' + result[:state] = 'superbad' elsif in_percent >= 65 - result[:state] = 'good' + result[:state] = 'bad' elsif in_percent >= 40 result[:state] = 'ok' elsif in_percent >= 20 - result[:state] = 'bad' + result[:state] = 'good' else - result[:state] = 'superbad' + result[:state] = 'supergood' end - result end