Exemplo n.º 1
0
def get_hosts():
    return [
        Host.from_ldap(
            hostname='hal',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname) for hostname in (
                    'fallingrocks',
                    'monsoon',
                )
            ],
        ),
        Host.from_ldap(
            hostname='jaws',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname) for hostname in (
                    'anthrax',
                    'biohazard',
                    'death',
                    'dementors',
                    'democracy',
                    'firestorm',
                    'flood',
                    'lightning',
                    'maelstrom',
                    'pestilence',
                    'reaper',
                    'supernova',
                    'thunder',
                    'tsunami',
                    'werewolves',
                    'whirlwind',
                    'whiteout',
                    'zombies',
                )
            ],
        ),
        Host.from_ldap(
            hostname='pandemic',
            type='hypervisor',
            children=[Host.from_ldap(hostname) for hostname in ('pileup', )],
        ),
        Host.from_ldap(
            hostname='riptide',
            type='hypervisor',
        ),
        Host.from_ldap(
            hostname='corruption',
            type='server',
        ),
        Host('blackhole', 'network',
             'Managed Cisco Catalyst 2960S-48TS-L Switch.', []),
        Host('pagefault', 'printer', '', []),
        Host('papercut', 'printer', '', []),
        Host.from_ldap('overheat', type='raspi'),
        Host.from_ldap('tornado', type='nuc'),
        *(Host.from_ldap(desktop, type='desktop')
          for desktop in sorted(list_desktops())),
    ]
Exemplo n.º 2
0
def get_daily_plot(day):
    """Return matplotlib plot representing a day's plot."""
    start, end = get_open_close(day)
    desktops = list_desktops(public_only=True)
    profiles = UtilizationProfile.from_hostnames(desktops, start, end).values()
    desks_count = len(desktops)

    now = datetime.now()
    latest = min(end, now)
    minute = timedelta(minutes=1)
    times = [start + i * minute for i in range((latest - start) // minute + 1)]
    if now >= end or now <= start:
        now = None
    sums = []

    for t in times:
        instant15 = t + timedelta(seconds=15)
        instant45 = t + timedelta(seconds=45)
        in_use = sum(
            1 if profile.in_use(instant15) or profile.in_use(instant45) else 0
            for profile in profiles)
        sums.append(in_use)

    # Do a weighted moving average to smooth out the data
    processed = [0] * len(sums)
    for i in range(len(sums)):
        for delta_i, weight in AVERAGE_WEIGHTS:
            m = i if (i + delta_i < 0
                      or i + delta_i >= len(sums)) else i + delta_i
            # Don't use data that hasn't occurred yet
            if now and times[i] <= now and times[m] >= now:
                processed[i] += weight * sums[i]
            elif now and times[i] > now:
                processed[i] = 0
            else:
                processed[i] += weight * sums[m]

    fig = Figure(figsize=(10, 4))
    ax = fig.add_subplot(1, 1, 1)

    ax.grid(True)
    ax.plot_date(times, processed, fmt='b-', color='k', linewidth=1.5)

    # Draw a vertical line, if applicable, showing current time
    if now:
        ax.axvline(now, linewidth=1.5)

    # Draw a horizontal line for total desktops
    ax.axhline(desks_count, ls='dashed')
    ax.annotate('   Total desktops', xy=(start, desks_count + 1))

    ax.xaxis.set_major_formatter(DateFormatter('%-I%P'))
    ax.set_xlim(start, end)

    ax.set_ylim(0, desks_count + 5)
    ax.set_ylabel('Computers in Use')

    ax.set_title(f'Lab Utilization {day:%a %b %d}')
    return fig
Exemplo n.º 3
0
def test_list_desktops_staff_only():
    desktops = list_desktops(public_only=True)
    assert 10 < len(desktops) < 50

    assert 'destruction' in desktops

    assert 'eruption' not in desktops
    assert 'death' not in desktops
Exemplo n.º 4
0
def test_list_desktops():
    desktops = list_desktops()
    assert 10 < len(desktops) < 50

    assert 'eruption' in desktops
    assert 'destruction' in desktops

    assert 'death' not in desktops
Exemplo n.º 5
0
def test_list_desktops_staff_only():
    desktops = list_desktops(public_only=True)
    assert 10 < len(desktops) < 50

    assert 'destruction' in desktops

    assert 'eruption' not in desktops
    assert 'death' not in desktops
Exemplo n.º 6
0
def test_list_desktops():
    desktops = list_desktops()
    assert 10 < len(desktops) < 50

    assert 'eruption' in desktops
    assert 'destruction' in desktops

    assert 'death' not in desktops
Exemplo n.º 7
0
def get_hosts():
    return [
        Host.from_ldap(
            hostname='hal',
            type='hypervisor',
            children=[],
        ),

        Host.from_ldap(
            hostname='jaws',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname)
                for hostname in [
                    'anthrax',
                    'coma',
                    'death',
                    'dementors',
                    'fallingrocks',
                    'firestorm',
                    'flood',
                    'lightning',
                    'maelstrom',
                    'pestilence',
                    'reaper',
                    'sandstorm',
                    'supernova',
                    'tsunami',
                    'typhoon',
                    'werewolves',
                    'whiteout',
                ]
            ],
        ),

        Host.from_ldap(
            hostname='pandemic',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname)
                for hostname in [
                    'zombies',
                ]
            ],
        ),

        Host('blackhole', 'network', 'Managed Cisco Catalyst 2960S-48TS-L Switch.', []),

        Host('deforestation', 'printer', '', []),
        Host('logjam', 'printer', '', []),
        Host.from_ldap('tornado', type='nuc'),
    ] + [
        Host.from_ldap(desktop, type='desktop')
        for desktop in list_desktops()
    ]
