public async Task <MonthlyAggregateCorporation> GetOrCreateMonthlyAggregateCorporation(Corporation corporation, DateTime date) { var dateMonth = new DateTime(date.Year, date.Month, 1); MonthlyAggregateCorporation aggregate = await GetMonthlyAggregateCorporation(corporation.Id, dateMonth); if (aggregate == null) { aggregate = new MonthlyAggregateCorporation { CorporationID = corporation.Id, TimeStamp = dateMonth }; context.Attach(aggregate); } return(aggregate); }
public async Task ProcessKillmailValue(Killmail killmail, float value) { // Check wormmhole kill if (IsWormholeKill(killmail)) { EveType victimType = await eSIService.GetEveType(killmail.Victim.ShipTypeId); // Process Victim if (killmail.Victim.CorporationId != null) { _logger.LogDebug("Loss for corp: {0}", killmail.Victim.CorporationId); var corp = await GetOrCreateCorporation(killmail.Victim.CorporationId.Value); DailyAggregateCorporation victimAggregate = await GetOrCreateDailyAggregateCorporation(corp, killmail.KillmailTime.Date); MonthlyAggregateCorporation victimAggregateMonthly = await GetOrCreateMonthlyAggregateCorporation(corp, killmail.KillmailTime.Date); IncrementAggregate(victimAggregate, victimType, value, killmail.Victim.DamageTaken, false); IncrementAggregate(victimAggregateMonthly, victimType, value, killmail.Victim.DamageTaken, false); } if (killmail.Victim.AllianceId != null) { var alliance = await GetOrCreateAlliance(killmail.Victim.AllianceId.Value); DailyAggregateAlliance victimAggregate = await GetOrCreateDailyAggregateAlliance(alliance, killmail.KillmailTime.Date); MonthlyAggregateAlliance victimAggregateMonthly = await GetOrCreateMonthlyAggregateAlliance(alliance, killmail.KillmailTime.Date); IncrementAggregate(victimAggregate, victimType, value, killmail.Victim.DamageTaken, false); IncrementAggregate(victimAggregateMonthly, victimType, value, killmail.Victim.DamageTaken, false); } // Process Attackers var attackerCorporations = killmail.Attackers.Where(a => a.CorporationId != null && a.CorporationId != killmail.Victim.CorporationId).Select(a => a.CorporationId.Value).Distinct(); foreach (var corporationId in attackerCorporations) { var corp = await GetOrCreateCorporation(corporationId); _logger.LogDebug("Kill for corp: {0}", killmail.Victim.CorporationId); DailyAggregateCorporation attackerAggregate = await GetOrCreateDailyAggregateCorporation(corp, killmail.KillmailTime.Date); MonthlyAggregateCorporation monthlyAggregateCorporation = await GetOrCreateMonthlyAggregateCorporation(corp, killmail.KillmailTime.Date); IncrementAggregate(attackerAggregate, victimType, value, killmail.Attackers.Where(a => a.CorporationId == corp.Id).Sum(a => a.DamageDone), true); IncrementAggregate(monthlyAggregateCorporation, victimType, value, killmail.Attackers.Where(a => a.CorporationId == corp.Id).Sum(a => a.DamageDone), true); } var attackerAlliances = killmail.Attackers.Where(a => a.AllianceId != null && a.AllianceId != killmail.Victim.AllianceId).Select(a => a.AllianceId.Value).Distinct(); foreach (var allianceId in attackerAlliances) { var alliance = await GetOrCreateAlliance(allianceId); DailyAggregateAlliance attackerAggregate = await GetOrCreateDailyAggregateAlliance(alliance, killmail.KillmailTime.Date); MonthlyAggregateAlliance monthlyAggregateAlliance = await GetOrCreateMonthlyAggregateAlliance(alliance, killmail.KillmailTime.Date); IncrementAggregate(attackerAggregate, victimType, value, killmail.Attackers.Where(a => a.AllianceId == alliance.Id).Sum(a => a.DamageDone), true); IncrementAggregate(monthlyAggregateAlliance, victimType, value, killmail.Attackers.Where(a => a.AllianceId == alliance.Id).Sum(a => a.DamageDone), true); } await context.SaveChangesAsync(); } }