Example #1
0
        private static void DeviceHealthStatusInitialize(Kiosk dev)
        {
            using (
                var ctx =
                    ContextRegistry.NamedContextsFor(ContextRegistry.CreateNamed(ContextRegistry.Kind,
                                                                                 UnikContextTypes.
                                                                                 UnikWarehouseContextResourceKind)))
            {
                using (var dc = DocumentStoreLocator.ContextualResolve())
                {
                    var deviceHealthChangeEvt = new DeviceHealthStatusEvent
                    {
                        DeviceId    = dev.Id,
                        DeviceName  = dev.Name,
                        From        = HealthStatus.Unknown,
                        To          = HealthStatus.Green,
                        DeviceTags  = dev.Tags,
                        TimeChanged = DateTime.UtcNow
                    };

                    dc.Store(deviceHealthChangeEvt);
                    dc.SaveChanges();
                }
            }
        }
        public HealthStatus ProcessAlerts(IDevice device, IEnumerable<Alert> alerts)
        {
            HealthStatus formerStatus = device.CurrentStatus;
            HealthStatus retval = device.CurrentStatus;

            var alertStatusEvents = new List<AlertStatusChangeEvent>();

            if (alerts.Any())
            {
                using ( var ctx = ContextRegistry.NamedContextsFor(
                        ContextRegistry.CreateNamed(ContextRegistry.Kind,
                        UnikContextTypes.UnikTenantContextResourceKind)))
                {
                    var batches = alerts.InBatchesOf(50);
                    foreach (var batch in batches)
                    {
                        //using (var dc = DocumentStoreLocator.ContextualResolve())
                        //{
                            foreach (var it in batch)
                            {
                                int matchHash = it.CreateMatchHash();
                                /* var matches = dc.Query<Alert>().Where(a => a.MatchHash == matchHash && a.Status != AlertStatus.Closed);
                                */

                                var querySpecification = new QuerySpecification()
                                {
                                    Where = new Filter
                                    {
                                        Rules = new Comparison[]     {
                                                    new Comparison { Data = matchHash,
                                                            Field = "MatchHash", Test = Test.Equal },
                                                     new Comparison { Data = AlertStatus.Closed,
                                                            Field = "Status", Test = Test.NotEqual }
                                                    }
                                    }
                                };

                                var matches = ReportDataProvider.GetFromRepository<Alert>(querySpecification);

                                bool isDuplicate = false;
                                foreach (var candidate in matches)
                                {
                                    if (candidate.RelatedDevice == it.RelatedDevice && it.Kind == candidate.Kind &&
                                        it.AlertHealthLevel == candidate.AlertHealthLevel)
                                    {
                                        isDuplicate = true;
                                        break;
                                    }
                                }

                                if (!isDuplicate)
                                {
                                    it.MatchHash = matchHash;
                                    if (it.AlertHealthLevel > retval)
                                    {

                                        retval = it.AlertHealthLevel;
                                        device.CurrentStatus = retval;

                                        alertStatusEvents.Add(new AlertStatusChangeEvent
                                        {
                                            Identifier = it.Identifier,
                                            AlertHealthLevel = it.AlertHealthLevel,
                                            AlertTitle = it.AlertTitle,
                                            Kind = it.Kind,
                                            KindDesc = it.Kind.EnumName().SpacePascalCase(),
                                            DeviceTags = it.Tags,
                                            Message = it.Message,
                                            RelatedDevice = it.RelatedDevice,
                                            RelatedDeviceName = it.RelatedDeviceName,
                                            Resolved = false,
                                            TimeGenerated = it.TimeGenerated,
                                            StaleAlertTime = (it.AlertHealthLevel == HealthStatus.Red) ? it.TimeGenerated + TimeSpan.FromHours(6.0) : it.TimeGenerated + TimeSpan.FromHours(24.0),
                                            LongestAssignmentTime = TimeSpan.FromSeconds(0.0)
                                        });
                                    }

                                    ReportDataProvider.StoreInRepository<Alert>(it);
                                    //dc.Store(it);
                                }
                            }

                            //dc.SaveChanges();
                        //}
                    }
                }
            }

            if (formerStatus != retval)
            {
                using (
                    var ctx =
                        ContextRegistry.NamedContextsFor(ContextRegistry.CreateNamed(ContextRegistry.Kind,
                                                                                     UnikContextTypes.
                                                                                         UnikWarehouseContextResourceKind)))
                {
                    //using (var dc = DocumentStoreLocator.ContextualResolve())
                    //{
                        var deviceHealthChangeEvt = new DeviceHealthStatusEvent
                        {
                            DeviceId = device.Id,
                            DeviceName = device.Name,
                            From = formerStatus,
                            To = retval,

                            DeviceTags = device.Tags,
                            DeviceTagHashes = DeviceHealthStatusEvent.ConvertTagsToHashs(device.Tags),
                            TimeChanged = DateTime.UtcNow
                        };

                        //dc.Store(deviceHealthChangeEvt);
                        ReportDataProvider.StoreInRepository<DeviceHealthStatusEvent>(deviceHealthChangeEvt);

                        var batches = alertStatusEvents.InBatchesOf(50);
                        foreach (var batch in batches)
                        {
                            foreach (var se in batch)
                            {
                                //dc.Store(se);
                                ReportDataProvider.StoreInRepository<AlertStatusChangeEvent>(se);
                            }
                            //dc.SaveChanges();
                        }
                    //}
                }
            }
            return retval;
        }