Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EES-5738 Add filtering by geographic level for Data Catalogue page #5468

Merged
merged 7 commits into from
Dec 30, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GovUk.Education.ExploreEducationStatistics.Admin.Models;
using GovUk.Education.ExploreEducationStatistics.Common.Extensions;
using GovUk.Education.ExploreEducationStatistics.Common.Model;
using GovUk.Education.ExploreEducationStatistics.Content.Model;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Controllers.Api;

[Route("api")]
[ApiController]
[Authorize(Roles = GlobalRoles.RoleNames.BauUser)]
public class DataSetFileMetaMigrationController(ContentDbContext contentDbContext) : ControllerBase
{
public class MigrationResult
{
public bool IsDryRun;
public int Processed;
public string Errors;
}

[HttpPut("bau/migrate-datasetfile-geographiclevels")]
public async Task<MigrationResult> DataSetFileVersionGeographicLevelsMigration(
[FromQuery] bool isDryRun = true,
[FromQuery] int? num = null,
CancellationToken cancellationToken = default)
{
var queryable = contentDbContext.Files
.Where(f => f.Type == FileType.Data
&& f.DataSetFileVersionGeographicLevels.Count == 0);

if (num != null)
{
queryable = queryable.Take(num.Value);
}

var files = queryable.ToList();

var numProcessed = 0;
List<string> errors = [];

foreach (var file in files)
{
var meta = file.DataSetFileMeta;

if (meta == null)
{
errors.Add($"No DataSetFileMeta found for File {file.Id}");
continue;
}
benoutram marked this conversation as resolved.
Show resolved Hide resolved

var dataSetFileVersionGeographicLevels = meta!.GeographicLevels
.Distinct()
.Select(gl => new DataSetFileVersionGeographicLevel
{
DataSetFileVersionId = file.Id,
GeographicLevel = gl,
})
.ToList();

contentDbContext.DataSetFileVersionGeographicLevels.AddRange(
dataSetFileVersionGeographicLevels);

numProcessed++;
}

if (!isDryRun)
{
await contentDbContext.SaveChangesAsync(cancellationToken);
}

return new MigrationResult
{
IsDryRun = isDryRun,
Processed = numProcessed,
Errors = errors.IsNullOrEmpty() ? "No errors" : errors.JoinToString("\n"),
};
}

}
Loading
Loading