public QLAfElementTemplateResolve(AFElementTemplate aAfElementTemplate, Field afElementsField)
        {
            name = aAfElementTemplate.Name;

            if (afElementsField != null)
            {
                var afElementsNameFilterStrings           = GraphQlHelpers.GetArgumentStrings(afElementsField, "nameFilter");
                var afElementsAttributeValueFilterStrings = GraphQlHelpers.GetArgumentStrings(afElementsField, "attributeValueFilter");

                var afElementsChildField   = GraphQlHelpers.GetFieldFromFieldOrContext(afElementsField, "afElements");
                var afAttributesChildField = GraphQlHelpers.GetFieldFromFieldOrContext(afElementsField, "afAttributes");

                var returnObject = new ConcurrentBag <QLAfElement>();

                List <AFElement> afElementList = aAfElementTemplate.FindInstantiatedElements(true, OSIsoft.AF.AFSortField.Name, OSIsoft.AF.AFSortOrder.Ascending, 10000).Select(x => x as AFElement).Where(x => x != null).ToList();
                Parallel.ForEach(afElementList, aAfChildElement =>
                {
                    if (GraphQlHelpers.JudgeElementOnFilters(aAfChildElement, afElementsNameFilterStrings, afElementsAttributeValueFilterStrings))
                    {
                        returnObject.Add(new QLAfElementResolve(aAfChildElement, afElementsChildField, afAttributesChildField));
                    }
                });

                afElements = returnObject.OrderBy(x => x.name).ToList();
            }
        }
Beispiel #2
0
        private void CreatePIPoints(AFDatabase afDatabase)
        {
            AFElementTemplate elemTemplate = afDatabase.ElementTemplates["BasicBoilerTemplate"];
            AFNamedCollectionList <AFBaseElement> baseElements = elemTemplate.FindInstantiatedElements(
                includeDerived: false,
                sortField: AFSortField.Name,
                sortOrder: AFSortOrder.Ascending,
                maxCount: 1000);

            foreach (AFBaseElement baseElement in baseElements)
            {
                int numModified = AFDataReference.CreateConfig(baseElement, false, null);
                Console.WriteLine("Modified or created: {0}", numModified);
            }
        }
        public static IList <AFElement> LoadElements(AFElementTemplate elementTemplate, IEnumerable <string> attributesToLoad)
        {
            int totalCount;
            int startIndex = 0;
            int pageSize   = 1000;

            List <AFElement> results = new List <AFElement>();

            // Paging pattern
            do
            {
                var baseElements = elementTemplate.FindInstantiatedElements(
                    includeDerived: true,
                    sortField: AFSortField.Name,
                    sortOrder: AFSortOrder.Ascending,
                    startIndex: startIndex,
                    maxCount: pageSize,
                    totalCount: out totalCount);

                // If there are no elements, break the process
                if (baseElements.Count == 0)
                {
                    break;
                }

                IEnumerable <AFElement> elements = baseElements.OfType <AFElement>();

                IEnumerable <IGrouping <AFElementTemplate, AFElement> > elementGroupings = elements.GroupBy(elm => elm.Template);
                foreach (var item in elementGroupings)
                {
                    // The passed in attribute template name may belong to a base element template.
                    // GetLastAttributeTemplateOverride searches upwards the template inheritance chain
                    // until it finds the desired attribute template.
                    List <AFAttributeTemplate> attrTemplates = attributesToLoad
                                                               .Select(atr => GetLastAttributeTemplateOverride(item.Key, atr))
                                                               .Where(atr => atr != null)
                                                               .ToList();

                    List <AFElement> elementsToLoad = item.ToList();
                    AFElement.LoadAttributes(elementsToLoad, attrTemplates);
                    results.AddRange(elementsToLoad);
                }

                startIndex += baseElements.Count;
            } while (startIndex < totalCount);

            return(results);
        }
        public static IList<AFElement> LoadElements(AFElementTemplate elementTemplate, IEnumerable<string> attributesToLoad)
        {
            int totalCount;
            int startIndex = 0;
            int pageSize = 1000;

            List<AFElement> results = new List<AFElement>();

            // Paging pattern
            do
            {
                var baseElements = elementTemplate.FindInstantiatedElements(
                                includeDerived: true,
                                sortField: AFSortField.Name,
                                sortOrder: AFSortOrder.Ascending,
                                startIndex: startIndex,
                                maxCount: pageSize,
                                totalCount: out totalCount);

                // If there are no elements, break the process
                if (baseElements.Count == 0)
                    break;

                IEnumerable<AFElement> elements = baseElements.OfType<AFElement>();

                IEnumerable<IGrouping<AFElementTemplate, AFElement>> elementGroupings = elements.GroupBy(elm => elm.Template);
                foreach (var item in elementGroupings)
                {
                    // The passed in attribute template name may belong to a base element template.
                    // GetLastAttributeTemplateOverride searches upwards the template inheritance chain
                    // until it finds the desired attribute template.
                    List<AFAttributeTemplate> attrTemplates = attributesToLoad
                        .Select(atr => GetLastAttributeTemplateOverride(item.Key, atr))
                        .Where(atr => atr != null)
                        .ToList();

                    List<AFElement> elementsToLoad = item.ToList();
                    AFElement.LoadAttributes(elementsToLoad, attrTemplates);
                    results.AddRange(elementsToLoad);
                }     

                startIndex += baseElements.Count;
            } while (startIndex < totalCount);

            return results;
        }
