コード例 #1
0
ファイル: AlertTests.cs プロジェクト: tdicks/LogicMonitor.Api
        public async void GetAlertsFilteredByDeviceRest()
        {
            var device = await GetWindowsDeviceAsync().ConfigureAwait(false);

            var deviceGroupFullPathFilter = new List <string> {
                "Collectors*"
            };
            const string    dataSourceNameFilter         = "Volume Usage-";
            const string    dataSourceInstanceNameFilter = @"WinVolumeUsage-C:\\";
            const string    dataPointNameFilter          = "Capacity";
            const AckFilter ackFilter    = AckFilter.Acked;
            const SdtFilter sdtFilter    = SdtFilter.Sdt;
            var             levelsFilter = new List <AlertLevel> {
                AlertLevel.Error, AlertLevel.Critical
            };
            bool?includeClearedFilter = true;
            var  utcNow = DateTime.UtcNow;

            var alertFilter = new AlertFilter
            {
                MonitorObjectGroupFullPaths = deviceGroupFullPathFilter,
                MonitorObjectName           = device.DisplayName,
                ResourceTemplateName        = dataSourceNameFilter,
                DataPointName      = dataPointNameFilter,
                InstanceName       = dataSourceInstanceNameFilter,
                AckFilter          = ackFilter,
                SdtFilter          = sdtFilter,
                Levels             = levelsFilter,
                IncludeCleared     = includeClearedFilter,
                StartEpochIsAfter  = utcNow.AddDays(-1).SecondsSinceTheEpoch(),
                StartEpochIsBefore = utcNow.SecondsSinceTheEpoch()
            };
            var alerts = await PortalClient.GetAlertsAsync(alertFilter).ConfigureAwait(false);

            // TODO CheckAlertsAreValid(alerts);

            // Make sure there are no alerts for hosts not mentioned by the hostFilter
            Assert.True(alerts.All(alert => alert.MonitorObjectName == device.DisplayName));
            //Assert.True(alerts.All(alert => alert.MonitorObjectGroups.Any(mog => mog.FullPath.StartsWith(deviceGroupFullPathFilter.Replace("*", "")))));
            Assert.True(alerts.All(alert => alert.InstanceName.StartsWith(dataSourceNameFilter)));
            Assert.True(alerts.All(alert => alert.DataPointName.StartsWith(dataPointNameFilter)));
        }
