Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/Api/Dirt/Controllers/OrganizationReportsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,13 @@ public async Task<IActionResult> UpdateOrganizationReportAsync(Guid organization
# region SummaryData Field Endpoints

/// <summary>
/// Gets summary data for organization reports within a specified date range.
/// The response is optimized for widget display by returning up to 6 entries that are
/// evenly spaced across the date range, including the most recent entry.
/// This allows the widget to show trends over time while ensuring the latest data point is always included.
/// Gets summary data for organization reports within a specified date range.
/// Returns all report summary entries within the range.
/// </summary>
/// <param name="organizationId"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
/// <param name="organizationId">The unique identifier of the organization.</param>
/// <param name="startDate">The start of the date range to query.</param>
/// <param name="endDate">The end of the date range to query.</param>
/// <returns>A collection of summary data entries within the date range.</returns>
/// <exception cref="NotFoundException"></exception>
/// <exception cref="BadRequestException"></exception>
[HttpGet("{organizationId}/data/summary")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Bit.Core.Dirt.Reports.ReportFeatures;

public class GetOrganizationReportSummaryDataByDateRangeQuery : IGetOrganizationReportSummaryDataByDateRangeQuery
{
private const int MaxRecordsForWidget = 6;
private readonly IOrganizationReportRepository _organizationReportRepo;
private readonly ILogger<GetOrganizationReportSummaryDataByDateRangeQuery> _logger;
private readonly IFusionCache _cache;
Expand Down Expand Up @@ -53,7 +52,7 @@ public async Task<IEnumerable<OrganizationReportSummaryDataResponse>> GetOrganiz
factory: async _ =>
{
var data = await _organizationReportRepo.GetSummaryDataByDateRangeAsync(organizationId, startDate, endDate);
return GetMostRecentEntries(data);
return data;
},
options: new FusionCacheEntryOptions(duration: OrganizationReportCacheConstants.DurationForSummaryData),
tags: [cacheTag]
Expand Down Expand Up @@ -98,27 +97,4 @@ private static (bool IsValid, string errorMessage) ValidateRequest(Guid organiza

return (true, string.Empty);
}

private static IEnumerable<OrganizationReportSummaryDataResponse> GetMostRecentEntries(IEnumerable<OrganizationReportSummaryDataResponse> data, int maxEntries = MaxRecordsForWidget)
{
if (data.Count() <= maxEntries)
{
return data;
}

// here we need to take 10 records, evenly spaced by RevisionDate,
// to cover the entire date range,
// and ensure we include the most recent record as well
var sortedData = data.OrderByDescending(d => d.RevisionDate).ToList();
var totalRecords = sortedData.Count;
var interval = (double)(totalRecords - 1) / (maxEntries - 1); // -1 the most recent record will be included by default
var result = new List<OrganizationReportSummaryDataResponse>();

for (int i = 0; i <= maxEntries - 1; i++)
{
result.Add(sortedData[(int)Math.Round(i * interval)]);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ await sutProvider.GetDependency<IOrganizationReportRepository>()

[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_ShouldReturnTopSixResults(
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_ShouldReturnAllResults(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task GetOrganizationReportSummaryDataByDateRangeAsync_ShouldReturnT

// Assert
Assert.NotNull(result);
Assert.Equal(6, result.Count());
Assert.Equal(12, result.Count());
await sutProvider.GetDependency<IOrganizationReportRepository>()
.Received(1).GetSummaryDataByDateRangeAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<DateTime>());
}
Expand Down
Loading