2013-03-12 17:51:35 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
from typing import Any, Callable, List, Optional, Sequence, TypeVar, Iterable, Set, Tuple
|
2013-08-08 16:50:58 +02:00
|
|
|
import base64
|
2013-03-20 15:31:27 +01:00
|
|
|
import hashlib
|
2016-08-14 18:33:29 +02:00
|
|
|
import heapq
|
|
|
|
import itertools
|
2013-08-08 16:50:58 +02:00
|
|
|
import os
|
2019-12-16 06:27:34 +01:00
|
|
|
import re
|
2018-08-01 11:18:37 +02:00
|
|
|
import string
|
2013-06-27 20:03:51 +02:00
|
|
|
from time import sleep
|
2017-11-05 06:39:22 +01:00
|
|
|
from itertools import zip_longest
|
2013-08-08 16:50:58 +02:00
|
|
|
|
2013-04-16 22:57:50 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-06-03 18:39:57 +02:00
|
|
|
T = TypeVar('T')
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def statsd_key(val: Any, clean_periods: bool=False) -> str:
|
2013-04-16 22:57:50 +02:00
|
|
|
if not isinstance(val, str):
|
|
|
|
val = str(val)
|
|
|
|
|
|
|
|
if ':' in val:
|
|
|
|
val = val.split(':')[0]
|
|
|
|
val = val.replace('-', "_")
|
2013-04-30 23:58:59 +02:00
|
|
|
if clean_periods:
|
|
|
|
val = val.replace('.', '_')
|
2013-04-16 22:57:50 +02:00
|
|
|
|
|
|
|
return val
|
|
|
|
|
2017-11-05 11:37:41 +01:00
|
|
|
class StatsDWrapper:
|
2013-04-16 22:57:50 +02:00
|
|
|
"""Transparently either submit metrics to statsd
|
|
|
|
or do nothing without erroring out"""
|
|
|
|
|
|
|
|
# Backported support for gauge deltas
|
|
|
|
# as our statsd server supports them but supporting
|
|
|
|
# pystatsd is not released yet
|
2017-11-05 11:15:10 +01:00
|
|
|
def _our_gauge(self, stat: str, value: float, rate: float=1, delta: bool=False) -> None:
|
2019-01-31 14:32:37 +01:00
|
|
|
"""Set a gauge value."""
|
|
|
|
from django_statsd.clients import statsd
|
|
|
|
if delta:
|
|
|
|
value_str = '%+g|g' % (value,)
|
|
|
|
else:
|
|
|
|
value_str = '%g|g' % (value,)
|
|
|
|
statsd._send(stat, value_str, rate)
|
2013-04-16 22:57:50 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __getattr__(self, name: str) -> Any:
|
2013-04-16 22:57:50 +02:00
|
|
|
# Hand off to statsd if we have it enabled
|
|
|
|
# otherwise do nothing
|
|
|
|
if name in ['timer', 'timing', 'incr', 'decr', 'gauge']:
|
2015-08-22 22:18:55 +02:00
|
|
|
if settings.STATSD_HOST != '':
|
2013-04-16 22:57:50 +02:00
|
|
|
from django_statsd.clients import statsd
|
|
|
|
if name == 'gauge':
|
|
|
|
return self._our_gauge
|
|
|
|
else:
|
|
|
|
return getattr(statsd, name)
|
|
|
|
else:
|
|
|
|
return lambda *args, **kwargs: None
|
|
|
|
|
|
|
|
raise AttributeError
|
|
|
|
|
|
|
|
statsd = StatsDWrapper()
|
2013-03-12 17:51:35 +01:00
|
|
|
|
|
|
|
# Runs the callback with slices of all_list of a given batch_size
|
2017-11-05 11:15:10 +01:00
|
|
|
def run_in_batches(all_list: Sequence[T],
|
|
|
|
batch_size: int,
|
|
|
|
callback: Callable[[Sequence[T]], None],
|
|
|
|
sleep_time: int=0,
|
|
|
|
logger: Optional[Callable[[str], None]]=None) -> None:
|
2013-03-12 17:51:35 +01:00
|
|
|
if len(all_list) == 0:
|
|
|
|
return
|
|
|
|
|
2016-11-09 13:44:29 +01:00
|
|
|
limit = (len(all_list) // batch_size) + 1
|
2015-11-01 17:15:05 +01:00
|
|
|
for i in range(limit):
|
2013-03-12 17:51:35 +01:00
|
|
|
start = i*batch_size
|
|
|
|
end = (i+1) * batch_size
|
|
|
|
if end >= len(all_list):
|
|
|
|
end = len(all_list)
|
|
|
|
batch = all_list[start:end]
|
|
|
|
|
|
|
|
if logger:
|
|
|
|
logger("Executing %s in batch %s of %s" % (end-start, i+1, limit))
|
|
|
|
|
|
|
|
callback(batch)
|
2013-03-18 18:09:16 +01:00
|
|
|
|
|
|
|
if i != limit - 1:
|
|
|
|
sleep(sleep_time)
|
2013-03-20 15:31:27 +01:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def make_safe_digest(string: str,
|
|
|
|
hash_func: Callable[[bytes], Any]=hashlib.sha1) -> str:
|
2013-03-20 15:31:27 +01:00
|
|
|
"""
|
|
|
|
return a hex digest of `string`.
|
|
|
|
"""
|
|
|
|
# hashlib.sha1, md5, etc. expect bytes, so non-ASCII strings must
|
|
|
|
# be encoded.
|
2017-11-04 17:30:42 +01:00
|
|
|
return hash_func(string.encode('utf-8')).hexdigest()
|
2013-04-16 22:57:50 +02:00
|
|
|
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def log_statsd_event(name: str) -> None:
|
2013-04-16 22:57:50 +02:00
|
|
|
"""
|
|
|
|
Sends a single event to statsd with the desired name and the current timestamp
|
|
|
|
|
|
|
|
This can be used to provide vertical lines in generated graphs,
|
|
|
|
for example when doing a prod deploy, bankruptcy request, or
|
|
|
|
other one-off events
|
|
|
|
|
|
|
|
Note that to draw this event as a vertical line in graphite
|
|
|
|
you can use the drawAsInfinite() command
|
|
|
|
"""
|
|
|
|
event_name = "events.%s" % (name,)
|
2013-06-07 23:53:20 +02:00
|
|
|
statsd.incr(event_name)
|
2013-08-08 16:50:58 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def generate_random_token(length: int) -> str:
|
2017-07-08 02:46:51 +02:00
|
|
|
return str(base64.b16encode(os.urandom(length // 2)).decode('utf-8').lower())
|
2016-08-08 23:30:46 +02:00
|
|
|
|
2018-08-01 11:18:37 +02:00
|
|
|
def generate_api_key() -> str:
|
|
|
|
choices = string.ascii_letters + string.digits
|
|
|
|
altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)]).encode("utf-8")
|
|
|
|
api_key = base64.b64encode(os.urandom(24), altchars=altchars).decode("utf-8")
|
|
|
|
return api_key
|
|
|
|
|
2019-12-16 06:27:34 +01:00
|
|
|
def has_api_key_format(key: str) -> bool:
|
|
|
|
return bool(re.fullmatch(r"([A-Za-z0-9]){32}", key))
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def query_chunker(queries: List[Any],
|
2018-03-23 23:42:54 +01:00
|
|
|
id_collector: Optional[Set[int]]=None,
|
2017-11-05 11:15:10 +01:00
|
|
|
chunk_size: int=1000,
|
2018-03-23 23:42:54 +01:00
|
|
|
db_chunk_size: Optional[int]=None) -> Iterable[Any]:
|
2016-08-14 18:33:29 +02:00
|
|
|
'''
|
|
|
|
This merges one or more Django ascending-id queries into
|
|
|
|
a generator that returns chunks of chunk_size row objects
|
|
|
|
during each yield, preserving id order across all results..
|
|
|
|
|
|
|
|
Queries should satisfy these conditions:
|
|
|
|
- They should be Django filters.
|
|
|
|
- They should return Django objects with "id" attributes.
|
|
|
|
- They should be disjoint.
|
|
|
|
|
|
|
|
The generator also populates id_collector, which we use
|
|
|
|
internally to enforce unique ids, but which the caller
|
|
|
|
can pass in to us if they want the side effect of collecting
|
|
|
|
all ids.
|
|
|
|
'''
|
|
|
|
if db_chunk_size is None:
|
|
|
|
db_chunk_size = chunk_size // len(queries)
|
|
|
|
|
|
|
|
assert db_chunk_size >= 2
|
|
|
|
assert chunk_size >= 2
|
|
|
|
|
|
|
|
if id_collector is not None:
|
|
|
|
assert(len(id_collector) == 0)
|
|
|
|
else:
|
|
|
|
id_collector = set()
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def chunkify(q: Any, i: int) -> Iterable[Tuple[int, int, Any]]:
|
2016-08-14 18:33:29 +02:00
|
|
|
q = q.order_by('id')
|
|
|
|
min_id = -1
|
|
|
|
while True:
|
2017-05-24 21:28:26 +02:00
|
|
|
assert db_chunk_size is not None # Hint for mypy, but also workaround for mypy bug #3442.
|
2016-08-14 18:33:29 +02:00
|
|
|
rows = list(q.filter(id__gt=min_id)[0:db_chunk_size])
|
|
|
|
if len(rows) == 0:
|
|
|
|
break
|
|
|
|
for row in rows:
|
|
|
|
yield (row.id, i, row)
|
|
|
|
min_id = rows[-1].id
|
|
|
|
|
|
|
|
iterators = [chunkify(q, i) for i, q in enumerate(queries)]
|
|
|
|
merged_query = heapq.merge(*iterators)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
tup_chunk = list(itertools.islice(merged_query, 0, chunk_size))
|
|
|
|
if len(tup_chunk) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Do duplicate-id management here.
|
|
|
|
tup_ids = set([tup[0] for tup in tup_chunk])
|
|
|
|
assert len(tup_ids) == len(tup_chunk)
|
|
|
|
assert len(tup_ids.intersection(id_collector)) == 0
|
|
|
|
id_collector.update(tup_ids)
|
|
|
|
|
|
|
|
yield [row for row_id, i, row in tup_chunk]
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2018-10-15 14:24:13 +02:00
|
|
|
def process_list_in_batches(lst: List[Any],
|
|
|
|
chunk_size: int,
|
|
|
|
process_batch: Callable[[List[Any]], None]) -> None:
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
while True:
|
|
|
|
items = lst[offset:offset+chunk_size]
|
|
|
|
if not items:
|
|
|
|
break
|
|
|
|
process_batch(items)
|
|
|
|
offset += chunk_size
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def split_by(array: List[Any], group_size: int, filler: Any) -> List[List[Any]]:
|
2017-01-06 18:56:36 +01:00
|
|
|
"""
|
|
|
|
Group elements into list of size `group_size` and fill empty cells with
|
|
|
|
`filler`. Recipe from https://docs.python.org/3/library/itertools.html
|
|
|
|
"""
|
|
|
|
args = [iter(array)] * group_size
|
|
|
|
return list(map(list, zip_longest(*args, fillvalue=filler)))
|
2017-04-28 06:55:22 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def is_remote_server(identifier: str) -> bool:
|
2017-04-28 06:55:22 +02:00
|
|
|
"""
|
|
|
|
This function can be used to identify the source of API auth
|
|
|
|
request. We can have two types of sources, Remote Zulip Servers
|
|
|
|
and UserProfiles.
|
|
|
|
"""
|
|
|
|
return "@" not in identifier
|