<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import itertools <EOL> import logging <EOL> import json <EOL> import collections <EOL> import re <EOL> import types <EOL> import sys <EOL> import csv <EOL> import tabulate <EOL> import esbench <EOL> logger = logging . getLogger ( __name__ ) <EOL> def _get_benchmarks ( conn = None , stats_index_name = esbench . STATS_INDEX_NAME ) : <EOL> """<STR_LIT>""" <EOL> path = "<STR_LIT>" % ( stats_index_name , ) <EOL> resp = conn . get ( path ) <EOL> return resp <EOL> def _benchmarks ( resp , benchmark_ids = None ) : <EOL> """<STR_LIT>""" <EOL> if not benchmark_ids : <EOL> benchmark_ids = [ '<STR_LIT:all>' ] <EOL> data = json . loads ( resp . data ) <EOL> try : <EOL> benchmarks = data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for benchmark_id in benchmark_ids : <EOL> if benchmark_id == '<STR_LIT>' : <EOL> yield benchmarks [ <NUM_LIT:0> ] <EOL> continue <EOL> if benchmark_id == '<STR_LIT>' : <EOL> yield benchmarks [ - <NUM_LIT:1> ] <EOL> continue <EOL> try : <EOL> yield benchmarks [ int ( benchmark_id ) ] <EOL> continue <EOL> except ( ValueError , IndexError ) as exc : <EOL> pass <EOL> for benchmark in benchmarks : <EOL> if ( benchmark_id == '<STR_LIT:all>' ) or ( benchmark_id == benchmark [ '<STR_LIT>' ] ) : <EOL> yield benchmark <EOL> except KeyError : <EOL> logger . warning ( "<STR_LIT>" , exc_info = False ) <EOL> return <EOL> def _get_observations ( conn = None , benchmark_id = None , stats_index_name = esbench . STATS_INDEX_NAME ) : <EOL> """<STR_LIT>""" <EOL> if not benchmark_id : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> path = "<STR_LIT>" % ( stats_index_name , benchmark_id , ) <EOL> resp = conn . get ( path ) <EOL> return resp <EOL> def _observations ( resp ) : <EOL> """<STR_LIT>""" <EOL> data = json . loads ( resp . data ) <EOL> for observation in data [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> yield observation <EOL> def get_data ( conn = None , benchmark_ids = None ) : <EOL> """<STR_LIT>""" <EOL> for benchmark in _benchmarks ( _get_benchmarks ( conn = conn ) , benchmark_ids = benchmark_ids ) : <EOL> for observation in _observations ( _get_observations ( conn = conn , benchmark_id = benchmark [ '<STR_LIT>' ] ) ) : <EOL> data = { <EOL> "<STR_LIT>" : benchmark [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : observation [ '<STR_LIT>' ] , <EOL> } <EOL> yield data <EOL> def filter_tuples ( tuples = None , pattern = "<STR_LIT>" , key_f = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> """<STR_LIT>""" <EOL> if type ( tuples ) is not list : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> compiled = re . compile ( pattern , re . IGNORECASE ) <EOL> filtered = [ t for t in tuples if compiled . match ( key_f ( t ) ) ] <EOL> return sorted ( filtered , key = key_f ) <EOL> def flatten_container ( container = None ) : <EOL> """<STR_LIT>""" <EOL> def _flatten ( container = container , prefix = None ) : <EOL> if type ( container ) in [ str , unicode , int , long , float , bool , types . NoneType ] : <EOL> flat . append ( ( prefix , container ) ) <EOL> elif type ( container ) is dict : <EOL> for key in container : <EOL> _flatten ( container = container [ key ] , prefix = ( "<STR_LIT>" % ( prefix , key ) ) if prefix else key ) <EOL> elif type ( container ) in [ list , set , tuple ] : <EOL> for n , v in enumerate ( container ) : <EOL> _flatten ( container = v , prefix = ( "<STR_LIT>" % ( prefix , n ) ) if prefix else str ( n ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % container ) <EOL> flat = list ( ) <EOL> _flatten ( ) <EOL> return flat <EOL> def group_observations ( data = None , fields = None ) : <EOL> data_flattened = [ flatten_container ( d ) for d in data ] <EOL> data_filtered = [ filter_tuples ( tuples = t , pattern = fields ) for t in data_flattened ] <EOL> def sort_f ( d ) : <EOL> _d = dict ( d ) <EOL> return ( _d [ '<STR_LIT>' ] , _d [ '<STR_LIT>' ] ) <EOL> data_sorted = sorted ( data_filtered , key = sort_f ) <EOL> groups = [ list ( benchmark_obs ) for benchmark_id , benchmark_obs in itertools . groupby ( data_sorted , lambda x : dict ( x ) [ '<STR_LIT>' ] ) ] <EOL> groups_sorted = sorted ( groups , key = lambda x : dict ( x [ <NUM_LIT:0> ] ) [ '<STR_LIT>' ] ) <EOL> return groups_sorted <EOL> FIELDS = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT:(>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:)>" <EOL> ) <EOL> def output_benchmark ( fh = None , fmt = None , observations = None ) : <EOL> keys = [ t [ <NUM_LIT:0> ] for t in observations [ <NUM_LIT:0> ] ] <EOL> values = [ [ t [ <NUM_LIT:1> ] for t in o ] for o in observations ] <EOL> if fmt == '<STR_LIT>' : <EOL> def _shorten ( s ) : <EOL> s = "<STR_LIT>" . join ( [ c for c in s if c not in "<STR_LIT>" ] ) <EOL> return s <EOL> keys = [ "<STR_LIT:.>" . join ( k . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:2> : ] ) for k in keys ] <EOL> print ( tabulate . tabulate ( values , headers = keys ) , file = fh ) <EOL> elif fmt == '<STR_LIT>' : <EOL> writer = csv . writer ( fh , delimiter = "<STR_LIT:\t>" ) <EOL> writer . writerow ( keys ) <EOL> writer . writerows ( values ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % fmt ) <EOL> def show_benchmarks ( conn = None , benchmark_ids = None , fields = None , fmt = None , fh = None ) : <EOL> data = list ( get_data ( conn = conn , benchmark_ids = benchmark_ids ) ) <EOL> benchmarks = group_observations ( data = data , fields = fields ) <EOL> for b in benchmarks : <EOL> output_benchmark ( fh = fh , fmt = fmt , observations = b ) <EOL> def dump_benchmarks ( conn = None , benchmark_ids = None , stats_index_name = esbench . STATS_INDEX_NAME ) : <EOL> """<STR_LIT>""" <EOL> for benchmark in _benchmarks ( _get_benchmarks ( conn = conn , stats_index_name = stats_index_name ) , benchmark_ids ) : <EOL> curl = """<STR_LIT>""" % ( stats_index_name , benchmark [ '<STR_LIT>' ] , json . dumps ( benchmark [ '<STR_LIT>' ] ) ) <EOL> print ( curl ) <EOL> for o in _observations ( _get_observations ( conn , benchmark [ '<STR_LIT>' ] , stats_index_name = stats_index_name ) ) : <EOL> curl = """<STR_LIT>""" % ( stats_index_name , o [ '<STR_LIT>' ] , json . dumps ( o [ '<STR_LIT>' ] ) ) <EOL> print ( curl ) <EOL> return <EOL> def delete_benchmarks ( conn = None , benchmark_ids = None , stats_index_name = esbench . STATS_INDEX_NAME ) : <EOL> if not benchmark_ids : <EOL> resp = conn . delete ( stats_index_name ) <EOL> logger . info ( resp . curl ) <EOL> else : <EOL> for benchmark in _benchmarks ( _get_benchmarks ( conn , stats_index_name = stats_index_name ) , benchmark_ids = benchmark_ids ) : <EOL> for observation in _observations ( _get_observations ( conn , benchmark_id = benchmark [ '<STR_LIT>' ] , stats_index_name = stats_index_name ) ) : <EOL> path = "<STR_LIT>" % ( stats_index_name , observation [ '<STR_LIT>' ] , ) <EOL> resp = conn . delete ( path ) <EOL> logger . info ( resp . curl ) <EOL> path = "<STR_LIT>" % ( stats_index_name , benchmark [ '<STR_LIT>' ] , ) <EOL> resp = conn . delete ( path ) <EOL> logger . info ( resp . curl ) <EOL> return </s>
