/// <summary> /// Close an Event Cluster /// </summary> /// <param name="eventObj">EventClusterCloseModel</param> /// <returns>EventClusterModel</returns> public async Task <EventClusterModel> CloseEventCluster(EventClusterCloseModel eventObj) { EventClusterDAO eventCluster = await _repoEventClusters.GetItemAsync(eventObj.EventClusterId); eventCluster.ClosureDate = eventObj.ClosureDate; eventCluster.EndDate = eventObj.EndDate; try { await _repoEventClusters.UpdateItemAsync(eventCluster); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { await CloseEventCluster(eventObj); } throw e; } var output = _mapper.Map <EventClusterModel>(eventCluster); output.Events = output.Events.Take(3); return(output); }
/// <summary> /// Create an Event Cluster /// </summary> /// <param name="eventObj">EventClusterCreationModel</param> /// <returns>EventClusterModel</returns> public async Task <EventClusterModel> CreateEventCluster(EventClusterCreationModel eventObj) { //If device doesn't exist, throw exception DeviceDAO deviceEntity = await _repoDevices.GetItemAsync(eventObj.DeviceId); if (deviceEntity == null) { throw new Exception($"No device found that matches DeviceId: {eventObj.DeviceId}"); } EventClusterDAO eventCluster = new EventClusterDAO() { Id = eventObj.EventClusterId.ToString(), Device = _mapper.Map <EventClusterDeviceDAOObject>(deviceEntity), EventType = eventObj.EventType.ToLower(), EventCount = 1, Events = new EventDAOObject[] { _mapper.Map <EventDAOObject>(eventObj) }, StartDate = eventObj.Date }; eventCluster.Id = await _repoEventClusters.CreateItemAsync(eventCluster); if (_repoEventClusters.IsDocumentKeyNull(eventCluster)) { throw new Exception($"An error occured when creating a new cluster id for DeviceId: {eventObj.DeviceId}"); } return(_mapper.Map <EventClusterModel>(eventCluster)); }
/// <summary> /// Create or Update an Event Cluster /// </summary> /// <param name="eventObj">EventClusterCreationModel</param> /// <returns>EventClusterModel</returns> public async Task <EventClusterModel> CreateOrUpdateEventCluster(EventClusterCreationModel eventObj) { //Look for existing cluster that matches DeviceId + EventType and hasn't ended yet. EventClusterDAO eventCluster = await _repoEventClusters.GetItemAsync(eventObj.EventClusterId); //Create if (eventCluster == null) { return(await CreateEventCluster(eventObj)); } eventCluster.Events = eventCluster.Events.Append(_mapper.Map <EventDAOObject>(eventObj)).OrderByDescending(p => p.Date).ToArray(); eventCluster.EventCount++; try { await _repoEventClusters.UpdateItemAsync(eventCluster); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await CreateOrUpdateEventCluster(eventObj)); } throw e; } var output = _mapper.Map <EventClusterModel>(eventCluster); output.Events = output.Events.Take(3); return(output); }
/// <summary> /// Update the geolocation of a mobile device /// </summary> /// <param name="geolocation">Geolocation of the event cluster</param> /// <param name="deviceId">Device Id</param> /// <returnsEventClusterGeolocationUpdateResultModel></returns> public async Task <EventClusterGeolocationUpdateResultModel> UpdateGeolocation(Geolocation geolocation, Guid deviceId) { if (geolocation == null) { throw new Exception($"No Geolocation found: {geolocation}"); } if (deviceId == Guid.Empty || deviceId == null) { throw new Exception($"DeviceId not found"); } EventClusterDAO eventClusterDAO = await _repoEventClusters.GetItemAsync(p => p.Device.DeviceId == deviceId && p.ClosureDate.Value == null); if (eventClusterDAO != null) { if (geolocation.Latitude == eventClusterDAO.Device.Geolocation.Latitude && geolocation.Longitude == eventClusterDAO.Device.Geolocation.Longitude) { return new EventClusterGeolocationUpdateResultModel() { Success = false } } ; string etag = eventClusterDAO.ETag; eventClusterDAO.Device.Geolocation = _mapper.Map <GeolocationDAOObject>(geolocation); try { var result = await _repoEventClusters.UpdateItemAsync(eventClusterDAO); if (result) { return(new EventClusterGeolocationUpdateResultModel() { Success = true, EventCluster = _mapper.Map <EventClusterModel>(eventClusterDAO) }); } throw new Exception($"Error while updating device geolocation: {deviceId}."); } catch (DocumentClientException e) { //Update concurrency issue, retrying if (e.StatusCode == HttpStatusCode.PreconditionFailed) { return(await UpdateGeolocation(geolocation, deviceId)); } throw e; } } return(new EventClusterGeolocationUpdateResultModel() { Success = true }); }
/// <summary> /// Get Event Cluster by Id /// </summary> /// <param name="eventClusterId">Event Cluster Id</param> /// <returns>EventClusterModel</returns> public async Task <EventClusterModel> GetEventCluster(Guid eventClusterId) { EventClusterDAO eventCluster = await _repoEventClusters.GetItemAsync(eventClusterId); return(_mapper.Map <EventClusterModel>(eventCluster)); }