8 Replies Latest reply on May 8, 2018 6:03 PM by Rick Davin

    Event Frames AF SDK backfilling issue

    eradwan

      Hi There,

       

      I'm using the following code to auto create an analysis with event frames backfilling, all works fine but it doesn't backfill and gives an error like the one in the screenshot. If I try to manually invoke backfill, it works!. Appreciate your thoughts, please.

       

       

       

              static void Main(string[] args)
              {
                  try
                  {
                      PISystem ps;
                      AFDatabase AF_Pilot;
                      AFElement ocd1SourAfElement;
                     
      
      
                      var connection = new AfConnectionHelper("SRLAAP81", "AF Pilot");
                      ps = connection.GetPiSystem();
                      connection.Connect();
                      AF_Pilot = ps.Databases.DefaultDatabase;
                     
      
      
                      Guid ocd1SourGuid = new Guid("bc8ac68b-4dc4-11e8-be45-005056a312a2");
                      ocd1SourAfElement = AFElement.FindElement(ps, ocd1SourGuid);
      
      
                      AFElementTemplate efTemplate = AF_Pilot.ElementTemplates["INHIBIT"];
      
      
                      AFAnalysis efAnalysis = ocd1SourAfElement.Analyses.Add("EF_Calc");
      
      
                      efAnalysis.AnalysisRulePlugIn = ps.AnalysisRulePlugIns["EventFrame"];
                      efAnalysis.AnalysisRule.ConfigString = String.Format("EFTGUID={0};EFTNAME={1}", efTemplate.UniqueID, efTemplate.Name);
      
      
                      AFAnalysisRule triggerRule = efAnalysis.AnalysisRule.AnalysisRules.Add(ps.AnalysisRulePlugIns["PerformanceEquation"]);
                      triggerRule.ConfigString = "StartTrigger:= '1300NDFPHLM14A028.PV' = \"INHIBIT\" ";
                      AFAnalysisRule timeTrueRule = efAnalysis.AnalysisRule.AnalysisRules.Add(ps.AnalysisRulePlugIns["TimeTrue"]);
                      timeTrueRule.ConfigString = "TimeTrueValue=0";
      
      
                      efAnalysis.TimeRulePlugIn = ps.TimeRulePlugIns["Natural"];
                      efAnalysis.SetStatus(AFStatus.Enabled);
                     
      
      
                      efAnalysis.CheckIn();
      
      
                      List<AFAnalysis> myAnalyses = new List<AFAnalysis>();
                      myAnalyses.Add(efAnalysis);
      
      
                      string startTime = "*-15d";
                      string endTime = "*";
                      AFAnalysisService myAFAnalysisSvc = ps.AnalysisService;
                      myAFAnalysisSvc.QueueCalculation(myAnalyses, new AFTimeRange(startTime, endTime),
                      AFAnalysisService.CalculationMode.DeleteExistingData);
      
      
                      efAnalysis.CheckIn();
                  }
                  catch (Exception ex)
                  {
                     
                      throw;
                  }
              }
      

       

       

        • Re: Event Frames AF SDK backfilling issue
          Dan Fishman

          I wonder if this is a race condition.  You write the changes to the AF database (create and start) but the analysis service might not have fully started the analysis yet.  I would think the analysis service upon loading the configuration has to check for errors, sign up with the cache, update dependency rankings and other activities.  Perhaps, there is a "pending" or "starting" state that the analysis is in and if you delay your backfilling three to five seconds later you will have success.

            • Re: Event Frames AF SDK backfilling issue
              Rick Davin

              Hi Emad,

               

              To explain Dan's "race condition" remark, consider this bit of logic:

               

              1. Start analysis
              2. Queue analysis for backfill

               

              This logic is similar to :

               

              1. Send friend to pick up lunch
              2. Eat lunch

               

              The problem you have attempted to eat lunch immediately after sending your friend to pick up lunch.  You haven't waited long enough for your friend to return with the lunch you are supposed to eat!  Instead the logic needs to be updated with:

               

              1. Send friend to pick up lunch
              2. Wait for friend to return with lunch
              3. Eat lunch

               

              This means you will have to introduce a waiting period in between requesting to set the Analysis State to be Enabled (strictly speaking you don't enable it; rather you ask that it become enabled) and queuing the backfill request.  This suggests you will need to add a Thread.Sleep() to your application.  Dan suggests waiting 3-5 seconds.  If you can wait upwards to 10 seconds, even better.

              • Re: Event Frames AF SDK backfilling issue
                eradwan

                Thanks Dan, I'll give it a try. BTW, I almost duplicated the thread for a reason. In the first one I was wondering if there is another way to execute the code in a loop for around 15000+ inhibit tags, is that wise? And this thread I just mentioned the error that you kindly replied to. Appreciate your thoughts on the first argument.

                  • Re: Event Frames AF SDK backfilling issue
                    Rick Davin

                    Hello Emad,

                     

                    CORRECTED INFORMATION

                     

                    According to our developers, starting all at once may be more efficient than trying to do chunks or batches on your own. 

                     

                    The difficulty isn't starting that many at once, but rather trying to start 15000+ analyses and also backfill, so this should be approached with caution (consider your current race condition, and multiply it by 1000).  You will need to pick a suitable wait period between the request-to-start and the queue-for-recalculation.  Hard to say what "suitable" is as a real number (1 minute, 2 minute, 5 minutes, more?).  The requested ability to check the actual, instantaneous status from the Analysis Service is not yet exposed in production releases of AF, nor will be available in AF 2018.

                    • Re: Event Frames AF SDK backfilling issue
                      Dan Fishman

                      You are welcome. Rick Davin, I like your lunch metaphor! 

                       

                      I would avoid looping for 15000 inhibit tags. A much more efficient practice would be to create an AF Analysis Template and apply it.  Is it an option to use templates?

                       

                      Dan