Exemplo n.º 8
0
def test_fast_slow_profiles_same():
    start = datetime(2015, 11, 23)
    end = start + timedelta(days=1)

    slow_profiles = {
        host + '.ocf.berkeley.edu': UtilizationProfile.from_hostname(host, start, end)
        for host in list_desktops()
    }
    fast_profiles = UtilizationProfile.from_hostnames(list_desktops(), start, end)

    assert set(slow_profiles.keys()) == set(fast_profiles.keys())

    for host in slow_profiles.keys():
        slow = slow_profiles[host]
        fast = fast_profiles[host]

        assert slow.hostname == fast.hostname
        assert slow.start == fast.start
        assert slow.end == fast.end
        assert slow.sessions == fast.sessions
Exemplo n.º 9
0
def test_fast_slow_profiles_same():
    start = datetime(2015, 11, 23)
    end = start + timedelta(days=1)

    slow_profiles = {
        host + '.ocf.berkeley.edu': UtilizationProfile.from_hostname(host, start, end)
        for host in list_desktops()
    }
    fast_profiles = UtilizationProfile.from_hostnames(list_desktops(), start, end)

    assert set(slow_profiles.keys()) == set(fast_profiles.keys())

    for host in slow_profiles.keys():
        slow = slow_profiles[host]
        fast = fast_profiles[host]

        assert slow.hostname == fast.hostname
        assert slow.start == fast.start
        assert slow.end == fast.end
        assert slow.sessions == fast.sessions
Exemplo n.º 10
0
Arquivo: lab.py Projeto: strellic/api
async def lab_desktops():
    public_desktops = list_desktops(public_only=True)

    with get_connection() as c:
        c.execute("SELECT * FROM `desktops_in_use_public`;", )
    desktops_in_use = {hostname_from_domain(session["host"]) for session in c}

    return {
        "public_desktops_in_use":
        desktops_in_use.intersection(public_desktops),
        "public_desktops_num": len(public_desktops),
    }
