Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve on last used function #163

Merged
merged 4 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ocflib/infra/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def hostname_from_domain(fqdn):
return fqdn.split('.')[0]


def domain_from_hostname(hostname):
"""Return the canonical domain from a hostname, and if it's already a hostname, just return itself.

>>> domain_from_hostname('tsunami')
'tsunami.ocf.berkeley.edu'
"""
if not hostname.endswith('.ocf.berkeley.edu'):
return hostname + '.ocf.berkeley.edu'
return hostname


def type_of_host(hostname):
"""Returns the type of a host as specified in LDAP.

Expand Down
33 changes: 8 additions & 25 deletions ocflib/lab/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cached_property import cached_property

from ocflib.infra import mysql
from ocflib.infra.hosts import domain_from_hostname
from ocflib.infra.ldap import ldap_ocf
from ocflib.infra.ldap import OCF_LDAP_HOSTS

Expand Down Expand Up @@ -140,32 +141,14 @@ def list_desktops(public_only=False):
return [entry['attributes']['cn'][0] for entry in c.response]


def add_ocf(hostname):
if not hostname.endswith('.ocf.berkeley.edu'):
return hostname + '.ocf.berkeley.edu'
return hostname
def last_used(host, ctx):
"""Show the last used statistics for a computer."""

TonyLianLong marked this conversation as resolved.
Show resolved Hide resolved
query = 'SELECT * FROM `session` WHERE `host` = %s ORDER BY `start` DESC LIMIT 1'
# we can't have another user start before current user ends so here we order by start

def last_used(host):
with open('/etc/ocfstats-ro.passwd', 'r') as fin:
password = fin.read().strip()
with mysql.get_connection(user='ocfstats-ro', password=password, db='ocfstats') as c:
# The reason for not using the partial `get_connection` is because of this:
# https://github.com/PyCQA/pylint/issues/2271

query = 'SELECT * FROM `session`'
query_args = []

query_conditions = []
query_conditions.append('`host` = %s')
query_args.append(add_ocf(host))

query += ' WHERE ' + ' AND '.join(query_conditions)
query += ' ORDER BY `start` DESC LIMIT 1'
# we can't have another user start before current user ends so here we order by start

c.execute(query, query_args)
return Session.from_row(c.fetchone())
ctx.execute(query, host)
return Session.from_row(ctx.fetchone())


class UtilizationProfile(namedtuple('UtilizationProfile', [
Expand Down Expand Up @@ -201,7 +184,7 @@ def from_hostname(cls, hostname, start, end):
@classmethod
def from_hostnames(cls, hostnames, start, end):

hostnames = tuple(map(add_ocf, hostnames))
hostnames = tuple(map(domain_from_hostname, hostnames))
TonyLianLong marked this conversation as resolved.
Show resolved Hide resolved

with get_connection() as c:
query = """
Expand Down