コード例 #2
0
        public async Task <List <Alert> > GetCachedAlertsAsync(
            long startSeconds,
            long endSeconds,
            bool includeInactive,
            int?skip,
            int?take,
            string id,
            AckFilter ackFilter,
            List <string> monitorObjectGroups,
            string monitorObjectName,
            int?monitorObjectId,
            List <AlertType> alertTypes,
            string resourceTemplateDisplayName,
            int?resourceTemplateId,
            string instanceName,
            string dataPointName,
            List <AlertLevel> alertLevels,
            string orderBy,
            OrderDirection orderDirection,
            SdtFilter sdtFilter,
            string problemSignature,
            bool isPercentageAvailability = false
            )
        {
            using (var context = new Context(DbContextOptions))
            {
                var queryable = context.Alerts.AsQueryable();

                if (id != null)
                {
                    queryable = queryable.Where(a => a.Id == id);
                }
                if (problemSignature != null)
                {
                    queryable = queryable.Where(a => a.InternalId == problemSignature);
                }
                if (monitorObjectId != null)
                {
                    queryable = queryable.Where(a => a.MonitorObjectId == monitorObjectId);
                }
                if (monitorObjectName != null)
                {
                    queryable = queryable.Where(a => a.MonitorObjectName == monitorObjectName);
                }
                if (resourceTemplateId != null)
                {
                    queryable = queryable.Where(a => a.ResourceTemplateId == resourceTemplateId);
                }
                if (resourceTemplateDisplayName != null)
                {
                    queryable = queryable.Where(a => a.ResourceTemplateName == resourceTemplateDisplayName);
                }
                if (instanceName != null)
                {
                    queryable = queryable.Where(a => a.InstanceName == instanceName);
                }
                if (dataPointName != null)
                {
                    queryable = queryable.Where(a => a.DataPointName == dataPointName);
                }
                if (alertLevels != null)
                {
                    queryable = queryable.Where(a => alertLevels.Select(al => (int)al).Contains(a.Severity));
                }
                if (alertTypes != null)
                {
                    queryable = queryable.Where(a => alertTypes.Contains(a.AlertType));
                }
                if (!includeInactive)
                {
                    queryable = queryable.Where(a => !a.IsCleared);
                }

                // Only one is ever passed
                var monitorObjectGroup = monitorObjectGroups?[0];
                if (monitorObjectGroup != null)
                {
                    var likeString = $"{monitorObjectGroup.TrimEnd('*')}%";
                    queryable = queryable
                                .Where(a =>
                                       (a.MonitorObjectGroup0.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup0.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup1.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup1.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup2.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup2.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup3.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup3.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup4.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup4.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup5.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup5.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup6.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup6.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup7.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup7.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup8.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup8.FullPath, likeString)) ||
                                       (a.MonitorObjectGroup9.MonitoredObjectType == a.MonitorObjectType && EF.Functions.Like(a.MonitorObjectGroup9.FullPath, likeString))
                                       );
                }

                switch (sdtFilter)
                {
                case SdtFilter.All:
                    // No constraint
                    break;

                case SdtFilter.Sdt:
                    queryable = queryable.Where(a => a.InScheduledDownTime);
                    break;

                case SdtFilter.NonSdt:
                    queryable = queryable.Where(a => !a.InScheduledDownTime);
                    break;

                default:
                    throw new NotSupportedException($"Unsupported SDT filter {sdtFilter}");
                }
                switch (ackFilter)
                {
                case AckFilter.All:
                    // No constraint
                    break;

                case AckFilter.Acked:
                    queryable = queryable.Where(a => a.Acked);
                    break;

                case AckFilter.Nonacked:
                    queryable = queryable.Where(a => !a.Acked);
                    break;

                default:
                    throw new NotSupportedException($"Unsupported SDT filter {sdtFilter}");
                }

                queryable = queryable.Where(a =>
                                            // Alert is not cleared and it started before the end of the period
                                            (a.StartOnSeconds <= endSeconds && !a.IsCleared)
                                            // Or it starts within the period
                                            || (a.StartOnSeconds > startSeconds && a.StartOnSeconds <= endSeconds)
                                            // Or it ends within the period
                                            || (a.EndOnSeconds > startSeconds && a.EndOnSeconds <= endSeconds)
                                            // Or it covers the entire period
                                            || (a.StartOnSeconds <= startSeconds && a.EndOnSeconds > endSeconds)
                                            );

                if (orderBy != null)
                {
                    switch ($"{orderBy}/{orderDirection}")
                    {
                    case "StartOnSeconds/Asc":
                        queryable = queryable.OrderBy(a => a.StartOnSeconds);
                        break;

                    case "StartOnSeconds/Desc":
                        queryable = queryable.OrderByDescending(a => a.StartOnSeconds);
                        break;

                    default:
                        throw new NotSupportedException("Only orderBy=StartOnSeconds currently supported.");
                    }
                }

                if (skip.HasValue)
                {
                    queryable = queryable.Skip(skip.Value);
                }

                if (take.HasValue)
                {
                    queryable = queryable.Take(take.Value);
                }

                // Use only the needed fields
                if (isPercentageAvailability)
                {
                    queryable = queryable.Select(a => new AlertStoreItem
                    {
                        Id                   = a.Id,
                        StartOnSeconds       = a.StartOnSeconds,
                        EndOnSeconds         = a.EndOnSeconds,
                        IsCleared            = a.IsCleared,
                        ClearValue           = a.ClearValue,
                        Severity             = a.Severity,
                        MonitorObjectId      = a.MonitorObjectId,
                        InstanceId           = a.InstanceId,
                        InstanceName         = a.InstanceName,
                        ResourceTemplateName = a.ResourceTemplateName
                    });
                }

                return((await queryable.ToListAsync().ConfigureAwait(false))
                       .Select(DatamartClient.MapperInstance.Map <AlertStoreItem, Alert>)
                       .ToList());
            }
        }