Beispiel #5
0
 public static int lookUpDepth(AFElementTemplate template)
 {
     try
     {
         var elements = template.FindInstantiatedElements(true, AFSortField.Name, AFSortOrder.Ascending, 1);
         var element  = (AFElement)elements[0];
         var parents  = 0;
         while (element.Parent != null)
         {
             parents++;
             element = element.Parent;
         }
         return(parents);
     }
     catch
     {
         return(0);
     }
 }
        /// <summary>
        /// Loads Elements from an AF Database in a manner that is not blocking the application
        /// and is more efficient with big databases
        /// </summary>
        /// <param name="database">The AFdatabase that contains the elements to load</param>
        /// <param name="template"> the Element Template associated with the elements to load</param>
        /// <param name="elementsConcurrentQueue">Concurrent queue in which the elements will be loaded</param>
        public static void LoadElementsByTemplate(AFDatabase database, AFElementTemplate template, ConcurrentQueue <AFElement> elementsConcurrentQueue)
        {
            // set variables
            const int chunkSize = 10000;
            int       index     = 0;
            int       total;

            do
            {
                // loads elements by chunk of 10K values
                var elements = template.FindInstantiatedElements(true,
                                                                 AFSortField.Name, AFSortOrder.Ascending, index, chunkSize, out total);

                var elementCount = elements.Count;
                if (elementCount == 0)
                {
                    break;
                }

                // Convert a list of AFBaseElement to a list of AFElement
                List <AFElement> elementsList = elements.Select(e => (AFElement)e).ToList();

                // forces full load of elements
                AFElement.LoadElementsToDepth(elementsList, true, 5, 1000000);

                // if you'd like to filter the elements by attributes...
                // however this would be sub-optimal, it would be better to filter directly on the FindInstanciated Elements query.
                //i.e. elementsList = elementsList.Where(e => (bool)e.Attributes["attribute to filter"].GetValue().Value == true).ToList();
                foreach (var afElement in elementsList)
                {
                    elementsConcurrentQueue.Enqueue(afElement);
                }

                _logger.InfoFormat(" Load Elements by Template | StartIndex = {1} | Found a chunk of {2}  elements", DateTime.Now, index, elementCount);

                index += chunkSize;
            } while (index < total);

            // the findElements call we are using returns a paged collection to lower the memory foortprint
        }
        /// <summary>
        /// Loads Elements from an AF Database in a manner that is not blocking the application
        /// and is more efficient with big databases
        /// </summary>
        /// <param name="database">The AFdatabase that contains the elements to load</param>
        /// <param name="template"> the Element Template associated with the elements to load</param>
        /// <param name="elementsConcurrentQueue">Concurrent queue in which the elements will be loaded</param>
        public static void LoadElementsByTemplate(AFDatabase database, AFElementTemplate template, ConcurrentQueue<AFElement> elementsConcurrentQueue)
        {
            // set variables
            const int chunkSize = 10000;
            int index = 0;
            int total;

            do
            {
                // loads elements by chunk of 10K values
                var elements = template.FindInstantiatedElements(true,
                    AFSortField.Name, AFSortOrder.Ascending, index, chunkSize, out total);

                var elementCount = elements.Count;
                if (elementCount == 0)
                    break;

                // Convert a list of AFBaseElement to a list of AFElement
                List<AFElement> elementsList = elements.Select(e => (AFElement)e).ToList();

                // forces full load of elements
                AFElement.LoadElementsToDepth(elementsList,true,5,1000000);

                // if you'd like to filter the elements by attributes...
                // however this would be sub-optimal, it would be better to filter directly on the FindInstanciated Elements query.
                //i.e. elementsList = elementsList.Where(e => (bool)e.Attributes["attribute to filter"].GetValue().Value == true).ToList();
                foreach (var afElement in elementsList)
                {
                    elementsConcurrentQueue.Enqueue(afElement);
                }

                _logger.InfoFormat(" Load Elements by Template | StartIndex = {1} | Found a chunk of {2}  elements", DateTime.Now, index, elementCount);

                index += chunkSize;

            } while (index < total);

            // the findElements call we are using returns a paged collection to lower the memory foortprint
        }
        /// <summary>
        /// Find elements implementing a given AF Element Template and load a list of attributes from these elements
        /// </summary>
        private List<AFElement> FindElements(AFElementTemplate elementTemplate, IEnumerable<string> attributesToLoad)
        {
            int totalCount;
            int startIndex = 0;
            var results = new List<AFElement>();

            do
            {
                var baseElements = elementTemplate.FindInstantiatedElements(
                                includeDerived: true,
                                sortField: AFSortField.Name,
                                sortOrder: AFSortOrder.Ascending,
                                startIndex: startIndex,
                                maxCount: ChunkSize,
                                totalCount: out totalCount);

                // if there is no new leaf elements, break the process
                if (baseElements.Count() == 0)
                    break;

                var elements = baseElements.Select(elm => (AFElement)elm).ToList();

                var elementGroupings = elements.GroupBy(elm => elm.Template);
                foreach (var item in elementGroupings)
                {
                    List<AFAttributeTemplate> attrTemplates = attributesToLoad.Select(atr => AFHelper.GetLastAttributeTemplateOverride(item.Key, atr)).ToList();
                    AFElement.LoadAttributes(item.ToList(), attrTemplates);
                }

                results.AddRange(elements);

                startIndex += baseElements.Count();
            } while (startIndex < totalCount);

            return results;
        }