Skip to content
Snippets Groups Projects
Unverified Commit fe37378e authored by REIG Julien's avatar REIG Julien
Browse files

create presence and retrieve it

parent a96b061a
No related branches found
No related tags found
1 merge request!16Node knowledge
......@@ -7,7 +7,8 @@ from consts.status import Status, response_offline
from models.command import Command
from models.resource import Resource as ResourceModel
from utils.database_utils import (get_resource, get_state, insert_command,
insert_resource, update_state)
insert_presence_bulk, insert_resource,
update_state)
from utils.http_utils import normalize_url
duplicate_controller = Blueprint('duplicate_controller', __name__)
......@@ -37,6 +38,9 @@ class DuplicateResourceResource(Resource):
insert_resource(resource)
insert_command(resource.id, resource.commands[0])
if resource.presences is not None and len(resource.presences) > 0:
insert_presence_bulk(resource)
update_state(Status.ONLINE, normalize_url(request.host_url))
return resource.to_full_dict(), 201
......
......@@ -5,8 +5,8 @@ from flask_restful import Api, Resource
from consts.status import Status, response_offline
from models.command import Command
from utils.database_utils import (get_resource, get_state, insert_command,
update_state)
from utils.database_utils import (get_presence, get_resource, get_state,
insert_command, update_state)
from utils.http_utils import normalize_url, send_command_to_other_nodes
resource_controller = Blueprint('resource_controller', __name__)
......@@ -18,9 +18,9 @@ class ResourceResource(Resource):
if get_state() == Status.OFFLINE:
return response_offline, 403
resource = get_resource(id, True)
resource = get_resource(id, with_commands=True, with_presence=True)
if resource:
return jsonify(resource.to_full_dict())
return jsonify(resource.to_full_dict(include_presence=True))
else:
return {"message": "Resource not found"}, 404
......@@ -31,7 +31,9 @@ class ResourceResource(Resource):
update_state(Status.UPDATING, node_address)
request_command: Optional[str] = request.get_json().get('command')
request_addresses: list[str] = request.get_json().get('addresses', [])
request_addresses: list[str] = request.get_json().get('addresses', None)
if (request_addresses is None or len(request_addresses) == 0):
request_addresses = get_presence(id)
if request_command is None:
update_state(Status.ONLINE, node_address, {"message": "command is missing", "statusCode": 400})
......@@ -54,7 +56,7 @@ class ResourceResource(Resource):
insert_command(id, command)
update_state(Status.ONLINE, node_address)
return jsonify(get_resource(id, True).to_full_dict())
return jsonify(get_resource(id, True, True).to_full_dict(include_presence=True))
def delete(self, id: str):
if get_state() == Status.OFFLINE:
......@@ -80,7 +82,7 @@ class ResourceResource(Resource):
insert_command(id, command)
update_state(Status.ONLINE, node_address)
return jsonify(get_resource(id, True).to_full_dict())
return jsonify(get_resource(id, True, True).to_full_dict(include_presence=True))
api.add_resource(ResourceResource, '/resource/<string:id>')
......@@ -7,7 +7,8 @@ from consts.status import Status, response_offline
from models.command import Command
from models.resource import Resource as ResourceModel
from utils.database_utils import (get_resources, get_state, insert_command,
insert_resource, update_state)
insert_presence_bulk, insert_resource,
update_state)
from utils.http_utils import normalize_url, send_resource_to_other_nodes
resources_controller = Blueprint('resources_controller', __name__)
......@@ -35,6 +36,7 @@ class ResourcesResource(Resource):
request_name: Optional[str] = request.get_json().get('name')
request_addresses: list[str] = request.get_json().get('addresses', [])
request_addresses = list(map(lambda x: normalize_url(x, True), request_addresses))
presences = request_addresses.copy()
if request_command is None:
update_state(Status.ONLINE, node_address, {"message": "command is missing", "statusCode": 400})
......@@ -49,7 +51,8 @@ class ResourcesResource(Resource):
resource = ResourceModel(
request_name,
commands=[Command(request_command)]
commands=[Command(request_command)],
presences=presences
)
# TODO: Potentiellement long, à mettre dans un thread ?
......@@ -57,6 +60,7 @@ class ResourcesResource(Resource):
insert_resource(resource)
insert_command(resource.id, resource.commands[0])
insert_presence_bulk(resource)
update_state(Status.ONLINE, node_address)
......
......@@ -6,7 +6,7 @@ from .command import Command
class Resource:
def __init__(self, name: str, commands: list[Command] = None):
def __init__(self, name: str, commands: list[Command] = None, presences: list[str] = None):
self.id = generate_hexadecimal(HEXADECIMAL_WORD_LENGTH)
self.name = name
......@@ -14,7 +14,7 @@ class Resource:
self.commands = [] if commands is None else commands
self.commands_count = len(self.commands)
self.deleted = False
self.presences = []
self.presences = presences if presences is not None else []
def __str__(self):
return f"Resource(id={self.id}, name={self.name}, last_update={self.last_update}, commands={self.commands}, commands_count={self.commands_count}, deleted={self.deleted}, presence={self.presences})"
......
......@@ -247,7 +247,7 @@ def insert_resource_queue(node_address: str, resource: Resource) -> bool:
try:
cursor = db_conn.cursor()
cursor.execute(
"INSERT INTO resource_queue (node_address, resource_object) VALUES (?, ?)", (node_address, json.dumps(resource.to_full_dict()),))
"INSERT INTO resource_queue (node_address, resource_object) VALUES (?, ?)", (node_address, json.dumps(resource.to_full_dict(include_presence=True)),))
db_conn.commit()
cursor.close()
return True
......
......@@ -44,7 +44,7 @@ def send_resource_to_other_nodes(addresses: list[str], resource: Resource):
result = requests.put(
f"{address}/api/duplicate/resource",
data=json.dumps({
"resource": resource.to_full_dict(),
"resource": resource.to_full_dict(include_presence=True),
}),
headers={"Content-Type": "application/json"})
if result.status_code != 201:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment