fix: 24 and 72 hour counts on reports

This commit is contained in:
Iain Learmonth 2023-10-31 17:37:05 +00:00
parent ba1b597c73
commit d754f0b2bc
2 changed files with 75 additions and 111 deletions

View file

@ -12,38 +12,40 @@ from app.models.mirrors import Proxy, Origin, Country
report = Blueprint("report", __name__)
def countries_report():
def generate_subqueries():
DeprecationAlias = aliased(Deprecation)
now = datetime.utcnow()
hours_24_ago = now - timedelta(hours=24)
hours_72_ago = now - timedelta(hours=72)
deprecations_24hr_subquery = (
db.session.query(
Proxy.origin_id,
func.count(Deprecation.id).label('deprecations_24hr')
DeprecationAlias.resource_id,
func.count(DeprecationAlias.resource_id).label('deprecations_24hr')
)
.join(Deprecation, Proxy.id == Deprecation.resource_id)
.filter(
Deprecation.deprecated_at >= hours_24_ago,
Deprecation.resource_type == "Proxy"
DeprecationAlias.reason.like('block_%'),
DeprecationAlias.deprecated_at >= now - timedelta(hours=24),
DeprecationAlias.resource_type == 'Proxy'
)
.group_by(Proxy.origin_id)
.group_by(DeprecationAlias.resource_id)
.subquery()
)
deprecations_72hr_subquery = (
db.session.query(
Proxy.origin_id,
func.count(Deprecation.id).label('deprecations_72hr')
DeprecationAlias.resource_id,
func.count(DeprecationAlias.resource_id).label('deprecations_72hr')
)
.join(Deprecation, Proxy.id == Deprecation.resource_id)
.filter(
Deprecation.deprecated_at >= hours_72_ago,
Deprecation.resource_type == "Proxy"
DeprecationAlias.reason.like('block_%'),
DeprecationAlias.deprecated_at >= now - timedelta(hours=72),
DeprecationAlias.resource_type == 'Proxy'
)
.group_by(Proxy.origin_id)
.group_by(DeprecationAlias.resource_id)
.subquery()
)
return deprecations_24hr_subquery, deprecations_72hr_subquery
def countries_report():
deprecations_24hr_subquery, deprecations_72hr_subquery = generate_subqueries()
return (
db.session.query(
@ -53,13 +55,32 @@ def countries_report():
)
.join(Origin, Country.origins)
.join(Proxy, Origin.proxies)
.outerjoin(deprecations_24hr_subquery, Origin.id == deprecations_24hr_subquery.c.origin_id)
.outerjoin(deprecations_72hr_subquery, Origin.id == deprecations_72hr_subquery.c.origin_id)
.outerjoin(deprecations_24hr_subquery, Proxy.id == deprecations_24hr_subquery.c.resource_id)
.outerjoin(deprecations_72hr_subquery, Proxy.id == deprecations_72hr_subquery.c.resource_id)
.group_by(Country.id)
.all()
)
def origins_report():
deprecations_24hr_subquery, deprecations_72hr_subquery = generate_subqueries()
return (
db.session.query(
Origin,
func.coalesce(func.sum(deprecations_24hr_subquery.c.deprecations_24hr), 0).label('total_deprecations_24hr'),
func.coalesce(func.sum(deprecations_72hr_subquery.c.deprecations_72hr), 0).label('total_deprecations_72hr')
)
.outerjoin(Proxy, Origin.proxies)
.outerjoin(deprecations_24hr_subquery, Proxy.id == deprecations_24hr_subquery.c.resource_id)
.outerjoin(deprecations_72hr_subquery, Proxy.id == deprecations_72hr_subquery.c.resource_id)
.filter(Origin.destroyed.is_(None))
.group_by(Origin.id)
.order_by(desc("total_deprecations_24hr"))
.all()
)
@report.app_template_filter('country_name')
def country_description_filter(country_code):
country = Country.query.filter_by(country_code=country_code).first()
@ -78,66 +99,8 @@ def report_blocks() -> ResponseReturnValue:
).filter(and_(Proxy.deprecated > datetime.utcnow() - timedelta(days=1),
Proxy.deprecation_reason.like('block_%'))).all()
# Aliased to avoid confusion in the join
DeprecationAlias = aliased(Deprecation)
# Current time
now = datetime.utcnow()
# Subquery for deprecations in the last 24 hours
deprecations_24hr_subquery = (
db.session.query(
DeprecationAlias.resource_id,
func.count(DeprecationAlias.resource_id).label('deprecations_24hr')
)
.filter(
DeprecationAlias.deprecated_at >= now - timedelta(hours=24),
DeprecationAlias.resource_type == 'Proxy' # Adjust based on your polymorphic setup
)
.group_by(DeprecationAlias.resource_id)
.subquery()
)
# Subquery for deprecations in the last 72 hours
deprecations_72hr_subquery = (
db.session.query(
DeprecationAlias.resource_id,
func.count(DeprecationAlias.resource_id).label('deprecations_72hr')
)
.filter(
DeprecationAlias.deprecated_at >= now - timedelta(hours=72),
DeprecationAlias.resource_type == 'Proxy' # Adjust based on your polymorphic setup
)
.group_by(DeprecationAlias.resource_id)
.subquery()
)
origins_with_deprecations = (
db.session.query(
Origin,
func.coalesce(func.sum(deprecations_24hr_subquery.c.deprecations_24hr), 0).label('total_deprecations_24hr'),
func.coalesce(func.sum(deprecations_72hr_subquery.c.deprecations_72hr), 0).label('total_deprecations_72hr')
)
.outerjoin(Proxy, Origin.proxies)
.outerjoin(deprecations_24hr_subquery, Proxy.id == deprecations_24hr_subquery.c.resource_id)
.outerjoin(deprecations_72hr_subquery, Proxy.id == deprecations_72hr_subquery.c.resource_id)
.filter(Origin.destroyed.is_(None))
.group_by(Origin.id)
.order_by(desc("total_deprecations_24hr"))
.all()
)
countries = (
db.session.query(Country)
.outerjoin(Origin, Country.origins)
.outerjoin(Proxy, Origin.proxies)
.filter(Country.destroyed.is_(None))
.group_by(Country.id)
.all()
)
return render_template("report_blocks.html.j2",
blocked_today=blocked_today,
origins=origins_with_deprecations,
countries=sorted(countries_report(), key=lambda c: c[0].risk_level),
origins=sorted(origins_report(), key=lambda o: o[1], reverse=True),
countries=sorted(countries_report(), key=lambda c: c[0].risk_level, reverse=True),
)

