8000 Add Workspace statistics · senthgit/twilio-java@ef418a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef418a4

Browse files
AlexPaymentskimbrel
authored andcommitted
Add Workspace statistics
1 parent dd5b1ea commit ef418a4

File tree

8 files changed

+403
-0
lines changed

8 files changed

+403
-0
lines changed

src/main/java/com/twilio/sdk/TwilioWdsClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.twilio.sdk.resource.instance.wds.Workflow;
1818
import com.twilio.sdk.resource.instance.wds.WorkflowStatistics;
1919
import com.twilio.sdk.resource.instance.wds.Workspace;
20+
import com.twilio.sdk.resource.instance.wds.WorkspaceStatistics;
2021
import com.twilio.sdk.resource.list.wds.ActivityList;
2122
import com.twilio.sdk.resource.list.wds.QueueListStatistics;
2223
import com.twilio.sdk.resource.list.wds.ReservationList;
@@ -573,4 +574,27 @@ public WorkspaceList getWorkspaces(final Map<String, String> filters) {
573574
return list;
574575
}
575576

577+
/**
578+
* Get workspace statistics.
579+
*
580+
* @param workspaceSid The 34 character sid starting with WS
581+
* @return queues statistics
582+
*/
583+
public WorkspaceStatistics getWorkspaceStatistics(final String workspaceSid) {
584+
return getWorkspaceStatistics(workspaceSid, null);
585+
}
586+
587+
/**
588+
* Get workspace statistics.
589+
*
590+
* @param workspaceSid The 34 character sid starting with WS
591+
* @param filters the filters
592+
* @return queues statistics
593+
*/
594+
public WorkspaceStatistics getWorkspaceStatistics(final String workspaceSid, final Map<String, String> filters) {
595+
WorkspaceStatistics workspaceStatistics = new WorkspaceStatistics(this, workspaceSid, filters);
596+
workspaceStatistics.setRequestAccountSid(getAccountSid());
597+
return workspaceStatistics;
598+
}
599+
576600
}

