Skip to content

Commit

Permalink
Subfilters for GetStatistics API added #179
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabruno91 committed Mar 19, 2024
1 parent 9fc81e7 commit f73606f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
84 changes: 70 additions & 14 deletions src/Ermes.Application/Ermes/Dashboard/DashboardAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Ermes.Dashboard
{
Expand Down Expand Up @@ -114,6 +115,20 @@ public virtual async Task<GetStatisticsOutput> GetStatistics(GetStatisticsInput
var hasPermission = _permissionChecker.IsGranted(_session.Roles, AppPermissions.Reports.Report_CanSeeCrossOrganization);
if (!hasPermission)
queryReport = queryReport.DataOwnership(person.OrganizationId.HasValue ? new List<int>() { person.OrganizationId.Value } : null);

if (input.ReportHazards != null && input.ReportHazards.Count > 0)
{
var hazardList = input.ReportHazards.Select(a => a.ToString()).ToList();
queryReport = queryReport.Where(a => hazardList.Contains(a.HazardString));
}

if (input.ReportVisibility != VisibilityType.All)
{
if (input.ReportVisibility == VisibilityType.Private)
queryReport = queryReport.Where(r => !r.IsPublic);
else
queryReport = queryReport.Where(r => r.IsPublic);
}
/////////////////////

//Missions///////////
Expand All @@ -122,6 +137,14 @@ public virtual async Task<GetStatisticsOutput> GetStatistics(GetStatisticsInput
hasPermission = _permissionChecker.IsGranted(_session.Roles, AppPermissions.Missions.Mission_CanSeeCrossOrganization);
if (!hasPermission)
queryMission = queryMission.DataOwnership(person.OrganizationId.HasValue ? new List<int>() { person.OrganizationId.Value } : null);

if (input.MissionStatus != null && input.MissionStatus.Count > 0)
{
//input.Status.Contains throw an exception
//I need to go through the strings rather then the enum
var list = input.MissionStatus.Select(a => a.ToString()).ToList();
queryMission = queryMission.Where(a => list.Contains(a.CurrentStatusString));
}
//////////////////////

//Persons////////////
Expand All @@ -133,7 +156,8 @@ public virtual async Task<GetStatisticsOutput> GetStatistics(GetStatisticsInput
orgIdList = _session.LoggedUserPerson.OrganizationId.HasValue ? new int[] { _session.LoggedUserPerson.OrganizationId.Value } : null;

string personName = _session.LoggedUserPerson.Username ?? _session.LoggedUserPerson.Email;
var items = _geoJsonBulkRepository.GetPersonActions(start, end, orgIdList, null, null, null, boundingBox, personName, null, _languageManager.CurrentLanguage.Name);
var teamIds = input.TeamIds?.ToArray();
var items = _geoJsonBulkRepository.GetPersonActions(start, end, orgIdList, input.ActionStatusTypes, null, teamIds, boundingBox, personName, null, _languageManager.CurrentLanguage.Name);
var deserialized = JsonConvert.DeserializeObject<GetActionsOutput>(items);
var actions = deserialized != null && deserialized.PersonActions != null ? deserialized.PersonActions : new List<PersonActionDto>();
////////////////////
Expand All @@ -147,15 +171,56 @@ public virtual async Task<GetStatisticsOutput> GetStatistics(GetStatisticsInput
if (_session.LoggedUserPerson.OrganizationId.HasValue)
queryMapRequests = queryMapRequests.DataOwnership(new List<int>() { _session.LoggedUserPerson.OrganizationId.Value });
}
queryMapRequests = queryMapRequests.Where(x => x.StatusString != MapRequestStatusType.Canceled.ToString());
//////////////////////

//Alerts///////////
IQueryable<Alert> queryAlerts = boundingBox != null ? _geoJsonBulkRepository.GetAlerts(start, end, boundingBox) : _alertManager.GetAlerts(start, end);
if (input.MapRequestStatus != null && input.MapRequestStatus.Count > 0)
{
var list = input.MapRequestStatus.Select(a => a.ToString()).ToList();
queryMapRequests = queryMapRequests.Where(a => list.Contains(a.StatusString));
}
else
queryMapRequests = queryMapRequests.Where(a => a.StatusString != MapRequestStatusType.Canceled.ToString());

if (input.MapRequestTypes != null && input.MapRequestTypes.Count > 0)
{
var list = input.MapRequestTypes.Select(a => a.ToString()).ToList();
queryMapRequests = queryMapRequests.Where(a => list.Contains(a.TypeString));
}
//////////////////////

//Communications///////////
bool includeNone = false;
var roles = await _personManager.GetPersonRoleNamesAsync(person.Id);
IQueryable<Communication> queryCommunications = boundingBox != null ? _geoJsonBulkRepository.GetCommunications(start, end, boundingBox) : _communicationManager.GetCommunications(start, end);
if (input.CommunicationScopes != null && input.CommunicationScopes.Count > 0)
{
var list = input.CommunicationScopes.Select(a => a.ToString()).ToList();
queryCommunications = queryCommunications.Where(a => list.Contains(a.ScopeString));
includeNone = input.CommunicationScopes.Contains(CommunicationScopeType.Public);

}
if (input.CommunicationRestrictions != null && input.CommunicationRestrictions.Count > 0)
{
if (includeNone)
input.CommunicationRestrictions.Add(CommunicationRestrictionType.None);
if (roles.Any(r => r == AppRoles.CITIZEN))
input.CommunicationRestrictions = new List<CommunicationRestrictionType> { CommunicationRestrictionType.None, CommunicationRestrictionType.Citizen };


var list = input.CommunicationRestrictions.Select(a => a.ToString()).ToList();
queryCommunications = queryCommunications.Where(a => list.Contains(a.RestrictionString));
}
else
{
input.CommunicationRestrictions = new List<CommunicationRestrictionType>() { CommunicationRestrictionType.None, CommunicationRestrictionType.Professional, CommunicationRestrictionType.Organization, CommunicationRestrictionType.Citizen };
if (roles.Any(r => r == AppRoles.CITIZEN))
input.CommunicationRestrictions = input.CommunicationRestrictions.Where(a => a == CommunicationRestrictionType.None || a == CommunicationRestrictionType.Citizen).ToList();

if (input.CommunicationRestrictions.Count > 0)
{
var list = input.CommunicationRestrictions.Select(a => a.ToString()).ToList();
queryCommunications = queryCommunications.Where(a => list.Contains(a.RestrictionString));
}
}

//Admin can see everything
hasPermission = _permissionChecker.IsGranted(_session.Roles, AppPermissions.Communications.Communication_CanSeeCrossOrganization);
Expand Down Expand Up @@ -261,15 +326,6 @@ public virtual async Task<GetStatisticsOutput> GetStatistics(GetStatisticsInput
Value = g.Count()
})
.ToList(),
AlertsByRestriction = queryAlerts
.GroupBy(a => a.Restriction)
.Select(g => new GroupDto()
{
Id = g.Key,
Label = g.Key,
Value = g.Count()
})
.ToList(),
Stations = stations
};
}
Expand Down
15 changes: 15 additions & 0 deletions src/Ermes.Application/Ermes/Dashboard/Dto/GetStatisticsInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ermes.Dto.Spatial;
using Ermes.Enums;
using Ermes.Filters;
using System;
using System.Collections.Generic;
Expand All @@ -12,5 +13,19 @@ public class GetStatisticsInput : IDateRangeFilter, IBBoxFilter
public DateTime? EndDate { get; set; }
public PointPosition NorthEastBoundary { get; set; }
public PointPosition SouthWestBoundary { get; set; }

public List<ActionStatusType> ActionStatusTypes { get; set; }
public List<int> TeamIds { get; set; }

public List<HazardType> ReportHazards { get; set; }
public VisibilityType ReportVisibility { get; set; }

public List<MissionStatusType> MissionStatus { get; set; }

public List<CommunicationRestrictionType> CommunicationRestrictions { get; set; }
public List<CommunicationScopeType> CommunicationScopes { get; set; }

public List<MapRequestStatusType> MapRequestStatus { get; set; }
public List<MapRequestType> MapRequestTypes { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class GetStatisticsOutput
public Dictionary<ActionStatusType, List<ActivationDto>> ActivationsByDay { get; set; }
public List<GroupDto> CommunicationsByRestriction { get; set; }
public List<GroupDto> MapRequestByType { get; set; }
public List<GroupDto> AlertsByRestriction { get; set; }
public List<StationDto> Stations { get; set; }

}
Expand Down
2 changes: 1 addition & 1 deletion src/Ermes.Web/Release-Notes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Ver.
--------------------------------

- Feature - Subfilters for GetStatistics API added (sdk to be updated)(#179)

Ver. 4.18.3 - 2024-03-13
--------------------------------
Expand Down

0 comments on commit f73606f

Please sign in to comment.