View file

@ -26,6 +26,37 @@
</table>
<div class="pagebreak"></div>
<h1>Bypass Censorship</h1>
<h2>Geography Censorship Report</h2>
<table>
<tr>
<th>Country</th>
<th>Risk Level</th>
<th>24h</th>
<th>72h</th>
</tr>
{% for country in countries %}
<tr style="font-weight: bold;">
<td>{{ country[0].country_code | country_flag }} {{ country[0].description }}</td>
<td
style="background-color:
{% if country[0].risk_level > 7 %}
red
{% elif country[0].risk_level > 5 %}
yellow
{% elif country[0].risk_level > 0 %}
green
{% else %}
lightgray
{% endif %};"
>{{ country[0].risk_level }}</td>
<td>{{ country[1] }}</td>
<td>{{ country[2] }}</td>
</tr>
{% endfor %}
</table>
<div class="pagebreak"></div>
<h1>Bypass Censorship</h1>
<h2>Origin Censorship Report</h2>
<table>
@ -50,9 +81,9 @@
style="background-color:
{% if origin[0].risk_level[country] > 15 %}
red
{% elif origin[0].risk_level[country] > 10 %}
yellow
{% elif origin[0].risk_level[country] > 5 %}
yellow
{% elif origin[0].risk_level[country] > 0 %}
green
{% else %}
lightgray
@ -62,37 +93,7 @@
{% endfor %}
{% endfor %}
</table>
<div class="pagebreak"></div>
<h1>Bypass Censorship</h1>
<h2>Geography Censorship Report</h2>
<table>
<tr>
<th>Country</th>
<th>Risk Level</th>
<th>24h</th>
<th>72h</th>
</tr>
{% for country in countries %}
<tr style="font-weight: bold;">
<td>{{ country[0].country_code | country_flag }} {{ country[0].description }}</td>
<td
style="background-color:
{% if country[0].risk_level > 7 %}
red
{% elif country[0].risk_level > 5 %}
yellow
{% elif country[0].risk_level > 2 %}
green
{% else %}
lightgray
{% endif %};"
>{{ country[0].risk_level }}</td>
<td>{{ country[1] }}</td>
<td>{{ country[2] }}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>