Exemplo n.º 11
0
def get_daily_plot(day):
    """Return matplotlib plot representing a day's plot."""
    start, end = get_open_close(day)
    profiles = UtilizationProfile.from_hostnames(list_desktops(public_only=True), start, end).values()

    now = datetime.now()
    latest = min(end, now)
    minute = timedelta(minutes=1)
    times = [start + i * minute for i in range((latest - start) // minute + 1)]
    if now >= end or now <= start:
        now = None
    sums = []

    for t in times:
        instant15 = t + timedelta(seconds=15)
        instant45 = t + timedelta(seconds=45)
        in_use = sum(1 if profile.in_use(instant15) or profile.in_use(instant45) else 0 for profile in profiles)
        sums.append(in_use)

    # Do a weighted moving average to smooth out the data
    processed = [0] * len(sums)
    for i in range(len(sums)):
        for delta_i, weight in AVERAGE_WEIGHTS:
            m = i if (i + delta_i < 0 or i + delta_i >= len(sums)) else i + delta_i
            # Don't use data that hasn't occurred yet
            if now and times[i] <= now and times[m] >= now:
                processed[i] += weight * sums[i]
            elif now and times[i] > now:
                processed[i] = 0
            else:
                processed[i] += weight * sums[m]

    fig = Figure(figsize=(10, 4))
    ax = fig.add_subplot(1, 1, 1)

    ax.grid(True)
    ax.plot_date(times, processed, fmt='b-', color='k', linewidth=1.5)

    # Draw a vertical line, if applicable, showing current time
    if now:
        ax.axvline(now, linewidth=1.5)

    ax.xaxis.set_major_formatter(DateFormatter('%-I%P'))
    ax.set_xlim(start, end)

    ax.set_ylim(0, len(profiles))
    ax.set_ylabel('Computers in Use')

    ax.set_title('Lab Utilization {:%a %b %d}'.format(day))
    return fig
Exemplo n.º 12
0
def get_hosts():
    return [
        Host.from_ldap(
            hostname='hal',
            type='hypervisor',
            children=[],
        ),
        Host.from_ldap(
            hostname='jaws',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname) for hostname in [
                    'anthrax',
                    'coma',
                    'death',
                    'dementors',
                    'fallingrocks',
                    'firestorm',
                    'flood',
                    'lightning',
                    'maelstrom',
                    'pestilence',
                    'reaper',
                    'sandstorm',
                    'supernova',
                    'tsunami',
                    'typhoon',
                    'werewolves',
                    'whiteout',
                ]
            ],
        ),
        Host.from_ldap(
            hostname='pandemic',
            type='hypervisor',
            children=[Host.from_ldap(hostname) for hostname in [
                'zombies',
            ]],
        ),
        Host('blackhole', 'network',
             'Managed Cisco Catalyst 2960S-48TS-L Switch.', []),
        Host('deforestation', 'printer', '', []),
        Host('logjam', 'printer', '', []),
        Host.from_ldap('tornado', type='nuc'),
    ] + [
        Host.from_ldap(desktop, type='desktop') for desktop in list_desktops()
    ]
Exemplo n.º 13
0
def desktop_profiles():
    open_, close = get_open_close(date.today())
    now = datetime.today()

    # If the lab has opened, but hasn't closed yet, only count
    # statistics until the current time. If the lab isn't open
    # yet, then don't count anything, and if it is closed, show
    # statistics from when it was open during the day.
    if now > open_ and now < close:
        end = now
    elif now <= open_:
        end = open_
    else:
        end = close

    return sorted(
        UtilizationProfile.from_hostnames(list_desktops(), open_, end).values(),
        key=attrgetter('hostname'),
    )
Exemplo n.º 14
0
def desktop_profiles() -> List[Any]:
    open_, close = get_open_close(date.today())
    now = datetime.today()

    # If the lab has opened, but hasn't closed yet, only count
    # statistics until the current time. If the lab isn't open
    # yet, then don't count anything, and if it is closed, show
    # statistics from when it was open during the day.
    if now > open_ and now < close:
        end = now
    elif now <= open_:
        end = open_
    else:
        end = close

    return sorted(
        UtilizationProfile.from_hostnames(list_desktops(), open_, end).values(),
        key=attrgetter('hostname'),
    )
Exemplo n.º 15
0
import time
from contextlib import contextmanager
from datetime import datetime
from datetime import timedelta

