-
Notifications
You must be signed in to change notification settings - Fork 265
/
delete_entities_by_query.py
executable file
·166 lines (129 loc) · 4.84 KB
/
delete_entities_by_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# Copyright 2018 Telefonica Investigacion y Desarrollo, S.A.U
#
# This file is part of Orion Context Broker.
#
# Orion Context Broker is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Orion Context Broker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/.
#
# For those usages not covered by this license please contact with
# iot_support at tid dot es
import requests
requests.packages.urllib3.disable_warnings()
__author__ = 'fermin'
##############################################################
# BEGIN of the configuration part (don't touch above this line ;)
cb_endpoint = 'http://localhost:1026'
headers = {
'fiware-service': 'service',
'fiware-servicepath': '/subservice',
'x-auth-token': 'token'
}
page_size = 500
# WARNING! use this filter or you will remove *all* entities
filter = '&type=device'
# END of the configuration part (don't touch below this line ;)
##############################################################
def testing_populate(n, entity_type):
"""
Populate a set of entities, just for testing
:param n: number of entitis 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))
entities = []
for i in range(1, 110):
entity = {
'id': 'e' + str(i),
'type': entity_type,
'a1': {
'value': 'foo',
'type': 'Text'
},
'a2': {
'value': 42,
'type': 'Number'
}
}
entities.append(entity)
body = {
'entities': entities,
'actionType': 'APPEND'
}
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()))
def get_entities_count():
"""
Return the total number of entities
:return: number of entities (or -1 if there was some error)
"""
# Trick: asking for a non existing attribute (attrs=noexist) will make response very small (only entity id/type)
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()))
return -1
else:
return int(res.headers['fiware-total-count'])
def initial_statistics():
"""
Print some initial data statistics
"""
total = get_entities_count()
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))
def remove_page():
"""
Removes the first page of entities
:return: True if removal was ok, False otherwise
"""
# Trick: asking for a non existing attribute (attrs=noexist) will make response very small (only entity id/type)
res = requests.get(cb_endpoint + '/v2/entities?attrs=noexist&limit=' + str(page_size) + filter, headers=headers,
verify=False)
if res.status_code != 200:
return False
entities = []
for entity in res.json():
entities.append({'id': entity['id'], 'type': entity['type']})
body = {
'entities': entities,
'actionType': 'DELETE'
}
res = requests.post(cb_endpoint + '/v2/op/update', json=body, headers=headers, verify=False)
if res.status_code != 204:
print('Error deleting entities (%d): %s' % (res.status_code, res.json()))
return False
return True
def remove_all():
"""
Remove all entities, page after page
"""
i = 1
while get_entities_count() > 0:
print('- Remove page %d' % i)
remove_page()
i += 1
### Main program starts here ###
# Warn user
print("WARNING!!!! This script will delete all the entities matching the filter '%s'" % filter)
print("This action cannot be undone. If you are sure you want to continue type 'yes' and press Enter")
confirm = input()
if (confirm != 'yes'):
sys.exit()
#testing_populate(109, 'testing')
initial_statistics()
remove_all()
print('We are done!')