Skip to content

Instantly share code, notes, and snippets.

@PanosJee
Created June 20, 2014 21:13
Show Gist options
  • Save PanosJee/d2e3d8160ceb5eef9685 to your computer and use it in GitHub Desktop.
Save PanosJee/d2e3d8160ceb5eef9685 to your computer and use it in GitHub Desktop.
Get last day errors across all BugSense projects
import requests
import json
base = 'https://www.bugsense.appspot.com'
def get_all_projects(token, **kwargs):
from datetime import datetime, timedelta
timestamp = (datetime.utcnow() - timedelta(days=1)).replace(hour=23,minute=59,second=59)
base_url = "%s/api/v1/projects.json" % (base)
headers = {'X-BugSense-Auth-Token': token}
resp = requests.get(base_url, headers=headers, verify=False)
projects = []
try:
# old requests
projects = resp.json()['projects']
except:
# new requests
projects = resp.json['projects']
# iterate through the projects
for project in projects:
api_key = project['api_key']
name = project['name']
_call = lambda url: requests.get(url, headers=headers, verify=False)
# Get the facets
base_url = "%s/api/v1/project/%s/analytics/facets.json?order=updated" % (base, api_key)
headers = {'X-BugSense-Auth-Token': token}
resp = _call(base_url)
try:
results = resp.json()
except:
results = resp.json
app_versions = ('|').join(results['app_versions'])
base_url = "%s/api/v1/project/%s/analytics/errors.json?app_versions=%s" % (base, api_key, app_versions)
resp = _call(base_url)
try:
results = resp.json()
except:
results = resp.json
# get yesterday's results
project['total_crashes'] = results['total'][-2]
project['new_crashes'] = results['new'][-2]
project['app_version_crashes'] = {}
project['timestamp'] = timestamp.isoformat()
for key, value in results.items():
if key.startswith('av:'):
project['app_version_crashes'][key.split(':')[1]] = value[-2]
# At this point you can iterate over projects and write to a Splunk modular input
print json.dumps(projects)
if __name__ == '__main__':
get_all_projects("YOUR API TOKEN") # you can find the API token at https://www.bugsense.com/account
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment