def metrics(request, game_uuid): try: game = Game.objects.get(uuid=game_uuid) except Game.DoesNotExist: return api_error('Requested game does not exist', code=404) except Game.MultipleObjectsReturned: return api_error('Internal server error', code=500) try: container = fieldmarshal.loads(MetricContainer, request.body) except ValueError: return api_error('Could not parse JSON body', code=400) events = {} for metric in container.metrics: if metric.event not in events: events[metric.event] = [] metric.properties['game_uuid'] = game.uuid events[metric.event].append(metric.properties) # Keen to the rescue keen.add_events(events) return jsonify('', code=204)
def errors(request, game_uuid): try: game = Game.objects.get(uuid=game_uuid) except Game.DoesNotExist: return api_error('Requested game does not exist', code=404) except Game.MultipleObjectsReturned: return api_error('Internal server error', code=500) try: container = fieldmarshal.loads(ReportContainer, request.body) except ValueError: return api_error('Could not parse JSON body', code=400) crashes = [] for report in container.errors: data = fieldmarshal.dumpd(report.tags) data['game_uuid'] = game.uuid crashes.append(data) keen.add_events({ 'crashes': crashes, }) for report in container.errors: c = CrashReport(game=game, traceback=report.message) c.distinct_id = report.tags.distinct_id c.os = report.tags.os c.version = report.tags.version c.save() return api_error('', code=204)
def handle_results(results, options): """ results is a dict that maps from hostname to the # of open conns on that host """ all_under_limit = True over_limit_hosts = [] for (host, num_conns) in results.iteritems(): if num_conns > options.maxconns: all_under_limit = False over_limit_hosts.append((host, num_conns)) if not all_under_limit: # one of the hosts was over, alert text = "DRPC server's open connections over threshold on the following hosts:\n\n" over_limit_hosts = sorted(over_limit_hosts) for host in over_limit_hosts: text += "{}: {}\n".format(host, results[host]) emailer.Emailer.send_email(addr_to=options.email, subject="DRPC Server Open Conns Too High", text=text, categories=["open_drpc_conn_mon"]) events = [] for (host, num_conns) in results.iteritems(): events.append({ "host": host, "numconns": num_conns, "maxconns": options.maxconns, "over_limit": host in over_limit_hosts }) keen.add_events({"open_drpc_conn_mon": events})
def test_module_level_add_events(self): keen.project_id = "5004ded1163d66114f000000" api_key = "2e79c6ec1d0145be8891bf668599c79a" keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]}) # client = KeenClient(project_id, write_key=write_key, read_key=read_key) keen.add_events({"python_test": [{"hello": "goodbye"}]})
def mark_user_views_event(event_id, user_id, ip_address): if user_id: view = View() view.event_id = event_id view.user_id = user_id view.save() ipview, created = ViewIP.objects.get_or_create(event_id=event_id, ip_address=ip_address) ipview.count = F('count') + 1 ipview.save() # Send info to keen staff_status = False if user_id: try: user = get_user_model().objects.get(pk=user_id) staff_status = user.is_staff except get_user_model().DoesNotExist: keen.add_event("Error", { "msg": "User.DoesNotExist", "id": user_id }) event = Event.objects.get(pk=event_id) keen_events = [] for category in event.category.all(): keen_events.append({ 'keen': { 'time_stamp': timezone.now().isoformat(), 'location': { 'coordinates': [float(event.venue.longitude), float(event.venue.latitude)], } }, "user": { "user_id": user_id, "staff": staff_status, "ip": ip_address }, "event": { "event_id": event_id, "category": category.name, "min_age": event.min_age, "max_age": event.max_age, "cost": event.cost, "start_date": event.start_date.isoformat(), "end_date": event.end_date.isoformat() if event.end_date else None, "publish_date": event.published_at.isoformat() if event.published_at else None }, "venue": { "venue_id": event.venue.id, "name": event.venue.name, "city": event.venue.city, "neighborhood": event.venue.neighborhood, }}) keen.add_events({"view_single": keen_events})
def get(self): current_time = datetime.datetime.utcnow() one_hour_ago = current_time - datetime.timedelta(hours=1) visits = [serialize_ndb_model(visit) for visit in Visit.query(Visit.timestamp >= one_hour_ago)] if visits: keen.add_events({ 'visits': visits, })
def keen_add_events(event_collection, payload): ''' Wrapper for keen.add_events(), adds project info to each event Param event_collection : collection event data is submitted to Param payload : array of events that is submitted ''' # add project info to each event payload = add_project_info_list(payload) # submit list of events to Keen.io keen.add_events({event_collection: payload})
def send_data_to_keen(keen_json): hey = "" contents = read_contents(keen_json) for key in contents.keys(): print "XANDER LOOK HERE" hey = key events = contents.get(key) for event in events: keen.add_events({str(key): [event]}) mynewkeys = event.keys() return hey, mynewkeys
def add_events(event_collection, payload): """ Wrapper for keen.add_events(), adds project info to each event. Param event_collection : collection event data is submitted to Param payload : array of events that is submitted """ # add project info to each event payload = add_project_info_list(payload) # submit list of events to Keen.io keen.add_events({event_collection: payload}) logger.info( "Sent multiple events to '%s' collection (Keen.io)", event_collection )
def test_direct_persistence_strategy(self, post): post.return_value = self.SINGLE_ADD_RESPONSE keen.add_event("python_test", {"hello": "goodbye"}) keen.add_event("python_test", {"hello": "goodbye"}) post.return_value = self.MULTI_ADD_RESPONSE keen.add_events( { "sign_ups": [{ "username": "******", "referred_by": "steve", "son_of": "my_mom" }], "purchases": [ {"price": 5}, {"price": 6}, {"price": 7} ]} )
def submit_order(request): json_request = json.loads(request.body) trades = json_request.get('trades') cash = json_request.get('cash') reasoning = json_request.get('reasoning') notes = json_request.get('notes') if trades is None or cash is None: return HttpResponseBadRequest('Error: missing trade information') group = get_group(request) if isinstance(group, HttpResponseForbidden): return group if DEBUG != 'True': keen.add_events({ 'trades': trades, 'orders': [{ 'group': group.get('group_account'), 'email': request.user.email }] }) try: sender = '%s %s <%s>' % (request.user.first_name, request.user.last_name, request.user.email) other_members = group.get('members').exclude(email=request.user.email) other_members_emails = [ '%s %s <%s>' % (member.first_name, member.last_name, member.email) for member in other_members ] supervisors = [ '%s %s <%s>' % (supervisor.first_name, supervisor.last_name, supervisor.email) for supervisor in group.get('supervisors') ] trader_content = render_to_string( 'trade/email_template.html', { 'trades': trades, 'cash': cash, 'notes': notes, 'group_account': group.get('group_account'), 'group_number': group.get('group_number') }, request) supervisor_content = render_to_string( 'trade/email_template.html', { 'trades': trades, 'cash': cash, 'reasoning': reasoning, 'notes': notes, 'group_account': group.get('group_account'), 'group_number': group.get('group_number') }, request) from aitrading.templatetags.template_tags import remove_extra_0 trader_msg = EmailMessage( from_email=sender, to=supervisors, bcc=[sender], cc=other_members_emails, subject='Applied Investments Trade Request - Group %s (%s)' % (group.get('group_number'), remove_extra_0(group.get('group_account'))), body=str(trader_content)) trader_msg.content_subtype = 'html' trader_msg.send() supervisor_msg = EmailMessage( from_email=sender, to=supervisors, bcc=[sender], cc=other_members_emails, subject= 'Reasoning for Applied Investments Trade Request - Group %s (%s)' % (group.get('group_number'), remove_extra_0(group.get('group_account'))), body=str(supervisor_content)) supervisor_msg.content_subtype = 'html' supervisor_msg.send() return JsonResponse(data={}) except Exception as e: return HttpResponse('Error: ' + str(e), status=500)
def test_module_level_add_events(self, post): post.return_value = self.MULTI_ADD_RESPONSE keen.add_events({"python_test": [{"hello": "goodbye"}]})
def _send_to_keen(survey_key, course_key, answer_keys): events = [] student = Student.query(Student.user == survey_key.get().participant).get() course = course_key.get() answers = ndb.get_multi(answer_keys) for answer in answers: question = answer.question.get() if answer.string_value != '': response = urlfetch.fetch( 'http://text-processing.com/api/sentiment', payload=urllib.urlencode({'text': answer.string_value}), method=urlfetch.POST) # If we've been throttled, just give up and die if response.status_code == 503: continue elif response.status_code != 200: raise deferred.PermanentTaskFailure() sentiment = json.loads(response.content) answer.sentiment = sentiment['label'] answer.put() lecturer = course.lecturer.get() event = { 'question_key': question.key.urlsafe(), 'survey_key': answer.key.parent().urlsafe(), 'course_key': course.key.urlsafe(), 'course': { 'name': course.course.get().name, 'department': course.course.get().department.urlsafe(), 'faculty': course.course.get().faculty.urlsafe(), 'school': course.course.get().faculty.get().school.urlsafe(), }, 'question_number': question.number, 'question_text': question.question, 'lecturer': { 'key': lecturer.key.urlsafe(), 'name': lecturer.name, 'department': lecturer.department.get().name, 'faculty': lecturer.department.get().faculty.get().name, }, 'student': { 'key': student.key.urlsafe(), 'age': student.calculate_age(), 'gender': student.gender, 'status': student.status, 'year': student.year, }, } if question.question_type == 'closed': event['response'] = answer.int_value else: event['sentiment'] = answer.sentiment events.append(event) keen.add_events({'answers': events})
def sendStats (stats): log(">>> send stats") keen.add_events(stats) log("<<< send stats")
def mark_user_views_event(event_id, user_id, ip_address): if user_id: view = View() view.event_id = event_id view.user_id = user_id view.save() ipview, created = ViewIP.objects.get_or_create(event_id=event_id, ip_address=ip_address) ipview.count = F('count') + 1 ipview.save() # Send info to keen staff_status = False if user_id: try: user = get_user_model().objects.get(pk=user_id) staff_status = user.is_staff except get_user_model().DoesNotExist: keen.add_event("Error", { "msg": "User.DoesNotExist", "id": user_id }) event = Event.objects.get(pk=event_id) keen_events = [] for category in event.category.all(): keen_events.append({ 'keen': { 'time_stamp': timezone.now().isoformat(), 'location': { 'coordinates': [ float(event.venue.longitude), float(event.venue.latitude) ], } }, "user": { "user_id": user_id, "staff": staff_status, "ip": ip_address }, "event": { "event_id": event_id, "category": category.name, "min_age": event.min_age, "max_age": event.max_age, "cost": event.cost, "start_date": event.start_date.isoformat(), "end_date": event.end_date.isoformat() if event.end_date else None, "publish_date": event.published_at.isoformat() if event.published_at else None }, "venue": { "venue_id": event.venue.id, "name": event.venue.name, "city": event.venue.city, "neighborhood": event.venue.neighborhood, } }) keen.add_events({"view_single": keen_events})