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

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

There is a newer version: 7.0.0-rc-10
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 workers.
 * See the TaskRouter documentation.
 */
public class WorkersStatistics extends NextGenInstanceResource {

	private static final String CUMULATIVE_PROPERTY = "cumulative";

	private static final String REALTIME_PROPERTY = "realtime";

	private static final String WORKSPACE_SID_PROPERTY = "workspace_sid";

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

	/**
	 * Instantiates a worker statistics.
	 *
	 * @param client the client
	 * @param workspaceSid the workspace sid
	 * @param filters the filters
	 */
	public WorkersStatistics(final TwilioTaskRouterClient client, final String workspaceSid,
	                         final Map filters) {
		super(client);
		if (StringUtils.isBlank(workspaceSid)) {
			throw new IllegalArgumentException("The workspaceSid for a WorkerStatistics cannot be null");
		}
		setProperty(WORKSPACE_SID_PROPERTY, workspaceSid);
		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 activity statistics.
	 *
	 * @return the activity statistics
	 */
	public Set getActivityStatistics() {
		try {
			List> props = (List>) getRealtime().get("activity_statistics");

			Set activityStatistics = new HashSet();

			for (Map prop : props) {
				ActivityStatistic activityStatistic = mapToActivityStatistic(prop);
				activityStatistics.add(activityStatistic);
			}

			return Collections.unmodifiableSet(activityStatistics);
		} 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");
	}

	/**
	 * Get the number of workers.
	 *
	 * @return the number of workers
	 */
	public Integer getTotalWorkers() {
		return (Integer) getRealtime().get("total_workers");
	}

	/**
	 * 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/Statistics";
	}

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

	private Map getRealtime() {
		return (Map) getObject(REALTIME_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);
	}

	private ActivityStatistic mapToActivityStatistic(final Map data) {
		String sid;
		String friendlyName;
		Integer workers;

		try {
			sid = (String) data.get(SID_PROPERTY);
			friendlyName = (String) data.get(ActivityStatistic.FRIENDLY_NAME_PROPERTY);
			workers = (Integer) data.get(ActivityStatistic.WORKERS_PROPERTY);
		} catch (Exception e) {
			throw new IllegalStateException("An Activity Statistic contained improperly formatted data.", e);
		}

		return new ActivityStatistic(sid, friendlyName, workers);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy