This commit is contained in:
ahmed531998 2023-07-24 10:53:10 +02:00
parent dd24d723bd
commit 21a1a7a0b8
1 changed files with 60 additions and 41 deletions

101
main.py
View File

@ -32,68 +32,87 @@ cors = CORS(app, resources={r"/api/predict": {"origins": url},
r"/health": {"origins": "*"} r"/health": {"origins": "*"}
}) })
users = {} users = {}
alive = "alive"
def vre_fetch(): def vre_fetch():
while True: while True:
time.sleep(1000) try:
print('getting new material') time.sleep(1000)
#users[token]['args']['vre'].get_vre_update() print('getting new material')
#users[token]['args']['vre'].index_periodic_update() #users[token]['args']['vre'].get_vre_update()
#users[token]['args']['rg'].update_index(vre.get_index()) #users[token]['args']['vre'].index_periodic_update()
#users[token]['args']['rg'].update_db(vre.get_db()) #users[token]['args']['rg'].update_index(vre.get_index())
vre.get_vre_update() #users[token]['args']['rg'].update_db(vre.get_db())
vre.index_periodic_update() vre.get_vre_update()
rg.update_index(vre.get_index()) vre.index_periodic_update()
rg.update_db(vre.get_db()) rg.update_index(vre.get_index())
rg.update_db(vre.get_db())
except Exception as e:
alive = "dead_vre_fetch"
def user_interest_decay(token): def user_interest_decay(token):
while True: while True:
if token in users: try:
print("decaying interests after 3 minutes for " + users[token]['username']) if token in users:
time.sleep(180) print("decaying interests after 3 minutes for " + users[token]['username'])
users[token]['user'].decay_interests() time.sleep(180)
else: users[token]['user'].decay_interests()
break else:
break
except Exception as e:
alive = "dead_interest_decay"
def clear_inactive(): def clear_inactive():
while True: while True:
time.sleep(1) try:
for username in users: time.sleep(1)
if users[username]['activity'] > 3600: for username in users:
del users[username] if users[username]['activity'] > 3600:
users[username]['activity'] += 1 del users[username]
users[username]['activity'] += 1
except Exception as e:
alive = "dead_clear_inactive"
@app.route("/health", methods=['GET']) @app.route("/health", methods=['GET'])
def health(): def health():
return "Success", 200 if alive=="alive":
return "Success", 200
else:
return alive, 500
@app.route("/api/dm", methods=['POST']) @app.route("/api/dm", methods=['POST'])
def init_dm(): def init_dm():
token = request.get_json().get("token") try:
status = request.get_json().get("stat") token = request.get_json().get("token")
if status == "start": status = request.get_json().get("stat")
message = {"stat": "waiting"} if status == "start":
elif status == "set": message = {"stat": "waiting"}
headers = {"gcube-token": token, "Accept": "application/json"} elif status == "set":
if token not in users: headers = {"gcube-token": token, "Accept": "application/json"}
url = 'https://api.d4science.org/rest/2/people/profile' if token not in users:
response = requests.get(url, headers=headers) url = 'https://api.d4science.org/rest/2/people/profile'
if response.status_code == 200: response = requests.get(url, headers=headers)
username = response.json()['result']['username'] if response.status_code == 200:
name = response.json()['result']['fullname'] username = response.json()['result']['username']
name = response.json()['result']['fullname']
users[token] = {'username': username, 'name': name, 'dm': DM(), 'activity': 0, 'user': User(username, token)} users[token] = {'username': username, 'name': name, 'dm': DM(), 'activity': 0, 'user': User(username, token)}
threading.Thread(target=user_interest_decay, args=(token,), name='decayinterest_'+users[token]['username']).start() threading.Thread(target=user_interest_decay, args=(token,), name='decayinterest_'+users[token]['username']).start()
message = {"stat": "done"} message = {"stat": "done"}
else:
message = {"stat": "rejected"}
else: else:
message = {"stat": "rejected"} message = {"stat": "done"}
else: return message
message = {"stat": "done"} except Exception as e:
return message message = {"stat": "init_dm_error"}
return message
@app.route("/api/predict", methods=['POST']) @app.route("/api/predict", methods=['POST'])
def predict(): def predict():