-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1309 from ucdavis/JCS/SearchIamPerson
Jcs/search iam person
- Loading branch information
Showing
15 changed files
with
382 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Keas.Core.Models | ||
{ | ||
public class IamAuthSettings | ||
{ | ||
public string IamKey { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using Keas.Core.Data; | ||
using Keas.Core.Models; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Ietws; | ||
using Microsoft.EntityFrameworkCore; | ||
using Serilog; | ||
|
||
namespace Keas.Core.Services | ||
{ | ||
public interface IUpdateFromIamService | ||
{ | ||
Task<int> UpdateUsersFromLastModifiedDateInIam(DateTime modifiedAfterDate); | ||
Task<int> UpdateAllUsersFromIam(); | ||
} | ||
|
||
public class UpdateFromIamService : IUpdateFromIamService | ||
{ | ||
private readonly IamAuthSettings _authSettings; | ||
private readonly ApplicationDbContext _context; | ||
|
||
public UpdateFromIamService(IOptions<IamAuthSettings> authSettings, ApplicationDbContext context) | ||
{ | ||
_authSettings = authSettings.Value; | ||
_context = context; | ||
} | ||
|
||
/// <summary> | ||
/// Note, this may or will contain IAM ids that do not exist in peaks. | ||
/// </summary> | ||
/// <param name="modifiedAfterDate"></param> | ||
/// <returns></returns> | ||
public async Task<int> UpdateUsersFromLastModifiedDateInIam(DateTime modifiedAfterDate) | ||
{ | ||
var count = 0; | ||
try | ||
{ | ||
Log.Information($"Update IAM by Modified Date - Starting for date {modifiedAfterDate}"); | ||
var clientws = new IetClient(_authSettings.IamKey); | ||
var result = await clientws.People.Search(PeopleSearchField.modifyDateAfter, modifiedAfterDate.ToString("yyyy-MM-dd")); | ||
Log.Information($"Update IAM by Modified Date - Number Changed: {result.ResponseData.Results.Length}"); | ||
if (result.ResponseData.Results.Length > 0) | ||
{ | ||
var iamIds = result.ResponseData.Results.Select(a => a.IamId).ToList(); | ||
|
||
var batches = Batch(iamIds, 100); | ||
foreach (var batch in batches) | ||
{ | ||
var batchCount = 0; | ||
var users = await _context.Users.Where(a => batch.Contains(a.Iam)).Include(a => a.People).ToListAsync(); | ||
foreach (var user in users) | ||
{ | ||
var ietData = result.ResponseData.Results.Where(a => a.IamId == user.Iam).FirstOrDefault(); | ||
if (ietData != null) | ||
{ | ||
if (user.FirstName != ietData.DFirstName || user.LastName != ietData.DLastName) | ||
{ | ||
count++; | ||
batchCount++; | ||
user.FirstName = ietData.DFirstName; | ||
user.LastName = ietData.DLastName; | ||
//user.pronouns = ietData.DPronouns; //if we add pronouns | ||
foreach (var person in user.People) | ||
{ | ||
person.FirstName = ietData.DFirstName; | ||
person.LastName = ietData.DLastName; | ||
} | ||
Log.Information($"Update IAM by Modified Date - Updating {user.Iam} from Iam."); | ||
} | ||
} | ||
} | ||
if (batchCount > 0) | ||
{ | ||
await _context.SaveChangesAsync(); | ||
} | ||
} | ||
|
||
|
||
Log.Information($"Update IAM by Modified Date - Updating {count} users from Iam."); | ||
|
||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Error("Update IAM by Modified Date - Getting List of Users to Update.", ex); | ||
} | ||
return count; | ||
} | ||
|
||
/// <summary> | ||
/// Don't run this on prod during working hours. | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<int> UpdateAllUsersFromIam() | ||
{ | ||
Log.Information("UpdateAllUsersFromIam - Starting"); | ||
var clientws = new IetClient(_authSettings.IamKey); | ||
//Take 100 users at a time and check them against IAM | ||
var count = 0; | ||
var currentBatch = 0; | ||
var userIamIds = await _context.Users.Where(a => a.Iam != null).Select(a => a.Iam).ToListAsync(); | ||
var batches = Batch(userIamIds, 100); | ||
foreach (var batch in batches) | ||
{ | ||
currentBatch++; | ||
Log.Information($"UpdateAllUsersFromIam - Starting batch number {currentBatch} ."); | ||
var batchCount = 0; | ||
//Pause for 5 seconds to not overload IAM | ||
await Task.Delay(5000); | ||
|
||
var users = await _context.Users.Where(a => batch.Contains(a.Iam)).Include(a => a.People).ToListAsync(); | ||
foreach (var user in users) | ||
{ | ||
var result = await clientws.People.Search(PeopleSearchField.iamId, user.Iam); | ||
if (result != null && result.ResponseData.Results.Length > 0) | ||
{ | ||
var ietData = result.ResponseData.Results.Where(a => a.IamId == user.Iam).FirstOrDefault(); | ||
if (ietData == null) | ||
{ | ||
continue; | ||
} | ||
|
||
if (user.FirstName != ietData.DFirstName || user.LastName != ietData.DLastName) | ||
{ | ||
count++; | ||
batchCount++; | ||
user.FirstName = ietData.DFirstName; | ||
user.LastName = ietData.DLastName; | ||
//user.pronouns = ietData.DPronouns; //if we add pronouns | ||
foreach (var person in user.People) | ||
{ | ||
person.FirstName = ietData.DFirstName; | ||
person.LastName = ietData.DLastName; | ||
} | ||
Log.Information($"Updating {user.Iam} from Iam."); | ||
} | ||
} | ||
} | ||
if (batchCount > 0) | ||
{ | ||
Log.Information($"UpdateAllUsersFromIam - Updated {batchCount} users ."); | ||
await _context.SaveChangesAsync(); | ||
} | ||
} | ||
|
||
|
||
Log.Information($"UpdateAllUsersFromIam - Updated total of {count} users ."); | ||
|
||
|
||
return count; | ||
} | ||
|
||
private static IEnumerable<IEnumerable<TSource>> Batch<TSource>(IEnumerable<TSource> source, int size) | ||
{ | ||
TSource[] bucket = null; | ||
var count = 0; | ||
|
||
foreach (var item in source) | ||
{ | ||
if (bucket == null) | ||
bucket = new TSource[size]; | ||
|
||
bucket[count++] = item; | ||
if (count != size) | ||
continue; | ||
|
||
yield return bucket; | ||
|
||
bucket = null; | ||
count = 0; | ||
} | ||
|
||
if (bucket != null && count > 0) | ||
yield return bucket.Take(count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<UserSecretsId>93abda06-2870-4006-81e8-1eecba3d977c</UserSecretsId> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="appsettings.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.15" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.15" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.15" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Keas.Core\Keas.Core.csproj" /> | ||
<ProjectReference Include="..\Keas.Jobs.Core\Keas.Jobs.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="run.cmd"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="settings.job"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Keas.Core.Data; | ||
using Keas.Core.Helper; | ||
using Keas.Core.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using Keas.Core.Services; | ||
using Keas.Jobs.Core; | ||
using Serilog; | ||
using Keas.Core.Extensions; | ||
|
||
namespace Keas.Jobs.LivedName | ||
{ | ||
public class Program : JobBase | ||
{ | ||
private static ILogger _log; | ||
|
||
static void Main(string[] args) | ||
{ | ||
Configure(); | ||
|
||
var assembyName = typeof(Program).Assembly.GetName(); | ||
_log = Log.Logger | ||
.ForContext("jobname", assembyName.Name) | ||
.ForContext("jobid", Guid.NewGuid()); | ||
|
||
_log.Information("Running {job} build {build}", assembyName.Name, assembyName.Version); | ||
|
||
// setup di | ||
var provider = ConfigureServices(); | ||
|
||
|
||
UpdateLivedNames(provider); | ||
|
||
|
||
} | ||
|
||
private static void UpdateLivedNames(ServiceProvider provider) | ||
{ | ||
Log.Information("Staring UpdateLivedNames"); | ||
var updateService = provider.GetService<IUpdateFromIamService>(); | ||
var count = updateService.UpdateUsersFromLastModifiedDateInIam(DateTime.UtcNow.AddDays(-1).ToPacificTime().Date).GetAwaiter().GetResult(); | ||
Log.Information("Updated {count} users", count); | ||
Log.Information("Finished UpdateLivedNames"); | ||
|
||
} | ||
|
||
private static ServiceProvider ConfigureServices() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
services.AddOptions(); | ||
services.AddDbContextPool<ApplicationDbContext>(o => o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | ||
|
||
services.Configure<IamAuthSettings>(Configuration.GetSection("Authentication")); | ||
|
||
services.AddTransient<IUpdateFromIamService, UpdateFromIamService>(); | ||
|
||
return services.BuildServiceProvider(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"profiles": { | ||
"Keas.Jobs.LivedName": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DefaultConnection": "Server=.\\sqlexpress;Database=keas;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
}, | ||
"Authentication": { | ||
"IamKey": "[External]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
|
||
dotnet Keas.Jobs.LivedName.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"schedule": "0 0 10 * * *" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.