diff --git a/README.org b/README.org index 044b79624b151621bb3d6ad26f2407aed394c5d2..48c4b90e31642e843bb0ab37fa4a850b58b2ee56 100644 --- a/README.org +++ b/README.org @@ -489,6 +489,65 @@ In [2]: # gk is your entry point site.vlans[vlanid].submit({"nodes": nodes}) #+END_SRC +** Metrics API +*** Get some timeseries (and plot them) + For this example you'll need ~matplotlib~, ~seaborn~ and ~pandas~. + + #+BEGIN_SRC python :exports code :tangle examples/timeseries.py + import logging + import os + + from grid5000 import Grid5000 + + import pandas as pd + import matplotlib.pyplot as plt + import seaborn as sns + import time + + logging.basicConfig(level=logging.DEBUG) + + conf_file = os.path.join(os.environ.get("HOME"), ".python-grid5000.yaml") + gk = Grid5000.from_yaml(conf_file) + + metrics = gk.sites["lyon"].metrics + print("--- available metrics") + print(metrics.list()) + + print("---- power metric") + print(metrics["power"]) + + print("----- a timeserie") + now = time.time() + kwargs = { + "only": "nova-1,nova-2,nova-3", + "resolution": 1, + "from": int(now - 600), + "to": int(now) + } + timeseries = metrics["power"].timeseries.list(**kwargs) + + # let's visualize this + df = pd.DataFrame() + for timeserie in timeseries: + print(timeserie) + timestamp = timeserie.timestamps + value = timeserie.values + measurement = timeserie.uid + df = pd.concat([df, pd.DataFrame({ + "timestamp": timestamp, + "value": value, + "measurement": [measurement]*len(timestamp) + })]) + + sns.relplot(data=df, + x="timestamp", + y="value", + hue="measurement", + kind="line") + plt.show() + #+END_SRC + + ** More snippets *** Site of a cluster diff --git a/README.rst b/README.rst index d5f94f1b8640d54a7de8e5f2bf05752a7782eb46..4cedc021a1ea6811db06ceebc1fde15c187fff39 100644 --- a/README.rst +++ b/README.rst @@ -505,10 +505,72 @@ Before starting, the file ``$HOME/.python-grid5000.yaml`` will be loaded. # set in vlan site.vlans[vlanid].submit({"nodes": nodes}) -4.8 More snippets +4.8 Metrics API +~~~~~~~~~~~~~~~ + +4.8.1 Get some timeseries (and plot them) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +For this example you’ll need ``matplotlib``, ``seaborn`` and ``pandas``. + +.. code:: python + + import logging + import os + + from grid5000 import Grid5000 + + import pandas as pd + import matplotlib.pyplot as plt + import seaborn as sns + import time + + logging.basicConfig(level=logging.DEBUG) + + conf_file = os.path.join(os.environ.get("HOME"), ".python-grid5000.yaml") + gk = Grid5000.from_yaml(conf_file) + + metrics = gk.sites["lyon"].metrics + print("--- available metrics") + print(metrics.list()) + + print("---- power metric") + print(metrics["power"]) + + print("----- a timeserie") + now = time.time() + kwargs = { + "only": "nova-1,nova-2,nova-3", + "resolution": 1, + "from": int(now - 600), + "to": int(now) + } + timeseries = metrics["power"].timeseries.list(**kwargs) + + # let's visualize this + df = pd.DataFrame() + for timeserie in timeseries: + print(timeserie) + timestamp = timeserie.timestamps + value = timeserie.values + measurement = timeserie.uid + df = pd.concat([df, pd.DataFrame({ + "timestamp": timestamp, + "value": value, + "measurement": [measurement]*len(timestamp) + })]) + + sns.relplot(data=df, + x="timestamp", + y="value", + hue="measurement", + kind="line") + plt.show() + +4.9 More snippets ~~~~~~~~~~~~~~~~~ -4.8.1 Site of a cluster +4.9.1 Site of a cluster ^^^^^^^^^^^^^^^^^^^^^^^ .. code:: python @@ -536,7 +598,7 @@ Before starting, the file ``$HOME/.python-grid5000.yaml`` will be loaded. clusters.remove(matching[0]) print("We found the following matches %s" % matches) -4.8.2 Get all job with a given name on all the sites +4.9.2 Get all job with a given name on all the sites ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code:: python @@ -577,7 +639,7 @@ Before starting, the file ``$HOME/.python-grid5000.yaml`` will be loaded. for job in jobs: job.delete() -4.8.3 Caching API responses +4.9.3 Caching API responses ^^^^^^^^^^^^^^^^^^^^^^^^^^^ The Grid’5000 reference API is static. In this situation to speed up the diff --git a/examples/timeseries.py b/examples/timeseries.py index 2f7c92166c1983261f99e274bb50ada1acd9cd04..82a7ba067aef76e489b6774f569ae17584944d8d 100644 --- a/examples/timeseries.py +++ b/examples/timeseries.py @@ -28,9 +28,9 @@ kwargs = { "from": int(now - 600), "to": int(now) } +timeseries = metrics["power"].timeseries.list(**kwargs) # let's visualize this -timeseries = metrics["power"].timeseries.list(**kwargs) df = pd.DataFrame() for timeserie in timeseries: print(timeserie) @@ -48,4 +48,4 @@ sns.relplot(data=df, y="value", hue="measurement", kind="line") -plt.show() \ No newline at end of file +plt.show() diff --git a/examples/vlan_set_2.py b/examples/vlan_set_2.py index 00476f94f726727568a37f6ba6fec48cdf84e9fb..fa9b154d27f508bacd724f5d617aa7c30fb84d3f 100644 --- a/examples/vlan_set_2.py +++ b/examples/vlan_set_2.py @@ -11,7 +11,7 @@ logging.basicConfig(level=logging.DEBUG) def _to_network_address(host, interface): """Translate a host to a network address e.g: - paravance-20.rennes.grid5000.fr -> paravance-20-eth2.rennes.grid5000.fr + paranoia-20.rennes.grid5000.fr -> paranoia-20-eth2.rennes.grid5000.fr """ splitted = host.split('.') splitted[0] = splitted[0] + "-" + interface @@ -26,7 +26,7 @@ site = gk.sites["rennes"] job = site.jobs.create({"name": "pyg5k", "command": "sleep 3600", - "resources": "{type='kavlan'}/vlan=1+{cluster='paravance'}nodes=1", + "resources": "{type='kavlan'}/vlan=1+{cluster='paranoia'}nodes=1", "types": ["deploy"] })