5 Replies Latest reply on Oct 30, 2018 11:45 AM by Rick Davin

    AFElement.GetElements obsolete

    PIAdminEnelGP

      Hi guys,

       

      I have a script on VB that help me create an automatic report of various variables from AF. What would be the best way to change my code as now I'm getting a warning that the action I try to do is now obsolete (AFElement.GetElements) .

       

      I'm not an expert on coding but the guy responsible for the code changed of company not a long ago. So now I need to fix this as my report is not being updated as before and I don't know if its because of this warning. I tried using the AFElementSearch but so far I cannot do the same logic as before with it.

       

      This is what the report does.

       

      And this is that part of the code.

       

      html.Append("<table width=\"100%\" class=\"tabla\" cellspacing=\"0\"><tr class=\"trOculta\"><th colspan=\"11\"><h1></h1></th></tr><tr><td class=\"cabecera2\">WTG</td><td class=\"cabecera2\">Fallas</td><td class=\"cabecera2\">Vel. Viento</td><td class=\"cabecera2\">Producción Real</td><td class=\"cabecera2\">Producción Esperada</td><td class=\"cabecera2\">Diferencias</td><td class=\"cabecera2\">E. Falla (kWh)</td><td class=\"cabecera2\">Ineficiencia (kWh)</td><td class=\"cabecera2\">Código Alarma</td><td class=\"cabecera2\">Descripción Alarma</td><td class=\"cabecera2\">Disponibilidad</td></tr>");
      int count = 0;
      int rowIndex = 0;
      foreach (var child in element.GetElements(AFSortField.Name, AFSortOrder.Ascending, 0, 50, out count))
          {
          double wind = 0.0;
          double.TryParse(child.Attributes["Average_wind_speed_previous_day"].GetValue().Value.ToString(), out wind);
          double power = 0.0;
          double.TryParse(child.Attributes["Energy_previous_day"].GetValue().Value.ToString(), out power);
          double expected = 0.0;
          double.TryParse(child.Attributes["Previous_day_estimated_energy"].GetValue().Value.ToString(), out expected);
          double difference = power - expected;
          double availability = difference < 0 ? (power * 100) / expected : 100;
      
      
      double ineficiency = 0.0;
          double.TryParse(child.Attributes["Previous_day_inefficiency"].GetValue().Value.ToString(), out ineficiency);
          //double.TryParse(child.Attributes["Disponibilidad Día Anterior"].GetValue().Value.ToString(), out availability);
      
      
      html.Append(string.Format("<tr class=\"{8}\"><td class=\"tdPunteado\">{0}</td><td class=\"tdPunteado\"> </td><td class=\"tdPunteado\">{1:N2}</td><td class=\"tdPunteado\">{2:N2}</td><td class=\"tdPunteado\">{3:N2}</td><td class=\"tdPunteado\">{4:N2}</td><td class=\"tdPunteado\">{5:N2}</td><td class=\"tdPunteado\">{6:N2}</td><td class=\"tdPunteado\"> </td><td class=\"tdPunteado\"> </td><td>{7:N2} %</td></tr>", child.Name, wind, power, expected, difference, 0, ineficiency, availability, IsOdd(rowIndex) ? "trGris" : "trBlanco"));
      rowIndex++;
      }
      

       

      Could you help me with this request?

      Thank you very much for your help.

        • Re: AFElement.GetElements obsolete
          John Messinger

          Hi Alan,

           

          Looking at your code snippet, the same thing could be accomplished by simply looping through the collection of child Elements using the Elements property. Thus, your code change might just need to be:

           

          foreach (var child in element.Elements)
              {
                    ...
              }
          

           

          From what I can see in your code, I don't think that using AFSearch at this point is necessary, as you already have an Element object that has been located from the hierarchy, and this particular code just iterates through it's collection of child Elements. The original code used a paged collection for this, but the above snippet will still do the same basic loop through the current Element object's children.

          3 of 3 people found this helpful
            • Re: AFElement.GetElements obsolete
              Rick Davin

              Hi Alan,

               

              Did John Messinger 's answer help you?  Are all you interested in are the direct child elements of a given element?  If so, then an AFElementSearch is not absolutely necessary but may still be beneficial if you have tens of thousands or more child elements.

               

              The element.Elements property does not perform a full load of the child elements and a separate RPC is made to load them one at a time.  For a couple dozen or fewer child elements, this is okay.  But if you have hundreds or a few thousand of child elements, this can be a performance drag.  In such cases, you may want to issue:

               

              AFElement.LoadElements(element.Elements)

               

              to perform a full load BEFORE the looping.  On the other hand, if you have tens of thousands of child elements, or more, then you may want to use an AFElementSearch, which performs a full load for pages of 1000 children at a time.

              2 of 2 people found this helpful
            • Re: AFElement.GetElements obsolete
              PIAdminEnelGP

              Hi Rick and John,

               

              Actually I tried John method and the compilation was fine. However now it doesn't seem to be working as before. Right now the only change I made was to put element.Elements in the foreach loop. I tried what Rick told me to put the AFElement.LoadElements(element.Elements) before the loop so this can load the variables before.

               

               

              However now it doesn't read all the variables and my report looks like this.

               

              Where it used to look like this.

               

               

              So I don't think the foreach (var child in element.Elements) is shifting between the childs of the element.

               

              How can I use the AFElementSearch in this context?

               

              Thank you so much for your help.

              • Re: AFElement.GetElements obsolete
                PIAdminEnelGP

                Ok it seems my values where at "Set to Bad" so my report generation put 0's. Instead but I believe now is working.


                Thanks a lot for your help guys.

                  • Re: AFElement.GetElements obsolete
                    Rick Davin

                    Thanks for the follow up, Alan.  One thing I do want to point out is that in your original post, you were employing a page count of 50.  However, you only grabbed the first page of results.  I don't see anywhere in the original code snippet where you retrieved a 2nd page of elements.  This differs from using element.Elements in that you would get all of the child elements, not just the first 50.

                    1 of 1 people found this helpful