How to use the ocflib.printing.quota.get_connection function in ocflib

To help you get started, we’ve selected a few ocflib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ocf / ocflib / tests-manual / printing / add_refund.py View on Github external
#!/usr/bin/env python3
"""Add a test refund."""
import getpass
import random
import string
from datetime import datetime

from ocflib.printing.quota import add_refund
from ocflib.printing.quota import get_connection
from ocflib.printing.quota import Refund


if __name__ == '__main__':
    user = 'ocfprinting'
    password = getpass.getpass('{} password: '.format(user))
    with get_connection(user=user, password=password) as c:
        add_refund(
            c,
            Refund(
                user=input('user: '),
                time=datetime.now(),
                pages=int(input('pages: ')),
                staffer=getpass.getuser(),
                reason=''.join(
                    random.choice(string.ascii_letters) for _ in range(30)
                ),
github ocf / ocfweb / ocfweb / stats / semester_job.py View on Github external
def get_jobs_plot(graph: str, start_day: datetime, end_day: datetime) -> HttpResponse:
    """Return matplotlib plot of the number of jobs of different page-jobs"""
    graph_config = graphs[graph]
    with quota.get_connection() as cursor:
        # Fix mypy error unsupported left operand type for + (Sequence[Any])
        q: Any = graph_config['quota']
        cursor.execute(
            graph_config['query'],
            q + (start_day, end_day),
        )
        data = cursor.fetchall()

    jobs_dict = {row['pages']: row['count'] for row in data}
    jobs_count = [jobs_dict.get(i, 0) for i in range(1, graph_config['quota'][0] + 1)]

    return freq_plot(jobs_count, graph_config['title'])
github ocf / ocfweb / ocfweb / stats / job_frequency.py View on Github external
day_of_week: int = pyday_to_sqlday(day.weekday())
    day_quota: int = quota.daily_quota(datetime.combine(day, datetime.min.time()))

    sql_today_freq = '''
    SELECT `pages`,  SUM(`count`) AS `count`
    FROM `public_jobs`
    WHERE
        (`pages` <= %s) AND
        (DAYOFWEEK(`day`) = %s) AND
        (`day` = %s )
    GROUP BY `pages`
    ORDER BY `pages` ASC
    '''

    # executing the sql query to get the data
    with quota.get_connection() as cursor:
        cursor.execute(sql_today_freq, (day_quota, day_of_week, day))
        today_freq_data = cursor.fetchall()

    # converting the data into a list
    today_jobs_dict = {row['pages']: row['count'] for row in today_freq_data}
    today_jobs_count = [today_jobs_dict.get(i, 0) for i in range(1, day_quota + 1)]

    # Generating the plot
    fig = Figure(figsize=(10, 4))
    ax = fig.add_subplot(1, 1, 1)

    tickLocations = np.arange(1, day_quota + 1)
    width = 0.8
    ax.bar(tickLocations, today_jobs_count, width)

    ax.set_xticks(ticks=tickLocations)
github ocf / ocfweb / ocfweb / stats / printing.py View on Github external
def _semester_histogram():
    with get_connection() as c:
        c.execute(
            'SELECT `user`, `semester` FROM `printed` WHERE `semester` > 0',
        )
        users = [SEMESTERLY_QUOTA - int(r['semester']) for r in c]

    fig = Figure(figsize=(10, 5))
    ax = fig.add_subplot(1, 1, 1)
    ax.locator_params(nbins=20)
    ax.hist(users, bins=list(range(0, 105, 5)))
    ax.grid(True)
    ax.set_xlim(SEMESTERLY_QUOTA, 0)
    ax.set_ylabel('Number of users')
    ax.set_xlabel('Remaining balance')
    ax.set_title('Remaining balances this semester')

    return fig