Created
April 27, 2017 19:16
-
-
Save iamsortiz/ea8ed0da90e4863565ae82f1315a72f8 to your computer and use it in GitHub Desktop.
Playing around with BI using Python (yml, request, pandas) Javascript (orbjs, chratjs)
This file contains hidden or 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
| #!/usr/bin/env bash | |
| docker run --rm -p 5678:5678 hashicorp/http-echo -text="a" |
This file contains hidden or 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
| #!/usr/bin/env python | |
| import requests | |
| # from pprint import pprint | |
| import json | |
| import time | |
| import yaml | |
| import pandas as pd | |
| from pandas import DataFrame | |
| conf_yml = """ | |
| algorithms: | |
| - a1 | |
| - a2 | |
| - a3 | |
| - a4 | |
| - a5 | |
| companies: | |
| - company1 | |
| - company2 | |
| """ | |
| facts = [] | |
| fact = None | |
| def timing(f): | |
| def wrap(*args): | |
| # print 'wrapper - start: %s' % ' '.join(args) | |
| global fact | |
| fact = {} | |
| time1 = time.time() | |
| ret = f(*args) | |
| time2 = time.time() | |
| elapsed = time2-time1 | |
| elapsed_ms = (elapsed)*1000.0 | |
| # print '%s function took %0.3f ms' % (f.func_name, elapsed_ms) | |
| fact['function'] = f.func_name | |
| fact['elapsed_ms'] = elapsed_ms | |
| # print fact | |
| facts.append(fact) | |
| # print 'wrapper - end: %s' % ' '.join(args) | |
| return ret | |
| return wrap | |
| @timing | |
| def do_post(algorithm, company): | |
| # print 'do_post' | |
| global fact | |
| response = requests.post( | |
| "http://localhost:5678", | |
| data={ | |
| 'algorithm': algorithm, | |
| 'company': company, | |
| } | |
| ) | |
| # print response.request.body | |
| fact['algorithm'] = algorithm | |
| fact['company'] = company | |
| # @timing | |
| def get_facts(): | |
| global fact | |
| conf = yaml.load(conf_yml) | |
| algorithms = conf['algorithms'] | |
| companies = conf['companies'] | |
| for algorithm in algorithms: | |
| for company in companies: | |
| fact = {} | |
| do_post(algorithm, company) | |
| # fact['algorithm'] = None | |
| # fact['company'] = None | |
| def main(): | |
| get_facts() | |
| # print json.dumps(facts, sort_keys=True, separators=(',', ': ')) | |
| # print json.dumps(facts, sort_keys=True, indent=4, separators=(',', ': ')) | |
| df = DataFrame.from_dict(facts) | |
| print df | |
| df_algorithms = df.groupby('algorithm')['algorithm', 'elapsed_ms'].mean() | |
| # df_algorithms = df.groupby('algorithm')['algorithm', 'elapsed_ms'].mean() | |
| print 'df_algorithms', df_algorithms | |
| print 'print df_algorithms.index', df_algorithms.index | |
| print 'df_algorithms.reset_index()', df_algorithms.reset_index() | |
| df_algorithms = df_algorithms.reset_index().sort_values('elapsed_ms') | |
| # print df_algorithms.to_json() | |
| # df_algorithms_json = df_algorithms.to_json() | |
| df_algorithms_labels = df_algorithms['algorithm'].values.tolist() | |
| df_algorithms_values = df_algorithms['elapsed_ms'].values.tolist() | |
| print 'df_algorithms', df_algorithms | |
| print 'df_algorithms_labels', df_algorithms_labels | |
| print 'df_algorithms_values', df_algorithms_values | |
| to_html(facts, df_algorithms_labels, df_algorithms_values) | |
| def to_html(facts, df_algorithms_labels, df_algorithms_values): | |
| html = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Orb pivot grid demo</title> | |
| <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"> | |
| <link rel="stylesheet" type="text/css" href="http://orbjs.net/static/css/orb/orb.min.css" /> | |
| <script type="text/javascript" src="http://orbjs.net/static/js/lib/react-0.12.2.min.js"></script> | |
| <script type="text/javascript" src="http://orbjs.net/static/js/orb/orb.min.js"></script> | |
| <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"></script> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <div class="row"> | |
| <div class="col-md-6 offset-md-3"> | |
| <canvas id="myChart" width="400" height="400"></canvas> | |
| </div> | |
| </div> | |
| </div> | |
| <div id="pgrid"></div> | |
| <script type="text/javascript"> | |
| // data source | |
| var data = %s; | |
| // pivot grid options | |
| var config = { | |
| dataSource: data, | |
| dataHeadersLocation: 'columns', | |
| theme: 'blue', | |
| toolbar: { | |
| visible: true | |
| }, | |
| grandTotal: { | |
| rowsvisible: false, | |
| columnsvisible: true | |
| }, | |
| subTotal: { | |
| visible: true, | |
| collapsed: true | |
| }, | |
| fields: [ | |
| { name: 'algorithm', caption: 'algorithm' }, | |
| { name: 'company', caption: 'company' }, | |
| { name: 'elapsed_ms', | |
| caption: 'elapsed_ms', | |
| aggregateFunc: 'avg' | |
| } | |
| ], | |
| rows : [ 'algorithm' ], | |
| columns : [ 'company' ], | |
| data : [ 'elapsed_ms' ], | |
| width: undefined, | |
| height: undefined | |
| }; | |
| // instantiate and show the pivot grid | |
| new orb.pgridwidget(config) | |
| .render(document.getElementById('pgrid')); | |
| </script> | |
| <script type="text/javascript"> | |
| backgroundColor = [ | |
| 'rgba(255, 99, 132, 0.2)', | |
| 'rgba(54, 162, 235, 0.2)', | |
| 'rgba(255, 206, 86, 0.2)', | |
| 'rgba(75, 192, 192, 0.2)', | |
| 'rgba(153, 102, 255, 0.2)', | |
| 'rgba(255, 159, 64, 0.2)' | |
| ]; | |
| borderColor = [ | |
| 'rgba(255,99,132,1)', | |
| 'rgba(54, 162, 235, 1)', | |
| 'rgba(255, 206, 86, 1)', | |
| 'rgba(75, 192, 192, 1)', | |
| 'rgba(153, 102, 255, 1)', | |
| 'rgba(255, 159, 64, 1)' | |
| ]; | |
| var labels = %s; | |
| var labels_sorted = labels.slice().sort(); | |
| var backgroundColorUnsorted = labels.map(function (label) { | |
| return backgroundColor[labels_sorted.indexOf(label)] | |
| }) | |
| var borderColorUnsorted = labels.map(function (label) { | |
| return borderColor[labels_sorted.indexOf(label)] | |
| }) | |
| var ctx = document.getElementById("myChart"); | |
| var myChart = new Chart(ctx, { | |
| type: 'bar', | |
| data: { | |
| labels: labels, | |
| datasets: [{ | |
| label: '# of Votes', | |
| data: %s, | |
| backgroundColor: backgroundColorUnsorted, | |
| borderColor: borderColorUnsorted, | |
| borderWidth: 1 | |
| }] | |
| }, | |
| options: { | |
| scales: { | |
| yAxes: [{ | |
| ticks: { | |
| beginAtZero:true | |
| } | |
| }] | |
| } | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> | |
| """ % (json.dumps(facts), json.dumps(df_algorithms_labels, sort_keys=False), json.dumps(df_algorithms_values, sort_keys=False)) | |
| with open('deleteme.html', 'w') as f: | |
| f.write(html) | |
| f.closed | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment