def monitor_command(argv): connoptions = {} opt_duration = 60 opt_interval = 10 opt_pid = None try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # monitor command specific options 'pid=', 'interval=', 'duration=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_monitor() return 0 elif opt in ('-p', '--pid', ): opt_pid = val elif opt in ('-i', '--interval', ): opt_interval = val elif opt in ('-d', '--duration', ): opt_duration = val try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 try: plp.enable_monitor(opt_pid, opt_interval) except Exception as err: print str(err) return 1 print "monitoring for %d seconds ..." %(int(opt_duration)) try: time.sleep(int(opt_duration)) finally: plp.disable_monitor() print "done." return 0
def delete_command(argv): connoptions = {} opt_name = None # ---- # Parse command line # ---- try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # edit command specific coptions 'name=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_delete() return 0 elif opt in ['--name']: opt_name = val if opt_name is None: sys.stderr.write("option --name must be given\n") return 2 # ---- # Delete the requested data set. # ---- try: plp = plprofiler() plp.connect(connoptions) plp.delete_dataset(opt_name) except Exception as err: sys.stderr.write(str(err) + '\n') return 1
def __init__(self, *args, **kwargs): profiler_args=args[0] self.prof = plprofiler.plprofiler() if profiler_args['pgPass']: os.environ['PGPASSWORD']=profiler_args['pgPass'] self.prof.connect({'dsn': 'host={0} dbname={1} user={2} port={3}'.format( profiler_args['hostName'], profiler_args['pgDB'], profiler_args['pgUser'], profiler_args['pgPort'] )}) self.prof.enable() del os.environ['PGPASSWORD']
def delete_command(argv): connoptions = {} opt_name = None # ---- # Parse command line # ---- try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # edit command specific coptions 'name=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_delete() return 0 elif opt in ['--name']: opt_name = val if opt_name is None: sys.stderr.write("option --name must be given\n") return 2 # ---- # Delete the requested data set. # ---- try: plp = plprofiler() plp.connect(connoptions) plp.delete_dataset(opt_name) except Exception as err: sys.stderr.write(str(err) + '\n') return 1
def reset_command(argv): connoptions = {} # ---- # Parse command line # ---- try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # edit command specific coptions ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_reset() return 0 # ---- # Delete the collected data from the pl_profiler_linestats_shared # and pl_profiler_callgraph_shared hashtables. # ---- try: plp = plprofiler() plp.connect(connoptions) plp.reset_shared() except Exception as err: sys.stderr.write(str(err) + '\n') return 1
def reset_command(argv): connoptions = {} # ---- # Parse command line # ---- try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # edit command specific coptions ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_reset() return 0 # ---- # Delete the collected data from the pl_profiler_linestats_shared # and pl_profiler_callgraph_shared hashtables. # ---- try: plp = plprofiler() plp.connect(connoptions) plp.reset_shared() except Exception as err: sys.stderr.write(str(err) + '\n') return 1
def import_command(argv): connoptions = {} opt_file = None opt_edit = False opt_force = False try: opts, args = getopt.getopt( argv, # Standard connection related options "d:f:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # report command specific options 'file=', 'edit', 'force', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_import() return 0 elif opt in ['-f', '--file']: opt_file = val elif opt in ['--edit']: opt_edit = True elif opt in ['--force']: opt_force = True if opt_file is None: sys.stderr.write("option --file must be given\n") return 2 try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 # ---- # Read the export data set and process it. # ---- with open(opt_file, 'r') as fd: import_set = json.loads(fd.read()) for report_data in import_set: # ---- # Launch an editor if we are asked to edit configs. # ---- if opt_edit: try: config = report_data['config'] edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 report_data['config'] = config # ---- # Try to save this report as a saved set. # ---- plp.save_dataset_from_report(report_data, opt_force) return 0
def export_command(argv): connoptions = {} opt_all = False opt_name = None opt_title = None opt_desc = None opt_top = pow(2, 31) opt_output = None opt_from_shared = False opt_edit = False try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:o:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # report command specific options 'all', 'name=', 'title=', 'desc=', 'description=', 'edit', 'output=', 'from-shared', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_export() return 0 elif opt in ['--all']: opt_all = True elif opt in ['--edit']: opt_edit = True elif opt in ['--name']: opt_name = val elif opt in ['--title']: opt_title = val elif opt in ['--desc', '--description']: opt_desc = val elif opt in ( '-o', '--output', ): opt_output = val elif opt in ('--from-shared', ): opt_from_shared = True if not opt_all and opt_name is None and not opt_from_shared: sys.stderr.write( "option --all, --name or --from-shared must be given\n") return 2 if opt_output is None: output_fd = sys.stdout else: output_fd = open(opt_output, 'w') try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 if opt_all: export_names = [row[0] for row in plp.get_dataset_list()] else: if opt_from_shared: export_names = ['collected_data'] else: export_names = [opt_name] # ---- # Build the export data set. # ---- export_set = [] for name in export_names: # ---- # Get the report data either from the collected *_data tables # or a saved dataset. # ---- if opt_from_shared: report_data = plp.get_shared_report_data(name, opt_top, args) else: report_data = plp.get_saved_report_data(name, opt_top, args) config = report_data['config'] if opt_title is not None: config['title'] = opt_title if opt_desc is not None: config['desc'] = opt_desc # ---- # Launch an editor if we are asked to edit configs. # ---- if opt_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 report_data['config'] = config export_set.append(report_data) # ---- # Write the whole thing out. # ---- output_fd.write(json.dumps(export_set, indent=2, sort_keys=True) + "\n") if opt_output is not None: output_fd.close() return 0
def report_command(argv): connoptions = {} opt_name = None opt_title = None opt_desc = None opt_top = 10 opt_output = None opt_from_shared = False need_edit = False try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:o:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # report command specific options 'name=', 'title=', 'desc=', 'description=', 'output=', 'top=', 'from-shared', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_report() return 0 elif opt in ['--name']: opt_name = val elif opt in ['--title']: opt_title = val elif opt in ['--desc', '--description']: opt_desc = val elif opt in ( '-o', '--output', ): opt_output = val elif opt in ('--top', ): opt_top = int(val) elif opt in ('--from-shared', ): opt_from_shared = True if opt_name is None and not opt_from_shared: sys.stderr.write("option --name or --from-shared must be given\n") return 2 if opt_name is None: opt_name = 'current_data' if opt_from_shared and (opt_name is None or opt_title is None or opt_desc is None): need_edit = True if opt_output is None: output_fd = sys.stdout else: output_fd = open(opt_output, 'w') try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 # ---- # Get the report data either from the collected *_data tables # or a saved dataset. # ---- if opt_from_shared: report_data = plp.get_shared_report_data(opt_name, opt_top, args) else: report_data = plp.get_saved_report_data(opt_name, opt_top, args) config = report_data['config'] if opt_title is not None: config['title'] = opt_title if opt_desc is not None: config['desc'] = opt_desc # ---- # Invoke the editor on the config if need be. # ---- if need_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 opt_name = config['name'] report_data['config'] = config plp.report(report_data, output_fd) if opt_output is not None: output_fd.close() return 0
def edit_command(argv): connoptions = {} opt_name = None # ---- # Parse command line # ---- try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # edit command specific coptions 'name=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_edit() return 0 elif opt in ['--name']: opt_name = val if opt_name is None: sys.stderr.write("option --name must be given\n") return 2 # ---- # Get the current values and create a config with that. # ---- try: plp = plprofiler() plp.connect(connoptions) config = plp.get_dataset_config(opt_name) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 # ---- # Launch the editor for the user to edit the info. # ---- try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 new_name = config['name'] # ---- # Update the dataset config # ---- try: plp.update_dataset_config(opt_name, new_name, config) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 return 0
def edit_command(argv): connoptions = {} opt_name = None # ---- # Parse command line # ---- try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # edit command specific coptions 'name=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_edit() return 0 elif opt in ['--name']: opt_name = val if opt_name is None: sys.stderr.write("option --name must be given\n") return 2 # ---- # Get the current values and create a config with that. # ---- try: plp = plprofiler() plp.connect(connoptions) config = plp.get_dataset_config(opt_name) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 # ---- # Launch the editor for the user to edit the info. # ---- try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 new_name = config['name'] # ---- # Update the dataset config # ---- try: plp.update_dataset_config(opt_name, new_name, config) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 return 0
def report_command(argv): connoptions = {} opt_name = None opt_title = None opt_desc = None opt_top = 10 opt_output = None opt_from_shared = False need_edit = False try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:o:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # report command specific options 'name=', 'title=', 'desc=', 'description=', 'output=', 'top=', 'from-shared', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_report() return 0 elif opt in ['--name']: opt_name = val elif opt in ['--title']: opt_title = val elif opt in ['--desc', '--description']: opt_desc = val elif opt in ('-o', '--output', ): opt_output = val elif opt in ('--top', ): opt_top = int(val) elif opt in ('--from-shared', ): opt_from_shared = True if opt_name is None and not opt_from_shared: sys.stderr.write("option --name or --from-shared must be given\n") return 2 if opt_name is None: opt_name = 'current_data' if opt_from_shared and (opt_name is None or opt_title is None or opt_desc is None): need_edit = True if opt_output is None: output_fd = sys.stdout else: output_fd = open(opt_output, 'w') try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 # ---- # Get the report data either from the collected *_data tables # or a saved dataset. # ---- if opt_from_shared: report_data = plp.get_shared_report_data(opt_name, opt_top, args) else: report_data = plp.get_saved_report_data(opt_name, opt_top, args) config = report_data['config'] if opt_title is not None: config['title'] = opt_title if opt_desc is not None: config['desc'] = opt_desc # ---- # Invoke the editor on the config if need be. # ---- if need_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 opt_name = config['name'] report_data['config'] = config plp.report(report_data, output_fd) if opt_output is not None: output_fd.close() return 0
def monitor(): db = get_db() stored_servers = db.execute('SELECT * FROM databases d').fetchall() if request.method == 'POST': # Connect to database, check for plprofiler extension, check if global profiling enabled if request.form['submit'] == 'Connect': # Set options for connecting connoptions = {} connoptions['host'] = request.form['host'] connoptions['port'] = request.form['port'] connoptions['user'] = request.form['username'] connoptions['database'] = request.form['dbname'] session['connoptions'] = connoptions try: # Connect/check for plprofiler extension global plp plp = plprofiler() plp.connect(connoptions) session['connected'] = True session['plprofiler_present'] = True # Check if global profiling is enabled session['global_enabled'] = check_global_enabled(plp) except Exception as err: if str(err).find('plprofiler extension not found'): session['plprofiler_present'] = False print(str(err) + '\n') session['error'] = str(err) return render_template('monitor.html', server_list=stored_servers, session=session) # Enable global profiling elif request.form['submit'] == 'Enable': session['interval'] = request.form['interval'] try: plp.enable_monitor(opt_interval=session['interval']) session['global_enabled'] = check_global_enabled(plp) except Exception as err: print(str(err) + '\n') session['error'] = str(err) return render_template('monitor.html', server_list=stored_servers, session=session) # Disable global profiling elif request.form['submit'] == 'Disable': try: plp.disable_monitor() session['global_enabled'] = check_global_enabled(plp) except Exception as err: print(str(err) + '\n') session['error'] = str(err) return render_template('monitor.html', server_list=stored_servers, session=session) # Reset shared datasets elif request.form['submit'] == 'Reset Shared Data': try: plp.reset_shared() except Exception as err: print(str(err) + '\n') session['error'] = str(err) return render_template('monitor.html', server_list=stored_servers, session=session) elif request.form['submit'] == 'save': try: name = request.form['name'] title = request.form['title'] desc = request.form['desc'] force = request.form.getlist('force')[0] \ if len(request.form.getlist('force')) > 0 \ else False if not name: name = 'current' if not title: title = 'PL PROFILER REPORT for %s' % (name, ) if not desc: desc = ("<h1>PL Profiler Report for %s</h1>\n" + "<p>\n<!-- description here -->\n</p>") % (name, ) config = { 'name': name, 'title': title, 'tabstop': 8, 'svg_width': 1200, 'table_width': '80%', 'desc': desc, } plp.save_dataset_from_shared(name, config, force) except Exception as err: print(str(err) + '\n') session['error'] = str(err) return render_template('monitor.html', server_list=stored_servers, session=session) if request.method == 'GET': connoptions = {} connoptions['host'] = '' connoptions['port'] = '' connoptions['database'] = '' session['connoptions'] = connoptions session['plprofiler_present'] = None session['global_enabled'] = None session['interval'] = 10 session['connected'] = None session['error'] = '' return render_template('monitor.html', server_list=stored_servers, session=session)
def list_command(argv): connoptions = {} # ---- # Parse command line # ---- try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # list command specific options (none at the moment) ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_list() return 0 # ---- # Get the list of saved data sets. # ---- try: plp = plprofiler() plp.connect(connoptions) rows = plp.get_dataset_list() except Exception as err: sys.stderr.write(str(err) + '\n') return 1 if len(rows) == 0: print "No saved data sets found" else: print "" max_name_len = 4 for row in rows: if len(row[0]) > max_name_len: max_name_len = len(row[0]) print 'Name' + ' '*(max_name_len - 4) + ' | Title' print '-'*max_name_len + '-+-' + '-'*(79 - 3 - max_name_len) for row in rows: config = json.loads(row[1]) pad = max_name_len - len(row[0]) print row[0] + ' '*pad + ' | ' + config.get('title', '') print "" print '(' + str(len(rows)) + ' data sets found)' print "" return 0
def save_command(argv): connoptions = {} opt_name = None opt_title = None opt_desc = None opt_force = False need_edit = False # ---- # Parse command line # ---- try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # save command specific options 'name=', 'title=', 'desc=', 'description=', 'force', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_save() return 0 elif opt in ['--name']: opt_name = val elif opt in ['--title']: opt_title = val elif opt in ['--desc', '--description']: opt_desc = val elif opt in ['--force']: opt_force = True if opt_name is None: sys.stderr.write("option --name must be given\n") return 2 # ---- # Set defaults if options not given. # ---- if opt_title is None: need_edit = True opt_title = 'PL Profiler Report for ' + opt_name if opt_desc is None: need_edit = True opt_desc = '<h1>' + opt_title + '</h1>\n' + \ '<p>\n<!-- description here -->\n</p>\n' # ---- # Create our config. # ---- config = { 'name': opt_name, 'title': opt_title, 'tabstop': 8, 'svg_width': 1200, 'table_width': '80%', 'desc': opt_desc, } # ---- # If we set defaults for config options, invoke an editor. # ---- if need_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 opt_name = config['name'] try: plp = plprofiler() plp.connect(connoptions) plp.save_dataset_from_shared(opt_name, config, opt_force) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 1
def run_command(argv): connoptions = {} opt_name = None opt_title = None opt_desc = None opt_sql_file = None opt_query = None opt_top = 10 opt_output = None opt_save = False opt_force = False need_edit = False try: opts, args = getopt.getopt(argv, # Standard connection related options "c:d:f:h:o:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # run command specific options 'name=', 'title=', 'desc=', 'description=', 'command=', 'file=', 'save', 'force', 'output=', 'top=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_run() return 0 elif opt in ['--name']: opt_name = val elif opt in ('-T', '--title', ): opt_title = val elif opt in ('-D', '--desc', '--description', ): opt_desc = val elif opt in ('-c', '--command', ): opt_query = val elif opt in ('-f', '--file', ): opt_sql_file = val elif opt in ('--top', ): opt_top = int(val) elif opt in ('-o', '--output', ): opt_output = val elif opt in ('-s', '--save', ): opt_save = True elif opt in ('-f', '--force', ): opt_force = True if opt_name is None: need_edit = True opt_name = 'current' if opt_title is None: need_edit = True opt_title = "PL Profiler Report for %s" %(opt_name, ) if opt_desc is None: need_edit = True opt_desc = ("<h1>PL Profiler Report for %s</h1>\n" + "<p>\n<!-- description here -->\n</p>") %(opt_name, ) if opt_sql_file is not None and opt_query is not None: sys.stderr.write("--query and --sql-file are mutually exclusive\n") return 2 if opt_sql_file is None and opt_query is None: sys.stderr.write("One of --query or --sql-file must be given\n") return 2 if opt_query is None: with open(opt_sql_file, 'r') as fd: opt_query = fd.read() try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 plp.enable() plp.reset_local() plp.execute_sql(opt_query, sys.stdout) # ---- # Create our config. # ---- config = { 'name': opt_name, 'title': opt_title, 'tabstop': 8, 'svg_width': 1200, 'table_width': '80%', 'desc': opt_desc, } # ---- # If we set defaults for config options, invoke an editor. # ---- if need_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 opt_name = config['name'] if opt_save: try: plp.save_dataset_from_local(opt_name, config, opt_force) except Exception as err: sys.stderr.write(str(err) + "\n") return 1 if opt_output is not None: with open(opt_output, 'w') as output_fd: report_data = plp.get_local_report_data(opt_name, opt_top, args) report_data['config'] = config plp.report(report_data, output_fd) output_fd.close() return 0
def import_command(argv): connoptions = {} opt_file = None opt_edit = False opt_force = False try: opts, args = getopt.getopt(argv, # Standard connection related options "d:f:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # report command specific options 'file=', 'edit', 'force', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_import() return 0 elif opt in ['-f', '--file']: opt_file = val elif opt in ['--edit']: opt_edit = True elif opt in ['--force']: opt_force = True if opt_file is None: sys.stderr.write("option --file must be given\n") return 2 try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 # ---- # Read the export data set and process it. # ---- with open(opt_file, 'r') as fd: import_set = json.loads(fd.read()) for report_data in import_set: # ---- # Launch an editor if we are asked to edit configs. # ---- if opt_edit: try: config = report_data['config'] edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 report_data['config'] = config # ---- # Try to save this report as a saved set. # ---- plp.save_dataset_from_report(report_data, opt_force) return 0
def export_command(argv): connoptions = {} opt_all = False opt_name = None opt_title = None opt_desc = None opt_top = pow(2, 31) opt_output = None opt_from_shared = False opt_edit = False try: opts, args = getopt.getopt(argv, # Standard connection related options "d:h:o:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # report command specific options 'all', 'name=', 'title=', 'desc=', 'description=', 'edit', 'output=', 'from-shared', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_export() return 0 elif opt in ['--all']: opt_all = True elif opt in ['--edit']: opt_edit = True elif opt in ['--name']: opt_name = val elif opt in ['--title']: opt_title = val elif opt in ['--desc', '--description']: opt_desc = val elif opt in ('-o', '--output', ): opt_output = val elif opt in ('--from-shared', ): opt_from_shared = True if not opt_all and opt_name is None and not opt_from_shared: sys.stderr.write("option --all, --name or --from-shared must be given\n") return 2 if opt_output is None: output_fd = sys.stdout else: output_fd = open(opt_output, 'w') try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 if opt_all: export_names = [row[0] for row in plp.get_dataset_list()] else: if opt_from_shared: export_names = ['collected_data'] else: export_names = [opt_name] # ---- # Build the export data set. # ---- export_set = [] for name in export_names: # ---- # Get the report data either from the collected *_data tables # or a saved dataset. # ---- if opt_from_shared: report_data = plp.get_shared_report_data(name, opt_top, args) else: report_data = plp.get_saved_report_data(name, opt_top, args) config = report_data['config'] if opt_title is not None: config['title'] = opt_title if opt_desc is not None: config['desc'] = opt_desc # ---- # Launch an editor if we are asked to edit configs. # ---- if opt_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 report_data['config'] = config export_set.append(report_data) # ---- # Write the whole thing out. # ---- output_fd.write(json.dumps(export_set, indent = 2, sort_keys = True) + "\n") if opt_output is not None: output_fd.close() return 0
def run_command(argv): connoptions = {} opt_name = None opt_title = None opt_desc = None opt_sql_file = None opt_query = None opt_top = 10 opt_output = None opt_save = False opt_force = False need_edit = False try: opts, args = getopt.getopt( argv, # Standard connection related options "c:d:f:h:o:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # run command specific options 'name=', 'title=', 'desc=', 'description=', 'command=', 'file=', 'save', 'force', 'output=', 'top=', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 2 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_run() return 0 elif opt in ['--name']: opt_name = val elif opt in ( '-T', '--title', ): opt_title = val elif opt in ( '-D', '--desc', '--description', ): opt_desc = val elif opt in ( '-c', '--command', ): opt_query = val elif opt in ( '-f', '--file', ): opt_sql_file = val elif opt in ('--top', ): opt_top = int(val) elif opt in ( '-o', '--output', ): opt_output = val elif opt in ( '-s', '--save', ): opt_save = True elif opt in ( '-f', '--force', ): opt_force = True if opt_name is None: need_edit = True opt_name = 'current' if opt_title is None: need_edit = True opt_title = "PL Profiler Report for %s" % (opt_name, ) if opt_desc is None: need_edit = True opt_desc = ("<h1>PL Profiler Report for %s</h1>\n" + "<p>\n<!-- description here -->\n</p>") % (opt_name, ) if opt_sql_file is not None and opt_query is not None: sys.stderr.write("--query and --sql-file are mutually exclusive\n") return 2 if opt_sql_file is None and opt_query is None: sys.stderr.write("One of --query or --sql-file must be given\n") return 2 if opt_query is None: with open(opt_sql_file, 'r') as fd: opt_query = fd.read() try: plp = plprofiler() plp.connect(connoptions) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 plp.enable() plp.reset_local() plp.execute_sql(opt_query, sys.stdout) # ---- # Create our config. # ---- config = { 'name': opt_name, 'title': opt_title, 'tabstop': 8, 'svg_width': 1200, 'table_width': '80%', 'desc': opt_desc, } # ---- # If we set defaults for config options, invoke an editor. # ---- if need_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 opt_name = config['name'] if opt_save: try: plp.save_dataset_from_local(opt_name, config, opt_force) except Exception as err: sys.stderr.write(str(err) + "\n") return 1 if opt_output is not None: with open(opt_output, 'w') as output_fd: report_data = plp.get_local_report_data(opt_name, opt_top, args) report_data['config'] = config plp.report(report_data, output_fd) output_fd.close() return 0
def save_command(argv): connoptions = {} opt_name = None opt_title = None opt_desc = None opt_force = False need_edit = False # ---- # Parse command line # ---- try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # save command specific options 'name=', 'title=', 'desc=', 'description=', 'force', ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_save() return 0 elif opt in ['--name']: opt_name = val elif opt in ['--title']: opt_title = val elif opt in ['--desc', '--description']: opt_desc = val elif opt in ['--force']: opt_force = True if opt_name is None: sys.stderr.write("option --name must be given\n") return 2 # ---- # Set defaults if options not given. # ---- if opt_title is None: need_edit = True opt_title = 'PL Profiler Report for ' + opt_name if opt_desc is None: need_edit = True opt_desc = '<h1>' + opt_title + '</h1>\n' + \ '<p>\n<!-- description here -->\n</p>\n' # ---- # Create our config. # ---- config = { 'name': opt_name, 'title': opt_title, 'tabstop': 8, 'svg_width': 1200, 'table_width': '80%', 'desc': opt_desc, } # ---- # If we set defaults for config options, invoke an editor. # ---- if need_edit: try: edit_config_info(config) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 2 opt_name = config['name'] try: plp = plprofiler() plp.connect(connoptions) plp.save_dataset_from_shared(opt_name, config, opt_force) except Exception as err: sys.stderr.write(str(err) + '\n') traceback.print_exc() return 1
def list_command(argv): connoptions = {} # ---- # Parse command line # ---- try: opts, args = getopt.getopt( argv, # Standard connection related options "d:h:p:U:", [ 'dbname=', 'host=', 'port=', 'user='******'help', # list command specific options (none at the moment) ]) except Exception as err: sys.stderr.write(str(err) + '\n') return 1 for opt, val in opts: if opt in ['-d', '--dbname']: if val.find('=') < 0: connoptions['database'] = val else: connoptions['dsn'] = val elif opt in ['-h', '--host']: connoptions['host'] = val elif opt in ['-p', '--port']: connoptions['port'] = int(val) elif opt in ['-U', '--user']: connoptions['user'] = val elif opt in ['--help']: help_list() return 0 # ---- # Get the list of saved data sets. # ---- try: plp = plprofiler() plp.connect(connoptions) rows = plp.get_dataset_list() except Exception as err: sys.stderr.write(str(err) + '\n') return 1 if len(rows) == 0: print "No saved data sets found" else: print "" max_name_len = 4 for row in rows: if len(row[0]) > max_name_len: max_name_len = len(row[0]) print 'Name' + ' ' * (max_name_len - 4) + ' | Title' print '-' * max_name_len + '-+-' + '-' * (79 - 3 - max_name_len) for row in rows: config = json.loads(row[1]) pad = max_name_len - len(row[0]) print row[0] + ' ' * pad + ' | ' + config.get('title', '') print "" print '(' + str(len(rows)) + ' data sets found)' print "" return 0