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