|
| 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 | +} |
0 commit comments