Files
linqpad/User Role Department PayPeriod query.linq
T

241 lines
10 KiB
C#

<Query Kind="Program">
<Connection>
<ID>77b65bea-e766-4e3c-a1ed-c4c2534b6019</ID>
<Persist>true</Persist>
<Server>D0001-SQL-11.sdt.local</Server>
<Database>jazz dev master Prod</Database>
<ShowServer>true</ShowServer>
</Connection>
<Reference>&lt;ProgramFilesX64&gt;\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll</Reference>
<NuGetReference>Amazon.Lambda.S3Events</NuGetReference>
<NuGetReference>AWSSDK.Core</NuGetReference>
<NuGetReference>AWSSDK.S3</NuGetReference>
<Namespace>Amazon</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy.ActionIdentifiers</Namespace>
<Namespace>Amazon.Internal</Namespace>
<Namespace>Amazon.Lambda.S3Events</Namespace>
<Namespace>Amazon.MissingTypes</Namespace>
<Namespace>Amazon.Runtime</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement.Internal</Namespace>
<Namespace>Amazon.Runtime.EventStreams</Namespace>
<Namespace>Amazon.Runtime.EventStreams.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal.Auth</Namespace>
<Namespace>Amazon.Runtime.Internal.Settings</Namespace>
<Namespace>Amazon.Runtime.Internal.Transform</Namespace>
<Namespace>Amazon.Runtime.Internal.Util</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces.Internal</Namespace>
<Namespace>Amazon.S3</Namespace>
<Namespace>Amazon.S3.Encryption</Namespace>
<Namespace>Amazon.S3.Encryption.Internal</Namespace>
<Namespace>Amazon.S3.Internal</Namespace>
<Namespace>Amazon.S3.IO</Namespace>
<Namespace>Amazon.S3.Model</Namespace>
<Namespace>Amazon.S3.Model.Internal.MarshallTransformations</Namespace>
<Namespace>Amazon.S3.Transfer</Namespace>
<Namespace>Amazon.S3.Util</Namespace>
<Namespace>Amazon.Util</Namespace>
<Namespace>Amazon.Util.Internal</Namespace>
<Namespace>Amazon.Util.Internal.PlatformServices</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Schema</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Diagnostics</Namespace>
<Namespace>System.Diagnostics.Tracing</Namespace>
<Namespace>System.IO.Compression</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1.Utilities</Namespace>
<Namespace>ThirdParty.BouncyCastle.Math</Namespace>
<Namespace>ThirdParty.BouncyCastle.OpenSsl</Namespace>
<Namespace>ThirdParty.BouncyCastle.Utilities.IO.Pem</Namespace>
<Namespace>ThirdParty.Ionic.Zlib</Namespace>
<Namespace>ThirdParty.Json.LitJson</Namespace>
<Namespace>ThirdParty.MD5</Namespace>
</Query>
/*
(from roleDetail in ViewS3RoleAssignmentDetail
where roleDetail.RoleName.StartsWith("MR ")
&& roleDetail.LootID.Contains("MRDEPT|")
select roleDetail.RoleName)
.Distinct()
.OrderBy(p => p).ToList()
.Aggregate("var roles = new [] { ", (x, y) => x + $"\"{y}\", ").TrimEnd(", ".ToCharArray()) + " }"
*/
void Main()
{
var userNames = new List<string>{
"dachee"
};
// var userGUIDs = from up in UserProfiles
// where userNames.Contains(up.UserName)
// select up.UserGUID;
// new List<Guid>{
// //new Guid("0e8a4d58-f9c1-4a49-837c-dd7db45793fe")
// new Guid("76CD86B8-D769-4AAE-9083-F8E6D005AA9D")
// };
var users = (from user in UserProfiles
select new User
{
UserInfo = new UserInfo
{
UserGUID = user.UserGUID,
UserName = user.UserName,
NameFirst = user.NameFirst,
NameLast = user.NameLast,
EmailAddress = user.EmailAddress
}
})
.Where(u => userNames.Contains(u.UserInfo.UserName))
.ToList();
var roles = new[] {
// "MR - Commenter",
"MR - Director",
"MR - Exec Director AVP",
"MR - Executive",
// "MR - Finance",
"MR - Manager",
// "MR - Read Only",
// "MR - Read Write Only",
"MR - Vice President"
};
users
.ForEach(u =>
{
int x;
u.Roles = (from roleDetail in ViewS3RoleAssignmentDetail
where roleDetail.UserGUID == u.UserInfo.UserGUID
select roleDetail.RoleName).Distinct().OrderBy(r => r).ToList();
u.DepartmentIDs = (from roleDetail in ViewS3RoleAssignmentDetail
where roleDetail.UserGUID == u.UserInfo.UserGUID
&& roleDetail.LootID.Contains("MRDEPT|")
&& roles.Contains(roleDetail.RoleName)
select new { depts = roleDetail.LootID.Split('|').ToList() })
.ToList().SelectMany(d => d.depts).Where(d => int.TryParse(d, out x)).ToList().ConvertAll(int.Parse).Distinct()
.ToList();
u.DeptPayPeriods = (from fact in FactDepartmentProductivityMetrics
join pp in DimPayPeriods on fact.PayPeriodID equals pp.PayPeriodID
where u.DepartmentIDs.Contains(fact.DepartmentID)
&& pp.PeriodEndCalendarDate < new DateTime(2019, 6, 17)
&& pp.PeriodStartCalendarDate.HasValue
orderby pp.PeriodStartCalendarDate descending
select new { fact.DepartmentID, pp.PayPeriodID, PeriodStartCalendarDate = pp.PeriodStartCalendarDate.Value, pp.PeriodEndCalendarDate })
.Distinct().ToList()
.GroupBy(dpp => dpp.DepartmentID, (key, grp) => new DeptPayPeriods
{
DepartmentID = key,
PayPeriods = grp.Select(g => new PayPeriod
{
PeriodID = g.PayPeriodID,
StartDate = g.PeriodStartCalendarDate,
EndDate = g.PeriodEndCalendarDate
})
.ToList().OrderByDescending(pp => pp.StartDate).Take(6).ToList()
}).ToList();
var payperiodIDs = u.DeptPayPeriods.SelectMany(d => d.PayPeriods.Select(pp => pp.PeriodID));
var hours = from fact in FactPayrollSampledPayPeriods
join payperiod in DimPayPeriods on fact.PayPeriodID equals payperiod.PayPeriodID
join paycode in DimPayCodeGroups on fact.PayCodeGroupID equals paycode.PayCodeGroupID
join prodclass in DimProductiveClasses on paycode.ProductiveClassID equals prodclass.ProductiveClassID
join department in DimDepartments on fact.DepartmentID equals department.DepartmentID
where new[] { 1, 4 }.Contains(fact.TimeClassID)
&& prodclass.Name == "Productive"
&& u.DepartmentIDs.Contains(fact.DepartmentID)
&& payperiodIDs.Contains(fact.PayPeriodID)
select new DeptPayPeriodTime(fact.DepartmentID, fact.PayPeriodID, fact.TimeClassID, fact.Hours, "hours");
var dollars = from fact in FactPayrollSampledPayPeriods
join payperiod in DimPayPeriods on fact.PayPeriodID equals payperiod.PayPeriodID
join paycode in DimPayCodeGroups on fact.PayCodeGroupID equals paycode.PayCodeGroupID
//join prodclass in DimProductiveClasses on paycode.ProductiveClassID equals prodclass.ProductiveClassID
join department in DimDepartments on fact.DepartmentID equals department.DepartmentID
where new[] { 1, 4 }.Contains(fact.TimeClassID)
//&& prodclass.Name == "Productive"
&& u.DepartmentIDs.Contains(fact.DepartmentID)
&& payperiodIDs.Contains(fact.PayPeriodID)
select new DeptPayPeriodTime(fact.DepartmentID, fact.PayPeriodID, fact.TimeClassID, fact.Dollars, "dollars");
var uos = from fact in ViewPrimaryStatisticsStaffingByPayPeriod
join payperiod in DimPayPeriods on fact.PayPeriodID equals payperiod.PayPeriodID
//join paycode in DimPayCodeGroups on fact.PayCodeGroupID equals paycode.PayCodeGroupID
//join prodclass in DimProductiveClasses on paycode.ProductiveClassID equals prodclass.ProductiveClassID
join department in DimDepartments on fact.DepartmentID equals department.DepartmentID
where new[] { 1, 4 }.Contains(fact.TimeClassID)
//&& prodclass.Name == "Productive"
&& u.DepartmentIDs.Contains(fact.DepartmentID)
&& payperiodIDs.Contains(fact.PayPeriodID)
select new DeptPayPeriodTime(fact.DepartmentID, fact.PayPeriodID, fact.TimeClassID, fact.Units.HasValue ? fact.Units.Value : 0.0m, "uos");
var unitSummary = hours.ToList().Concat(dollars.ToList()).Concat(uos.ToList())
.GroupBy(dppt => new { dppt.DepartmentID, dppt.PeriodID, dppt.TimeClassID, dppt.UnitType },
(key, grp) => new DeptPayPeriodTime(key.DepartmentID, key.PeriodID, key.TimeClassID,
grp.Where(pp => u.DeptPeriodDict[pp.DepartmentID].Contains(pp.PeriodID)).Sum(i => i.Units), key.UnitType))
.Where(item => u.DeptPeriodDict[item.DepartmentID].Contains(item.PeriodID))
.OrderBy(item => item.DepartmentID)
.ThenByDescending(item => item.PeriodID).ToList();
u.Units = unitSummary;
});
users.Dump("Users");
users//.Where(u => userGUIDs.Contains(u.UserInfo.UserGUID))
.Aggregate("", (x, y) => x + y.DepartmentIDs.Aggregate(", ", (a, b) => $"{a}, {b}")).Substring(2)
.Dump();
users//.Where(u => userGUIDs.Contains(u.UserInfo.UserGUID))
.Aggregate("", (x, y) => x + y.DeptPayPeriods.SelectMany(dpp => dpp.PayPeriods.Select(pp => pp.PeriodID)).Distinct()
.Aggregate(", ", (a, b) => $"{a}, {b}")).Substring(2)
.Dump();
}
// Define other methods and classes here
public class User
{
public UserInfo UserInfo { get; set; }
public List<string> Roles { get; set; }
public List<int> DepartmentIDs { get; set; }
public List<DeptPayPeriods> DeptPayPeriods { get; set; }
internal Dictionary<int, List<short>> DeptPeriodDict =>
DeptPayPeriods.ToDictionary(dpp => dpp.DepartmentID, dpp => dpp.PayPeriods.Select(pp => pp.PeriodID).ToList());
public List<DeptPayPeriodTime> Units { get; set; }
}
public class UserInfo
{
public Guid UserGUID { get; set; }
public string UserName { get; set; }
public string NameFirst { get; set; }
public string NameLast { get; set; }
public string EmailAddress { get; set; }
}
public class DeptPayPeriods
{
public int DepartmentID { get; set; }
public List<PayPeriod> PayPeriods { get; set; }
//public List<DeptPayPeriodTime> Units { get; set; }
}
public class PayPeriod
{
public short PeriodID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class DeptPayPeriodTime
{
public int DepartmentID { get; set; }
public short PeriodID { get; set; }
public byte TimeClassID { get; set; }
public decimal Units { get; set; }
public string UnitType { get; set; }
public DeptPayPeriodTime(int departmentID, short periodID, byte timeClassID, decimal units, string unitType)
{
DepartmentID = departmentID;
PeriodID = periodID;
TimeClassID = timeClassID;
Units = units;
UnitType = unitType;
}
}