def check_meters(env): """ Function to check the state of a meter. Run via the command line python process_check_meters.py config.ini or via a cron job. """ sql_settings = env["registry"].settings["sqlalchemy.url"] # initialze the database and bind the models to the connection. # bad things happen if we don't call this. initialize_sql(sql_settings) session = DBSession() meters = session.query(Meter).all() for meter in meters: time_difference = datetime.now() - timedelta(hours=1) log.info("Check meter %s" % meter) for c in meter.get_circuits(): log.info("--> Check for circuit %s " % c) last_log = c.get_last_log() # if we have a log # often circuits are configured and we never hear from them. if last_log is not None: if time_difference > last_log.gateway_time: alert = UnresponsiveCircuit(datetime.now(), meter, c, last_log.gateway_time) session.add(alert) else: log.info("We have not heard from circuit %s" % c.id)
def initialize_command(parser): """ Stuff we need to do for every command""" parser.add_argument('--config', dest='config', help='the pyramid config file') args = parser.parse_args() assert args.config,\ 'You must have a --config config.ini, or we can\'t run the command' # load the pyramid config env env = bootstrap(args.config) # load the database initialize_sql( env['registry'].settings.get('sqlalchemy.url')) # return the args and the pyramid env for later use return args, env
def find_message_gaps(env, start_time_str, end_time_str): """ Arguments: start_time_str: YYYY-MM-DD end_time_str: YYYY-MM-DD env: Pyramid context Values: returns None Purpose: """ sql_settings = env['registry'].settings['sqlalchemy.url'] # initialze the database and bind the models to the connection. # bad things happen if we don't call this. initialize_sql(sql_settings) session = DBSession() try: start_date = datetime.strptime(start_time_str, time_format) except: raise NameError(time_error_msg) try: end_date = datetime.strptime(end_time_str, time_format) except: raise NameError(time_error_msg) if start_date >= end_date: raise NameError('start must be before end') time_diff = end_date - start_date time_diff_hours = ((time_diff.total_seconds() / 60) / 60) print 'Find gaps for %s ' % time_diff_hours logs = session.query(PrimaryLog)\ .filter(PrimaryLog.meter_time >= start_date)\ .filter(PrimaryLog.meter_time < end_date) print 'Total number of logs %s ' % logs.count() excepted = len(session.query(Circuit).all()) * time_diff_hours * 2 print (logs.count() / excepted)
def compare_logs(day_str, env): """ Function to compare csv file format to the actually Gateway database. Arguments: day: a string repersentation of a date object YYYY-MM-DD 2011-08-02 Values: returns the difference between the numnber of messages sent to the gateway and the amount in the database. Purpose: To give a better understand of the precent update provided by Airtel/WIFI """ sql_settings = env['registry'].settings['sqlalchemy.url'] # initialze the database and bind the models to the connection. # bad things happen if we don't call this. initialize_sql(sql_settings) session = DBSession() try: query_date = datetime.strptime(day_str, '%Y-%m-%d') except: raise NameError('You must provided the time object in YYYY-MM-DD') print query_date
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ initialize_sql(settings.get('sqlalchemy.url'), echo=False) #change for a production machine authn_policy = AuthTktAuthenticationPolicy('seekrit') authz_policy = ACLAuthorizationPolicy() config = Configurator( settings=settings, root_factory='ssgateway.auth.RootFactory', authentication_policy=authn_policy, authorization_policy=authz_policy ) session_factory = session_factory_from_settings(settings) config.set_session_factory(session_factory) # add the static url for development config.add_static_view('static', 'ssgateway:static') # add the routes config.add_route('index', '/') config.add_route('login', '/login') config.add_route('logout', '/logout') # admin interface for users config.add_route('admin-users', '/admin/users') config.add_route('new-user', '/admin/user/new') config.add_route('edit-user', '/admin/user/{user}/edit') # manage groups config.add_route('new-group', '/admin/group/new') # manage sites config.add_route('list-sites', '/list-sites') config.add_route('new-site', '/site/new') config.add_route('list-organizations', '/list-organization') config.add_route('new-orgainzation', '/organization/new') config.add_route('list-countries', '/list-countries') config.add_route('new-country', '/country/new') # maybe these should not be the same url's config.add_route('list-meters', '/list-meters') config.add_route('new-meter', '/meter/new') config.add_route('show-meter', '/meter/{meter_id}') # vendor applications config.add_route('list-devices', '/list-devices') config.add_route('new-device', '/device/new') # tokens urls. config.add_route('list-tokens', '/list-tokens') config.add_route('make-tokens', '/manage/make_tokens') config.add_route('update_tokens', '/manage/update_tokens') # message relays config.add_route('list-relays', '/list-relays') config.add_route('new-relay', '/message-relay/new') # messages config.add_route('list-messages', '/messages/list') config.add_route('show-message', '/message/index/{message}') config.add_route('meter-messages', '/messages/show') # alerts and alarms config.add_route('list-alerts', '/list-alerts') config.add_route('send-message', '/send/message') config.add_route('check-comm-gaps', '/check-comm-gaps') # end routes config.scan() # return the wsgi application return config.make_wsgi_app()