Skip to content

Commit

Permalink
mov
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlolly committed Jan 12, 2024
1 parent 417c63a commit c7e9ea6
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 22 deletions.
54 changes: 54 additions & 0 deletions tmp/Web_API_Thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging
import threading
from datetime import datetime
from enum import Enum
from wannadb.data.data import Attribute, Document
from wannadb.statistics import Statistics
from wannadb_web.worker.Web_API import Web_API
from wannadb.resources import ResourceManager

logger = logging.getLogger(__name__)


class Status(Enum):
"""Gives the status of the application."""
IDLE = 1
RUNNING = 2
CREATED = 3
DEAD = 98
ERROR = 99


class Web_API_Thread(threading.Thread):
def __init__(self, thread_id):
super().__init__()
self.function = None
self.thread_id = thread_id
self.wannadb_web_api = Web_API()
self.event = threading.Event()
self.status = Status.IDLE
self.last_call = datetime.now()
self.exit_flag = False

def run(self):
ResourceManager()
self.status = Status.RUNNING
while True:
if self.exit_flag:
self.status = Status.DEAD
logger.info(f"Thread {self.thread_id} exited")
return
self.event.wait()
self.event.clear()
if self.function is not None:
self.function()
self.last_call = datetime.now()
else:
raise Exception("No function set")
self.function = None

def create_document_base(self, documents: [Document], attributes: [Attribute], statistics: Statistics):
if self.function is not None:
raise Exception("Function running")
self.function = lambda: self.wannadb_web_api.create_document_base_task(documents, attributes, statistics)
self.event.set()
48 changes: 48 additions & 0 deletions tmp/Web_Thread_Manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import threading
import logging.config
import time
from datetime import datetime
from wannadb_web.worker.Web_API_Thread import Web_API_Thread

logger = logging.getLogger(__name__)


class Web_Thread_Manager(threading.Thread):
def __init__(self, idle_time=60):
super().__init__()
logger.info("Web_Thread_Manager initialized")
self.idle_time = idle_time
self.threads: dict[int, Web_API_Thread] = {}
self.thread_limit = 2
global web_Thread_Manager
web_Thread_Manager = self

def run(self):
logger.info("Web_Thread_Manager running")
while True:
time.sleep(self.idle_time)
for thread_id, thread in self.threads.items():
if not thread.is_alive():
logger.info(f"Thread {thread_id} cleaned")
del self.threads[thread_id]
elif (datetime.now() - thread.last_call).total_seconds() > self.idle_time:
thread.exit_flag = True

def access_thread(self, thread_id):
if thread_id not in self.threads:
logger.error("Thread not found")
raise threading.ThreadError("Thread not found")
logger.debug(f"Thread {thread_id} accessed")
return self.threads[thread_id]

def new_thread(self, thread_id):
if thread_id in self.threads:
logger.debug(f"Thread {thread_id} already exists")
return self.threads[thread_id]
if len(self.threads) >= self.thread_limit:
logger.error("Thread limit reached")
raise threading.ThreadError("Thread limit reached")
thread = Web_API_Thread(thread_id)
thread.start()
logger.debug(f"Thread {thread_id} created and started")
return thread
File renamed without changes.
30 changes: 20 additions & 10 deletions postgres/transactions.py → wannadb_web/postgres/transactions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Union

import bcrypt
from psycopg2 import sql, IntegrityError
from config import Token, Authorisation, tokenDecode
from postgres.queries import checkPassword
from postgres.util import execute_transaction
from wannadb_web.util import Token, Authorisation, tokenDecode
from wannadb_web.postgres.queries import checkPassword
from wannadb_web.postgres.util import execute_transaction


# WARNING: This is only for development purposes!
Expand Down Expand Up @@ -59,8 +61,9 @@ def createDocumentsTable(schema):
create_table_query = sql.SQL(f"""CREATE TABLE {schema}.documents
(
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
name text COLLATE pg_catalog."default" NOT NULL,
content text COLLATE pg_catalog."default" NOT NULL,
name text NOT NULL,
content text ,
content_byte bytea,
organisationid bigint NOT NULL,
userid bigint NOT NULL,
CONSTRAINT dokumentid PRIMARY KEY (id),
Expand All @@ -74,6 +77,8 @@ def createDocumentsTable(schema):
ON UPDATE CASCADE
ON DELETE CASCADE
NOT VALID
CONSTRAINT check_only_one_filled
check (((content IS NOT NULL) AND (content_byte IS NULL)) OR ((content IS NOT NULL) AND (content_byte IS NULL)))
)
TABLESPACE pg_default;""")
Expand Down Expand Up @@ -142,7 +147,7 @@ def addUser(user: str, password: str):
pwBytes = password.encode('utf-8')
salt = bcrypt.gensalt()
pwHash = bcrypt.hashpw(pwBytes, salt)
# Needed this for the correct password check dont know why...
# Needed this for the correct password check don't know why...
pwHash = pwHash.decode('utf-8')

insert_data_query = sql.SQL("INSERT INTO users (username, password) VALUES (%s, %s) returning id;")
Expand Down Expand Up @@ -198,7 +203,6 @@ def addOrganisation(organisationName: str, sessionToken: str):
try:
token: Token = tokenDecode(sessionToken)
userid = token.id

insert_query = sql.SQL("with a as (INSERT INTO organisations (name) VALUES (%s) returning id) "
"INSERT INTO membership (userid,organisationid) select (%s),id from a returning organisationid")
organisation_id = execute_transaction(insert_query, (organisationName, userid), commit=True)
Expand Down Expand Up @@ -344,13 +348,19 @@ def adjUserAuthorisation(organisationName: str, sessionToken: str, userToAdjust:
print("adjUserAuthorisation failed because: \n", e)


def addDocument(name: str, content: str, organisationId: int, userid: int):
def addDocument(name: str, content: Union[str, bytes], organisationId: int, userid: int):
try:
insert_data_query = sql.SQL("INSERT INTO documents (name,content,organisationid,userid) "
"VALUES (%s, %s,%s, %s) returning id;")
if isinstance(content, str):
insert_data_query = sql.SQL("INSERT INTO documents (name,content,organisationid,userid) "
"VALUES (%s, %s,%s, %s) returning id;")
else:
insert_data_query = sql.SQL("INSERT INTO documents (name,content_byte,organisationid,userid) "
"VALUES (%s, %s,%s, %s) returning id;")
data_to_insert = (name, content, organisationId, userid)
response = execute_transaction(insert_data_query, data_to_insert, commit=True)
return int(response[0][0])
except IntegrityError:
return -1

except Exception as e:
print("addDocument failed because: \n", e)
12 changes: 7 additions & 5 deletions postgres/util.py → wannadb_web/postgres/util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os

import psycopg2
from psycopg2 import extensions, IntegrityError, sql
from psycopg2.sql import SQL

DB_NAME = "userManagement"
DB_USER = "postgres"
DB_PASSWORD = "0"
DB_HOST = "postgres"
DB_NAME = os.environ.get("DATABASE_NAME")
DB_USER = os.environ.get("DATABASE_USER")
DB_PASSWORD = os.environ.get("DATABASE_PASSWORD")
DB_HOST = os.environ.get("DATABASE_HOST")
#DB_HOST = "127.0.0.1"
DB_PORT = "5432"
DB_PORT = os.environ.get("DATABASE_PORT")


def connectPG():
Expand Down
15 changes: 8 additions & 7 deletions flask_app/user.py → wannadb_web/routing/user.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# main_routes.py
from flask import Blueprint, request, make_response

from config import Token, tokenEncode, tokenDecode
from postgres.queries import (checkPassword, getMembersOfOrganisation,
getOrganisationIDsFromUserId, getOrganisationName, getOrganisationFromUserId)
from postgres.transactions import addUser, addOrganisation, addUserToOrganisation, addUserToOrganisation2, deleteUser, leaveOrganisation
from wannadb_web.util import Token, tokenEncode, tokenDecode
from wannadb_web.postgres.queries import checkPassword, getOrganisationIDsFromUserId
from wannadb_web.postgres.transactions import (addUser, addOrganisation, addUserToOrganisation2, deleteUser,
leaveOrganisation)

user_management = Blueprint('user_management', __name__)

Expand All @@ -23,9 +23,10 @@ def register():

return make_response({'message': 'User registered successfully',
'token': token}, 201)
if _id < 0:
elif _id < 0:
return make_response({'message': 'Conflicting username'}, 409)
return make_response({'message': 'User register failed'}, 422)
else:
return make_response({'message': 'User register failed'}, 422)


@user_management.route('/login', methods=['POST'])
Expand Down Expand Up @@ -174,5 +175,5 @@ def get_organisation_members(_id):
members = []
for member in members_raw:
members.append(member[0])

return make_response({"members": members}, 200)
4 changes: 4 additions & 0 deletions config.py → wannadb_web/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import datetime
import json
import logging
from enum import Enum
from typing import Any

import jwt

logger: logging.Logger = logging.getLogger(__name__)


class Authorisation(Enum):
Owner = 0
Expand Down
Loading

0 comments on commit c7e9ea6

Please sign in to comment.