Marcos Vainer Loeff

Creating and viewing Asset-based analytics programmatically

Blog Post created by Marcos Vainer Loeff Employee on Apr 28, 2014

As PI Asset Based Framework 2014 was released this month, I want to take the opportunity to show some features that can be done with this new product programmatically. One of the goals of this first release is to provide users with a rich configuration experience.  Over time, we will provide all the tools needed for users to interact with analyses programmatically.  The purpose of this blog post is to provide you a glimpse of the object and methods currently available within the OSIsoft.AF.Analysis namespace.

Viewing Asset-based analytics properties programmatically

 

 

The OSIsoft.AF.Analysis namespace lets you have access programmatically to the asset based analytics where you can have access to its description and plugins settings. You can view which the analysis type, its expressions as well as its scheduling configuration.  In order to make the code snippet below, there must be an analysis properly configured like shown on the following screenshot:

 

 

 

 3617.sc1b.png

 

Figure 1 – Asset based analytics settings.

 

This analysis calculates the average of two different averages calculation Avg1d and Avg2d of the attribute Washington Temperature with two different time ranges. The first variable Avg2d calculates the average value of the last 48 hours and the variable Avg1d store the result of the average calculation from the last 24 hours. The variable SumAvg store the values which are the result of the expression 0.5*Avg1d+0.5*Avg2d. This variable is linked to the Temperature Average which is an Output Attribute.

 

 

 
            PISystems myPISystems = new PISystems();

            //Connect to the AF Server
            PISystem myPISystem = myPISystems["MARC-PI2014"];

            //Connect to the AF Database
            AFDatabase myDb = myPISystem.Databases["AFSDKWorkshop"];
            AFElement Cities = myDb.Elements["Cities"];

            //Store the Washigton element on the Washigton AFEElement object
            AFElement Washington = Cities.Elements["Washington"];

            //Create an AFAttribute linked with Temperature Average of the Washington element
            AFAttribute TempAvg = Washington.Attributes["Temperature Average"];

            //Access the "Washginton Temperature Avg" analyses object
            AFAnalysis WashingtonTempAvg = Washington.Analyses["Washginton Temperature Avg"];
           
            //Get the analyses description
            Console.WriteLine("\nAnalysis description: " + WashingtonTempAvg.Description);

            //Get the config string of the analyses rule plugin
            Console.WriteLine("\nAnalysis rule plugin config string: " + WashingtonTempAvg.AnalysisRule.ConfigString);
            
            //Get variable mapping information
            Console.WriteLine("\nVariable mapping information: " + WashingtonTempAvg.AnalysisRule.VariableMapping);

            //Get the config string of the time rule plugin
            Console.WriteLine("\nTime rule config string: " + WashingtonTempAvg.TimeRule.ConfigString);

            //Get the path of analysis
            Console.WriteLine("\nAnalysis path: " + WashingtonTempAvg.GetPath());

            //Get the analysis status
            Console.WriteLine("\nAnalysis status: " + WashingtonTempAvg.Status);

            Console.WriteLine("\nThe script execution has finished. Press any key to quit.");
            Console.ReadKey();

 

 

 This will result in the following console output:

 

 

 

8831.sc2.png

 

Figure 2 – Console output.

 

 

 

 

Creating an Asset-based analytic programmatically

 

 

The namespace also lets you create the same analysis programmatically. The code snippet below creates the same analysis used on the previous item whose type is Expression:

 

 

 

  

 

 

 
            PISystems myPISystems = new PISystems();
            PISystem myPISystem = myPISystems["MARC-PI2014"];
            AFDatabase myDb = myPISystem.Databases["AFSDKWorkshop"];
            AFElement Cities = myDb.Elements["Cities"];
            AFElement Washington = Cities.Elements["Washington"];
            AFAttribute TempAvg = Washington.Attributes["Temperature Average"];
            AFAnalysis WashingtonTempAvg = Washington.Analyses["Washginton Temperature Avg"];
            if (WashingtonTempAvg==null)
            {
                WashingtonTempAvg = Washington.Analyses.Add("Washginton Temperature Avg");
            }
            WashingtonTempAvg.Description = "Analysis for Washington Temeperature Average";
            WashingtonTempAvg.AnalysisRulePlugIn = myPISystem.AnalysisRulePlugIns["PerformanceEquation"];
            WashingtonTempAvg.AnalysisRule.ConfigString="Avg2d := TagAvg('Temperature','*-2d','*');\r\nAvg1d := TagAvg('Temperature','*-1d','*');\r\nSumAvg := Avg1d+Avg2d+2;";
            WashingtonTempAvg.AnalysisRule.MapVariable("SumAvg",TempAvg);
            WashingtonTempAvg.TimeRulePlugIn = myPISystem.TimeRulePlugIns["Periodic"];
            WashingtonTempAvg.TimeRule.ConfigString="Frequency=300";
            WashingtonTempAvg.SetStatus(AFStatus.Enabled);
            WashingtonTempAvg.CheckIn();  

 

 

 This will result on the same analysis shown on Figure 1.

Conclusion

 

 

This short blog post shows that the same analysis configured on PI System Explorer could be added, deleted or access programmatically. Nevertheless, the programmatically access is still incomplete since currently you are only able to change the configuration. On future releases,  we plan to release additional methods for public use allowing you to interact with Asset Based Analytics programmatically.  Stay tuned!

Outcomes