Adding max notification constraint. (#704)
* Adds additional constraints to the max notification time. With an increasing number of certificates we need to limit the max notification time to reduce the number of certificates that need to be analyzed for notification eligibility.
This commit is contained in:
@ -9,6 +9,9 @@
|
||||
import string
|
||||
import random
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import and_, func
|
||||
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
@ -97,3 +100,57 @@ def validate_conf(app, required_vars):
|
||||
for var in required_vars:
|
||||
if not app.config.get(var):
|
||||
raise InvalidConfiguration("Required variable '{var}' is not set in Lemur's conf.".format(var=var))
|
||||
|
||||
|
||||
# https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/WindowedRangeQuery
|
||||
def column_windows(session, column, windowsize):
|
||||
"""Return a series of WHERE clauses against
|
||||
a given column that break it into windows.
|
||||
|
||||
Result is an iterable of tuples, consisting of
|
||||
((start, end), whereclause), where (start, end) are the ids.
|
||||
|
||||
Requires a database that supports window functions,
|
||||
i.e. Postgresql, SQL Server, Oracle.
|
||||
|
||||
Enhance this yourself ! Add a "where" argument
|
||||
so that windows of just a subset of rows can
|
||||
be computed.
|
||||
|
||||
"""
|
||||
def int_for_range(start_id, end_id):
|
||||
if end_id:
|
||||
return and_(
|
||||
column >= start_id,
|
||||
column < end_id
|
||||
)
|
||||
else:
|
||||
return column >= start_id
|
||||
|
||||
q = session.query(
|
||||
column,
|
||||
func.row_number().over(order_by=column).label('rownum')
|
||||
).from_self(column)
|
||||
|
||||
if windowsize > 1:
|
||||
q = q.filter(sqlalchemy.text("rownum %% %d=1" % windowsize))
|
||||
|
||||
intervals = [id for id, in q]
|
||||
|
||||
while intervals:
|
||||
start = intervals.pop(0)
|
||||
if intervals:
|
||||
end = intervals[0]
|
||||
else:
|
||||
end = None
|
||||
yield int_for_range(start, end)
|
||||
|
||||
|
||||
def windowed_query(q, column, windowsize):
|
||||
""""Break a Query into windows on a given column."""
|
||||
|
||||
for whereclause in column_windows(
|
||||
q.session,
|
||||
column, windowsize):
|
||||
for row in q.filter(whereclause).order_by(column):
|
||||
yield row
|
||||
|
Reference in New Issue
Block a user