8 Replies Latest reply on Jul 5, 2017 1:19 PM by Rick Davin

    Update Multiple AFValues for a Single TimeStamp

    Kailash_Ravuri

      I am working with c# AFSDK to update all the AFValues(against same timestamp) Status to Questionable using "PIPoint.UpdateValues" method of AFAttribute, but this method is updating only the first value in the list. I am looking for some help on this.

       

      Steps of my Code:

      1. Fetch All AFValues for particular Attribute In a timestamp range -- Here StartTime and EndTime are same.

      2. This returns multiple AFValues for the given TimeStamp.

      3. Iterate though each AFValue and update Status to Questionable.

      4. Call the "PIPoint.UpdateValues" method of AFAttribute with the updated list in the above step.

      5. The result is - only first value in the list is updated with questionable property.

        • Re: Update Multiple AFValues for a Single TimeStamp
          Rick Davin

          Hi Kailash,

           

          The community could provide a better answer to you if you posted the relevant code.

           

          For duplicate AFValues (same .Value, same .Timestamp), you must change each of them.  They are treated individually.

            • Re: Update Multiple AFValues for a Single TimeStamp
              Kailash_Ravuri
              public void MarkStatus(IEnumerable<AFValue> values)
                      {
                          foreach (var afValue in values)
                          {
                              try
                              {
                                       (afValue.Status == AFValueStatus.Questionable || afValue.Status == AFValueStatus.Bad)
                                              continue;
                                          afValue.Status = AFValueStatus.Questionable;                         
                                  }
              
                                  if (afValue.PIPoint != null)
                                  {
                                      afValue.PIPoint.UpdateValue(afValue, AFUpdateOption.ReplaceOnly);
                                  }
                              }
                              catch (Exception e)
                              {
                                  Console.WriteLine(e);
                              }
                          }
                      }
              
                • Re: Update Multiple AFValues for a Single TimeStamp
                  Rick Davin

                  Your Visual Studio code window should be letting you know that you have unreachable code on line 09.  You are missing a very critical if at the start of line 07.  Since it's not treated as an if conditional, the continuation on line 08 occurs for EVERY SUBSEQUENT VALUE.

                   

                  Also line 10 needs to be deleted.

                  • Re: Update Multiple AFValues for a Single TimeStamp
                    Rick Davin

                    This fixes some of your errors and issues a more efficient bulk UpdateValues:

                     

                    public void MarkStatus(IEnumerable<AFValue> values)
                    {
                        var tag = values?.FirstOrDefault()?.PIPoint;
                        if (tag == null)
                            return;
                        var replacements = new AFValues();
                        foreach (var afValue in values)
                        {
                            try
                            {
                                if (afValue.Status == AFValueStatus.Questionable || afValue.Status == AFValueStatus.Bad)
                                    continue;
                                var pv = new AFValue(afValue);
                                pv.Status = AFValueStatus.Questionable;
                                replacements.Add(pv);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                        }
                        if (replacements.Count > 0)
                        {
                            var errors = tag.UpdateValues(replacements, AFUpdateOption.ReplaceOnly);
                            if (errors != null)
                            {
                                //Homework: write something to show errors
                            }
                        }
                    }
                    
                      • Re: Update Multiple AFValues for a Single TimeStamp
                        Kailash_Ravuri

                        I tried your suggestion - This is still updating only the first AFValue in the list to questionable.

                        I am not sure this is a bug or Replace works like that - Because the code you shared/i shared is working when the AFValues are not Unique in Value and TimeStamp. The issue is only when you have multiple AF values with same value and same timestamp for a particular pi-point - updating with Replace option is updating only one from the values.

                        But Remove is working fine even the AFValues are identical in values and timestamp it is removing all the Values but Replace is happening only for the first AFValue.

                          • Re: Update Multiple AFValues for a Single TimeStamp
                            Rick Davin

                            Hi Kailash,

                             

                            This is a limitation of how ReplaceOnly works in conjunction with duplicate values (same Timestamp, same Value, and even same Status).  If you have 6 identical values and issue an update of 5 values with ReplaceOnly, then the first value will get updated 5 times.

                             

                            I would ask what your use-case is and whether it's acceptable to your application that you have so many duplicates at one event.  I would think you may try to find out what's causing the duplicates so that duplication can be stopped (looking forward), and you would also to write a routine to purge the existing duplicates (looking backward).

                             

                            If you insist on keeping so much duplication in your data archives, there is a workaround to your stated need of setting the questionable flag:

                            1. Read all the values from the data archive into memory (i.e. a variable of AFValues)
                            2. Delete the values on the data archive
                            3. Change the Status flag on each value in AFValues in memory
                            4. UpdateValues using AFUpdateOption.Insert
                    • Re: Update Multiple AFValues for a Single TimeStamp
                      Kailash_Ravuri

                      Kailash_Ravuri wrote:

                       

                      I am working with c# AFSDK to update all the AFValues(against same timestamp) Status to Questionable using "PIPoint.UpdateValues" method of AFAttribute, but this method is updating only the first value in the list. I am looking for some help on this.

                       

                      Steps of my Code:

                      1. Fetch All AFValues for particular Attribute In a timestamp range -- Here StartTime and EndTime are same.

                      2. This returns multiple AFValues for the given TimeStamp.

                      3. Iterate though each AFValue and update Status to Questionable.

                      4. Call the "PIPoint.UpdateValues" method of AFAttribute with the updated list in the above step.

                      5. The result is - only first value in the list is updated with questionable property.

                      Here is the code i tried :

                      1. Now I am passing a list of AFValues for a particular attribute --> Same Value and Same TimeStamp

                      2. Updating each AFValue to Questionable

                      3. Updating them Again using

                       

                       

                              public void MarkStatus(IEnumerable<AFValue> values)
                              {
                                  foreach (var afValue in values)
                                  {
                                      try
                                      {
                                               (afValue.Status == AFValueStatus.Questionable || afValue.Status == AFValueStatus.Bad)
                                                      continue;
                                                  afValue.Status = AFValueStatus.Questionable;                          
                                          }
                      
                                          if (afValue.PIPoint != null)
                                          {
                                              afValue.PIPoint.UpdateValue(afValue, AFUpdateOption.ReplaceOnly);
                                          }
                                      }
                                      catch (Exception e)
                                      {
                                          Console.WriteLine(e);
                                      }
                                  }
                              }