Skip to content

Commit

Permalink
Merge pull request #4510 from ArqamFarooqui110719/issue/4509
Browse files Browse the repository at this point in the history
updated attribute deleting script with python3
  • Loading branch information
fgalan authored Feb 12, 2024
2 parents 3e43d05 + 498d133 commit 4f3b04c
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions scripts/utils/delete_attribute_by_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U
#
# This file is part of Orion Context Broker.
Expand Down Expand Up @@ -53,7 +53,7 @@ def testing_populate(n, entity_type):
:param n: number of entities to populate (with ids from e1 to e<n>)
:param entity_type: entity type used for the populated entities (all with the same type)
"""
print 'Populating %d entities of type %s' % (n, entity_type)
print('Populating %d entities of type %s' % (n, entity_type))

entities = []
for i in range(0, n):
Expand All @@ -78,7 +78,7 @@ def testing_populate(n, entity_type):

res = requests.post(cb_endpoint + '/v2/op/update', json=body, headers=headers, verify=False)
if res.status_code != 204:
print 'Error populating entities (%d): %s' % (res.status_code, res.json())
print('Error populating entities (%d): %s' % (res.status_code, res.json()))


def get_entities_count():
Expand All @@ -92,7 +92,7 @@ def get_entities_count():
res = requests.get(cb_endpoint + '/v2/entities?limit=1&options=count&attrs=noexist' + filter, headers=headers,
verify=False)
if res.status_code != 200:
print 'Error getting entities count (%d): %s' % (res.status_code, res.json())
print('Error getting entities count (%d): %s' % (res.status_code, res.json()))
return -1
else:
return int(res.headers['fiware-total-count'])
Expand All @@ -105,10 +105,10 @@ def initial_statistics():
:return: the total number of entities
"""
total = get_entities_count()
print 'There are %d entities' % total
pages = total / page_size
print('There are %d entities' % total)
pages = total // page_size
rest = total - (pages * page_size)
print 'There are %d pages of %d entities (and a final page of %d entities)' % (pages, page_size, rest)
print('There are %d pages of %d entities (and a final page of %d entities)' % (pages, page_size, rest))

return total

Expand All @@ -128,7 +128,7 @@ def send_batch(entities):

res = requests.post(cb_endpoint + '/v2/op/update?options=keyValues', json=body, headers=headers, verify=False)
if res.status_code != 204:
print 'Error in batch operation (%d): %s' % (res.status_code, res.json())
print('Error in batch operation (%d): %s' % (res.status_code, res.json()))
return False

return True
Expand All @@ -137,10 +137,10 @@ def send_batch(entities):
### Main program starts here ###

# Warn user
print "WARNING!!!! This script will delete the atrribute '%s' in all the entities matching the filter '%s'" % (attr_to_delete, filter)
print "This action cannot be undone. If you are sure you want to continue type 'yes' and press Enter"
print("WARNING!!!! This script will delete the atrribute '%s' in all the entities matching the filter '%s'" % (attr_to_delete, filter))
print("This action cannot be undone. If you are sure you want to continue type 'yes' and press Enter")

confirm = raw_input()
confirm = input()

if (confirm != 'yes'):
sys.exit()
Expand All @@ -150,21 +150,21 @@ def send_batch(entities):
count = initial_statistics()

if count == 0:
print 'Nothing to do'
print('Nothing to do')
sys.exit(0)

entities = []

# Number of batches
pages = count / page_size
pages = count // page_size
for i in range(0, pages + 1):
print '- Getting entities page %d' % (i + 1)
print('- Getting entities page %d' % (i + 1))
offset = i * page_size
res = requests.get('%s/v2/entities?offset=%s&limit=%s%s' % (cb_endpoint, str(offset), str(page_size), filter),
headers=headers, verify=False)

if res.status_code != 200:
print 'Error getting entities (%d): %s' % (res.status_code, res.json())
print('Error getting entities (%d): %s' % (res.status_code, res.json()))
sys.exit(1)

for entity in res.json():
Expand All @@ -178,14 +178,15 @@ def send_batch(entities):
accum += 1

if (accum == page_size):
print '- Update batch of %d entities' % len(batch)
print('- Update batch of %d entities' % len(batch))
send_batch(batch)
accum = 0
batch = []

# Last batch
if len(batch) > 0:
print '- Update batch of %d entities' % len(batch)
print('- Update batch of %d entities' % len(batch))
send_batch(batch)

print 'We are done!'
print('We are done!')

0 comments on commit 4f3b04c

Please sign in to comment.