private async Task <long> GetCountQuery(Guid dashboardId, int groupIndex, int itemIndex, ItemDuration duration, DateTime durationFrom, DateTime durationTo, string[] queryParts) { var item = _config.Dashboards[dashboardId].Select(d => d.Value).ToList()[groupIndex][itemIndex]; string itemQuery; if (duration == ItemDuration.Custom) { itemQuery = GetQueryString(item, durationFrom, durationTo, durationFrom.GetIntervalString(durationTo)); } else { var durationString = duration.GetString(); var intervalString = duration.GetIntervalString(); itemQuery = GetQueryString(item, durationString, intervalString); } var structuredQuery = QueryBuilder.Parse(itemQuery); var queryGroup = new QueryGroup(structuredQuery); queryGroup.AddParts(queryParts); queryGroup.RemoveProjectAndSummarize(); queryGroup.Append(QueryBuilder.Parse("| summarize sum(itemCount)")); var query = queryGroup.ToString(); var table = await AppInsightsClient.GetTableQuery(item.ApiToken.Id, item.ApiToken.Key, query); return(table.Rows.First()[0] as long? ?? 0); }
public async Task <dynamic> Analyzer(Guid dashboardId, int groupIndex, int itemIndex, string analyzer, ItemDuration duration, DateTime durationFrom, DateTime durationTo, string searchQuery, string[] queryParts) { var item = _config.Dashboards[dashboardId].Select(d => d.Value).ToList()[groupIndex][itemIndex]; string itemQuery; if (duration == ItemDuration.Custom) { itemQuery = GetQueryString(item, durationFrom, durationTo, durationFrom.GetIntervalString(durationTo)); } else { itemQuery = GetQueryString(item, duration.GetString(), duration.GetIntervalString()); } var query = QueryBuilder.Parse(itemQuery); if (!string.IsNullOrWhiteSpace(searchQuery)) { var queryGroup = new QueryGroup(query); foreach (var searchQueryPart in GetSearchQueryParts(searchQuery)) { queryGroup.AddWhere(searchQueryPart); } query = QueryBuilder.Parse(queryGroup.ToString()); } switch (analyzer) { case "RequestsAnalyzer": return(await RequestsAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "UrlAnalyzer": return(await UrlAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "DomainAnalyzer": return(await DomainAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "RoleAnalyzer": return(await RoleAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "StatusCodesAnalyzer": return(await StatusCodesAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "RequestExceptionsAnalyzer": return(await ExceptionsAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "StacktraceAnalyzer": return(await StacktraceAnalyzer.Analyze(item.ApiToken, query, queryParts)); case "ExceptionMessageAnalyzer": return(await ExceptionMessageAnalyzer.Analyze(item.ApiToken, query, queryParts)); } throw new InvalidOperationException($"Analyzer \"{analyzer}\" was not found."); }
public async Task <dynamic> Details(Guid dashboardId, int groupIndex, int itemIndex, ItemDuration duration, DateTime durationFrom, DateTime durationTo, string searchQuery, string[] queryParts) { var dashboard = _config.Dashboards[dashboardId]; var groupKey = dashboard.Keys.ToList()[groupIndex]; var item = _config.Dashboards[dashboardId].Select(d => d.Value).ToList()[groupIndex][itemIndex]; string itemQuery; string intervalString; if (duration == ItemDuration.Custom) { intervalString = durationFrom.GetIntervalString(durationTo); itemQuery = GetQueryString(item, durationFrom, durationTo, intervalString); } else { intervalString = duration.GetIntervalString(); itemQuery = GetQueryString(item, duration.GetString(), intervalString); } var structuredQuery = QueryBuilder.Parse(itemQuery); var queryGroup = new QueryGroup(structuredQuery); queryGroup.AddParts(queryParts); if (!string.IsNullOrWhiteSpace(searchQuery)) { foreach (var searchQueryPart in GetSearchQueryParts(searchQuery)) { queryGroup.AddWhere(searchQueryPart); } } var values = await GetChartValues(item, queryGroup.ToString()); // Fallback when all values are errors if ((queryGroup.IsRequestsQuery() || queryGroup.IsExceptionsQuery()) && values.Count > 4 && (int)values.OrderByDescending(v => v.Value).Skip(values.Count / 2).Take(values.Count / 2).Max(v => v.Value) == (int)values.OrderByDescending(v => v.Value).Skip(values.Count / 2).Take(values.Count / 2).Average(v => v.Value)) { queryGroup.RemoveProjectAndSummarize(); queryGroup.Append(QueryBuilder.Parse($"summarize _count=sum(itemCount) by bin(timestamp, {intervalString}) | project timestamp, _count")); values = await GetChartValues(item, queryGroup.ToString()); } List <(DateTime Date, double Value)> chart; if (duration == ItemDuration.Custom) { chart = FillEmptySlotsInTimeRange(values, durationFrom, durationTo, intervalString.GetTimeSpan()); } else { chart = FillEmptySlotsInTimeRange(values, duration.GetString().GetTimeSpan(), intervalString.GetTimeSpan()); } var chartMax = chart.Any() ? chart.Max(c => c.Value) : 0; var minMax = item.MinChartValue / item.Duration.GetIntervalString().GetTimeSpan().TotalMinutes *intervalString.GetTimeSpan().TotalMinutes; minMax = Math.Min(minMax, chartMax * 5); var max = Math.Max(minMax, chartMax); queryGroup.RemoveProjectAndSummarize(); var count = await GetCountQuery(dashboardId, groupIndex, itemIndex, duration, durationFrom, durationTo, queryParts); return(new { Name = $"{groupKey} / {item.Name}", ChartValues = chart.Select(c => new { c.Date, c.Value }), ChartMax = max, Query = queryGroup.ToString(), Count = count }); }