private List <EventItem> GetEvents(Event e, List <DataItemInfo> dataItemInfos, List <Sample> samples, DateTime from)
        {
            var l = new List <EventItem>();

            if (!samples.IsNullOrEmpty())
            {
                // Create a list of SampleInfo objects with DataItem information contained
                var infos = SampleInfo.Create(dataItemInfos, samples);

                // Get a list of instance values
                var instance = infos.FindAll(o => o.Timestamp <= from);

                // Find all distinct timestamps greater than or equal to 'from'
                var timestamps = infos.FindAll(o => o.Timestamp > from).Select(o => o.Timestamp).Distinct().OrderBy(o => o).ToList();

                int      i         = 0;
                DateTime timestamp = from;

                do
                {
                    // Evaluate Event
                    var response = e.Evaluate(instance);
                    if (response != null)
                    {
                        var item = new EventItem();
                        item.Timestamp        = response.Timestamp;
                        item.Name             = e.Name;
                        item.Description      = e.Description;
                        item.Value            = response.Value;
                        item.ValueDescription = response.Description;

                        l.Add(item);
                    }

                    if (timestamps.Count > 0)
                    {
                        // Update instance values
                        var atTimestamp = infos.FindAll(o => o.Timestamp == timestamps[i]);
                        foreach (var sample in atTimestamp)
                        {
                            var match = instance.Find(o => o.Id == sample.Id);
                            if (match != null)
                            {
                                instance.Remove(match);
                            }
                            instance.Add(sample);
                        }
                    }
                    else
                    {
                        break;
                    }

                    i++;
                } while (i < timestamps.Count - 1);
            }

            return(l);
        }
        public bool GetResponse(Uri requestUri, Stream stream)
        {
            var query = new RequestQuery(requestUri);

            if (query.IsValid)
            {
                try
                {
                    List <ComponentDefinition> components = null;
                    List <DataItemDefinition>  dataItems  = null;

                    // Get Current Agent
                    var agent = Database.ReadAgent(query.DeviceId);
                    if (agent != null)
                    {
                        var e = GetEvent("Program Status", agent.Version);
                        if (e != null)
                        {
                            // Get Components
                            components = Database.ReadComponents(query.DeviceId, agent.InstanceId);

                            // Get Data Items
                            dataItems = Database.ReadDataItems(query.DeviceId, agent.InstanceId);

                            if (!dataItems.IsNullOrEmpty())
                            {
                                var ids = GetEventIds(e, dataItems, components);
                                if (!ids.IsNullOrEmpty())
                                {
                                    // Program Name DataItem
                                    var programNameItem = dataItems.Find(o => o.Type == "PROGRAM");
                                    if (programNameItem != null)
                                    {
                                        if (!ids.Exists(o => o == programNameItem.Id))
                                        {
                                            ids.Add(programNameItem.Id);
                                        }
                                    }

                                    // Execution DataItem
                                    var executionItem = dataItems.Find(o => o.Type == "EXECUTION");
                                    if (executionItem != null)
                                    {
                                        if (!ids.Exists(o => o == executionItem.Id))
                                        {
                                            ids.Add(executionItem.Id);
                                        }
                                    }

                                    // Get Samples
                                    var samples = Database.ReadSamples(ids.ToArray(), query.DeviceId, query.From, query.To, DateTime.MinValue, 500000);
                                    if (!samples.IsNullOrEmpty())
                                    {
                                        // Get the initial timestamp
                                        //DateTime timestamp = samples.Select(o => o.Timestamp).OrderBy(o => o).First();
                                        DateTime timestamp;
                                        if (query.From > DateTime.MinValue)
                                        {
                                            timestamp = query.From;
                                        }
                                        else
                                        {
                                            timestamp = samples.Select(o => o.Timestamp).OrderByDescending(o => o).First();
                                        }

                                        // Create a list of DataItemInfos (DataItems with Parent Component info)
                                        var dataItemInfos = DataItemInfo.CreateList(dataItems, components);

                                        // Get Path Components
                                        var paths = components.FindAll(o => o.Type == "Path");

                                        var currentSamples = samples.FindAll(o => o.Timestamp <= timestamp);

                                        if (programNameItem != null && executionItem != null)
                                        {
                                            // Previous variables
                                            DateTime previousTime        = DateTime.MinValue;
                                            string   previousValue       = null;
                                            string   previousProgramName = null;

                                            // Stored variables
                                            var  parts = new List <Part>();
                                            Part part  = null;

                                            // Get distinct timestamps
                                            var timestamps = samples.FindAll(o => o.Timestamp >= timestamp).OrderBy(o => o.Timestamp).Select(o => o.Timestamp).Distinct().ToList();
                                            for (int i = 0; i < timestamps.Count; i++)
                                            {
                                                var time = timestamps[i];

                                                // Update CurrentSamples
                                                foreach (var sample in samples.FindAll(o => o.Timestamp == time))
                                                {
                                                    int j = currentSamples.FindIndex(o => o.Id == sample.Id);
                                                    if (j >= 0)
                                                    {
                                                        currentSamples[j] = sample;
                                                    }
                                                    else
                                                    {
                                                        currentSamples.Add(sample);
                                                    }
                                                }


                                                // Program Name
                                                string programName = null;
                                                if (currentSamples.Exists(o => o.Id == programNameItem.Id))
                                                {
                                                    programName = currentSamples.Find(o => o.Id == programNameItem.Id).CDATA;
                                                }

                                                // Execution
                                                string execution = null;
                                                if (currentSamples.Exists(o => o.Id == executionItem.Id))
                                                {
                                                    execution = currentSamples.Find(o => o.Id == executionItem.Id).CDATA;
                                                }

                                                // Create a list of SampleInfo objects with DataItem information contained
                                                var infos = SampleInfo.Create(dataItemInfos, currentSamples);

                                                // Evaluate the Event and get the Response
                                                var response = e.Evaluate(infos);
                                                if (response != null)
                                                {
                                                    if (part != null)
                                                    {
                                                        // Update the program stop time
                                                        part.Stop = time;

                                                        // Check if program changed
                                                        if (part != null && programName != previousProgramName)
                                                        {
                                                            part          = null;
                                                            previousValue = null;
                                                        }
                                                    }


                                                    if (part == null && !string.IsNullOrEmpty(programName) && programName != "UNAVAILABLE" &&
                                                        response.Value != "Stopped" && response.Value != "Completed")
                                                    {
                                                        // Create a new Part object
                                                        part             = new Part();
                                                        part.ProgramName = programName;
                                                        part.Start       = time;
                                                    }


                                                    if (part != null)
                                                    {
                                                        if (response.Value != previousValue)
                                                        {
                                                            if (response.Value == "Stopped" || response.Value == "Completed")
                                                            {
                                                                parts.Add(part);
                                                                part = null;
                                                            }
                                                        }
                                                    }

                                                    previousValue = response.Value;
                                                    previousTime  = time;
                                                }

                                                previousProgramName = programName;
                                            }

                                            var toTime = query.To > DateTime.MinValue ? query.To : DateTime.UtcNow;

                                            if (part != null)
                                            {
                                                part.Stop = toTime;
                                                parts.Add(part);
                                            }

                                            if (!parts.IsNullOrEmpty())
                                            {
                                                var partIds = parts.Select(o => o.Id).ToArray();

                                                // Get Rejected Parts
                                                var rejectedParts = Database.ReadRejectedParts(query.DeviceId, partIds, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
                                                if (!rejectedParts.IsNullOrEmpty())
                                                {
                                                    foreach (var matchedPart in parts)
                                                    {
                                                        var rejectedPart = rejectedParts.Find(o => o.PartId == matchedPart.Id);
                                                        if (rejectedPart != null)
                                                        {
                                                            var rejection = new Rejection(rejectedPart.Message, rejectedPart.Timestamp);
                                                            matchedPart.Rejection = rejection;
                                                        }
                                                    }
                                                }

                                                // Get Verified Parts
                                                var verifiedParts = Database.ReadVerifiedParts(query.DeviceId, partIds, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
                                                if (!verifiedParts.IsNullOrEmpty())
                                                {
                                                    foreach (var matchedPart in parts)
                                                    {
                                                        var verifiedPart = verifiedParts.Find(o => o.PartId == matchedPart.Id);
                                                        if (verifiedPart != null)
                                                        {
                                                            var verification = new Verification(verifiedPart.Message, verifiedPart.Timestamp);
                                                            matchedPart.Verification = verification;
                                                        }
                                                    }
                                                }

                                                // Write JSON to stream
                                                string json  = TrakHound.Api.v2.Json.Convert.ToJson(parts, true);
                                                var    bytes = Encoding.UTF8.GetBytes(json);
                                                stream.Write(bytes, 0, bytes.Length);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Info("Parts Stream Closed");
                    log.Trace(ex);
                }

                return(true);
            }

            return(false);
        }
Пример #3
0
        public static List <Part> GetParts(DateTime from, DateTime to, Event e, List <Sample> samples, List <DataItemInfo> dataItemInfos)
        {
            if (!samples.IsNullOrEmpty())
            {
                // Program Name DataItem
                var programNameItem = dataItemInfos.Find(o => o.Type == "PROGRAM");

                // Execution DataItem
                var executionItem = dataItemInfos.Find(o => o.Type == "EXECUTION");

                // Get the initial timestamp
                DateTime timestamp;
                if (from > DateTime.MinValue)
                {
                    timestamp = from;
                }
                else
                {
                    timestamp = samples.Select(o => o.Timestamp).OrderByDescending(o => o).First();
                }

                var currentSamples = samples.FindAll(o => o.Timestamp <= timestamp);

                if (programNameItem != null && executionItem != null)
                {
                    // Previous variables
                    DateTime previousTime        = DateTime.MinValue;
                    string   previousValue       = null;
                    string   previousProgramName = null;

                    // Stored variables
                    var  parts = new List <Part>();
                    Part part  = null;

                    // Get distinct timestamps
                    var timestamps = samples.FindAll(o => o.Timestamp >= timestamp).OrderBy(o => o.Timestamp).Select(o => o.Timestamp).Distinct().ToList();
                    for (int i = 0; i < timestamps.Count; i++)
                    {
                        var time = timestamps[i];

                        // Update CurrentSamples
                        foreach (var sample in samples.FindAll(o => o.Timestamp == time))
                        {
                            int j = currentSamples.FindIndex(o => o.Id == sample.Id);
                            if (j >= 0)
                            {
                                currentSamples[j] = sample;
                            }
                            else
                            {
                                currentSamples.Add(sample);
                            }
                        }


                        // Program Name
                        string programName = null;
                        if (currentSamples.Exists(o => o.Id == programNameItem.Id))
                        {
                            programName = currentSamples.Find(o => o.Id == programNameItem.Id).CDATA;
                        }

                        // Execution
                        string execution = null;
                        if (currentSamples.Exists(o => o.Id == executionItem.Id))
                        {
                            execution = currentSamples.Find(o => o.Id == executionItem.Id).CDATA;
                        }

                        // Create a list of SampleInfo objects with DataItem information contained
                        var infos = SampleInfo.Create(dataItemInfos, currentSamples);

                        // Evaluate the Event and get the Response
                        var response = e.Evaluate(infos);
                        if (response != null)
                        {
                            if (part != null)
                            {
                                // Update the program stop time
                                part.Stop = time;

                                // Check if program changed
                                if (part != null && programName != previousProgramName)
                                {
                                    part          = null;
                                    previousValue = null;
                                }
                            }


                            if (part == null && !string.IsNullOrEmpty(programName) && programName != "UNAVAILABLE" &&
                                response.Value != "Stopped" && response.Value != "Completed")
                            {
                                // Create a new Part object
                                part             = new Part();
                                part.ProgramName = programName;
                                part.Start       = time;
                            }


                            if (part != null)
                            {
                                if (response.Value != previousValue)
                                {
                                    if (response.Value == "Stopped" || response.Value == "Completed")
                                    {
                                        parts.Add(part);
                                        part = null;
                                    }
                                }
                            }

                            previousValue = response.Value;
                            previousTime  = time;
                        }

                        previousProgramName = programName;
                    }

                    var toTime = to > DateTime.MinValue ? to : DateTime.UtcNow;

                    if (part != null)
                    {
                        part.Stop = toTime;
                        parts.Add(part);
                    }

                    return(parts);
                }
            }

            return(null);
        }
Пример #4
0
        public static Availability Get(DateTime from, DateTime to, Event e, List <Sample> samples, List <DataItemInfo> dataItemInfos, bool details)
        {
            if (!samples.IsNullOrEmpty())
            {
                // Get the initial timestamp
                DateTime timestamp;
                if (from > DateTime.MinValue)
                {
                    timestamp = from;
                }
                else
                {
                    timestamp = samples.Select(o => o.Timestamp).OrderByDescending(o => o).First();
                }

                var instanceSamples = samples.FindAll(o => o.Timestamp <= timestamp);

                double operatingTime = 0;
                bool   addPrevious   = false;
                string previousEvent = null;

                var events = new List <AvailabilityEvent>();

                // Get Distinct Timestamps
                var timestamps = new List <DateTime>();
                timestamps.Add(from);
                timestamps.AddRange(samples.FindAll(o => o.Timestamp >= from).Select(o => o.Timestamp).Distinct().ToList());
                timestamps.Add(to);
                timestamps.Sort();

                var filteredSamples = samples.ToList();

                // Calculate the Operating Time
                for (int i = 0; i < timestamps.Count; i++)
                {
                    var time = timestamps[i];

                    // Update CurrentSamples
                    foreach (var sample in filteredSamples.FindAll(o => o.Timestamp == time))
                    {
                        int j = instanceSamples.FindIndex(o => o.Id == sample.Id);
                        if (j >= 0)
                        {
                            instanceSamples[j] = sample;
                        }
                        else
                        {
                            instanceSamples.Add(sample);
                        }
                    }

                    //Create a list of SampleInfo objects with DataItem information contained
                    var infos = SampleInfo.Create(dataItemInfos, instanceSamples);

                    // Evaluate the Event and get the Response
                    var response = e.Evaluate(infos);
                    if (response != null)
                    {
                        if (addPrevious && i > 0)
                        {
                            var    previousTime = timestamps[i - 1] < from ? from : timestamps[i - 1];
                            double seconds      = (time - previousTime).TotalSeconds;
                            events.Add(new AvailabilityEvent(previousEvent, previousTime, time));
                            operatingTime += seconds;
                        }

                        addPrevious   = response.Value == EVENT_VALUE;
                        previousEvent = response.Value;
                    }
                }

                var toTimestamp = to > DateTime.MinValue ? to : DateTime.UtcNow;

                if (addPrevious)
                {
                    var eventDuration = (toTimestamp - timestamps[timestamps.Count - 1]).TotalSeconds;
                    if (eventDuration > 0)
                    {
                        operatingTime += eventDuration;
                        events.Add(new AvailabilityEvent(previousEvent, timestamps[timestamps.Count - 1], toTimestamp));
                    }
                }

                // Calculate the TotalTime that is being evaluated
                var totalTime = (toTimestamp - timestamps[0]).TotalSeconds;

                var availability = new Availability(operatingTime, totalTime, events);
                if (details)
                {
                    availability.Events = availability._events;
                }

                return(availability);
            }

            return(null);
        }