Commit 1efcabb3 authored by Nicolas Herbaut's avatar Nicolas Herbaut
Browse files

graph

parent 44f7dc26
from python:3.6-slim
RUN apt-get update && apt-get install tk-dev --yes --no-install-recommends && rm -rf /var/lib/apt/lists/*
COPY requirements-python3.txt /tmp/requirements-python3.txt
RUN pip install -r /tmp/requirements-python3.txt
COPY ./dist/Flow_Matrix_Web_page-0.0.3-py3.6.egg /tmp/Flow_Matrix_Web_page-0.0.3-py3.6.egg
......
all : clean build docker-build docker-push
all : clean build docker-build
clean: clean-build clean-pyc
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template
from flowmatrix.flowlib import get_data_full, get_data
import argparse
import os
app = Flask(__name__)
app.debug = True
app.autoescape = False
parser = argparse.ArgumentParser(description='Start Flask app for flow matrix')
parser.add_argument('--influxdb_host', type=str, help='hostname or IP for the influxdb server', required=True)
parser.add_argument('--flow_matrix_port', type=int, help='the port on which to run the flow_matrix', default=5011)
parser.add_argument('--influxdb_port', type=str, help='port of the influxdb server', default="8086")
parser.add_argument('--influxdb_database', type=str, help='database to used in the influxdb server', default="telegraf")
args = parser.parse_args()
@app.route("/")
def data():
matrix, svg = get_data(args.influxdb_host, args.influxdb_port)
return render_template('index.html', data=matrix.to_dict(), svg=svg)
@app.route("/full")
def data_full():
return render_template('index.html', data=get_data_full(args.influxdb_host, args.influxdb_port).to_dict())
app.template_folder = "/usr/local/lib/python3.6/site-packages/Flow_Matrix_Web_page-0.0.3-py3.6.egg/flowmatrix/templates/"
app.run(host="0.0.0.0", port=args.flow_matrix_port)
from influxdb import DataFrameClient
import pandas as pd
from colour import Color
import numpy as np
import networkx as nx
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import io
from colour import Color
white = Color("white")
to_blue = list(white.range_to(Color("blue"), 10))
......@@ -13,9 +19,9 @@ query_template_full = """SELECT last("bytes") as value FROM "telegraf"."autogen"
def sizeof_get_color(num, matrix_mean=0, std_dev=999999999999):
if num < matrix_mean:
col = to_blue[min(int(abs(num - matrix_mean) / std_dev), len(to_blue)-1)]
col = to_blue[min(int(abs(num - matrix_mean) / std_dev), len(to_blue) - 1)]
else:
col = to_red[min(int(abs(num - matrix_mean) / std_dev), len(to_red)-1)]
col = to_red[min(int(abs(num - matrix_mean) / std_dev), len(to_red) - 1)]
return col.get_web()
......@@ -51,16 +57,20 @@ def get_data(influxdb_host, influxdb_port, formatter=sizeof_fmt):
columns=apps,
orient="index")
matrix_mean = np.mean((matrix!=0).mean())
print(matrix.to_csv())
matrix_mean = np.mean((matrix != 0).mean())
matrix_std = np.mean((matrix.std()))
def custo_formatter(v):
col=sizeof_get_color(v,matrix_mean,matrix_std )
return (col,formatter(v))
col = sizeof_get_color(v, matrix_mean, matrix_std)
return (col, formatter(v))
svg = get_svg(matrix)
matrix = matrix.applymap(custo_formatter)
return matrix
return matrix, svg
def get_data_full(influxdb_host, influxdb_port, formatter=sizeof_fmt):
......@@ -74,5 +84,62 @@ def get_data_full(influxdb_host, influxdb_port, formatter=sizeof_fmt):
columns=apps,
orient="index")
svg = get_svg(matrix)
matrix.applymap(formatter)
return matrix
return matrix, svg
def get_svg(d):
black = Color("black")
to_red = list(black.range_to(Color("red"), 5))
to_blue = list(black.range_to(Color("blue"), 5))
d.apply(lambda x: 1 / x)
def sizeof_get_color(num, matrix_mean=0, std_dev=999999999999):
if num < matrix_mean:
col = to_blue[min(int(abs(num - matrix_mean) / std_dev), len(to_blue) - 1)]
else:
col = to_red[min(int(abs(num - matrix_mean) / std_dev), len(to_red) - 1)]
return col.get_web(), (num - matrix_mean) / std_dev
def type_to_col(name_full):
name = name_full.split("_")[-1]
if name == "cass":
return "green"
if name == "zoo":
return "gray"
if name == "kafka":
return "blue"
if name == "spark":
return "red"
upper = d.where(~np.tril(np.ones(d.shape)).astype(np.bool))
bottom = d.transpose().where(~np.tril(np.ones(d.shape)).astype(np.bool))
bc = upper + bottom
bc = bc.where(bc != 0)
g = nx.Graph()
mmean = np.mean(bc.mean())
mstd = np.mean(bc.std())
for row, array in bc.items():
for column in array.index:
if not pd.isnull(array[column]):
g.add_edge(row, column, length=array[column], type=row)
values = [type_to_col(node) for node in g.nodes()]
node_labels = {node: node for node in g.nodes()}
values_edge_color = [sizeof_get_color(e[2]["length"], mmean, mstd)[0] for e in g.edges(data=True)]
values_edge_width = [2 * sizeof_get_color(e[2]["length"], mmean, mstd)[1] for e in g.edges(data=True)]
nx.draw(g, with_labels=True, node_color=values, edge_color=values_edge_color, width=values_edge_width,
labels=node_labels)
# plt.show()
s = io.StringIO()
plt.legend(numpoints=1)
plt.savefig(s, format="svg")
plt.cla()
return s.getvalue()
<html>
<!doctype html>
<html lang="en-US">
<head>
<header>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"
integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn"
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</header>
</head>
<body>
<h1> Flow matrix </h1>
<table class="table table-striped">
<table class="table">
<thead>
<tr>
<th>#</th>
{% for key1 in data %}
{%- for key1 in data -%}
<th>{{ key1 }}</th>
{% endfor %}
{%- endfor -%}
</tr>
</thead>
<tbody>
{% for key1 in data %}
{%- for key1 in data -%}
<tr>
<th scope="row">{{ key1 }}</th>
{% for key2 in data %}
{%- for key2 in data -%}
<td bgcolor="{{ data[key2][key1][0] }}">{{data[key2][key1][1]}}</td>
{% endfor %}
{%- endfor -%}
</tr>
{% endfor %}
{%- endfor -%}
</tbody>
</table>
<h1> Flow graph </h1>
<div>
{{ svg | safe}}
<div>
</body>
......
certifi==2017.11.5
chardet==3.0.4
click==6.7
colour==0.1.5
cycler==0.10.0
decorator==4.1.2
enum34==1.1.6
Flask==0.12.2
idna==2.6
influxdb==5.0.0
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
matplotlib==2.1.0
networkx==2.0
numpy==1.13.3
pandas==0.21.0
pyparsing==2.2.0
python-dateutil==2.6.1
pytz==2017.3
requests==2.18.4
......
......@@ -10,7 +10,7 @@ setup(
author='Nicolas Herbaut',
author_email='nicolas.herbaut@univ-grenoble-alpes.fr',
url='https://nextnet.top',
scripts=['flow-matrix'],
scripts=['bin/flow-matrix'],
include_package_data=True,
zip_safe=False,
install_requires=[
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment