-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget_occurrence_data.py
More file actions
102 lines (75 loc) · 3.39 KB
/
Copy pathget_occurrence_data.py
File metadata and controls
102 lines (75 loc) · 3.39 KB
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
# _______________________________________________________________
# Get all occurrence details for a given Rollbar item
# _______________________________________________________________
# _______________________________________________________________
# Importing requirements
# _______________________________________________________________
import requests, json, time
# _______________________________________________________________
# Initializing variables
# Place your Rollbar access token and item counter here
# _______________________________________________________________
account_read_token = ''
# Counter of the item you're trying to pull occurrences (this is the item number in the UI)
item_counter = ''
# If you know the occurrence ID you want to start at, place it here. Otherwise, leave as None
last_occurrence_id = None
# Set to True to create a local file of occurrence data
make_file = False
# _______________________________________________________________
# Get item ID using the item counter above
# _______________________________________________________________
def retrieve_item_id(item_counter):
headers = {
'X-Rollbar-Access-Token': account_read_token
}
payload = {}
url = 'https://api.rollbar.com/api/1/item_by_counter/' + str(item_counter)
response = requests.request('GET', url, headers = headers, data = payload)
# Parse the response to JSON
response_data = response.json()
# Getting a list of the project IDs
rollbar_item_id = response_data['result']['id']
return rollbar_item_id
# _______________________________________________________________
# Retrieve occurrence information
# _______________________________________________________________
def get_occurrences(rollbar_item_id, last_occurrence_id):
headers = {
'X-Rollbar-Access-Token': account_read_token
}
payload = {}
url = 'https://api.rollbar.com/api/1/item/' + str(rollbar_item_id) + '/instances?lastId=' + str(last_occurrence_id)
response = requests.request('GET', url, headers = headers, data = payload)
# Parse the response to JSON
response_data = response.json()
# Check if any data is returned, if not, let's end the loop
if len(response_data['result']['instances']) == 0:
print('There are no more occurrences')
return 'data_finished'
# If there is data, then let's parse those
for i in response_data['result']['instances']:
last_occurrence_id = i['id']
print(str(i))
# Write to file if necessary
if make_file:
occurrence_file.write(str(i) + "\n")
return last_occurrence_id
# _______________________________________________________________
# Running the functions above
# Find the ID of your item, then pull the occurrence data
# Optionally add this data to a document
# _______________________________________________________________
# Create file to store occurrence data
if make_file:
occurrence_file = open('occurrence_data.txt', 'w')
# Get item ID from counter
rollbar_item_id = retrieve_item_id(item_counter)
print('Item ID: ' + str(rollbar_item_id))
# Get occurrence data using item ID
while not last_occurrence_id == 'data_finished':
last_occurrence_id = get_occurrences(rollbar_item_id, last_occurrence_id)
print('Last occurrence ID is: ' + str(last_occurrence_id))
# Close file to store occurrence data
if make_file:
occurrence_file.close()