5 Replies Latest reply on Aug 10, 2009 3:30 PM by cescamilla

    Fastest Search Method for finding Attributes values

    kkumar100

      Hi,

      I need your assistance in one issue. As per our requirement I have to create a function which will accept ElementName, CommaSepratedAttributeName and return attribute Values.

      In my function I called AFElement.FindElements() method which is returning list of elements, now I started the loop and fetching the attribute values.

      But My problem is that it is taking long time. I need a function which will take less time. Please find my code below

      PISystems systems = new PISystems();
      mySystem = (PISystem)systems[serverName].PISystem;
      myDB = (AFDatabase)mySystem.Databases[afDatabase].Database;

      string[] strAtrr = commaSepratedAttributes.Split(',');
      myElements = (AFElements)myDB.Elements;

      AFNamedCollectionList<AFElement> AFElementsList = AFElement.FindElements(myDB, null, elementTemplate, AFSearchField.Template, true, AFSortField.Name, AFSortOrder.Ascending, 1000);

      foreach (AFElement Afelement in AFElementsList)
      {
          StrAttrVal = new StringBuilder();
          if (intLen > 0)
          {
              int intLength = 0;
              while (intLength <= intLen)
              {
                  string strAttribute = strAtrr[intLength];
                  StrAttrVal.Append(Afelement.Attributes[strAttribute].GetValue().Value);
                  if (intLength < intLen)
                      StrAttrVal.Append(",");
                  intLength += 1;
              }
          }
      }



      Please advise me, Thanks in Advance!!!!.

      Thanks,

        • Re: Fastest Search Method for finding Attributes values
          cescamilla

          I remember it was preferred (and about 10 times faster) to get values like this:
          myAFCase.GetResult(youAFCase.Elements[iIndex].Attributes[sAttribute], UOM).Value;

           

          rather than this:
          myAFCase.Elements[iIndex].Attributes[sAttribute].GetValue(youAFCase, youAFCase.TimeRange, UOM).Value;

           

          Because the first one does consider the edits and substitutions of values, and the second one does not, and the first one is optimized, if the first one fails then you should call the second one.

           

          Try this and let us know if it works for you :)

           

          This was intended for AF 1.x but I'll try it in the 2.x version and tell you. This should give you an idea.

            • Re: Fastest Search Method for finding Attributes values

              Cristobal...but retrieving a value from an AFCase has the potential to be faster than from a Data Reference.  AFCase results do not invoke the Data Reference for an Attribute, they jump straight to SQL for the stored value. 

                • Re: Fastest Search Method for finding Attributes values
                  cmanhard

                  Use AFAttributeList to retrieve multiple values at one time.  This reduces round trips to PI to get the data.  Something like:

                  string[] strAttrs = commaSepratedAttributes.Split(',');
                  int attrPerElement = strAttrs.Length;
                  AFNamedCollectionList<AFElement> elements = AFElement.FindElements(myDB, null, elementTemplate, AFSearchField.Template, true, AFSortField.Name, AFSortOrder.Ascending, 1000);
                  AFAttributeList alist = new AFAttributeList();
                  foreach (AFElement element in elements)
                  {
                      foreach (string strAttr in strAttrs)
                      {
                          alist.Add(element.Attributes[strAttr]);
                      }
                  }

                  if (alist.Count > 0)
                  {
                      StringBuilder allValues = new StringBuilder();
                      AFValues values = alist.GetValue();
                      for (int i=0; i<values.Count; i++)
                      {
                          AFValue val = values;
                          AFAttribute attr = alist;
                          allValues.Append(val.ToString());
                          if (i % attrPerElement > 0)
                              allValues.Append(",");
                          else if (i % attrPerElement == 0 && i != 0)
                              allValues.AppendLine();
                      }
                      return allValues.ToString();
                  }

                  Note in the upcoming AF 2.1 release, there are additional examples and AFSDK methods for finding attribute values, bulk loading and retrieving attribute values.

                • Re: Fastest Search Method for finding Attributes values
                  kkumar100

                  Hi Cristobal,

                   

                  Thanks for your reply. I am new to PI AF. I don't know how to use AFCase and i tried but not able to get sucess. It would be nice, if you can post the code....

                   

                  Thanks

                   

                   

                • Re: Fastest Search Method for finding Attributes values
                  cescamilla

                  Sorry for the late reply...

                   

                  Are you reading values through a Case?