Beispiel #1
0
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()
Beispiel #2
0
    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
Beispiel #3
0
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)
Beispiel #4
0
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()