Exemplo n.º 1
0
 def send_process_status(self):
     from main.app import db  # import here to avoid import loop
     from main.app import message_queue  # import here to avoid import loop
     from main.resources.resource_util import find_resource  # import here to avoid import loop
     process_id = os.getpid()
     connections = []
     for ws_conn in self.connections:
         connections.append({
             'connected':
             ws_conn.connected(),
             'controller_id':
             ws_conn.controller_id,
             'user_id':
             ws_conn.user_id,
             'auth_method':
             ws_conn.auth_method,
             'process_id':
             process_id,
             'subscriptions': [s.as_dict() for s in ws_conn.subscriptions],
         })
     parameters = {
         'process_id': process_id,
         'clients': connections,  # fix(later): rename to connections?
         'db_pool': db.engine.pool.size(),
         'db_conn': db.engine.pool.checkedout(),
     }
     system_folder_id = find_resource('/system').id
     message_queue.add(system_folder_id, '/system', 'processStatus',
                       parameters)
Exemplo n.º 2
0
 def post(self):
     folder_path = request.values.get('folderPath',
                                      request.values.get('folder_path', ''))
     if not folder_path:
         abort(400)
     folder = find_resource(folder_path)
     if not folder:
         abort(404)
     if access_level(folder.query_permissions()) < ACCESS_LEVEL_WRITE:
         abort(403)
     type = request.values['type']
     parameters = json.loads(request.values['parameters'])
     auth_code = request.values.get('authCode',
                                    '')  # fix(soon): migrate away from this
     if auth_code:
         key = find_key_by_code(
             auth_code
         )  # fix(faster): we already looked up key in access_level function
     elif request.authorization:
         key = find_key(request.authorization.password)
     if not key:
         abort(403)
     sender_controller_id = key.access_as_controller_id  # None if access as user
     sender_user_id = key.access_as_user_id  # None if access as controller
     message_queue.add(folder.id,
                       type,
                       parameters,
                       sender_controller_id=sender_controller_id,
                       sender_user_id=sender_user_id)
     return {'status': 'ok'}
Exemplo n.º 3
0
 def post(self):
     folder_path = request.values.get('folderPath',
                                      request.values.get('folder_path', ''))
     if not folder_path:
         abort(400)
     if not folder_path.startswith('/'):
         abort(400)
     folder = find_resource(folder_path)
     if not folder:
         abort(404)
     if access_level(folder.query_permissions()) < ACCESS_LEVEL_WRITE:
         abort(403)
     if not request.authorization:
         abort(403)
     key = find_key(request.authorization.password)
     if not key:
         abort(403)
     message_type = request.values['type']
     parameters = json.loads(request.values['parameters'])
     sender_controller_id = key.access_as_controller_id  # None if access as user
     sender_user_id = key.access_as_user_id  # None if access as controller
     message_queue.add(folder.id,
                       None,
                       message_type,
                       parameters,
                       sender_controller_id=sender_controller_id,
                       sender_user_id=sender_user_id)
     return {'status': 'ok'}
Exemplo n.º 4
0
def update_sequence_value(resource, resource_path, timestamp, value, emit_message=True):
    data_type = json.loads(resource.system_attributes)['data_type']

    # determine min interval between updates
    system_attributes = json.loads(resource.system_attributes) if resource.system_attributes else {}
    min_storage_interval = system_attributes.get('min_storage_interval')
    if min_storage_interval is None:
        if data_type == Resource.TEXT_SEQUENCE:
            min_storage_interval = 0
        else:
            min_storage_interval = 50

    # prep sequence update message data
    if emit_message:
        message_params = {
            'id': resource.id,
            'name': resource_path,  # full/absolute path of the sequence
            'timestamp': timestamp.isoformat() + 'Z',
        }
        if data_type != Resource.IMAGE_SEQUENCE:  # for images we'll send revision IDs
            message_params['value'] = value  # fix(soon): json.dumps crashes if this included binary data

    # if too soon since last update, don't store a new value (but do still send out an update message)
    if min_storage_interval == 0 or timestamp >= resource.modification_timestamp + datetime.timedelta(seconds=min_storage_interval):
        resource_revision = add_resource_revision(resource, timestamp, value.encode())
        resource.modification_timestamp = timestamp

        # create thumbnails for image sequences
        if data_type == Resource.IMAGE_SEQUENCE:
            max_width = 240
            name = 'thumbnail-%d-x' % max_width
            thumbnail_contents = compute_thumbnail(value, max_width)[0]
            try:
                thumbnail_resource = Resource.query.filter(Resource.parent_id == resource.id, Resource.name == name, not_(Resource.deleted)).one()
            except NoResultFound:
                thumbnail_resource = create_sequence(resource, name, Resource.IMAGE_SEQUENCE)
            thumbnail_revision = add_resource_revision(thumbnail_resource, timestamp, thumbnail_contents)
            if emit_message:
                message_params['revision_id'] = resource_revision.id
                message_params['thumbnail_revision_id'] = thumbnail_revision.id

    # create a short lived update message for subscribers to the folder containing this sequence
    if emit_message:
        folder_path = resource_path.rsplit('/', 1)[0]
        message_queue.add(
            folder_id=resource.parent_id, folder_path=folder_path, message_type='sequence_update', parameters=message_params, timestamp=timestamp)