Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split web socket #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Controllers/EvaluatorController.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from uuid import uuid4

import request
# from flask import json

# issue 12 https://github.com/quicksloth/source-code-recommendation-server/issues/11
Expand All @@ -10,7 +11,6 @@
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

import server
from Modules.NlpModule import NlpModule
from Modules.LowCouplingModule import LowCouplingModule
from Modules.UnderstandingModule import UnderstandingModule
Expand All @@ -37,7 +37,7 @@ def __init__(self, complex_network=None):
self.complex_network = complex_network
self.nlp_module = NlpModule(weight=5, complex_network=complex_network)
self.low_coupling_module = LowCouplingModule(weight=5)
self.understanding_module = UnderstandingModule(weight=1)
self.understanding_module = UnderstandingModule(weight=5)

@staticmethod
def get_recommendation_code(request_id, query, libs, comments, language):
Expand All @@ -50,7 +50,7 @@ def get_recommendation_code(request_id, query, libs, comments, language):
data = request_code.toRequestJSON()
print(data)
RequestDB().add(request_code)
server.get_source_codes(data=data)
request.get_source_codes(data=data)

@staticmethod
def init_get_recommendation_code_with_mocked_data():
Expand All @@ -65,9 +65,9 @@ def init_get_recommendation_code_with_mocked_data():
data = request_code.toRequestJSON()
print(data)
RequestDB().add(request_code)
server.get_source_codes(data=data)
request.get_source_codes(data=data)

def evaluate_search_codes(self, request):
def evaluate_search_codes(self, request, emitFn):
max_score = 0

request_json = request.get_json()
Expand All @@ -93,7 +93,7 @@ def evaluate_search_codes(self, request):

code_results = code_results.toJSON()
print(code_results)
server.emit_code_recommendations(request_id, code_results)
emitFn(request_id, code_results)
return code_results

def evaluate_codes(self, code, idx, idy, input_bus):
Expand All @@ -105,9 +105,9 @@ def evaluate_codes(self, code, idx, idy, input_bus):
nlp_score = self.nlp_module.evaluate_code(input_bus_vo=input_bus, search_result_id=idx,
code_id=idy)

print('low_coupling_score', low_coupling_score / self.low_coupling_module.weight)
print('understanding_score', understanding_score / self.understanding_module.weight)
print('nlp_score', nlp_score / self.nlp_module.weight)
# print('low_coupling_score', low_coupling_score / self.low_coupling_module.weight)
# print('understanding_score', understanding_score / self.understanding_module.weight)
# print('nlp_score', nlp_score / self.nlp_module.weight)
sum_weight = (self.low_coupling_module.weight + self.understanding_module.weight + self.nlp_module.weight)
print('----------')
final_score = (low_coupling_score + understanding_score + nlp_score) / sum_weight
Expand Down
3 changes: 3 additions & 0 deletions src/FileExamples/ExampleSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __save_file(self, filename=None):

def __reading_file(self):
# Return a file with list of lines
# Open the file for reading.
with open('my_file.txt', 'r') as infile:
data = infile.read() # Read the contents of the file into memory.



Expand Down
2 changes: 1 addition & 1 deletion src/Models/DTO/Client/CodeDTO.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import requests
import request
from flask import json