src/main/java/com/twilio/sdk/resource/instance/wds/QueueStatistics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public QueueStatistics(final TwilioWdsClient client, final String workspaceSid,
4242
* @param client the client
4343
* @param workspaceSid the workspace sid
4444
* @param queueSid the queue sid
45+
* @param filters the filters
4546
*/
4647
public QueueStatistics(final TwilioWdsClient client, final String workspaceSid, final String queueSid,
4748
final Map<String, String> filters) {

src/main/java/com/twilio/sdk/resource/instance/wds/WorkerStatistics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public WorkerStatistics(final TwilioWdsClient client, final String workspaceSid,
3838
* @param client the client
3939
* @param workspaceSid the workspace sid
4040
* @param workerSid the worker sid
41+
* @param filters the filters
4142
*/
4243
public WorkerStatistics(final TwilioWdsClient client, final String workspaceSid, final String workerSid,
4344
final Map<String, String> filters) {

src/main/java/com/twilio/sdk/resource/instance/wds/WorkersStatistics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public WorkersStatistics(final TwilioWdsClient client, final String workspaceSid
3636
*
3737
* @param client the client
3838
* @param workspaceSid the workspace sid
39+
* @param filters the filters
3940
*/
4041
public WorkersStatistics(final TwilioWdsClient client, final String workspaceSid,
4142
final Map<String, String> filters) {

src/main/java/com/twilio/sdk/resource/instance/wds/WorkflowStatistics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public WorkflowStatistics(final TwilioWdsClient client, final String workspaceSi
3838
* @param client the client
3939
* @param workspaceSid the workspace sid
4040
* @param workflowSid the workflow sid
41+
* @param filters the filters
4142
*/
4243
public WorkflowStatistics(final TwilioWdsClient client, final String workspaceSid, final String workflowSid,
4344
final Map<String, String> filters) {
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
package com.twilio.sdk.resource.instance.wds;
2+
3+
import com.twilio.sdk.TwilioWdsClient;
4+
import com.twilio.sdk.resource.InstanceResource;
5+
6+
import java.util.Collections;
7+
import java.util.Date;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
/**
14+
* Statistics about {@link com.twilio.sdk.resource.instance.wds.Workspace}
15+
*/
16+
public class WorkspaceStatistics extends InstanceResource<TwilioWdsClient> {
17+
18+
private static final String CUMULATIVE_PROPERTY = "cumulative";
19+
20+
private static final String REALTIME_PROPERTY = "realtime";
21+
22+
private static final String TASKS_BY_STATUS_PROPERTY = "tasks_by_status";
23+
24+
private static final String WORKSPACE_SID_PROPERTY = "workspace_sid";
25+
26+
/**
27+
* Instantiates a workspace statistics.
28+
*
29+
* @param client the client
30+
* @param workspaceSid the workspace sid
31+
*/
32+
public WorkspaceStatistics(final TwilioWdsClient client, final String workspaceSid) {
33+
this(client, workspaceSid, null);
34+
}
35+
36+
/**
37+
* Instantiates a workspace statistics.
38+
*
39+
* @param client the client
40+
* @param workspaceSid the workspace sid
41+
* @param filters the filters
42+
*/
43+
public WorkspaceStatistics(final TwilioWdsClient client, final String workspaceSid,
44+
final Map<String, String> filters) {
45+
super(client);
46+
if (workspaceSid == null || "".equals(workspaceSid)) {
47+
throw new IllegalArgumentException("The workspaceSid for a WorkspaceStatistics cannot be null");
48+
}
49+
setProperty(WORKSPACE_SID_PROPERTY, workspaceSid);
50+
this.filters = filters;
51+
}
52+
53+
/**
54+
* Get the activity statistics.
55+
*
56+
* @return the activity statistics
57+
*/
58+
public Set<ActivityStatistic> getActivityStatistics() {
59+
try {
60+
List<Map<String, Object>> props = (List<Map<String, Object>>) getRealtime().get("activity_statistics");
61+
62+
Set<ActivityStatistic> activityStatistics = new HashSet<ActivityStatistic>();
63+
64+
for (Map<String, Object> prop : props) {
65+
ActivityStatistic activityStatistic = mapToActivityStatistic(prop);
66+
activityStatistics.add(activityStatistic);
67+
}
68+
69+
return Collections.unmodifiableSet(activityStatistics);
70+
} catch (IllegalArgumentException e) {
71+
return null;
72+
}
73+
}
74+
75+
/**
76+
* Get the number of assigned tasks.
77+
*
78+
* @return number of assigned tasks
79+
*/
80+
public Integer getAssignedTasks() {
81+
Map<String, Object> tasksByStatus = (Map<String, Object>) getRealtime().get(TASKS_BY_STATUS_PROPERTY);
82+
return (Integer) tasksByStatus.get("assigned");
83+
}
84+
85+
/**
86+
* Get the average time of task acceptance in seconds.
87+
*
88+
* @return the average time of task acceptance in seconds
89+
*/
90+
public Double getAverageTaskAcceptanceTime() {
91+
try {
92+
Object prop = getCumulative().get("avg_task_acceptance_time");
93+
if (prop instanceof Integer) {
94+
return Double.parseDouble(prop.toString());
95+
} else {
96+
return (Double) prop;
97+
}
98+
} catch (IllegalArgumentException e) {
99+
return null;
100+
}
101+
}
102+
103+
/**
104+
* Get the end time.
105+
*
106+
* @return the end time
107+
*/
108+
public Date getEndTime() {
109+
return parseDate((String) getCumulative().get("start_time"));
110+
}
111+
112+
/**
113+
* Get the longest task waiting age in seconds.
114+
*
115+
* @return the longest task waiting age in seconds
116+
*/
117+
public Integer getLongestTaskWaitingAge() {
118+
return (Integer) getRealtime().get("longest_task_waiting_age");
119+
}
120+
121+
/**
122+
* Get the sid of the longest waiting task.
123+
*
124+
* @return the sid of the longest waiting task
125+
*/
126+
public String getLongestTaskWaitingSid() {
127+
return (String) getRealtime().get("longest_task_waiting_sid");
128+
}
129+
130+
/**
131+
* Get the number of pending tasks.
132+
*
133+
* @return the number of pending tasks
134+
*/
135+
public Integer getPendingTasks() {
136+
Map<String, Object> tasksByStatus = (Map<String, Object>) getRealtime().get(TASKS_BY_STATUS_PROPERTY);
137+
return (Integer) tasksByStatus.get("pending");
138+
}
139+
140+
/**
141+
* Get the number of accepted reservations.
142+
*
143+
* @return the number of accepted reservations
144+
*/
145+
public Integer getReservationsAccepted() {
146+
return (Integer) getCumulative().get("reservations_accepted");
147+
}
148+
149+
/**
150+
* Get the number of rejected reservations.
151+
*
152+
* @return the number of rejected reservations
153+
*/
154+
public Integer getReservationsRejected() {
155+
return (Integer) getCumulative().get("reservations_rejected");
156+
}
157+
158+
/**
159+
* Get the number of timed out reservations.
160+
*
161+
* @return the number of timed out reservations
162+
*/
163+
public Integer getReservationsTimedOut() {
164+
return (Integer) getCumulative().get("reservations_timed_out");
165+
}
166+
167+
/**
168+
* Get the number of reserved tasks.
169+
*
170+
* @return the number of reserved tasks
171+
*/
172+
public Integer getReservedTasks() {
173+
Map<String, Object> tasksByStatus = (Map<String, Object>) getRealtime().get(TASKS_BY_STATUS_PROPERTY);
174+
return (Integer) tasksByStatus.get("reserved");
175+
}
176+
177+
/**
178+
* Get the start time.
179+
*
180+
* @return the start time
181+
*/
182+
public Date getStartTime() {
183+
return parseDate((String) getCumulative().get("start_time"));
184+
}
185+
186+
/**
187+
* Get the number of tasks canceled.
188+
*
189+
* @return the number of tasks canceled
190+
*/
191+
public Integer getTasksCanceled() {
192+
return (Integer) getCumulative().get("tasks_canceled");
193+
}
194+
195+
/**
196+
* Get the number of tasks created.
197+
*
198+
* @return the number of tasks created
199+
*/
200+
public Integer getTasksCreated() {
201+
return (Integer) getCumulative().get("tasks_created");
202+
}
203+
204+
/**
205+
* Get the number of tasks moved.
206+
*
207+
* @return the number of tasks moved
208+
*/
209+
public Integer getTasksMoved() {
210+
return (Integer) getCumulative().get("tasks_moved");
211+
}
212+
213+
/**
214+
* Get the number of tasks that timed out in the workflow.
215+
*
216+
* @return the number of tasks that timed out in the workflow
217+
*/
218+
public Integer getTasksTimedOutInWorkflow() {
219+
return (Integer) getCumulative().get("tasks_timed_out_in_workflow");
220+
}
221+
222+
/**
223+
* Get the total number of tasks.
224+
*
225+
* @return the total number of tasks
226+
*/
227+
public Integer getTotalTasks() {
228+
return (Integer) getRealtime().get("total_tasks");
229+
}
230+
231+
/**
232+
* Get the total number of workers.
233+
*
234+
* @return the total number of workers
235+
*/
236+
public Integer getTotalWorkers() {
237+
return (Integer) getRealtime().get("total_workers");
238+
}
239+
240+
/**
241+
* Gets the workspace sid.
242+
*
243+
* @return the workspace sid
244+
*/
245+
public String getWorkspaceSid() {
246+
return getProperty(WORKSPACE_SID_PROPERTY);
247+
}
248+
249+
@Override
250+
protected String getResourceLocation() {
251+
return "/" + TwilioWdsClient.DEFAULT_VERSION + "/Accounts/" + getRequestAccountSid() + "/Workspaces/" +
252+
getWorkspaceSid() + "Statistics";
253+
}
254+
255+
private Map<String, Object> getCumulative() {
256+
return (Map<String, Object>) getObject(CUMULATIVE_PROPERTY);
257+
}
258+
259+
private Map<String, Object> getRealtime() {
260+
return (Map<String, Object>) getObject(REALTIME_PROPERTY);
261+
}
262+
263+
private ActivityStatistic mapToActivityStatistic(final Map<String, Object> data) {
264+
String sid;
265+
String friendlyName;
266+
Integer workers;
267+
268+
try {
269+
sid = (String) data.get(SID_PROPERTY);
270+
friendlyName = (String) data.get(ActivityStatistic.FRIENDLY_NAME_PROPERTY);
271+
workers = (Integer) data.get(ActivityStatistic.WORKERS_PROPERTY);
272+
} catch (Exception e) {
273+
throw new IllegalStateException("An Activity Statistic contained improperly formatted data.", e);
274+
}
275+
276+
return new ActivityStatistic(sid, friendlyName, workers);
277+
}
278+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.twilio.sdk.resource.instance.wds;
2+
3+
import com.twilio.sdk.resource.instance.BasicRequestTester;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotNull;
9+
import static org.junit.Assert.assertNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class WorkspaceStatisticsTest extends BasicRequestTester {
13+
14+
@Before
15+
public void setup() throws Exception {
16+
setExpectedServerContentType("application/json");
17+
setExpectedServerAnswer(
18+
"/" + getClass().getPackage().getName().replace(".", "/") + "/workspace_statistics.json");
19+
}
20+
21+
@Test
22+
public void testGetWorkspaceStatistics() throws Exception {
23+
setExpectedServerReturnCode(200);
24+
WorkspaceStatistics workspaceStatistics = wdsClient
25+
.getWorkspaceStatistics("WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
26+
assertNotNull(workspaceStatistics);
27+
assertNotNull(workspaceStatistics.getActivityStatistics());
28+
assertEquals(4, workspaceStatistics.getActivityStatistics().size());
29+
assertTrue(workspaceStatistics.getAssignedTasks() == 1);
30+
assertTrue(workspaceStatistics.getAverageTaskAcceptanceTime() == 0.0);
31+
assertNotNull(workspaceStatistics.getEndTime());
32+
assertTrue(workspaceStatistics.getLongestTaskWaitingAge() == 0);
33+
assertNull(workspaceStatistics.getLongestTaskWaitingSid());
34+
assertTrue(workspaceStatistics.getPendingTasks() == 0);
35+
assertTrue(workspaceStatistics.getReservationsAccepted() == 0);
36+
assertTrue(workspaceStatistics.getReservationsRejected() == 0);
37+
assertTrue(workspaceStatistics.getReservationsTimedOut() == 0);
38+
assertTrue(workspaceStatistics.getReservedTasks() == 0);
39+
assertNotNull(workspaceStatistics.getStartTime());
40+
assertTrue(workspaceStatistics.getTasksCanceled() == 0);
41+
assertTrue(workspaceStatistics.getTasksCreated() == 0);
42+
assertTrue(workspaceStatistics.getTasksMoved() == 0);
43+
assertTrue(workspaceStatistics.getTasksTimedOutInWorkflow() == 0);
44+
assertTrue(workspaceStatistics.getTotalTasks() == 1);
45+
assertTrue(workspaceStatistics.getTotalWorkers() == 3);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)
0