public static IEnumerable<ExceptionsCount> GetExceptionsCounts(ExceptionsDataLoadParams dataLoadParams)
        {
            if (dataLoadParams == null) throw new ArgumentNullException(nameof(dataLoadParams));

            var query = @"
            SELECT CREATED_AT FROM FEATURE_EXCEPTION_ENTRIES
            WHERE (@VERSION IS NULL OR VERSION_ID = @VERSION)
            AND (@FROMDATE <= CREATED_AT AND CREATED_AT <= @TODATE)";

            if (dataLoadParams.Settings.RemoveExcluded)
            {
                query = @"
                SELECT CREATED_AT FROM FEATURE_EXCEPTION_ENTRIES E
                WHERE (@VERSION IS NULL OR VERSION_ID = @VERSION)
                AND (@FROMDATE <= CREATED_AT AND CREATED_AT <= @TODATE)
                AND NOT EXISTS (SELECT 1 FROM FEATURE_EXCEPTIONS_EXCLUDED EX WHERE E.EXCEPTION_ID = EX.EXCEPTION_ID)";
            }

            var versionId = default(long?);
            var version = dataLoadParams.Version;
            if (version != null)
            {
                var value = version.Row.Id;
                if (value > 0)
                {
                    versionId = value;
                }
            }

            var toDate = DateTime.Now;
            var timePeriodRow = dataLoadParams.TimePeriod.Row;
            var fromDate = toDate.Add(-timePeriodRow.TimeOffset);

            var sqlParams = new[]
            {
                new QueryParameter(@"@VERSION", versionId),
                new QueryParameter(@"@FROMDATE", fromDate),
                new QueryParameter(@"@TODATE", toDate),
            };

            var bukets = new int[timePeriodRow.ChartSamples];
            var interval = (toDate - fromDate).TotalSeconds / bukets.Length;

            var _ = new Dictionary<int, int>(0);
            dataLoadParams.Context.Fill(_, (r, m) =>
            {
                bukets[Math.Min((int)(Math.Round((toDate - r.GetDateTime(0)).TotalSeconds / interval, MidpointRounding.AwayFromZero)), bukets.Length - 1)]++;
            }, new Query(query, sqlParams));

            var counts = new ExceptionsCount[bukets.Length];
            for (var i = 0; i < bukets.Length; i++)
            {
                counts[i] = new ExceptionsCount(fromDate.AddSeconds(interval * i), bukets[bukets.Length - 1 - i]);
            }

            return counts;
        }
Exemple #2
0
		private ExceptionsDataLoadParams GetExceptionsDataLoadParams(IDbContext context, ExceptionsSettings settings)
		{
			var loadParams = new ExceptionsDataLoadParams(context, settings)
			{
				Version = this.Version,
				TimePeriod = this.TimePeriod
			};
			return loadParams;
		}
        public static IEnumerable<FeatureExceptionEntry> GetExceptions(ExceptionsDataLoadParams dataLoadParams)
        {
            if (dataLoadParams == null) throw new ArgumentNullException(nameof(dataLoadParams));

            var settings = dataLoadParams.Settings;

            var query = @"
            SELECT E.ID, E.FEATURE_ID, E.EXCEPTION_ID, E.USER_ID, E.VERSION_ID, E.CREATED_AT
            FROM FEATURE_EXCEPTION_ENTRIES E
            ORDER BY E.CREATED_AT DESC
            LIMIT @maxEntries";

            if (settings.RemoveExcluded)
            {
                query = @"
            SELECT E.ID, E.FEATURE_ID, E.EXCEPTION_ID, E.USER_ID, E.VERSION_ID, E.CREATED_AT
            FROM FEATURE_EXCEPTION_ENTRIES E
            WHERE NOT EXISTS
                (SELECT 1
                    FROM FEATURE_EXCEPTIONS_EXCLUDED EX
                    WHERE E.EXCEPTION_ID = EX.EXCEPTION_ID)
            ORDER BY E.CREATED_AT DESC
            LIMIT @maxEntries";
            }

            var sqlParams = new[]
            {
                new QueryParameter(@"@maxEntries", settings.MaxExceptionEntries)
            };

            var context = dataLoadParams.Context;
            var exceptionEntries = context.Execute(new Query<ExceptionEntryRow>(query, FeatureExceptionEntryRowCreator, sqlParams));
            if (exceptionEntries.Count == 0) return Enumerable.Empty<FeatureExceptionEntry>().ToArray();

            var features = GetFeatures(context, exceptionEntries);
            var exceptions = GetExceptions(context, exceptionEntries);
            var users = GetUsers(context, exceptionEntries);
            var versions = GetVersions(context, exceptionEntries);

            var featureExceptions = new FeatureExceptionEntry[exceptionEntries.Count];
            for (var i = 0; i < exceptionEntries.Count; i++)
            {
                var r = exceptionEntries[i];
                featureExceptions[i] = new FeatureExceptionEntry(r.Id, features[r.FeatureId], exceptions[r.ExceptionId], users[r.UserId], versions[r.VersionId], r.CreatedAt);
            }

            return featureExceptions;
        }