Expand Down
Empty file added src/__init__.py
Empty file.
5 changes: 4 additions & 1 deletion src/fake-crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def hello_world():
# TODO: temporary until all set client and crawler
@app.route('/crawl', methods=['GET'])
def crawl():
print('REQUEST AQUI')
request_body = request.get_json()
print('request_body', request_body)
url = 'http://0.0.0.0:10443/source-codes'
data = {
"requestID": request_body.get('requestID'),
Expand Down Expand Up @@ -237,8 +239,9 @@ def crawl():
# "Days of the Week\nMonday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n"]}]}
headers = {'Content-Type': 'application/json'}

print("POST")
requests.post(url=url, data=json.dumps(data), headers=headers)

print("POST")
return json.dumps({'success': True})


Expand Down
9 changes: 9 additions & 0 deletions src/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import requests


def get_source_codes(data):
url = 'http://0.0.0.0:1111/crawl'
headers = {'Content-Type': 'application/json'}
print('going to request new version')
requests.request(url=url, method='GET', data=data, headers=headers)
print('REQUEST')
62 changes: 5 additions & 57 deletions src/server.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
import flask
import time
from flask import Flask, request, json
from flask_socketio import SocketIO
import requests
from logging.config import fileConfig
import logging
# from logging.config import fileConfig
# import logging

from Controllers.EvaluatorController import EvaluatorController

# fileConfig('logging.conf')
# log = logging.getLogger(__name__)
from Modules.Concepts.ComplexNetwork import ComplexNetwork
import webSocket

app = Flask(__name__, static_folder='')
app.config['SECRET_KEY'] = '@server-secret'
# socketio = SocketIO(app, allow_upgrades=True, engineio_logger=log, logger=log)

socketio = SocketIO(app, allow_upgrades=True)
complex_network = ComplexNetwork()
evaluator_controller = EvaluatorController(complex_network=complex_network)


class Socket:
"""
Class used to emit answer to specific client
"""

def __init__(self, sid):
self.sid = sid
self.connected = True

# Emits data to a socket's unique room
def emit(self, event, data):
print('going to emit to', self.sid)
socketio.emit(event, data, room=self.sid, namespace='/code-recommendations')


@app.route('/')
def index():
return 'Hello, World new version!'
Expand All @@ -44,19 +25,12 @@ def index():
@app.route('/source-codes', methods=['POST'])
def source_codes():
start = time.time()
evaluator_controller.evaluate_search_codes(request)
evaluator_controller.evaluate_search_codes(request, webSocket.emit_code_recommendations)
end = time.time()
print('Receive Source code and evaluate took', (end - start), 'seconds')
return json.dumps({'success': True})


def get_source_codes(data):
url = 'http://0.0.0.0:1111/crawl'
headers = {'Content-Type': 'application/json'}
print('going to request new version')
requests.request(url=url, method='GET', data=data, headers=headers)


@app.route('/train-network', methods=['POST'])
def train_network():
print('Train Start')
Expand All @@ -81,31 +55,5 @@ def get_complex_network_cluster():
return resp


# TODO: maybe use on connect
@socketio.on('connect')
def connect():
print('connectttsssss')


@socketio.on('getCodes', namespace='/code-recommendations')
def get_recommendation_codes(data):
data = json.loads(data)
print(data)
evaluator_controller.get_recommendation_code(request_id=request.sid,
language=data['language'],
query=data['query'],
comments=data['comments'],
libs=data['libs'])


def emit_code_recommendations(request_id, data):
Socket(request_id).emit('recommendationCodes', data)


def run_server():
socketio.run(app, host='0.0.0.0', port=10443, threaded=True)


if __name__ == "__main__":
socketio.start_background_task(run_server)
# socketio.run(app, host='0.0.0.0', port=10443, threaded=True)
app.run(host='0.0.0.0', port=10443)
4 changes: 2 additions & 2 deletions src/testTrainServer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import requests
import request
from flask import json


Expand All @@ -11,7 +11,7 @@ def train_database():
'Python', 'sites stackoverflow lore lorem', 'lorem ipsumb amdksd'],
}
print('going to request')
requests.post(url=url, data=json.dumps(data), headers=headers)
request.post(url=url, data=json.dumps(data), headers=headers)


train_database()
59 changes: 59 additions & 0 deletions src/webSocket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from flask import Flask, request, json
from flask_socketio import SocketIO
from logging.config import fileConfig
import logging

app = Flask(__name__, static_folder='')
app.config['SECRET_KEY'] = '@server-secret'
socketio = SocketIO(app)

# socketio = SocketIO(app, allow_upgrades=True, engineio_logger=log, logger=log)

# fileConfig('logging.conf')
# log = logging.getLogger(__name__)


from Controllers.EvaluatorController import EvaluatorController
from Modules.Concepts.ComplexNetwork import ComplexNetwork

complex_network = ComplexNetwork()
evaluator_controller = EvaluatorController(complex_network=complex_network)


class Socket:
"""
Class used to emit answer to specific client
"""

def __init__(self, sid):
self.sid = sid
self.connected = True

# Emits data to a socket's unique room
def emit(self, event, data):
print('going to emit to', self.sid)
socketio.emit(event, data, room=self.sid, namespace='/code-recommendations')


@socketio.on('connect')
def connect():
print('CONECTADO A UM CLIENTE ', request.sid)


@socketio.on('getCodes', namespace='/code-recommendations')
def get_recommendation_codes(data):
data = json.loads(data)
print(data)
evaluator_controller.get_recommendation_code(request_id=request.sid,
language=data['language'],
query=data['query'],
comments=data['comments'],
libs=data['libs'])


def emit_code_recommendations(request_id, data):
Socket(request_id).emit('recommendationCodes', data)


if __name__ == "__main__":
socketio.run(app, host='0.0.0.0', port=10442, threaded=True)