def set_metadata_view(request): results = {} if request.method == 'GET': metric = request.GET['metric'] key = request.GET['key'] value = request.GET['value'] try: results[metric] = CarbonLink.set_metadata(metric, key, value) except: log.exception() results[metric] = dict(error="Unexpected error occurred in CarbonLink.set_metadata(%s, %s)" % (metric, key)) elif request.method == 'POST': if request.META.get('CONTENT_TYPE') == 'application/json': operations = json.loads( request.raw_post_data ) else: operations = json.loads( request.POST['operations'] ) for op in operations: metric = None try: metric, key, value = op['metric'], op['key'], op['value'] results[metric] = CarbonLink.set_metadata(metric, key, value) except: log.exception() if metric: results[metric] = dict(error="Unexpected error occurred in bulk CarbonLink.set_metadata(%s)" % metric) else: results = dict(error="Invalid request method") return HttpResponse(json.dumps(results), mimetype='application/json')
def post_event(request): if request.method == 'POST': event = json.loads(request.raw_post_data) assert isinstance(event, dict) values = {} values["what"] = event["what"] values["tags"] = event.get("tags", None) values["when"] = datetime.datetime.fromtimestamp( event.get("when", time.time())) if "data" in event: values["data"] = event["data"] e = models.Event(**values) e.save() return HttpResponse(status=200) else: return HttpResponse(status=405)