-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
245 lines (214 loc) · 7.56 KB
/
Copy pathtasks.py
File metadata and controls
245 lines (214 loc) · 7.56 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
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
import logging
import json
from uuid import uuid4
from datetime import datetime
from celery import Celery
from celery.schedules import crontab
import geoip2.database
import user_agents
from decimal import Decimal
from db import get_connection, redis_client,safe_close
from consts import REDIS_HOST, REDIS_PORT
from fraud import is_fraud, check_velocity, check_behavior
from metrics import (
UNIQUE_VISITORS,
TOP_REFERRERS,
CLICKS_BY_COUNTRY,
CLICKS_BY_DEVICE,
CLICKS_BY_BROWSER,
CLICKS_BY_HOUR,
SUSPICIOUS_CLICKS,
SUSPICIOUS_IP_URLS,
SUSPICIOUS_IPS,
SUSPICIOUS_REQUESTS
)
from analytics import increment_hourly_analytics, update_user_sequence,update_url_referres
class DecimalEncoder(json.JSONEncoder):
def default(self, obj): # type: ignore
if isinstance(obj, Decimal):
return float(obj) # or str(obj) if you prefer exact
return super(DecimalEncoder, self).default(obj)
# ---------- Logging ----------
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ---------- Celery ----------
celery = Celery("tasks", broker=f"redis://{REDIS_HOST}:{REDIS_PORT}/0")
celery.conf.update(
beat_schedule={
"update-trending-scores-every-1-minute": {
"task": "tasks.update_trending_urls",
"schedule": crontab(minute="*/1"),
}
},
timezone="UTC",
)
# ---------- GeoIP ----------
try:
GEOIP_READER = geoip2.database.Reader("data/GeoLite2-City.mmdb")
except FileNotFoundError:
GEOIP_READER = None
logger.warning("⚠️ GeoLite2-City.mmdb not found. Falling back to 'unknown' country")
def parse_user_agent(ua_string: str):
ua = user_agents.parse(ua_string)
if ua.is_mobile:
device = "mobile"
elif ua.is_tablet:
device = "tablet"
elif ua.is_pc:
device = "pc"
else:
device = "other"
browser = ua.browser.family.lower() or "unknown"
return device, browser
def get_country_from_ip(ip: str) -> str:
if not GEOIP_READER:
return "unknown"
try:
response = GEOIP_READER.city(ip)
return response.country.iso_code.lower() if response.country.iso_code else "unknown"
except Exception:
return "unknown"
# ---------------- Celery Tasks ----------------
@celery.task(bind=True, max_retries=3, default_retry_delay=5)
def log_click(self, url_id: str, ip: str, user_agent: str, referrer: str, fingerprint: str):
"""
Log a click event:
- Stores click in DB
- Updates Prometheus metrics
- Updates analytics
"""
conn = None
cursor = None
try:
country = get_country_from_ip(ip)
device, browser = parse_user_agent(user_agent or "")
click_id = str(uuid4())
now = datetime.utcnow()
date_str = now.strftime("%Y-%m-%d")
hour = now.hour
# --- DB ---
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute(
"""
INSERT INTO url_clicks (id, url_id, fingerprint, ip, user_agent, referrer, country_code, device_type, browser)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(click_id, url_id, fingerprint, ip, user_agent, referrer,
country if country != "unknown" else None, device, browser)
)
cursor.execute(
"UPDATE urls SET clicks = clicks + 1 WHERE id=%s",
(url_id,)
)
conn.commit()
# --- Metrics ---
UNIQUE_VISITORS.labels(url=url_id, date=date_str).inc()
if referrer:
TOP_REFERRERS.labels(url=url_id, referrer=referrer).inc()
CLICKS_BY_COUNTRY.labels(url=url_id, country=country).inc()
CLICKS_BY_DEVICE.labels(url=url_id, device=device).inc()
CLICKS_BY_BROWSER.labels(url=url_id, browser=browser).inc()
CLICKS_BY_HOUR.labels(url=url_id).observe(hour)
# --- Analytics ---
increment_hourly_analytics(url_id, fingerprint, suspicious=False)
update_user_sequence(fingerprint, url_id)
update_url_referres(url_id=url_id,referrer=referrer)
except Exception as e:
logger.error(f"Error logging click: {e}", exc_info=True)
raise self.retry(exc=e)
finally:
if cursor:
cursor.close()
if conn:
conn.close()
@celery.task(bind=True, max_retries=3, default_retry_delay=5)
def check_fraud(self, ip: str, url_id: str, user_agent: str, referrer: str, fingerprint: str):
"""
Detect suspicious activity:
- Checks heuristic fraud rules
- Stores suspicious clicks
- Updates analytics & metrics
"""
suspicious = (
is_fraud(ip, url_id, user_agent, referrer) or
check_velocity(fingerprint) or
check_behavior(fingerprint, url_id)
)
if suspicious:
logger.warning(f"🚨 Suspicious click: ip={ip}, url={url_id}, fingerprint={fingerprint}")
# --- Prometheus Metrics ---
SUSPICIOUS_REQUESTS.labels(type="task_detected").inc()
SUSPICIOUS_CLICKS.labels(url=url_id, type="heuristic_detected").inc()
SUSPICIOUS_IPS.inc()
SUSPICIOUS_IP_URLS.inc()
# --- Store in DB ---
conn = None
cursor = None
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO suspicious_clicks
(id, fingerprint, ip, user_agent, referrer, reason, url_code)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""",
(str(uuid4()), fingerprint, ip, user_agent, referrer, "heuristic_detected", url_id)
)
conn.commit()
# Analytics for suspicious click
increment_hourly_analytics(url_id, fingerprint, suspicious=True)
except Exception as e:
logger.error(f"Error logging suspicious click: {e}", exc_info=True)
raise self.retry(exc=e)
finally:
if cursor:
cursor.close()
if conn:
safe_close(conn)
return suspicious
@celery.task
def update_trending_urls(top_n: int = 20):
"""
Updates trending URLs based on last 4 hours of hourly analytics
Stores result in MySQL and Redis
"""
conn = None
cursor = None
try:
conn = get_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute("""
SELECT url_id,
SUM(
clicks * CASE
WHEN TIMESTAMPDIFF(HOUR, date_hour, NOW()) < 1 THEN 1
WHEN TIMESTAMPDIFF(HOUR, date_hour, NOW()) < 2 THEN 0.5
WHEN TIMESTAMPDIFF(HOUR, date_hour, NOW()) < 4 THEN 0.25
ELSE 0
END
) AS trending_score
FROM url_analytics_hourly
WHERE date_hour >= NOW() - INTERVAL 4 HOUR
GROUP BY url_id
ORDER BY trending_score DESC
LIMIT %s
""", (top_n,))
trending = cursor.fetchall()
for row in trending:
cursor.execute(
"UPDATE urls SET trending_score=%s WHERE id=%s",
(row["trending_score"], row["url_id"])
)
conn.commit()
# Store in Redis
redis_client.set("trending_urls", json.dumps(trending, cls=DecimalEncoder))
logger.info(f"✅ Updated trending URLs ({len(trending)} rows)")
except Exception as e:
logger.error(f"Error updating trending URLs: {e}", exc_info=True)
finally:
if cursor:
cursor.close()
if conn:
safe_close(conn)