public static Task<List<DashboardException>> GetExceptionsAsync(DashboarLoadParams loadParams) { if (loadParams == null) throw new ArgumentNullException(nameof(loadParams)); var feature = loadParams.FeatureContext.Feature; { var dbContext = loadParams.FeatureContext.DbContext; var settings = loadParams.Settings; var relativeTimePeriod = settings.ExceptionsRelativeTimePeriod; var samples = settings.ExceptionsChartEntries; var rangeTimePeriod = relativeTimePeriod.ToRange(DateTime.Now); var unit = ((long)relativeTimePeriod.TimeOffset.TotalMilliseconds) / samples; var baseDate = rangeTimePeriod.FromDate; var map = new int[samples]; var statement = @"SELECT CREATED_AT FROM FEATURE_EXCEPTION_ENTRIES WHERE @FROMDATE <= CREATED_AT AND CREATED_AT <= @TODATE"; var sqlParams = new[] { new QueryParameter(@"@FROMDATE", rangeTimePeriod.FromDate), new QueryParameter(@"@TODATE", rangeTimePeriod.ToDate), }; var query = new Query(statement, sqlParams); dbContext.Fill(new Dictionary<long, int>(0), (r, m) => { var delta = ((long)(r.GetDateTime(0) - baseDate).TotalMilliseconds) / unit; map[delta]++; }, query); var result = new List<DashboardException>(samples); for (var i = 0; i < map.Length; i++) { result.Add(new DashboardException(baseDate.AddMilliseconds(unit * i), map[i])); } return Task.FromResult(result); } }
public static Task<List<DashboardVersion>> GetVersionsAsync(DashboarLoadParams loadParams) { if (loadParams == null) throw new ArgumentNullException(nameof(loadParams)); var context = loadParams.FeatureContext; var feature = context.Feature; { var maxVersions = loadParams.Settings.MaxVersions; var statement = @"SELECT ID, NAME FROM FEATURE_VERSIONS ORDER BY ID DESC LIMIT @MAXVERSIONS"; var sqlParams = new[] { new QueryParameter(@"@MAXVERSIONS", maxVersions), }; var query = new Query<DbFeatureVersionRow>(statement, r => new DbFeatureVersionRow(r.GetInt32(0), r.GetString(1)), sqlParams); var dbContext = context.DbContext; var versions = dbContext.Execute(query); var exceptionsByVersion = CountExceptionsByVersion(dbContext); var usersByVersion = CountUsersByVersion(dbContext); var dashboardVersions = new List<DashboardVersion>(versions.Count); foreach (var version in versions) { var versionId = version.Id; int usersCount; usersByVersion.TryGetValue(versionId, out usersCount); int exceptionsCount; exceptionsByVersion.TryGetValue(versionId, out exceptionsCount); dashboardVersions.Add(new DashboardVersion(version, usersCount, exceptionsCount)); } dashboardVersions.Sort((x, y) => { var cmp = string.Compare(x.Version.Name, y.Version.Name, StringComparison.OrdinalIgnoreCase); return cmp; }); return Task.FromResult(dashboardVersions); } }
private static Dictionary<long, VersionRow> GetVersions(IDbContext context, List<ExceptionEntryRow> entries) { var values = new Dictionary<long, VersionRow>(entries.Count); var batchSize = 16; var sqlParams = CreateBatchParams(batchSize); var query = new Query(@"SELECT ID, NAME FROM FEATURE_VERSIONS WHERE ID IN (@p0,@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15)", sqlParams); var index = 0; foreach (var id in entries.Select(r => r.VersionId).Distinct()) { if (index < batchSize) { sqlParams[index++].Value = id; continue; } context.Fill(values, (r, map) => { var row = new VersionRow(r.GetInt64(0), r.GetString(1)); map.Add(row.Id, row); }, query); sqlParams[0].Value = id; index = 1; } if (index > 0) { for (var i = index; i < sqlParams.Length; i++) { sqlParams[i].Value = -1; } context.Fill(values, (r, map) => { var row = new VersionRow(r.GetInt64(0), r.GetString(1)); map.Add(row.Id, row); }, query); } return values; }
public static Task<List<DashboardUser>> GetUsersAsync(DashboarLoadParams loadParams) { if (loadParams == null) throw new ArgumentNullException(nameof(loadParams)); var context = loadParams.FeatureContext; { var statement = @"SELECT U.ID, U.NAME UNAME, U.REPLICATED_AT, V.NAME VNAME FROM FEATURE_USERS U INNER JOIN FEATURE_VERSIONS V ON U.VERSION_ID = V.ID ORDER BY REPLICATED_AT DESC LIMIT @MAXUSERS"; var sqlParams = new[] { new QueryParameter(@"@MAXUSERS", loadParams.Settings.MaxUsers), }; var query = new Query<DashboardUser>(statement, r => new DashboardUser(r.GetInt64(0), r.GetString(1), r.GetDateTime(2), r.GetString(3)), sqlParams); var users = context.DbContext.Execute(query); return Task.FromResult(users); } }
public static long ExecuteInsert(IDbContext context, Query query) { if (context == null) throw new ArgumentNullException(nameof(context)); if (query == null) throw new ArgumentNullException(nameof(query)); // Insert the record context.Execute(query); return GetNewId(context); }