From 29c3e5badf0a0a6bf4f008388249f77ebe431781 Mon Sep 17 00:00:00 2001 From: Daniel Hazelbaker Date: Tue, 5 Sep 2017 10:34:46 -0700 Subject: [PATCH] Reduced to building query a single time with a do/while loop instead of while loop. Replaced Chunks with simplified .Take(100 ).ToList() call instead. Added status message for how many people were modified. Added ability to configure job to only modify people created within a relative date range. --- EntityFrameworkUtil.cs | 32 ------------ FixCombinedGivers.cs | 51 +++++++++++-------- ...ksandmortarstudio.FixCombinedGivers.csproj | 2 +- 3 files changed, 32 insertions(+), 53 deletions(-) delete mode 100644 EntityFrameworkUtil.cs diff --git a/EntityFrameworkUtil.cs b/EntityFrameworkUtil.cs deleted file mode 100644 index 1cd13d5..0000000 --- a/EntityFrameworkUtil.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace com.bricksandmortarstudio.FixCombinedGivers -{ - public static class EntityFrameworkUtil - { - public static IEnumerable QueryInChunksOf( this IQueryable queryable, int chunkSize ) - { - return queryable.QueryChunksOfSize( chunkSize ).SelectMany( chunk => chunk ); - } - - public static IEnumerable QueryChunksOfSize( this IQueryable queryable, int chunkSize ) - { - int chunkNumber = 0; - while ( true ) - { - var query = ( chunkNumber == 0 ) - ? queryable - : queryable.Skip( chunkNumber * chunkSize ); - var chunk = query.Take( chunkSize ).ToArray(); - if ( chunk.Length == 0 ) - yield break; - yield return chunk; - chunkNumber++; - } - } - } -} diff --git a/FixCombinedGivers.cs b/FixCombinedGivers.cs index e5d4bee..5063f70 100644 --- a/FixCombinedGivers.cs +++ b/FixCombinedGivers.cs @@ -1,46 +1,57 @@ using System.Linq; using Quartz; +using Rock.Attribute; using Rock.Data; using Rock.Model; using Rock.Web.Cache; +using Rock.Web.UI.Controls; namespace com.bricksandmortarstudio.FixCombinedGivers { [DisallowConcurrentExecution] + [SlidingDateRangeField( "Date Range", "Apply to Person records created within this time frame. If not set then all records are considered.", false, "", enabledSlidingDateRangeTypes: "Previous, Last, Current, DateRange" )] public class FixCombinedGivers : IJob { public void Execute( IJobExecutionContext context ) { - var rockContext = new RockContext(); - + var dataMap = context.JobDetail.JobDataMap; + RockContext rockContext = null; + IQueryable familyMembers = null; + var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( dataMap.GetString( "DateRange" ) ?? "-1||" ); + int peopleUpdated = 0; var familyGroupType = GroupTypeCache.Read( Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY ); - var familyMembers = new GroupMemberService( rockContext ) - .Queryable( "Group,Person" ) - .Where( - g => - g.Group.GroupType.Id == familyGroupType.Id && g.Person.GivingGroupId == 0 || - g.Person.GivingGroupId == null ); - while (familyMembers.Any()) + + do { - //Chunk to prevent foreach saving thread errors - foreach ( var chunk in familyMembers.OrderBy( f => f.Id ).QueryChunksOfSize( 100 ) ) + if ( familyMembers != null ) { - foreach ( var familyMember in chunk ) + foreach ( var familyMember in familyMembers.OrderBy( f => f.Id ).Take( 100 ).ToList() ) { familyMember.Person.GivingGroupId = familyMember.GroupId; + peopleUpdated += 1; } rockContext.SaveChanges(); } + rockContext = new RockContext(); + familyMembers = new GroupMemberService( rockContext ) - .Queryable( "Group,Person" ) - .Where( - g => - g.Group.GroupType.Id == familyGroupType.Id && g.Person.GivingGroupId == 0 || - g.Person.GivingGroupId == null ); - } - - } + .Queryable( "Group,Person" ) + .Where( g => g.Group.GroupType.Id == familyGroupType.Id && + ( g.Person.GivingGroupId == 0 || g.Person.GivingGroupId == null ) ); + + if ( dateRange.Start.HasValue ) + { + familyMembers = familyMembers.Where( g => g.Person.CreatedDateTime >= dateRange.Start ); + } + + if ( dateRange.End.HasValue ) + { + familyMembers = familyMembers.Where( g => g.Person.CreatedDateTime < dateRange.End ); + } + } while ( familyMembers.Any() ); + context.Result = string.Format( "Combined giving on {0} {1}", peopleUpdated, peopleUpdated == 1 ? "person" : "people" ); + } } } diff --git a/com.bricksandmortarstudio.FixCombinedGivers.csproj b/com.bricksandmortarstudio.FixCombinedGivers.csproj index e444583..5c12eaf 100644 --- a/com.bricksandmortarstudio.FixCombinedGivers.csproj +++ b/com.bricksandmortarstudio.FixCombinedGivers.csproj @@ -38,6 +38,7 @@ + @@ -46,7 +47,6 @@ -