-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground_processing.py
444 lines (369 loc) · 16.5 KB
/
background_processing.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
from pymongo import MongoClient
from datetime import datetime, timedelta
import yaml
import uuid
import requests
import logging
import random
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize MongoDB handler
class MongoDBHandler:
def __init__(self, connection_string="mongodb://localhost:27017/", max_retries=3):
self.connection_string = connection_string
self.max_retries = max_retries
self.connect()
def connect(self):
for attempt in range(self.max_retries):
try:
self.client = MongoClient(self.connection_string)
self.client.server_info() # Test connection
self.db = self.client['wikidata_verification']
self.html_collection = self.db['html_content']
self.entailment_collection = self.db['entailment_results']
self.stats_collection = self.db['parser_stats']
self.status_collection = self.db['status']
print("Successfully connected to MongoDB")
return
except Exception as e:
print(f"MongoDB connection attempt {attempt + 1} failed: {e}")
if attempt == self.max_retries - 1:
raise
time.sleep(5) # Wait before retry
def ensure_connection(self):
"""Ensure MongoDB connection is alive, reconnect if needed"""
try:
self.client.server_info()
except:
print("MongoDB connection lost, attempting to reconnect...")
self.connect()
def save_html_content(self, html_df):
"""Save HTML content data with task_id"""
try:
if html_df.empty:
print("Warning: html_df is empty")
return
print(f"Attempting to save {len(html_df)} HTML records")
records = html_df.to_dict('records')
for record in records:
try:
if 'reference_id' not in record:
print(f"Warning: record missing reference_id: {record}")
continue
# Convert pandas Timestamp to datetime
if 'fetch_timestamp' in record and isinstance(record['fetch_timestamp'], pd.Timestamp):
record['fetch_timestamp'] = record['fetch_timestamp'].to_pydatetime()
# Add save timestamp
record['save_timestamp'] = datetime.now()
result = self.html_collection.update_one(
{
'reference_id': record['reference_id'],
'task_id': record['task_id']
},
{'$set': record},
upsert=True
)
print(f"Updated HTML document with reference_id {record['reference_id']}: "
f"matched={result.matched_count}, modified={result.modified_count}, "
f"upserted_id={result.upserted_id}")
except Exception as e:
print(f"Error saving HTML record: {record}")
print(f"Error details: {e}")
except Exception as e:
print(f"Error in save_html_content: {e}")
raise
def save_entailment_results(self, entailment_df):
"""Save entailment results with task_id"""
try:
if entailment_df.empty:
print("Warning: entailment_df is empty")
return
print(f"Attempting to save {len(entailment_df)} entailment records")
records = entailment_df.to_dict('records')
for record in records:
try:
# Convert timestamp string to datetime object
if 'processed_timestamp' in record:
record['processed_timestamp'] = datetime.strptime(
record['processed_timestamp'],
'%Y-%m-%dT%H:%M:%S.%f'
)
# Add save timestamp
record['save_timestamp'] = datetime.now()
# Insert new document without checking for duplicates
result = self.entailment_collection.insert_one(record)
print(f"Inserted new entailment document with reference_id {record['reference_id']}: "
f"inserted_id={result.inserted_id}")
except Exception as e:
print(f"Error saving entailment record: {record}")
print(f"Error details: {e}")
except Exception as e:
print(f"Error in save_entailment_results: {e}")
raise
def save_parser_stats(self, stats_dict):
"""Save parser statistics with task_id"""
try:
# Convert Pandas Timestamp to datetime
if isinstance(stats_dict.get('parsing_start_timestamp'), pd.Timestamp):
stats_dict['parsing_start_timestamp'] = stats_dict['parsing_start_timestamp'].to_pydatetime()
# Add save timestamp
stats_dict['save_timestamp'] = datetime.now()
result = self.stats_collection.update_one(
{
'entity_id': stats_dict['entity_id'],
'task_id': stats_dict['task_id']
},
{'$set': stats_dict},
upsert=True
)
print(f"Updated parser stats for entity {stats_dict['entity_id']}")
except Exception as e:
print(f"Error in save_parser_stats: {e}")
raise
def save_status(self, status_dict):
"""Save task status information to MongoDB"""
try:
# List of timestamp fields to process
timestamp_fields = [
'requested_timestamp',
'processing_start_timestamp',
'completed_timestamp'
]
# Convert string timestamps to datetime objects
for field in timestamp_fields:
if status_dict.get(field) and status_dict[field] != 'null':
if isinstance(status_dict[field], str):
status_dict[field] = datetime.strptime(
status_dict[field].rstrip('Z'),
'%Y-%m-%dT%H:%M:%S.%f'
)
# Add last update timestamp
status_dict['last_updated'] = datetime.now()
# Find existing document by task_id and qid
existing_doc = self.status_collection.find_one({
'task_id': status_dict['task_id'],
'qid': status_dict['qid']
})
if existing_doc:
# Update existing document
result = self.status_collection.update_one(
{
'task_id': status_dict['task_id'],
'qid': status_dict['qid']
},
{'$set': status_dict}
)
print(f"Updated status for task {status_dict['task_id']}: "
f"matched={result.matched_count}, modified={result.modified_count}")
else:
# Insert new document
result = self.status_collection.insert_one(status_dict)
print(f"Created new status for task {status_dict['task_id']}: "
f"inserted_id={result.inserted_id}")
except Exception as e:
print(f"Error in save_status: {e}")
raise
def reset_database(self):
"""Reset all collections in the database"""
try:
# Drop all collections
self.html_collection.drop()
self.entailment_collection.drop()
self.stats_collection.drop()
self.status_collection.drop()
print("All collections have been reset successfully")
except Exception as e:
print(f"Error resetting database: {e}")
raise
def get_next_user_request(self):
"""
Get the next pending user request from status collection
Returns a dictionary with request information or None if no requests found
"""
try:
# Find the oldest user request that hasn't been processed
pending_request = self.status_collection.find_one(
{
'status': 'in queue'
},
sort=[('requested_timestamp', 1)] # Get oldest request first
)
if pending_request:
# Update status to processing and add processing start timestamp
status_dict = {
'qid': pending_request['qid'],
'task_id': pending_request['task_id'],
'status': 'processing',
'algo_version': pending_request.get('algo_version', '1.0'),
'request_type': pending_request['request_type'],
'requested_timestamp': pending_request['requested_timestamp'],
'processing_start_timestamp': datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z',
'completed_timestamp': 'null'
}
# Update the document in MongoDB
self.save_status(status_dict)
return status_dict
return None
except Exception as e:
print(f"Error getting next user request: {e}")
return None
# Load config
def load_config(config_path: str):
with open(config_path, 'r') as file:
return yaml.safe_load(file)
config = load_config('config.yaml')
algo_version = config['version']['algo_version']
mongo_handler = MongoDBHandler()
def requestItemProcessing(qid: str, request_type: str) -> str:
"""
Request processing for a specific QID
Args:
qid: Wikidata QID
request_type: Type of processing request
Returns:
str: Status message
"""
try:
# Check if item is already in queue
existing_request = mongo_handler.status_collection.find_one({
'qid': qid,
'status': 'in queue'
})
if existing_request:
return f"QID {qid} is already in queue. Skipping..."
# Create new status document
status_dict = {
'qid': qid,
'task_id': str(uuid.uuid4()),
'status': 'in queue',
'algo_version': algo_version,
'request_type': request_type,
'requested_timestamp': datetime.utcnow(),
'processing_start_timestamp': None,
'completed_timestamp': None
}
# Insert into MongoDB
mongo_handler.status_collection.insert_one(status_dict)
return f"Successfully queued QID {qid} for processing"
except Exception as e:
logger.error(f"Error processing request for QID {qid}: {e}")
return f"Error processing request for QID {qid}: {str(e)}"
def fetch_qid_by_label(label):
"""
Fetch QID for a given label using SPARQL.
"""
url = "https://query.wikidata.org/sparql"
query = f"""
SELECT ?item WHERE {{
?item rdfs:label "{label}"@en.
}}
"""
headers = {
"User-Agent": "ProVe/1.1.0 ([email protected])",
"Accept": "application/sparql-results+json"
}
response = requests.get(url, params={"query": query}, headers=headers)
if response.status_code == 200:
data = response.json()
results = data.get("results", {}).get("bindings", [])
if results:
return results[0]["item"]["value"].split("/")[-1] # Extract QID from URI
return None # No QID found
else:
print(f"Error {response.status_code}: {response.text}")
return None
def fetch_top_pageviews_and_qid(project, access, year, month, day, limit=10):
"""
Fetches the top viewed pages and their QIDs.
Args:
project: The Wikipedia project (e.g., "en.wikipedia").
access: The access method (e.g., "all-access").
year: The year of the data.
month: The month of the data.
day: The day of the data.
limit: The maximum number of articles to return.
Returns:
List of tuples containing (title, views, QID).
"""
url = f"https://wikimedia.org/api/rest_v1/metrics/pageviews/top/{project}/{access}/{year}/{month}/{day}"
headers = {
"User-Agent": "ProVe/1.1.0 ([email protected])"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
articles = data.get('items', [])[0].get('articles', [])
top_articles = []
for article in articles: # Iterate through all articles
title = article['article'].replace("_", " ") # Replace underscores with spaces
views = article['views']
qid = fetch_qid_by_label(title) # Use the correct function to fetch QID
# Exclude specific titles
if title in ["Main Page", "Special:Search"]:
print(f"Excluding title: {title}")
continue
# Debugging output
if qid is None:
print(f"QID not found for title: {title}")
top_articles.append((title, views, qid))
# Stop if we have reached the desired limit
if len(top_articles) >= limit:
break
return top_articles
else:
print(f"Error {response.status_code}: {response.text}")
return None
def process_top_viewed_items(project="en.wikipedia", access="all-access", limit=5):
"""
Process the top viewed items from yesterday and queue them for processing.
Args:
project: The Wikipedia project (e.g., "en.wikipedia").
access: The access method (e.g., "all-access").
limit: The maximum number of articles to return.
"""
# Get yesterday's date
yesterday = datetime.utcnow() - timedelta(days=1)
year = yesterday.strftime("%Y")
month = yesterday.strftime("%m")
day = yesterday.strftime("%d")
# Fetch top viewed items
top_items = fetch_top_pageviews_and_qid(project, access, year, month, day, limit)
if top_items:
print("\nTop viewed items from yesterday:")
for idx, (title, views, qid) in enumerate(top_items, 1):
print(f"{idx}. Title: {title} - {views} views (QID: {qid})")
# Queue each item for processing
if qid: # Only queue if QID is found
result = requestItemProcessing(qid, 'top_viewed')
print(f" Queue status: {result}")
else:
print("No articles found.")
def process_pagepile_list(file_path='utils/pagepileList.txt'):
"""
Process the QIDs from the pagepile list file and queue them for processing.
Args:
file_path: The path to the pagepile list file.
"""
try:
with open(file_path, 'r') as file:
qids = file.read().splitlines()
for qid in qids:
if qid: # Ensure the QID is not empty
result = requestItemProcessing(qid, 'pagepile_weekly_update')
print(f"Queued QID {qid} for processing: {result}")
except Exception as e:
logger.error(f"Error processing pagepile list: {e}")
def process_random_qid():
"""
Generate a random QID and queue it for processing.
"""
random_number = random.randint(0, 129999999) # Generate a random number less than 130,000,000
random_qid = f"Q{random_number}" # Create QID by prefixing 'Q'
# Queue the random QID for processing
result = requestItemProcessing(random_qid, 'Random_processing')
print(f"Queued random QID {random_qid} for processing: {result}")
if __name__ == "__main__":
# Uncomment the desired function to run
# process_top_viewed_items(limit=300) # Process top viewed items
process_pagepile_list() # Process QIDs from pagepile list
#process_random_qid() # Process a random QID