def handle(self, *args, **options):
     CollectorRegistry.load()
     if options.get('list_stats'):
         cols = CollectorRegistry.all()
         print 'Available Statistics:'
         for col in cols:
            print "{0:20} - {1}".format(col.stat.collector, col.desc)
         sys.exit(0)
     
     stats = options.get('stat')
     run_time = datetime.now()
     cur_col = None
     director = None
     
     if options.get('debug'):
         logger = logging.getLogger('django.db.backends')
         logger.setLevel(logging.DEBUG)
     
     try:
         with transaction.commit_on_success():
             dbs = settings.TABLEIZER_DBS    #Get all db settings except for the default
             txn_id = Snapshot.objects.get_next_txn()
             director = CollectionDirector(run_time)
             rds = {}
             for k,v in dbs.items():
                 host = v.get('HOST') if v.get('HOST') is not None and v.get('HOST') != '' else 'localhost'
                 for coller in CollectorRegistry.all():
                     if stats is None or stats in coller.stat.collector:
                         cur_col = coller
                         rd = director.collect(host, coller)
                         if rds.get(rd.stat) is None:
                             rds[rd.stat] = []
                         print "NEW TXN: %d" % (txn_id)
                         print "rd changed: %s" % (rd.changed)
                         rds[rd.stat].append(rd)
                             
             for k,v in rds.items():
                 changed = False
                 for i in v:
                     if i.changed:
                         changed = True
                         break
                 if changed:
                     for i in v:
                         i.save(txn_id)
     except Exception, e:
         tb = traceback.format_exc()
         if settings.SEND_CRASHREPORTS:
             logger = logging.getLogger('tableizer')
         else:
             logger = logging.getLogger('management_command')
         logger.error(tb)
 def handle(self, *args, **options):
     warnings.filterwarnings('ignore')   # turn warnings off for MySQLdb
     args = list(args)
     CollectorRegistry.load()
     
     if len(args) < 1:
         print "Need an action (try --help)."
         sys.exit(1)
         
     if options.get('backup'):
         db = settings.DATABASES.get('default', {})
         if db.get('ENGINE') != 'django.db.backends.sqlite3':
             print 'Can only backup sqlite3 Tableizer dbs.'
             sys.exit(1)
         if options.get('debug'):
             print 'Backing up Tableizer db..'
         shutil.copyfile(db.get('NAME', ''), db.get('NAME', '') + '.bak')
                         
     action = args.pop(0)
     if action == 'purge':
         action = PurgeAction(options.get('debug', False), args)
     elif action == 'rename':
         action = RenameAction(options.get('debug', False), args)
     elif action == 'list':
         action = ListAction(options.get('debug', False), args)
     else:
         print 'Unknown action (try --help).'
         sys.exit(1)
       
     try:
         ecode = action.execute()
     except Exception, e:
         tb = traceback.format_exc()
         if settings.SEND_CRASHREPORTS:
             logger = logging.getLogger('tableizer')
         else:
             logger = logging.getLogger('management_command')
         logger.error(tb)
         sys.exit(1)
 def handle(self, *args, **options):
     output_cfg = {}
     sql_conditions = {}
     find_type = 'normal'
     code = 0
 
     CollectorRegistry.load()
     if options.get('list_stats'):
         cols = CollectorRegistry.all()
         print 'Available Statistics:'
         for col in cols:
            print "{0:20} - {1}".format(col.stat.collector, col.desc)
         sys.exit(0)
         
     since_regex = re.search('(\d+(?:\.?\d+)?)([hdwm])?', options.get('since', ''))
     if options.get('since') == 'last':
         find_type = 'last'
     elif since_regex is not None:
         num, unit = since_regex.groups()
         num = float(num)
         if unit == 'h':
             time = datetime.now() - timedelta(hours=num)
         elif unit == 'd':
             time = datetime.now() - timedelta(days=num)
         elif unit == 'w':
             time = datetime.now() - timedelta(weeks=num)
         elif unit == 'm':
             time = datetime.now() - timedelta(minutes=num)
         else:
             time = datetime.now() - timedelta(seconds=num)
         sql_conditions['since'] = time
             
     if options.get('where') is not None:
         sql_conditions['where'] = options.get('where')
     
     if options.get('group_by') is not None:
         sql_conditions['group'] = options.get('group_by')
         
     if options.get('select_columns') is not None:
         sql_conditions['select'] = options.get('select_columns')
         
     if options.get('order_by') is not None:
         sql_conditions['order'] = options.get('order_by')
         
     if options.get('limit') is not None:
         sql_conditions['limit'] = options.get('limit')
     
     for k,v in settings.REPORT_OPTIONS.items():
         output_cfg[k] = v
             
     output_cfg['full'] = options.get('output_full')
     output_cfg['raw'] = options.get('output_raw')
     output_cfg['display_width'] = options.get('output_width')
     
     output = Formatter.get_runner_for(options.get('output'))(sys.stderr)
     
     try:
         Model = TrackingTable._TrackingTable__tables.get(options.get('stat', 'definition'))
         if find_type == 'normal':
             query = Model.objects.all()
         else:
             query = Model.objects.find_most_recent_versions(Model)
         
         if sql_conditions.get('since') is not None:
             query = query.filter(run_time__gte=sql_conditions.get('since'))
         
         if sql_conditions.get('where') is not None:
             query = query.extra(where=[sql_conditions.get('where'),])
         
         if sql_conditions.get('order') is not None:
             cols = sql_conditions.get('order').split(',')
             query = query.order_by(*cols)
         
         # Do reject_ignores filter before limit
         if not output_cfg.get('raw', False):
             if settings.USE_INCLUDE_NOT_IGNORE:
                 query = output.report_include(query)
             else:
                 query = output.reject_ignores(query)
         if sql_conditions.get('limit') is not None:
             query = query[:sql_conditions.get('limit')]
         code = output.format(query, output_cfg)
     except Exception, e:
         tb = traceback.format_exc()
         if settings.SEND_CRASHREPORTS:
             logger = logging.getLogger('tableizer')
         else:
             logger = logging.getLogger('management_command')
         logger.error(tb)
Exemple #4
0
 def get_formatter_for(self, collector, mtype=None):
     mtype = self.media
     CollectorRegistry.load()
     return Formatter.formatters.get(collector, {}).get(mtype)