Skip to content

Commit

Permalink
[ADMINAPI-1077] Add Get/Post endpoints for healthchecks (#177)
Browse files Browse the repository at this point in the history
* Add Db Contexts

* Add correct injection of context

* Add GenericRepository

* Add tenantId

* Fix program.cs

* Fix db provider issue

* Fix mssql action

* Fix github actions

* Fix single tenant pg

* Fix github actions

* Update System.Text.Json nugget

* Add not found exception
Changed the way services were registered
  • Loading branch information
dfernandez-gap committed Jan 27, 2025
1 parent e3b33cd commit 11f8519
Show file tree
Hide file tree
Showing 41 changed files with 1,222 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@

<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.7.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
<Folder Include="Infrastructure\DataAccess\Artifacts\" />
<Folder Include="Mockdata\" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System.ComponentModel.DataAnnotations;
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.Services.HealthChecks.Commands;
using FluentValidation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;

namespace EdFi.Ods.AdminApi.AdminConsole.Features.Healthcheck;
internal class AddHealthCheck : IFeature
{
public void MapEndpoints(IEndpointRouteBuilder endpoints)
{
AdminApiAdminConsoleEndpointBuilder.MapPost(endpoints, "/healthcheck", Execute)
.WithRouteOptions(b => b.WithResponseCode(201))
.BuildForVersions();
}

public async Task<IResult> Execute(Validator validator, IAddHealthCheckCommand addHealthCheckCommand, AddHealthCheckRequest request)
{
await validator.GuardAsync(request);
var addedHealthCheck = await addHealthCheckCommand.Execute(request);
return Results.Created($"/healthcheck/{addedHealthCheck.DocId}", null);
}

[SwaggerSchema(Title = nameof(AddHealthCheckRequest))]
public class AddHealthCheckRequest : IAddHealthCheckModel
{
[Required]
public int DocId { get; set; }
[Required]
public int InstanceId { get; set; }
[Required]
public int EdOrgId { get; set; }
[Required]
public int TenantId { get; set; }
[Required]
public string Document { get; set; }
}

public class Validator : AbstractValidator<AddHealthCheckRequest>
{
public Validator()
{
RuleFor(m => m.InstanceId)
.NotNull();

RuleFor(m => m.EdOrgId)
.NotNull();

RuleFor(m => m.TenantId)
.NotNull();

RuleFor(m => m.Document)
.NotNull()
.Must(BeValidDocument).WithMessage("Document must be a valid JSON.");
}

private bool BeValidDocument(string document)
{
try
{
Newtonsoft.Json.Linq.JToken.Parse(document);
return true;
}
catch (Newtonsoft.Json.JsonReaderException)
{
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,52 @@
// See the LICENSE and NOTICES files in the project root for more information.

using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;

namespace EdFi.Ods.AdminApi.AdminConsole.Features.Healthcheck
namespace EdFi.Ods.AdminApi.AdminConsole.Features.Healthcheck;

[SwaggerSchema(Title = "HealthCheck")]
public class HealthCheckModel
{
public class HealthcheckModel
{
[JsonProperty("localEducationAgencyId")]
public long LocalEducationAgencyId { get; set; }
[JsonProperty("localEducationAgencyId")]
public long LocalEducationAgencyId { get; set; }

[JsonProperty("studentSpecialEducationProgramAssociations")]
public long StudentSpecialEducationProgramAssociations { get; set; }
[JsonProperty("studentSpecialEducationProgramAssociations")]
public long StudentSpecialEducationProgramAssociations { get; set; }

[JsonProperty("studentDisciplineIncidentBehaviorAssociations")]
public long StudentDisciplineIncidentBehaviorAssociations { get; set; }
[JsonProperty("studentDisciplineIncidentBehaviorAssociations")]
public long StudentDisciplineIncidentBehaviorAssociations { get; set; }

[JsonProperty("studentSchoolAssociations")]
public long StudentSchoolAssociations { get; set; }
[JsonProperty("studentSchoolAssociations")]
public long StudentSchoolAssociations { get; set; }

[JsonProperty("studentSchoolAttendanceEvents")]
public long StudentSchoolAttendanceEvents { get; set; }
[JsonProperty("studentSchoolAttendanceEvents")]
public long StudentSchoolAttendanceEvents { get; set; }

[JsonProperty("studentSectionAssociations")]
public long StudentSectionAssociations { get; set; }
[JsonProperty("studentSectionAssociations")]
public long StudentSectionAssociations { get; set; }

[JsonProperty("staffEducationOrganizationAssignmentAssociations")]
public long StaffEducationOrganizationAssignmentAssociations { get; set; }
[JsonProperty("staffEducationOrganizationAssignmentAssociations")]
public long StaffEducationOrganizationAssignmentAssociations { get; set; }

[JsonProperty("staffEducationOrganizationEmploymentAssociations")]
public long StaffEducationOrganizationEmploymentAssociations { get; set; }
[JsonProperty("staffEducationOrganizationEmploymentAssociations")]
public long StaffEducationOrganizationEmploymentAssociations { get; set; }

[JsonProperty("staffSectionAssociations")]
public long StaffSectionAssociations { get; set; }
[JsonProperty("staffSectionAssociations")]
public long StaffSectionAssociations { get; set; }

[JsonProperty("courseTranscripts")]
public long CourseTranscripts { get; set; }
[JsonProperty("courseTranscripts")]
public long CourseTranscripts { get; set; }

[JsonProperty("basicReportingPeriodAttendances")]
public long BasicReportingPeriodAttendances { get; set; }
[JsonProperty("basicReportingPeriodAttendances")]
public long BasicReportingPeriodAttendances { get; set; }

[JsonProperty("sections")]
public long Sections { get; set; }
[JsonProperty("sections")]
public long Sections { get; set; }

[JsonProperty("reportingPeriodExts")]
public long ReportingPeriodExts { get; set; }
[JsonProperty("reportingPeriodExts")]
public long ReportingPeriodExts { get; set; }

[JsonProperty("healthy")]
public bool Healthy { get; set; }
}
[JsonProperty("healthy")]
public bool Healthy { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,35 @@
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using EdFi.Ods.AdminApi.AdminConsole.Features.UserProfiles;
using AutoMapper;
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.Services.HealthChecks.Queries;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;

namespace EdFi.Ods.AdminApi.AdminConsole.Features.Healthcheck
namespace EdFi.Ods.AdminApi.AdminConsole.Features.Healthcheck;

public class ReadHealthcheck : IFeature
{
public class ReadHealthcheck : IFeature
public void MapEndpoints(IEndpointRouteBuilder endpoints)
{
AdminApiAdminConsoleEndpointBuilder.MapGet(endpoints, "/healthcheck", GetHealthchecks)
.BuildForVersions();

AdminApiAdminConsoleEndpointBuilder.MapGet(endpoints, "/healthcheck/{tenantId}", GetHealthcheck)
.BuildForVersions();
}

internal async Task<IResult> GetHealthcheck(IMapper mapper, IGetHealthCheckQuery getHealthCheckQuery, int tenantId)
{
public void MapEndpoints(IEndpointRouteBuilder endpoints)
{
AdminApiAdminConsoleEndpointBuilder.MapGet(endpoints, "/healthcheck", GetHealthcheck)
.BuildForVersions();
}
var healthChecks = await getHealthCheckQuery.Execute(tenantId);
var model = mapper.Map<HealthCheckModel>(healthChecks);
return Results.Ok(model);
}

internal Task<IResult> GetHealthcheck()
{
using (StreamReader r = new StreamReader("Mockdata/data-healthcheck.json"))
{
string json = r.ReadToEnd();
HealthcheckModel? result = JsonConvert.DeserializeObject<HealthcheckModel>(json);
return Task.FromResult(Results.Ok(result));
}
}
internal async Task<IResult> GetHealthchecks(IMapper mapper, IGetHealthChecksQuery getHealthChecksQuery)
{
var healthChecks = await getHealthChecksQuery.Execute();
var model = mapper.Map<IEnumerable<HealthCheckModel>>(healthChecks);
return Results.Ok(model);
}
}
13 changes: 13 additions & 0 deletions Application/EdFi.Ods.AdminApi.AdminConsole/Features/IFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using Microsoft.AspNetCore.Routing;

namespace EdFi.Ods.AdminApi.AdminConsole.Features;

public interface IFeature
{
void MapEndpoints(IEndpointRouteBuilder endpoints);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System.Collections.Generic;
using System.Dynamic;
using EdFi.Ods.AdminApi.AdminConsole.Features.Tenants;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System.Dynamic;
using EdFi.Ods.AdminApi.AdminConsole.Features.Tenants;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
Expand All @@ -29,9 +28,3 @@ internal Task<IResult> GetUserProfiles()
}
}
}


public interface IFeature
{
void MapEndpoints(IEndpointRouteBuilder endpoints);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using AutoMapper;
using EdFi.Ods.AdminApi.AdminConsole.Features.Healthcheck;
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.DataAccess.Models;
using Newtonsoft.Json;

namespace EdFi.Ods.AdminApi.AdminConsole.Infrastructure.AutoMapper;

public class AdminConsoleMappingProfile : Profile
{
public AdminConsoleMappingProfile()
{
CreateMap<HealthCheck, HealthCheckModel>().ConvertUsing(src => JsonConvert.DeserializeObject<HealthCheckModel>(src.Document));

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 11f8519

Please sign in to comment.