Skip to content

Commit

Permalink
Merge pull request #318 from wanglin86769/convert-stopArchivingCurren…
Browse files Browse the repository at this point in the history
…tlyDisconnectedPVs.py-to-python3

Convert stopArchivingCurrentlyDisconnectedPVs.py to python3
  • Loading branch information
jacomago authored Jan 13, 2025
2 parents 42c4e68 + 9a38612 commit efc513c
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions docs/docs/source/samples/stopArchivingCurrentlyDisconnectedPVs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
'''This script gets a list of PVs that are currently disconnected. Of these PVs, we stop archiving (and delete data) for those PVs from whom we did not receive a timestamp at all.
Be careful, this script has the ability to delete data.
'''
Expand All @@ -7,42 +7,38 @@
import sys
import argparse
import time
import urllib
import urllib2
import requests
import urllib.parse
import json
import datetime
import time

def getCurrentlyDisconnectedPVs(bplURL):
'''Get a list of PVs that are currently disconnected'''
url = bplURL + '/getCurrentlyDisconnectedPVs'
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
currentlyDisconnectedPVs = json.loads(the_page)
response = requests.get(url)
response.raise_for_status()
currentlyDisconnectedPVs = response.json()
return currentlyDisconnectedPVs

def pauseArchivingPV(bplURL, pv):
'''Pauses the archiving of the PV.'''
values = url_values = urllib.urlencode({'pv' : pv})
url = bplURL + '/pauseArchivingPV?' + values
print "Pausing request for pv", pv, "using url", url
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
pauseResponse = json.loads(the_page)
url = bplURL + '/pauseArchivingPV'
params = {'pv' : pv}
print("Pausing request for pv", pv, "using url", url + "?" + urllib.parse.urlencode(params))
response = requests.get(url, params=params)
response.raise_for_status()
pauseResponse = response.json()
return pauseResponse


def stopArchivingPV(bplURL, pv, deleteData):
'''Stops archiving of a PV. If deleteData is true, we also delete the data associated with the PV.'''
values = url_values = urllib.urlencode({'pv' : pv, 'deleteData' : 'true' if deleteData else 'false'})
url = bplURL + '/deletePV?' + values
print "Aborting request for pv", pv, "using url", url
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
stopResponse = json.loads(the_page)
url = bplURL + '/deletePV'
params = {'pv' : pv, 'deleteData' : 'true' if deleteData else 'false'}
print("Aborting request for pv", pv, "using url", url + "?" + urllib.parse.urlencode(params))
response = requests.get(url, params=params)
response.raise_for_status()
stopResponse = response.json()
return stopResponse


Expand All @@ -52,18 +48,18 @@ def stopArchivingPV(bplURL, pv, deleteData):
parser.add_argument("url", help="This is the URL to the mgmt bpl interface of the appliance cluster. For example, http://arch.slac.stanford.edu/mgmt/bpl")
args = parser.parse_args()
if not args.url.endswith('bpl'):
print "The URL needs to point to the mgmt bpl; for example, http://arch.slac.stanford.edu/mgmt/bpl. ", args.url
print("The URL needs to point to the mgmt bpl; for example, http://arch.slac.stanford.edu/mgmt/bpl. ", args.url)
sys.exit(1)
currentlyDisconnectedPVs = getCurrentlyDisconnectedPVs(args.url)
print len(currentlyDisconnectedPVs), "PVs are currently disconnected"
print(len(currentlyDisconnectedPVs), "PVs are currently disconnected")

currentlyDisconnectedPVsWithUnknownLastKnowEvent = [x['pvName'] for x in currentlyDisconnectedPVs if x['lastKnownEvent'] == 'Never']
print len(currentlyDisconnectedPVs), "PVs are currently disconnected and have Never as the last known event."
print(len(currentlyDisconnectedPVsWithUnknownLastKnowEvent), "PVs are currently disconnected and have Never as the last known event.")

for currentlyDisconnectedPVWithUnknownLastKnowEvent in currentlyDisconnectedPVsWithUnknownLastKnowEvent:
pauseResponse = pauseArchivingPV(args.url, currentlyDisconnectedPVWithUnknownLastKnowEvent)
time.sleep(1)
stopResponse = stopArchivingPV(args.url, currentlyDisconnectedPVWithUnknownLastKnowEvent, True)
print stopResponse
print(stopResponse)
time.sleep(1)

0 comments on commit efc513c

Please sign in to comment.