def post(self): try: data = request.get_json() recognized_objects = data.get("recognized_objects") camera_id = data.get("camera_id") video_filename = data.get("filename") log = Log() log.camera_id = camera_id log.recognized_objects = recognized_objects log.video_filename = video_filename db.session.add(log) db.session.flush() log_id = log.id notification = Notification(content=recognized_objects, log_id=log_id) db.session.add(notification) message = json.dumps({ "type": "camera_log", "link": f"{CLIENT_APP_BASE_URL}/logs/camera_logs/{log_id}", "message": recognized_objects }) notify_about_warning(message) db.session.commit() except Exception as e: print(e) return "Internal Server Error", 500
def create_preview(content, log_type, expires, **kwargs) -> dict: """ Create a log using specified data. :param content: Raw content of log. :type content: str :param log_type: Type of log, can be none if :param content is a list. :type log_type: Union[str, None] :param expires: Expiration time of log. :type expires: Union[int, None] :param kwargs: Extraneous data. """ data = { 'type': log_type, 'content': content, 'uuid': Log.generate_uuid(content), 'expires': pendulum.now().add( seconds=int(expires)).isoformat() if expires else None } result = celery.chain(v1_tasks.parse_text.s(log_type, content), tasks.parse_json.s())() data['data'] = {**result.get(), **kwargs} return data
def create_log(content, log_type, owner, expires, privacy, guild, **kwargs) -> Log: """ Create a log using specified data. :param content: Raw content of log. :type content: list :param log_type: Type of log, can be none if :param content is a list. :type log_type: Union[str, None] :param owner: Log owner. :param expires: Expiration time of log. :type expires: Union[str, None] :param privacy: Log privacy setting. :type privacy: str :param guild: Linked guild of log. Must be set if privacy setting is either guild or mods. :type guild: int :param kwargs: Extraneous data. """ data = {'type': log_type, 'content': json.dumps(content, indent=4), 'owner': owner, 'privacy': privacy, 'guild': guild} uuid = data['uuid'] = Log.generate_uuid(content) if Log.objects.filter(uuid=uuid).exists(): return Log.objects.get(uuid=uuid) data['expires'] = pendulum.parse(expires) if expires else None result = celery.chain(tasks.parse_json.s(content) | tasks.create_pages.s(uuid))() data['data'] = {'tasks': list(reversed(result.as_list())), **kwargs} return Log.objects.create(**data)
def student_aus(request, course_id='all'): """ Lists """ if api.views.is_cached(request): return api.views.api_cacherender(request) courses = [] if course_id is 'all': courselist = api.views.get_all_courses() for course in courselist: courses.append(courselist[course]['id']) pass else: course = api.views.get_course(course_id) if course is None: return api.views.api_render(request, {'error': 'Unknown course code'}, status.HTTP_404_NOT_FOUND) courses.append(course['id']) data = OrderedDict() total = 0 for course_id in courses: course = api.views.get_course(course_id) coursedata = Log.countcountryenrolments('clickstream', course['mongoname']) for countrydata in coursedata: if countrydata['country'] not in data: data[countrydata['country']] = {'count': 0, 'percentage': 0} data[countrydata['country']]['count'] = countrydata['count'] data[countrydata['country']]['percentage'] = countrydata['percentage'] # total += countrydata['count'] # for country in data: # data[country]['percentage'] = float(data[country]['count']) / float(total)*100 return api.views.api_render(request, data, status.HTTP_200_OK)
def student_countries(request, course_id='all'): """ Lists """ if api.views.is_cached(request): return api.views.api_cacherender(request) courses = [] if course_id == 'all' or course_id == 'allcourses': courselist = api.views.get_all_courses() for course in courselist: courses.append(courselist[course]['id']) pass else: course = api.views.get_course(course_id) if course is None: return api.views.api_render(request, {'error': 'Unknown course code'}, status.HTTP_404_NOT_FOUND) courses.append(course['id']) data = OrderedDict() total = 0 for course_id in courses: course = api.views.get_course(course_id) coursedata = Log.countcountryenrolments('clickstream', course['mongoname']) for countrydata in coursedata: if countrydata['country'] not in data: data[countrydata['country']] = {'count': 0, 'percentage': 0} data[countrydata['country']]['count'] += countrydata['count'] total += countrydata['count'] for country in data: data[country]['percentage'] = float(data[country]['count']) / float(total)*100 return api.views.api_render(request, data, status.HTTP_200_OK)
def log(user, host, action): """ Log actions """ mytime = time.strftime('%Y/%m/%d %H:%M:%S') Log(time=mytime, action=action, user=user, host=host).save()
def download_countries(request): """ Returns a count of countries which have downloaded videos from the file server (not including youtube) """ if api.views.is_cached(request): return api.views.api_cacherender(request) data = Log.countfield('country',False,"online_access_logs") return api.views.api_render(request, data, status.HTTP_200_OK)
def download_browsers(request): """ Returns a count of browsers which have downloaded videos from the file server (not including youtube) """ if api.views.is_cached(request): return api.views.api_cacherender(request) data = Log.countfield('request_header_user_agent__browser__family',False,"online_access_logs") return api.views.api_render(request, data, status.HTTP_200_OK)
def student_active(request, course_id='all'): if api.views.is_cached(request): return api.views.api_cacherender(request) courses = [] if course_id is 'all': courselist = api.views.get_all_courses() for course in courselist: courses.append(courselist[course]['id']) pass else: course = api.views.get_course(course_id) if course is None: return api.views.api_render(request, {'error': 'Unknown course code'}, status.HTTP_404_NOT_FOUND) courses.append(course['id']) data = OrderedDict() datadays = {} dataweeks = {} data['days'] = OrderedDict() data['weeks'] = OrderedDict() for course_id in courses: course = api.views.get_course(course_id) activeusers = Log.activeusersdaily('clickstream', course['mongoname']) for day in activeusers: if day['_id'] not in data['days']: datadays[day['_id']] = 0 datadays[day['_id']] += day['userCount'] activeusersweekly = Log.activeusersweekly('clickstream', course['mongoname']) for week in activeusersweekly: theyear = week['_id'][0:4] theweek = week['_id'][5:].zfill(2) realdate = datetime.datetime.strptime(theyear+theweek+'1', '%Y%W%w') thedate = realdate.strftime("%Y-%m-%d") if thedate not in data['weeks']: dataweeks[thedate] = 0 dataweeks[thedate] += week['userCount'] for key in sorted(datadays.iterkeys()): data['days'][key] = datadays[key] for key in sorted(dataweeks.iterkeys()): data['weeks'][key] = dataweeks[key] return api.views.api_render(request, data, status.HTTP_200_OK)
def student_active(request, course_id='all'): if api.views.is_cached(request): return api.views.api_cacherender(request) courses = [] if course_id == 'all' or course_id == 'allcourses': courselist = api.views.get_all_courses() for course in courselist: courses.append(courselist[course]['id']) pass else: course = api.views.get_course(course_id) if course is None: return api.views.api_render(request, {'error': 'Unknown course code'}, status.HTTP_404_NOT_FOUND) courses.append(course['id']) data = OrderedDict() datadays = {} dataweeks = {} data['days'] = OrderedDict() data['weeks'] = OrderedDict() for course_id in courses: course = api.views.get_course(course_id) activeusers = Log.activeusersdaily('clickstream', course['mongoname']) for day in activeusers: if day['_id'] not in data['days']: datadays[day['_id']] = 0 datadays[day['_id']] += day['userCount'] activeusersweekly = Log.activeusersweekly('clickstream', course['mongoname']) for week in activeusersweekly: theyear = week['_id'][0:4] theweek = week['_id'][5:].zfill(2) realdate = datetime.datetime.strptime(theyear+theweek+'1', '%Y%W%w') thedate = realdate.strftime("%Y-%m-%d") if thedate not in data['weeks']: dataweeks[thedate] = 0 dataweeks[thedate] += week['userCount'] for key in sorted(datadays.iterkeys()): data['days'][key] = datadays[key] for key in sorted(dataweeks.iterkeys()): data['weeks'][key] = dataweeks[key] return api.views.api_render(request, data, status.HTTP_200_OK)
def temperature(): # todo: add authentication, and validations to this route data = request.form result = { 'city': 'Address not found', 'temp': '', } # Save logs log = Log(search=json.dumps(data)) save_model(log) # validate required field if 'address' in data and data['address']: # Get geo geometry = get_geometry_locations(data['address']) if len(geometry) >= 2: result['city'] = geometry['city'] # Check if was already added to the database cached = Address.query.filter_by( geo=json.dumps(geometry['geo'])).first() if cached: # set cached values result['temp'] = cached.temperature # Assume the temperature for a Zipcode does not vary within 1-hour window if is_cache_invalid(cached): # if not valid, update it # I choose using lat, lng because some searche (ex: brazil) do not have zipcode temp = get_temperature_by_geometry(geometry['geo']) result['temp'] = "{0}° C".format(round(temp['temp'])) # Update model temperature cached.temperature = result['temp'] cached.updated = datetime.datetime.utcnow() save_model(cached) else: temp = get_temperature_by_geometry(geometry['geo']) result['temp'] = "{0}° C".format(round(temp['temp'])) # Todo: encapsulate it address_model = Address( address=result['city'], geo=json.dumps(geometry['geo']), temperature=result['temp'], ) save_model(address_model) else: return "address required parameter not found in form data", 400 return jsonify(result)
def _add_log_item(tsk_id, log_type, log_msg): user = session.get('USER', None) user_id = None if user: user_id = user[0] log_item = Log( log_type=log_type, log_msg=log_msg, log_tsk_id=tsk_id, log_usr_id=user_id, ) db.session.add(log_item) db.session.commit()
def create_log(content, log_type, owner, expires, privacy, guild, **kwargs) -> Log: """ Create a log using specified data. :param content: Raw content of log. :type content: str :param log_type: Type of log, can be none if :param content is a list. :type log_type: Union[str, None] :param owner: Log owner. :param expires: Expiration time of log. :type expires: Union[int, None] :param privacy: Log privacy setting. :type privacy: str :param guild: Linked guild of log. Must be set if privacy setting is either guild or mods. :type guild: int :param kwargs: Extraneous data. """ data = { 'type': log_type, 'content': content, 'owner': owner, 'privacy': privacy, 'guild': guild } uuid = data['uuid'] = Log.generate_uuid(content) if Log.objects.filter(uuid=uuid).exists(): return Log.objects.get(uuid=uuid) data['expires'] = pendulum.now().add( seconds=int(expires)) if expires else None messages = [ '{current}/{total} messages parsed... ({percent}%)', '{current}/{total} messages formatted... ({percent}%)', 'Saving messages... ({percent}%)' ] result = celery.chain(v1_tasks.parse_text.s(log_type, content), tasks.parse_json.s() | tasks.create_pages.s(uuid))() task_ids = utils.get_chain_tasks(result) data['data'] = { 'tasks': utils.add_task_messages(task_ids, messages=messages), **kwargs } return Log.objects.create(**data)