from ocflib.lab.stats import list_desktops
from ocflib.lab.stats import UtilizationProfile


@contextmanager
def timeit():
    start = time.time()
    yield
    print('Time taken: {}'.format(time.time() - start))


if __name__ == '__main__':
    start = datetime(2015, 11, 23)
    end = start + timedelta(days=1)

    print('Testing naive time to create profiles.')
    with timeit():
        slow_profiles = {
            host + '.ocf.berkeley.edu': UtilizationProfile.from_hostname(host, start, end)
            for host in list_desktops()
        }

    print('Testing optimized time to create profiles.')
    with timeit():
        fast_profiles = UtilizationProfile.from_hostnames(list_desktops(), start, end)
Exemplo n.º 16
0
def _list_public_desktops():
    return list_desktops(public_only=True)
Exemplo n.º 17
0
from datetime import datetime
from datetime import timedelta

from ocflib.lab.stats import list_desktops
from ocflib.lab.stats import UtilizationProfile


@contextmanager
def timeit():
    start = time.time()
    yield
    print('Time taken: {}'.format(time.time() - start))


if __name__ == '__main__':
    start = datetime(2015, 11, 23)
    end = start + timedelta(days=1)

    print('Testing naive time to create profiles.')
    with timeit():
        slow_profiles = {
            host + '.ocf.berkeley.edu':
            UtilizationProfile.from_hostname(host, start, end)
            for host in list_desktops()
        }

    print('Testing optimized time to create profiles.')
    with timeit():
        fast_profiles = UtilizationProfile.from_hostnames(
            list_desktops(), start, end)
Exemplo n.º 18
0
def _list_public_desktops() -> List[Any]:
    return list_desktops(public_only=True)
Exemplo n.º 19
0
def get_hosts():
    return [
        Host.from_ldap(
            hostname='hal',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname)
                for hostname in (
                    'monsoon',
                )
            ]
        ),

        Host.from_ldap(
            hostname='jaws',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname)
                for hostname in (
                    'anthrax',
                    'biohazard',
                    'death',
                    'dementors',
                    'democracy',
                    'firestorm',
                    'flood',
                    'lightning',
                    'maelstrom',
                    'pestilence',
                    'reaper',
                    'supernova',
                    'thunder',
                    'tsunami',
                    'werewolves',
                    'whirlwind',
                    'whiteout',
                )
            ],
        ),

        Host.from_ldap(
            hostname='pandemic',
            type='hypervisor',
            children=[
                Host.from_ldap(hostname)
                for hostname in (
                    'fallingrocks',
                    'pileup',
                    'zombies',
                )
            ],
        ),

        Host('blackhole', 'network', 'Managed Cisco Catalyst 2960S-48TS-L Switch.', []),

        Host('logjam', 'printer', '', []),
        Host('pagefault', 'printer', '', []),
        Host('papercut', 'printer', '', []),
        Host.from_ldap('tornado', type='nuc'),
    ] + [
        Host.from_ldap(desktop, type='desktop')
        for desktop in list_desktops()
    ]
Exemplo n.º 20
0
from django.http import JsonResponse
from ocflib.lab.stats import users_in_lab_count
from ocflib.infra.hosts import hosts_by_filter
from ocflib.lab.stats import list_desktops
from ocflib.lab.stats import get_connection

public_desktops = list_desktops(public_only=True)


def collect_desktops():
    """Collect the currently in use desktops for today."""
    with get_connection() as c:
        query = '''
            SELECT `host`
            FROM `session_duration_public`
            WHERE `duration` is NULL
        '''
        c.execute(query)
    # trimming suffix "ocf.berkeley.edu" from each host
    desktops = {r['host'][:-17] for r in c}
    return desktops


def desktop_usage(request):
    num_desktops = len(public_desktops)

    # get the list of public-only dekstops in use
    public_desktops_inuse = collect_desktops().intersection(public_desktops)

    return JsonResponse({
        "public_desktops": list(public_desktops_inuse),