-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaster.py
More file actions
129 lines (104 loc) · 4.2 KB
/
Master.py
File metadata and controls
129 lines (104 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask, render_template, request
import zmq
import time
app = Flask(__name__)
# ZeroMQ setup
context = zmq.Context()
socket = context.socket(zmq.REQ)
# Hubungkan ke setiap worker melalui alamat IP dan port
worker_addresses = [
# "tcp://192.168.0.124:5555", # Worker 1 dengan 2000 database
# "tcp://192.168.56.1:5555", # Worker 2 dengan 1000 database
# "tcp://192.168.0.115:5555", # Worker 3 dengan 1000 database
]
for address in worker_addresses:
socket.connect(address)
def send_query_to_workers(query):
results = []
for address in worker_addresses:
try:
socket.send_json({"query": query})
response = socket.recv_json()
results.extend(response.get("results", []))
except zmq.ZMQError as e:
print(f"Error connecting to worker at {address}: {e}")
return results
def fetch_document_content(document_id):
for address in worker_addresses:
try:
socket.send_json({"fetch": document_id})
response = socket.recv_json()
if response.get("content"):
return response.get("content")
except zmq.ZMQError as e:
print(f"Error fetching content from worker at {address}: {e}")
return "Content not found."
@app.route('/')
def index():
return render_template('index.html')
# @app.route('/search', methods=['POST'])
# def search():
# query = request.form.get('query')
# if not query:
# return render_template('index.html', results=[], error="Please enter a query")
# # Kirim query ke workers dan dapatkan hasil
# results = send_query_to_workers(query)
# sorted_results = sorted(results, key=lambda x: x["score"], reverse=True)
# return render_template('index.html', results=sorted_results, query=query)
@app.route('/search', methods=['GET', 'POST'])
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'POST':
# Proses pencarian baru
query = request.form.get('query')
page = 1 # Default halaman pertama
else: # request.method == 'GET'
# Navigasi melalui pagination
query = request.args.get('query')
page = int(request.args.get('page', 1)) # Halaman saat ini
if not query:
return render_template('index.html', results=[], error="Please enter a query")
per_page = 7 # Jumlah hasil per halaman
# Mulai pengukuran waktu
start_time = time.time()
# Kirim query ke workers dan dapatkan hasil
results = send_query_to_workers(query)
sorted_results = sorted(results, key=lambda x: x["score"], reverse=True)
# Akhir pengukuran waktu
end_time = time.time()
search_duration = end_time - start_time # Hitung durasi pencarian
# Pagination logic
total_results = len(sorted_results)
total_pages = (total_results + per_page - 1) // per_page # Hitung total halaman
start_index = (page - 1) * per_page
end_index = start_index + per_page
paginated_results = sorted_results[start_index:end_index]
# Rentang halaman untuk ditampilkan (batas 7 halaman)
max_pages_to_display = 7
start_page = max(1, page - 3) # Mulai 3 halaman sebelumnya
end_page = min(total_pages, start_page + max_pages_to_display - 1) # Hingga batas 7 halaman
if end_page - start_page < max_pages_to_display - 1:
start_page = max(1, end_page - max_pages_to_display + 1)
return render_template(
'index.html',
results=paginated_results,
query=query,
page=page,
total_pages=total_pages,
start_page=start_page,
end_page=end_page,
search_duration=search_duration # Kirim durasi pencarian ke template
)
@app.route('/document/<int:document_id>')
def document(document_id):
# Kirim permintaan ke worker untuk mendapatkan detail dokumen
socket.send_json({"document_id": document_id})
response = socket.recv_json()
# Ambil data dokumen
document = response.get("document")
if not document:
return f"Document with ID {document_id} not found.", 404
# Kirim data dokumen ke template
return render_template('document.html', document=document)
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=5000)