Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sizer/sizer/chart.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (59 sloc)
2.34 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import csv | |
import plotly.graph_objs as go | |
import plotly | |
import pandas as pd | |
class DockerGraph(object): | |
def __init__(self, container_name, filename): | |
self.container_name = container_name | |
self.filename = filename | |
self.prefix = 'sensors_' + self.container_name | |
self._normalize_csv() | |
self._data = pd.read_csv(self.filename) | |
def _normalize_csv(self): | |
with open(self.filename) as input: | |
reader = csv.reader(input) | |
rows = 0 | |
with open(self.filename + '.tmp', 'w') as output: | |
writer = csv.writer(output) | |
for i, row in enumerate(reader): | |
if i == 0: | |
rows = len(row) | |
row = row[:rows] | |
writer.writerow(row) | |
os.rename(self.filename + '.tmp', self.filename) | |
def get_trace(self, name, title, field, op=None): | |
xs = [] | |
ys = [] | |
for x, y in enumerate(self._data[self.prefix + '_' + name]): | |
# XXX encoding ??!! | |
try: | |
y = json.loads(y.strip('"').replace("'", '"')) | |
value = y.get(field, 0) | |
if op is not None: | |
value = op(value) | |
ys.append(value) | |
except Exception as e: | |
continue | |
xs.append(x) | |
return go.Scatter(x=xs, y=ys, name=title) | |
def create(self, title=None): | |
if title is None: | |
title = '%s Resource Usage' % self.container_name | |
traces = [] | |
for name, ltitle, field in (('cpu', 'CPU', 'total'), | |
('memory', 'Memory', 'usage'), | |
('network', 'Network RX', 'cumulative_rx'), | |
('network', 'Network TX', 'cumulative_tx'), | |
('io', 'I/O', 'usage')): | |
if name == 'cpu': | |
op = lambda x: x / 10. | |
else: | |
op = lambda x: x | |
traces.append(self.get_trace(name, ltitle, field, op)) | |
layout = go.Layout(title=title, plot_bgcolor='rgb(230, 230,230)', | |
showlegend=True) | |
fig = go.Figure(data=traces, layout=layout) | |
plotly.offline.plot(fig, | |
filename='sizer_%s.html' % self.container_name) |