diff --git a/README.org b/README.org index 3c0b9e62bd05c8e0a7106f123d423900887fbdba..bffe4b4fbf639c696f66d4c4af8894a7200cf75f 100644 --- a/README.org +++ b/README.org @@ -490,6 +490,47 @@ In [2]: # gk is your entry point #+END_SRC ** Metrics API +*** Get the timeseries corresponding to a job + Credits to ~lturpin~. + #+BEGIN_SRC python :exports code :tangle examples/job_timeseries.py +import logging +import os + +from grid5000 import Grid5000 + + +logging.basicConfig(level=logging.DEBUG) + + +def get_job_consumption(job_id, gk, site): + metrics = gk.sites[site].metrics + job = gk.sites[site].jobs[job_id] + # nodes as list : "cluster-number.site.grid5000.fr" + nodes_dom = job.assigned_nodes + # nodes as list : "cluster-number" + nodes = map(lambda node_dom: node_dom.split('.')[0], nodes_dom) + # nodes as string : "cluster-number,cluster-number,..." + nodes_str = ','.join(nodes) + + start = job.started_at + end = job.stopped_at + kwargs = { + "only": nodes_str, + "resolution": 1, + "from": start, + "to": end + } + timeseries = metrics["power"].timeseries.list(**kwargs) + return timeseries + + +conf_file = os.path.join(os.environ.get("HOME"), ".python-grid5000.yaml") +gk = Grid5000.from_yaml(conf_file) + +timeseries = get_job_consumption("1092446", gk, "lyon") +print(timeseries) + #+END_SRC + *** Get some timeseries (and plot them) For this example you'll need ~matplotlib~, ~seaborn~ and ~pandas~. @@ -548,6 +589,7 @@ In [2]: # gk is your entry point #+END_SRC + ** More snippets *** Site of a cluster diff --git a/README.rst b/README.rst index 011f87e0201a61e4eb44a31156646c125c4668a5..7e504fad0f47f8ea1b1ce2d4e178afd2097461af 100644 --- a/README.rst +++ b/README.rst @@ -508,7 +508,51 @@ Before starting, the file ``$HOME/.python-grid5000.yaml`` will be loaded. 4.8 Metrics API ~~~~~~~~~~~~~~~ -4.8.1 Get some timeseries (and plot them) +4.8.1 Get the timeseries corresponding to a job +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Credits to ``lturpin``. + +.. code:: python + + import logging + import os + + from grid5000 import Grid5000 + + + logging.basicConfig(level=logging.DEBUG) + + + def get_job_consumption(job_id, gk, site): + metrics = gk.sites[site].metrics + job = gk.sites[site].jobs[job_id] + # nodes as list : "cluster-number.site.grid5000.fr" + nodes_dom = job.assigned_nodes + # nodes as list : "cluster-number" + nodes = map(lambda node_dom: node_dom.split('.')[0], nodes_dom) + # nodes as string : "cluster-number,cluster-number,..." + nodes_str = ','.join(nodes) + + start = job.started_at + end = job.stopped_at + kwargs = { + "only": nodes_str, + "resolution": 1, + "from": start, + "to": end + } + timeseries = metrics["power"].timeseries.list(**kwargs) + return timeseries + + + conf_file = os.path.join(os.environ.get("HOME"), ".python-grid5000.yaml") + gk = Grid5000.from_yaml(conf_file) + + timeseries = get_job_consumption("1092446", gk, "lyon") + print(timeseries) + +4.8.2 Get some timeseries (and plot them) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ For this example you’ll need ``matplotlib``, ``seaborn`` and ``pandas``.