Skip to content

Commit

Permalink
Merge pull request itpartnersillinois#86 from itpartnersillinois/jonk…
Browse files Browse the repository at this point in the history
…er/AutoStart

Update to add database cleanup for startup
  • Loading branch information
bryanjonker-illinois authored Oct 2, 2024
2 parents 1f1b782 + 870525d commit dbb2cf3
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 59 deletions.
20 changes: 12 additions & 8 deletions uofi-itp-directory-data/DataAccess/EmployeeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace uofi_itp_directory_data.DataAccess {

public class EmployeeHelper(DirectoryRepository directoryRepository, DirectoryHookHelper directoryHookHelper, DirectoryContext directoryContext, EmployeeAreaHelper employeeAreaHelper, LogHelper logHelper) {
private readonly DirectoryContext _directoryContext = directoryContext;
private readonly DirectoryHookHelper _directoryHookHelper = directoryHookHelper;
private readonly DirectoryRepository _directoryRepository = directoryRepository;
private readonly EmployeeAreaHelper _employeeAreaHelper = employeeAreaHelper;
private readonly LogHelper _logHelper = logHelper;
public class EmployeeHelper(DirectoryRepository? directoryRepository, DirectoryHookHelper? directoryHookHelper, DirectoryContext? directoryContext, EmployeeAreaHelper? employeeAreaHelper, LogHelper? logHelper) {
private readonly DirectoryContext _directoryContext = directoryContext ?? throw new ArgumentNullException("directoryContext");
private readonly DirectoryHookHelper? _directoryHookHelper = directoryHookHelper;
private readonly DirectoryRepository _directoryRepository = directoryRepository ?? throw new ArgumentNullException("directoryRepository");
private readonly EmployeeAreaHelper _employeeAreaHelper = employeeAreaHelper ?? throw new ArgumentNullException("employeeAreaHelper");
private readonly LogHelper? _logHelper = logHelper;

public async Task<int> DeleteEmployee(string netId) {
var returnValue = 0;
Expand Down Expand Up @@ -79,8 +79,12 @@ public async Task<AreaSettings> GetEmployeeSettings(Employee? employee) => emplo
public async Task<int> SaveEmployee(Employee employee, string changedByNetId, string message) {
employee.ProfileUrl = await _employeeAreaHelper.ProfileViewUrl(employee.NetId);
var returnValue = await _directoryRepository.UpdateAsync(employee);
_ = await _directoryHookHelper.SendHook(employee.Id, true);
_ = await _logHelper.CreateEmployeeLog(changedByNetId, message, employee.ToString(), employee.Id, employee.NetId);
if (_directoryHookHelper != null) {
_ = await _directoryHookHelper.SendHook(employee.Id, true);
}
if (_logHelper != null) {
_ = await _logHelper.CreateEmployeeLog(changedByNetId, message, employee.ToString(), employee.Id, employee.NetId);
}
return returnValue;
}

Expand Down
9 changes: 9 additions & 0 deletions uofi-itp-directory-function/EdwRefreshFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using uofi_itp_directory_data.Data;
using uofi_itp_directory_data.DataAccess;
using uofi_itp_directory_external.DataWarehouse;
using uofi_itp_directory_function.ViewModels;

namespace uofi_itp_directory_function {

Expand All @@ -17,6 +18,14 @@ public class EdwRefreshFunction(ILogger<EdwRefreshFunction> logger, DataWarehous
private readonly EmployeeHelper _employeeHelper = employeeHelper;
private readonly ILogger<EdwRefreshFunction> _logger = logger;

[Function("EdwPull")]
[OpenApiOperation(operationId: "EdwPull", tags: "Load", Description = "Look up first and last names of users from EDW")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(NameInformation), Description = "First and Last Name of person")]
public async Task<IActionResult> Pull([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "EdwPull/{username}")] HttpRequest req, string username) {
var edw = await _dataWarehouseManager.GetDataWarehouseItem(username);
return new OkObjectResult(new NameInformation() { NetId = username, FirstName = edw.FirstName, LastName = edw.LastName });
}

[Function("EdwRefresh")]
[OpenApiOperation(operationId: "EdwRefresh", tags: "Load", Description = "Purge the directory database if names are no longer in EDW. This will run a long time.")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "A status of what it did. If it deleted names, it will list the net IDs of the names that were deleted.")]
Expand Down
27 changes: 0 additions & 27 deletions uofi-itp-directory-function/LoadMapperFunction.cs

This file was deleted.

10 changes: 8 additions & 2 deletions uofi-itp-directory-function/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
_ = services.ConfigureFunctionsApplicationInsights();
_ = services.AddDbContextFactory<DirectoryContext>(options => options.UseSqlServer(hostContext.Configuration["Values:AppConnection"]).EnableSensitiveDataLogging(true));
_ = services.AddScoped<DirectoryRepository>();
_ = services.AddScoped(c => new PersonMapper(hostContext.Configuration["Values:SearchUrl"], Console.WriteLine));
_ = services.AddScoped<LogHelper>();
_ = services.AddScoped<EmployeeAreaHelper>();
_ = services.AddScoped<AreaHelper>();
_ = services.AddScoped(c => new DirectoryHookHelper(c.GetService<DirectoryRepository>(), hostContext.Configuration["Values:FacultyLoadUrl"]));
_ = services.AddScoped<EmployeeHelper>();
_ = services.AddScoped(c => new DirectoryHookHelper(c.GetService<DirectoryRepository>(), ""));
_ = services.AddScoped(c => new EmployeeHelper(c.GetService<DirectoryRepository>(), null, c.GetService<DirectoryContext>(), c.GetService<EmployeeAreaHelper>(), c.GetService<LogHelper>()));
_ = services.AddScoped<QueueManager>();
_ = services.AddScoped(c => new DataWarehouseManager(hostContext.Configuration["Values:DataWarehouseUrl"], hostContext.Configuration["Values:DataWarehouseKey"]));
_ = services.AddScoped(c => new IllinoisExpertsManager(hostContext.Configuration["Values:ExpertsUrl"], hostContext.Configuration["Values:ExpertsSecretKey"]));
Expand All @@ -50,4 +51,9 @@
})
.Build();

using var scope = host.Services.CreateScope();
var services = scope.ServiceProvider;
var personMapper = services.GetService<PersonMapper>() ?? throw new NullReferenceException("personMapper");
Console.WriteLine(await personMapper.Map());

host.Run();
8 changes: 8 additions & 0 deletions uofi-itp-directory-function/ViewModels/NameInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace uofi_itp_directory_function.ViewModels {

public class NameInformation {
public string FirstName { get; set; } = "";
public string LastName { get; set; } = "";
public string NetId { get; set; } = "";
}
}
11 changes: 0 additions & 11 deletions uofi-itp-directory-search/LoadHelper/LoadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ public class LoadManager(DataWarehouseManager? dataWarehouseManager, EmployeeHel
private readonly ProgramCourseInformation _programCourseInformation = programCourseInformation ?? default!;
private readonly string _searchUrl = searchUrl ?? "";

public async Task<string> LoadMapping() {
AddLog($"Starting mapping process");
try {
var personMapper = new PersonMapper(_searchUrl, AddLog);
AddLog(await personMapper.Map() ? "Mapping loaded complete" : "Mapping did not load");
} catch (Exception e) {
AddLog($"Error in process, aborting: {e.Message}");
}
return _logger.ToString();
}

public async Task<string> LoadPerson(string netId, string source) {
AddLog($"Starting process with Net ID {netId} and source {source}");
try {
Expand Down
15 changes: 5 additions & 10 deletions uofi-itp-directory-search/LoadHelper/PersonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace uofi_itp_directory_search.LoadHelper {

public class PersonMapper(string mapUrl, Action<string> logger) {
public class PersonMapper(string? mapUrl, Action<string> logger) {

private const string _map = "{ \"settings\": { \"analysis\": { \"filter\": { \"english_stop\": { \"type\": \"stop\", \"stopwords\": \"_english_\" }, \"english_stemmer\": { \"type\": \"stemmer\", \"language\": \"english\" }, \"english_possessive_stemmer\": { \"type\": \"stemmer\", \"language\": \"possessive_english\" } }, \"analyzer\": { \"english\": { \"tokenizer\": \"standard\", \"filter\": [ \"lowercase\", \"english_possessive_stemmer\", \"english_stop\", \"english_stemmer\" ] } } } }, " +
"\"mappings\" : { \"properties\" : { " +
Expand Down Expand Up @@ -47,26 +47,21 @@ public class PersonMapper(string mapUrl, Action<string> logger) {

private readonly Action<string> _logger = logger;
private readonly bool _logOnly = string.IsNullOrWhiteSpace(mapUrl);
private readonly string _mapUrl = (mapUrl.TrimEnd('/') ?? "") + "/dr_person";
private readonly string _mapUrl = (mapUrl?.TrimEnd('/') ?? "") + "/dr_person";

public async Task<bool> Map() {
public async Task<string> Map() {
_logger($"{_mapUrl}: PUT {(_logOnly ? _map : "")}");
if (_logOnly) {
return true;
return "";
}
return await CreateMapping();
}

private async Task<bool> CreateMapping() {
using var httpClient = new HttpClient();
var response = await httpClient.SendAsync(new HttpRequestMessage {
Version = HttpVersion.Version10,
Content = new StringContent(_map, Encoding.UTF8, "application/json"),
RequestUri = new Uri(_mapUrl),
Method = HttpMethod.Put
}).ConfigureAwait(continueOnCapturedContext: false);
_ = response.EnsureSuccessStatusCode();
return true;
return await response.Content.ReadAsStringAsync();
}
}
}
8 changes: 7 additions & 1 deletion uofi-itp-directory/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,11 @@
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Lifetime.ApplicationStarted.Register(() => {
var factory = app.Services.GetService<IServiceScopeFactory>() ?? throw new NullReferenceException("service scope factory is null");
using var serviceScope = factory.CreateScope();
var context = serviceScope.ServiceProvider.GetRequiredService<DirectoryContext>();
_ = context.Database.EnsureCreated();
});

app.Run();
app.Run();

0 comments on commit dbb2cf3

Please sign in to comment.