All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twilio.sdk.resource.instance.taskrouter.WorkerStatistics Maven / Gradle / Ivy

There is a newer version: 7.0.0-rc-7
Show newest version
package com.twilio.sdk.resource.instance.taskrouter;

import com.twilio.sdk.TwilioTaskRouterClient;
import com.twilio.sdk.resource.NextGenInstanceResource;

import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * Statistics about {@link com.twilio.sdk.resource.instance.taskrouter.Worker}.
 * See the TaskRouter documentation.
 */
public class WorkerStatistics extends NextGenInstanceResource {

	private static final String CUMULATIVE_PROPERTY = "cumulative";

	private static final String WORKER_SID_PROPERTY = "worker_sid";

	private static final String WORKSPACE_SID_PROPERTY = "workspace_sid";

	/**
	 * Instantiates a worker statistics.
	 *
	 * @param client the client
	 * @param workspaceSid the workspace sid
	 * @param workerSid the worker sid
	 */
	public WorkerStatistics(final TwilioTaskRouterClient client, final String workspaceSid, final String workerSid) {
		this(client, workspaceSid, workerSid, null);
	}

	/**
	 * Instantiates a worker statistics.
	 *
	 * @param client the client
	 * @param workspaceSid the workspace sid
	 * @param workerSid the worker sid
	 * @param filters the filters
	 */
	public WorkerStatistics(final TwilioTaskRouterClient client, final String workspaceSid, final String workerSid,
	                        final Map filters) {
		super(client);
		if (StringUtils.isBlank(workspaceSid)) {
			throw new IllegalArgumentException("The workspaceSid for a WorkerStatistics cannot be null");
		}
		if (StringUtils.isBlank(workerSid)) {
			throw new IllegalArgumentException("The workerSid for a WorkerStatistics cannot be null");
		}
		setProperty(WORKSPACE_SID_PROPERTY, workspaceSid);
		setProperty(WORKER_SID_PROPERTY, workerSid);
		this.filters = filters;
	}

	/**
	 * Get the activity durations.
	 *
	 * @return the activity durations
	 */
	public Set getActivityDurations() {
		try {
			List> props = (List>) getCumulative().get("activity_durations");

			Set activityDurations = new HashSet();

			for (Map prop : props) {
				ActivityDuration activityDuration = mapToActivityDuration(prop);
				activityDurations.add(activityDuration);
			}

			return Collections.unmodifiableSet(activityDurations);
		} catch (IllegalArgumentException e) {
			return null;
		}
	}

	/**
	 * Get the end time.
	 *
	 * @return the end time
	 */
	public Calendar getEndTime() {
		return parseCalendar((String) getCumulative().get("end_time"));
	}

	/**
	 * Get the number of accepted reservations.
	 *
	 * @return the number of accepted reservations
	 */
	public Integer getReservationsAccepted() {
		return (Integer) getCumulative().get("reservations_accepted");
	}

	/**
	 * Get the number of rejected reservations.
	 *
	 * @return the number of rejected reservations
	 */
	public Integer getReservationsRejected() {
		return (Integer) getCumulative().get("reservations_rejected");
	}

	/**
	 * Get the number of timed out reservations.
	 *
	 * @return the number of timed out reservations
	 */
	public Integer getReservationsTimedOut() {
		return (Integer) getCumulative().get("reservations_timed_out");
	}

	/**
	 * Get the start time.
	 *
	 * @return the start time
	 */
	public Calendar getStartTime() {
		return parseCalendar((String) getCumulative().get("start_time"));
	}

	/**
	 * Get the number of tasks assigned.
	 *
	 * @return the number of tasks assigned
	 */
	public Integer getTasksAssigned() {
		return (Integer) getCumulative().get("tasks_assigned");
	}

	/**
	 * Gets the worker's sid.
	 *
	 * @return the worker's sid
	 */
	public String getWorkerSid() {
		return getProperty(WORKER_SID_PROPERTY);
	}

	/**
	 * Gets the workspace sid.
	 *
	 * @return the workspace sid
	 */
	public String getWorkspaceSid() {
		return getProperty(WORKSPACE_SID_PROPERTY);
	}

	@Override
	protected String getResourceLocation() {
		return "/" + TwilioTaskRouterClient.DEFAULT_VERSION + "/Workspaces/" + getWorkspaceSid() + "/Workers/" +
		       getWorkerSid() + "/Statistics";
	}

	private Map getCumulative() {
		return (Map) getObject(CUMULATIVE_PROPERTY);
	}

	private ActivityDuration mapToActivityDuration(final Map data) {
		String sid;
		String friendlyName;
		Double average;
		Integer maximum;
		Integer minimum;

		try {
			sid = (String) data.get(SID_PROPERTY);
			friendlyName = (String) data.get(ActivityDuration.FRIENDLY_NAME_PROPERTY);
			maximum = (Integer) data.get(ActivityDuration.MAXIMUM_PROPERTY);
			minimum = (Integer) data.get(ActivityDuration.MINIMUM_PROPERTY);
			try {
				Object prop = getObject(ActivityDuration.AVERAGE_PROPERTY);
				if (prop instanceof Integer) {
					average = Double.parseDouble(prop.toString());
				} else {
					average = (Double) prop;
				}
			} catch (IllegalArgumentException e) {
				average = null;
			}
		} catch (Exception e) {
			throw new IllegalStateException("An Activity Duration contained improperly formatted data.", e);
		}

		return new ActivityDuration(sid, friendlyName, average, maximum, minimum);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy