/// <summary> /// Creates a new object from the underlying failing query. /// </summary> /// <param name="underlyingFailingQuery">The underlying failing query dataset.</param> /// <param name="penaltyInfo">The penalty info.</param> /// <remarks> /// Yes, this is code duplication with BgpRestApiController and a common base-class would make sense design-wise (is-a FailingQuery). /// But this has been added later and the underlying classes are Entity Framework Database Models. /// I'm simply scared of modifying them to have a common base class. Not sure if EF migration framework is able to handle this. /// </remarks> public RssiFailingQueryWithPenaltyInfo(RssiFailingQuery underlyingFailingQuery, ISingleFailureInfo penaltyInfo) { if (underlyingFailingQuery == null) { throw new ArgumentNullException(nameof(underlyingFailingQuery), "The underlying failing query data is null"); } this.AffectedHosts = underlyingFailingQuery.AffectedHosts; this.ErrorInfo = underlyingFailingQuery.ErrorInfo; this.Subnet = underlyingFailingQuery.Subnet; this.TimeStamp = underlyingFailingQuery.TimeStamp; this.PenaltyInfo = underlyingFailingQuery.PenaltyInfo; if (penaltyInfo != null) { // only replacing the object that has potentially been retrieved from database // if we have a more recent one directly from handler. if (this.PenaltyInfo == null) { // we have not penalty info -> unconditionally set the new one this.PenaltyInfo = penaltyInfo; } else if (this.PenaltyInfo.LastOccurance <= penaltyInfo.LastOccurance) { // we have a penalty info -> only set if new one is newer or same this.PenaltyInfo = penaltyInfo; } } }
/// <summary> /// Records a failing query. /// </summary> /// <param name="databaseContext">The database context to work with.</param> /// <param name="ex">The exception that caused the failure.</param> /// <param name="pair">The pair of hosts inside the subnet to query.</param> private void DoRecordFailingRssiQueryEntry(QueryResultDatabaseContext databaseContext, Exception ex, KeyValuePair <IHamnetDbSubnet, IHamnetDbHosts> pair) { var failingSubnetString = pair.Key.Subnet.ToString(); var failEntry = databaseContext.RssiFailingQueries.Find(failingSubnetString); var hamnetSnmpEx = ex as HamnetSnmpException; if (failEntry == null) { failEntry = new RssiFailingQuery { Subnet = failingSubnetString, AffectedHosts = (hamnetSnmpEx != null) ? hamnetSnmpEx.AffectedHosts : pair.Value.Select(h => h.Address?.ToString()).ToArray() }; databaseContext.RssiFailingQueries.Add(failEntry); } failEntry.TimeStamp = DateTime.UtcNow; failEntry.PenaltyInfo = this.failureRetryFilteringDataHandler?.QueryPenaltyDetails(QueryType.RssiQuery, pair.Key.Subnet); #if DEBUG failEntry.ErrorInfo = ex?.ToString() ?? string.Empty; #else failEntry.ErrorInfo = ex?.Message ?? string.Empty; #endif }