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

Add last used function #159

Merged
merged 6 commits into from
Mar 6, 2019
Merged
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
32 changes: 28 additions & 4 deletions ocflib/lab/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,34 @@ def list_desktops(public_only=False):
return [entry['attributes']['cn'][0] for entry in c.response]


def add_ocf(hostname):
TonyLianLong marked this conversation as resolved.
Show resolved Hide resolved
if not hostname.endswith('.ocf.berkeley.edu'):
return hostname + '.ocf.berkeley.edu'
return hostname


def last_used(host):
with open('/etc/ocfstats-ro.passwd', 'r') as fin:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ocflib shouldn't concern itself with getting credentials itself - it should be able to operate regardless of where the credentials are, i.e. these functions should operate by accepting an authenticated context object, e.g. shorturls.py, mail.py rather than by hardcoding the location of a file on disk, especially when this file isn't universally available.

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
TonyLianLong marked this conversation as resolved.
Show resolved Hide resolved

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'
TonyLianLong marked this conversation as resolved.
Show resolved Hide resolved
# 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())


class UtilizationProfile(namedtuple('UtilizationProfile', [
'hostname', 'start', 'end', 'sessions'
])):
Expand Down Expand Up @@ -172,10 +200,6 @@ def from_hostname(cls, hostname, start, end):

@classmethod
def from_hostnames(cls, hostnames, start, end):
def add_ocf(hostname):
if not hostname.endswith('.ocf.berkeley.edu'):
return hostname + '.ocf.berkeley.edu'
return hostname

hostnames = tuple(map(add_ocf, hostnames))

Expand Down