From b1306f3e3ae4bacef83bde23485d0c40688f2b61 Mon Sep 17 00:00:00 2001 From: Tony Lian <1040424979@qq.com> Date: Sun, 3 Mar 2019 22:12:26 -0800 Subject: [PATCH 1/6] Add last used function --- ocflib/lab/last_used.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ocflib/lab/last_used.py diff --git a/ocflib/lab/last_used.py b/ocflib/lab/last_used.py new file mode 100644 index 00000000..fa7aff42 --- /dev/null +++ b/ocflib/lab/last_used.py @@ -0,0 +1,35 @@ +import ocflib.lab.stats + +def make_fqdn(hostname): + """Argparse custom type that appends ".ocf.berkeley.edu" to short hostnames.""" + if '.' not in hostname: + return hostname + '.ocf.berkeley.edu' + else: + return hostname + +def build_query(host): + """Builds the MySQL query string with the given conditions.""" + query = 'SELECT * FROM `session`' + query_args = [] + + query_conditions = [] + query_conditions.append('`host` = %s') + query_args.append(make_fqdn(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 + + return (query, query_args) + +def last_used(desktop_host): + try: + with open('/etc/ocfstats-ro.passwd', 'r') as fin: + password = fin.read().strip() + except FileNotFoundError: + return {"err": 'Could not find the file for ocfstats credentials. Are you running this on supernova?'} + with ocflib.lab.stats.get_connection(user='ocfstats-ro', + password=password) as c: + query = build_query(desktop_host) + c.execute(*query) + return c.fetchone() \ No newline at end of file From a7e2ca40c6e3770e1130dcfca933b554ac770683 Mon Sep 17 00:00:00 2001 From: Tony Lian <1040424979@qq.com> Date: Mon, 4 Mar 2019 11:34:26 -0800 Subject: [PATCH 2/6] Move code into stats.py --- ocflib/lab/last_used.py | 35 ----------------------------------- ocflib/lab/stats.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 35 deletions(-) delete mode 100644 ocflib/lab/last_used.py diff --git a/ocflib/lab/last_used.py b/ocflib/lab/last_used.py deleted file mode 100644 index fa7aff42..00000000 --- a/ocflib/lab/last_used.py +++ /dev/null @@ -1,35 +0,0 @@ -import ocflib.lab.stats - -def make_fqdn(hostname): - """Argparse custom type that appends ".ocf.berkeley.edu" to short hostnames.""" - if '.' not in hostname: - return hostname + '.ocf.berkeley.edu' - else: - return hostname - -def build_query(host): - """Builds the MySQL query string with the given conditions.""" - query = 'SELECT * FROM `session`' - query_args = [] - - query_conditions = [] - query_conditions.append('`host` = %s') - query_args.append(make_fqdn(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 - - return (query, query_args) - -def last_used(desktop_host): - try: - with open('/etc/ocfstats-ro.passwd', 'r') as fin: - password = fin.read().strip() - except FileNotFoundError: - return {"err": 'Could not find the file for ocfstats credentials. Are you running this on supernova?'} - with ocflib.lab.stats.get_connection(user='ocfstats-ro', - password=password) as c: - query = build_query(desktop_host) - c.execute(*query) - return c.fetchone() \ No newline at end of file diff --git a/ocflib/lab/stats.py b/ocflib/lab/stats.py index 6315c738..a43da01c 100644 --- a/ocflib/lab/stats.py +++ b/ocflib/lab/stats.py @@ -139,6 +139,34 @@ def list_desktops(public_only=False): c.search(OCF_LDAP_HOSTS, filter, attributes=['cn']) return [entry['attributes']['cn'][0] for entry in c.response] +def build_query(host): + def add_ocf(hostname): + if not hostname.endswith('.ocf.berkeley.edu'): + return hostname + '.ocf.berkeley.edu' + return hostname + 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 + + return (query, query_args) + +def last_used(desktop_host): + try: + with open('/etc/ocfstats-ro.passwd', 'r') as fin: + password = fin.read().strip() + except FileNotFoundError: + return {"err": 'Could not find the file for ocfstats credentials. Are you running this on supernova?'} + with get_connection(user='ocfstats-ro', password=password) as c: + query = build_query(desktop_host) + c.execute(*query) + return Session.from_row(c.fetchone()) class UtilizationProfile(namedtuple('UtilizationProfile', [ 'hostname', 'start', 'end', 'sessions' From 86417b188245961077254d102f2f502c0dc9444b Mon Sep 17 00:00:00 2001 From: Tony Lian <1040424979@qq.com> Date: Mon, 4 Mar 2019 13:37:40 -0800 Subject: [PATCH 3/6] Switch get_connection to mysql.get_connection --- ocflib/lab/stats.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ocflib/lab/stats.py b/ocflib/lab/stats.py index a43da01c..a9d21adc 100644 --- a/ocflib/lab/stats.py +++ b/ocflib/lab/stats.py @@ -163,7 +163,9 @@ def last_used(desktop_host): password = fin.read().strip() except FileNotFoundError: return {"err": 'Could not find the file for ocfstats credentials. Are you running this on supernova?'} - with get_connection(user='ocfstats-ro', password=password) as c: + 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 = build_query(desktop_host) c.execute(*query) return Session.from_row(c.fetchone()) From 7523b8c7a793aaa7b1153efc06f5af60f5bc51c5 Mon Sep 17 00:00:00 2001 From: Tony Lian <1040424979@qq.com> Date: Mon, 4 Mar 2019 13:52:40 -0800 Subject: [PATCH 4/6] Move build_query into last_used and make add_ocf global --- ocflib/lab/stats.py | 47 ++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/ocflib/lab/stats.py b/ocflib/lab/stats.py index a9d21adc..07124404 100644 --- a/ocflib/lab/stats.py +++ b/ocflib/lab/stats.py @@ -139,35 +139,30 @@ def list_desktops(public_only=False): c.search(OCF_LDAP_HOSTS, filter, attributes=['cn']) return [entry['attributes']['cn'][0] for entry in c.response] -def build_query(host): - def add_ocf(hostname): - if not hostname.endswith('.ocf.berkeley.edu'): - return hostname + '.ocf.berkeley.edu' - return hostname - 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 - - return (query, query_args) - -def last_used(desktop_host): - try: - with open('/etc/ocfstats-ro.passwd', 'r') as fin: - password = fin.read().strip() - except FileNotFoundError: - return {"err": 'Could not find the file for ocfstats credentials. Are you running this on supernova?'} +def add_ocf(hostname): + 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: + 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 = build_query(desktop_host) - c.execute(*query) + + 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()) class UtilizationProfile(namedtuple('UtilizationProfile', [ From 6b12c260bafb3b639c4f1e359b70f227817aa11b Mon Sep 17 00:00:00 2001 From: Tony Lian <1040424979@qq.com> Date: Mon, 4 Mar 2019 13:55:17 -0800 Subject: [PATCH 5/6] Remove redundant add_ocf --- ocflib/lab/stats.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ocflib/lab/stats.py b/ocflib/lab/stats.py index 07124404..d3bc3b75 100644 --- a/ocflib/lab/stats.py +++ b/ocflib/lab/stats.py @@ -197,10 +197,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)) From b7c030ea48caca9ae9aeb04ee87c7b42f7fda768 Mon Sep 17 00:00:00 2001 From: Tony Lian <1040424979@qq.com> Date: Tue, 5 Mar 2019 19:37:18 -0800 Subject: [PATCH 6/6] Pre-commit --- ocflib/lab/stats.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ocflib/lab/stats.py b/ocflib/lab/stats.py index d3bc3b75..f6cc78b8 100644 --- a/ocflib/lab/stats.py +++ b/ocflib/lab/stats.py @@ -139,25 +139,27 @@ def list_desktops(public_only=False): c.search(OCF_LDAP_HOSTS, filter, attributes=['cn']) 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): 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 - + # 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 @@ -165,6 +167,7 @@ def last_used(host): c.execute(query, query_args) return Session.from_row(c.fetchone()) + class UtilizationProfile(namedtuple('UtilizationProfile', [ 'hostname', 'start', 'end', 'sessions' ])):