Previously I created delete events sample by AFSDK.

Delete PI Data Archive tag values by AFSDK Sample

I want to achieve the same functions by PI Web API. (I want to delete values from LinuxOS client machine)

I created index.html and js file and put these on the windows machine's IIS. This web page allows users to delete PI tag's events.

I have CentOS (Linux) client machine and could delete events by this web page.

I am using Basic Authentication and that is why I could connect from Linux machine.

This webpage use PI Web API Search query for "Search Tags" button. Using batch request to delete events.

(Remember that PI Web API search crawler could not run by "Basic" authentication.

You can just see the indexed file but the added new points/assets could not be crawled. You can use Indexed Search Shared Index CTP function https://techsupport.osisoft.com/Downloads/File/53b2552a-2d62-4ae0-aa75-82b02c5a0ac6

to run the PI Web API with Basic authentication and updating PI Web API Search Crawler)

 

The files are following codes.

Index.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <meta name="viewport" content="width=400"/>
  <script type="text/javascript" src="http://code.angularjs.org/angular-1.0.1.js"></script>
  <script src="app.js"></script>
  <title>Delete Tag Events</title>  
</head>

<body ng-app="myApp">
    <div ng-controller="MyCtrl" >
        Search Tag Name : <input type="Text" ng-model="tagmask"><br>
        <button class="test" type="button" ng-click="searchtags()">
        <span class="ng-scope">Search Tags</span>
        </button>
        <div ng-show="TagSearchResult">
            <hr>
            Search Tag Results:<br>
            <div ng-repeat="item in search_items">
                {{item.Name}}
            </div>
            <hr>
            Start Time : <input type="Text" ng-model="starttime"><br>
            End Time : <input type="Text" ng-model="endtime"><br>
            Max delete event count : <input type="Text" ng-model="maxdelevnum" style="width:40px;">
            <br>
            <button class="test" type="button" ng-click="delete()">
            <span class="ng-scope">Delete Events</span>
            </button>
        </div>
        <div ng-show="DelResult">
            <hr>
            Delete Event Results:<br>
            <div ng-repeat="item in del_items">
                {{item.Content.Name}} 
                : Deleted {{item.Content.Count}} events
            </div>
            <hr>
        </div>
    </div>
</body>
</html>

 

app.js (Please change var servername ="localhost"; part to your PI Web API Server name. Adding if you want to use Basic authentication, please enter XXXXX part too.)

angular.module('myApp', [])
.controller('MyCtrl', ['$scope','$http', function($scope,$http) {
    //Set PIWebAPI Server Name
    var servername ="localhost";
    //Basic Authentication
    //put the 64bit encoded (username:password) to the XXXXXXXXXXXXXXXXXXXXXXXXXXXXX for using Basic Authentication.
    $http.defaults.headers.common['Authorization'] = "Basic " + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $http.defaults.headers.common['Content-Type'] = 'application/json';
    // Default values
    $scope.tagmask = "sin*";
    $scope.starttime = "*-8h";
    $scope.endtime = "*";
    $scope.maxdelevnum = "50000";
    $scope.TagSearchResult = false;
    $scope.DelResult = false;
    
    //"Search Tags" Button
    $scope.searchtags= function(){
        var batch_searchtags = {  
            "searchTags": {  
                "Method": "GET",  
                "Resource": "https://" + servername + "/piwebapi/search/query?q=name:"+$scope.tagmask +"&fields=Name"
            }
        }
        $http.post("https://" + servername + "/piwebapi/batch", batch_searchtags, {}).success(function(response) {
            $scope.search_items = response.searchTags.Content.Items;
        });
        //display the results
        $scope.TagSearchResult = true;
    };


    //"Delete Events Button
    $scope.delete= function(){
        var batch_data = {
            "searchTags": {  
                "Method": "GET",  
                "Resource": "https://" + servername + "/piwebapi/search/query?q=name:" + $scope.tagmask + "&fields=Name;Links"
            },  
            "getTagInfo": {  
                "Method": "GET",  
                "RequestTemplate" : {"Resource": "{0}?selectedfields=Name;Links.RecordedData"},
                "ParentIds": ["searchTags"],  
                "Parameters": ["$.searchTags.Content.Items[*].Links.Self"]  
            },
            "getData":{
                "Method" : "GET",
                "RequestTemplate" : {
                    "Resource": "{0}?maxCount=" + $scope.maxdelevnum + "&selectedfields=Items.Timestamp;Items.Value&StartTime="
                     + $scope.starttime + "&EndTime=" + $scope.endtime 
                },
                "ParentIds": ["getTagInfo"],
                "Parameters" : ["$.getTagInfo.Content.Items[*].Content.Links.RecordedData"]
            }
        }
        $http.post("https://" + servername + "/piwebapi/batch", batch_data, {}).success(function(response) {
            $scope.del_items = response.getTagInfo.Content.Items;
            //Tags loop
            var del_data_body="";
            for (var i = 0; i < response.getTagInfo.Content.Items.length; i++) {
                $scope.del_items[i].Content.Count = response.getData.Content.Items[i].Content.Items.length;
                del_data_body= response.getData.Content.Items[i].Content.Items;
                var tagaddress = response.getTagInfo.Content.Items[i].Content.Links.RecordedData + "?updateOption=remove";
                $http.post(tagaddress, del_data_body, {}).success(function(response) {
                    //alert("success to delete");
                })
            }
        });
        //display the results
        $scope.DelResult = true;
    }
}]);

 

Remember that deleting events affects PI Data Archive machine's performance.

Also I found that trying to delete over 200,000 events, I got an error.

However, I could delete 100,000 events from CentOS7 (Firefox) browser.

 

The source code is on the Github.

GitHub - kenji0711/PIWebAPIDeleteEvents

 

If your PI Data Archive is test/ developing environment, you can enjoy it.

For running these kind of custom application tools that use PI Web API/ AFSDK etc... to the production environment, the end user needs to have PSA license.

I worked with Jerome Lefebvre to create this example.