Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_sessions_plot(start_day: datetime, end_day: datetime) -> Figure:
"""Return matplotlib plot representing median session length between start
and end day.."""
with get_connection() as c:
query = '''
SELECT
CAST(start AS DATE) AS date,
AVG(TIME_TO_SEC(duration)) as mean_duration_seconds
FROM session_duration_public
WHERE
CAST(start AS DATE) BETWEEN %s AND %s
AND end IS NOT NULL
GROUP BY date
'''
c.execute(query, (start_day, end_day))
days = {r['date']: r for r in c}
fig = Figure(figsize=(10, 3))
ax = fig.add_subplot(1, 1, 1)
def get_sessions_plot(start_day: date, end_day: date) -> Figure:
"""Return matplotlib plot representing unique sessions between start and
end day.."""
with get_connection() as c:
query = '''
SELECT `date`, `unique_logins`
FROM `daily_sessions_public`
WHERE `date` BETWEEN %s AND %s
'''
c.execute(query, (start_day, end_day))
days = {r['date']: r for r in c}
fig = Figure(figsize=(10, 3))
ax = fig.add_subplot(1, 1, 1)
x = []
unique_logins = []
day = start_day
while day <= end_day:
def _get_desktops_in_use() -> Set[Any]:
"""List which desktops are currently in use."""
# https://github.com/ocf/ocflib/blob/90f9268a89ac9d53c089ab819c1aa95bdc38823d/ocflib/lab/ocfstats.sql#L70
# we don't use users_in_lab_count_public because we're looking for
# desktops in use, and the view does COUNT(DISTINCT users)
with get_connection() as c:
c.execute(
'SELECT * FROM `desktops_in_use_public`;',
)
return {hostname_from_domain(session['host']) for session in c}
def _toner_used_by_printer(printer, cutoff=.05, since=None):
"""Returns toner used for a printer since a given date (by default it
returns toner used for this semester).
Toner numbers can be significantly noisy, including significant diffs
whenever toner gets taken out and put back in whenever there is a jam.
Because of this it's hard to determine if a new toner is inserted into a
printer or if it was the same toner again. To reduce this noise we only
count diffs that are smaller than a cutoff which empirically seems to be
more accurate.
"""
if not since:
since = stats.current_semester_start()
with stats.get_connection() as cursor:
cursor.execute(
'''
CREATE TEMPORARY TABLE ordered1
(PRIMARY KEY (position))
AS (
SELECT * FROM (
SELECT
T.*,
@rownum := @rownum + 1 AS position
FROM (
(
SELECT * FROM printer_toner_public
WHERE printer = %s AND
date > %s
ORDER BY date
) AS T,
def _pages_per_day():
with stats.get_connection() as cursor:
cursor.execute('''
SELECT max(value) as value, cast(date as date) as date, printer
FROM printer_pages_public
GROUP BY cast(date as date), printer
ORDER BY date ASC, printer ASC
''')
last_seen = {}
pages_printed = {}
for row in cursor:
if row['printer'] in last_seen:
pages_printed.setdefault(row['date'], defaultdict(int))
pages_printed[row['date']][row['printer']] = (
row['value'] - last_seen[row['printer']]
)