Skip to content

Commit 53a1778

Browse files
committed
Support for multiple ticket pages
Code takes into account that there could be more than 100 tickets
1 parent 26fae58 commit 53a1778

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

GetZendesAgentStatsInGroup/GetZendeskAgentStatsInGroup.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using ZendeskApi_v2;
34
using Common;
5+
using ZendeskApi_v2.Models.Tickets;
46

57
namespace GetZendeskAgentStatsInGroup
68
{
@@ -43,13 +45,32 @@ private static void GetResult(long groupId)
4345
// Loop through each agent in group
4446
foreach (var agent in agentsInGroup)
4547
{
46-
// Get the agents tickets
47-
var agentTickets = _api.Tickets.GetTicketsByUserID((long)agent.Id).Tickets.Where(t => t.GroupId == groupId).ToList();
4848
string[] solvedStatus = { "closed", "solved" };
4949

50-
// Create a channel for this agent with the number of open tickets.
51-
var openTickets = agentTickets.Count(t => !solvedStatus.Contains(t.Status.ToLower()));
52-
_xml.AddChannel(agent.Name + "(Open)", openTickets.ToString());
50+
// Get the agents tickets
51+
if (agent.Id != null)
52+
{
53+
var ticketPage = _api.Tickets.GetTicketsByUserID((long) agent.Id);
54+
var agentTickets = ticketPage.Tickets.Where(t => t.GroupId == groupId).ToList();
55+
56+
// If more than 100 exist, import the rest as well
57+
while (!string.IsNullOrEmpty(ticketPage.NextPage))
58+
{
59+
ticketPage = _api.Tickets.GetByPageUrl<GroupTicketResponse>(ticketPage.NextPage);
60+
agentTickets.AddRange(ticketPage.Tickets.Where(t => t.GroupId == groupId).ToList());
61+
}
62+
63+
// Create a channel for this agent with the number of open tickets.
64+
var openTickets = agentTickets.Where(t => !solvedStatus.Contains(t.Status.ToLower())).ToList();
65+
var problems = openTickets.Count(t => t.Type.Equals("problem"));
66+
var incidents = openTickets.Count(t => t.Type.Equals("incident"));
67+
var tasks = openTickets.Count(t => t.Type.Equals("task"));
68+
69+
_xml.AddChannel(agent.Name + "(Problem)", problems.ToString());
70+
_xml.AddChannel(agent.Name + "(Incident)", incidents.ToString());
71+
_xml.AddChannel(agent.Name + "(Tasks)", tasks.ToString());
72+
_xml.AddChannel(agent.Name + "(Total open)", openTickets.Count.ToString());
73+
}
5374
}
5475

5576
// Output the finished XML.

GetZendeskGroupStats/GetZendeskGroupStats.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using ZendeskApi_v2;
8+
using ZendeskApi_v2.Models.Tickets;
89

910
namespace GetZendeskGroupStats
1011
{
@@ -101,8 +102,21 @@ private static void GetNumOnHOldTicketsInGroup()
101102
private static void GetResult(long groupId)
102103
{
103104
string[] unwantedStatuses = { "closed", "solved" };
104-
_allTicketsInGroup = _api.Tickets.GetAllTickets().Tickets.Where(t => t.GroupId == groupId).ToList();
105-
_allOpenTicketsInGroup = _allTicketsInGroup.Where(t => !unwantedStatuses.Contains(t.Status.ToLower())).ToList();
105+
string[] ticketTypes = {"problem", "incident"};
106+
107+
// Import first 100 tickets.
108+
var ticketPage = _api.Tickets.GetAllTickets();
109+
_allTicketsInGroup = new List<Ticket>();
110+
_allTicketsInGroup.AddRange(ticketPage.Tickets.Where(t => t.GroupId == groupId).ToList());
111+
112+
// If more than 100 exist, import the rest as well
113+
while (!string.IsNullOrEmpty(ticketPage.NextPage))
114+
{
115+
ticketPage = _api.Tickets.GetByPageUrl<GroupTicketResponse>(ticketPage.NextPage);
116+
_allTicketsInGroup.AddRange(ticketPage.Tickets.Where(t => t.GroupId == groupId).ToList());
117+
}
118+
119+
_allOpenTicketsInGroup = _allTicketsInGroup.Where(t => !unwantedStatuses.Contains(t.Status.ToLower()) && ticketTypes.Contains(t.Type.ToLower())).ToList();
106120

107121
// Get number of open tickets
108122
GetNumOpenTicketsInGroup();
@@ -156,10 +170,9 @@ private static void GetNumSolvedToday()
156170
/// </summary>
157171
private static void GetNewTicketsToday()
158172
{
173+
159174
var numNewTicketsToday = _allTicketsInGroup.Count(t => t.CreatedAt >= dayStartDateTimeOffset && t.CreatedAt <= DateTimeOffset.Now);
160175
_xml.AddChannel("New tickets today", numNewTicketsToday.ToString());
161176
}
162-
163-
164177
}
165178
}

0 commit comments

Comments
 (0)