Ejemplo n.º 1
0
def create_db(args):
  LOG.info('Creating DB schema')

  db_string = get_global_config(args.dir).get('db_string')
  db = database.connect(db_string)

  database.create_db(db)
Ejemplo n.º 2
0
def sync_db_desc(args):
  LOG.info('Syncing boards description')

  db_string = get_global_config(args.dir).get('db_string')
  db = database.connect(db_string)
  boards_map = utils.load_config(args.dir + '/boards.yaml')

  database.sync_board(db, boards_map)
Ejemplo n.º 3
0
 def __init__(self, db_string, mqtt_config, feeds_map_file):
   super(Feeder, self).__init__()
   self.name = 'feeder'
   self.enabled = Event()
   self.enabled.set()
   self.status = {'feeder': 1}
   self.mqtt_config = mqtt_config
   self.db = database.connect(db_string)
   self.start_mqtt()
   self._validate_feeds(feeds_map_file)
Ejemplo n.º 4
0
  def __init__(self, db_string, mqtt_config):
    super(Dbsm, self).__init__()
    self.name = 'dbsm'
    self.enabled = Event()
    self.enabled.set()
    self.status = {'dbsm': 1}
    self.db = database.connect(db_string)
    self.mqtt_config = mqtt_config
    self.metric_queue = Queue.Queue()

    self.start_mqtt()
Ejemplo n.º 5
0
def clean_feed(args):
  LOG.info('Cleaning feeds table')

  db_string = get_global_config(args.dir).get('db_string')
  db = database.connect(db_string)

  feeds = database.get_feed(db, start=args.start, end=args.end)
  LOG.info('Got %d feeds between %s and %s', len(feeds), args.start, args.end)

  ids_to_delete = [feed.id for feed in feeds]

  if args.execute:
    database.delete_row(db, database.Feed, ids_to_delete)
Ejemplo n.º 6
0
def clean_action(args):
  LOG.info('Cleaning action table')

  db_string = get_global_config(args.dir).get('db_string')
  db = database.connect(db_string)

  actions = database.get_action(db, start=args.start, end=args.end)
  LOG.info('Got %d actions between %s and %s', len(actions), args.start, args.end)

  ids_to_delete = [action.id for action in actions]

  if args.execute:
    database.delete_row(db, database.Action, ids_to_delete)
Ejemplo n.º 7
0
  def __init__(self, db_string, sensors_map_file, action_config, mqtt_config):
    super(Executor, self).__init__()
    self.name = 'executor'
    self.enabled = Event()
    self.enabled.set()
    self.status = {'executor': 1, 'armed': 1}
    self.db = database.connect(db_string)
    self.action_config = action_config
    self.mqtt_config = mqtt_config
    self.action_queue = Queue.PriorityQueue()

    signal.signal(signal.SIGHUP, self._handle_signal)
    signal.signal(signal.SIGUSR1, self._handle_signal)

    self.sensors_map_file = sensors_map_file
    self._validate_sensors_map(self.sensors_map_file)

    self._get_boards(self.db)

    self.start_mqtt()
Ejemplo n.º 8
0
def main():
    parser = utils.create_arg_parser("Meact API service")
    args = parser.parse_args()

    conf = utils.load_config(args.dir + "/global.yaml")
    conf = conf.get("api", {})

    # static_dir in config file should be specified only if
    # static files are located somewhere else than app package
    if not conf.get("static_dir"):
        conf["static_dir"] = os.path.join(os.path.dirname(__file__), "static")

    app.config["appconfig"] = conf
    logging_conf = conf.get("logging", {})

    app.config["mqtt"] = Api(conf["mqtt"])
    app.config["mqtt"].loop_start()

    utils.create_logger(logging_conf)

    app.config["db"] = database.connect(conf["db_string"])

    app.run(host="0.0.0.0", port=8080, server="tornado")
Ejemplo n.º 9
0
def aggregate_metric(args):

  def compute_value(aggregate_detail, total_sum, count):
    value_type = aggregate_detail['type']

    if value_type == 'int':
      value = int(total_sum)/int(count)
    elif value_type == 'float':
      value = float(total_sum)/int(count)
      value = round(value, aggregate_detail['precision'])
    elif value_type == 'text':
      value = None

    return value

  def aggregate(aggregate_details, db, start, end, sensor_type, execute):
    ids_to_delete = []
    a_metrics = database.get_metric_aggregated(db, start=start, end=end, sensor_type=sensor_type)

    for a_metric in a_metrics:
      metric = {
        'id': a_metric[0],
        'board_id': a_metric[1],
        'sensor_type': a_metric[2],
        'total_sum': a_metric[3],
        'count': a_metric[4]
      }

      for aggregate_detail in aggregate_details:
        if metric['sensor_type'] in aggregate_detail['sensor_type']:
          m_to_delete = database.get_metric(db, board_ids=metric['board_id'], sensor_type=metric['sensor_type'], start=start, end=end)
          ids_to_delete += [m.id for m in m_to_delete if m.id != metric['id']]

          new_value = compute_value(aggregate_detail, metric['total_sum'], metric['count'])

          LOG.info("Update record with id '%d' for sensor_type '%s' for board_id '%s' with value '%s'",
                metric['id'],
                metric['sensor_type'],
                metric['board_id'],
                new_value)

          if execute and new_value:
            database.update_metric(db, metric['id'], new_value)


    LOG.info("Delete %d metrics", len(ids_to_delete))
    if execute:
      database.delete_row(db, database.Metric, ids_to_delete)

  # Map policy name to seconds
  policy_map = {
    '10m': 60*10,
    '30m': 60*30,
    'hour': 60*60,
    'day': 60*60*24,
    'week': 60*60*24*7,
    'month': 60*60*24*7*4
  }

  LOG.info('Aggregating metric')

  db_string = get_global_config(args.dir).get('db_string')
  db = database.connect(db_string)

  aggregate_details = get_global_config(args.dir).get('aggregate')

  policy_start = int(args.start)
  policy_end = int(args.start) + policy_map[args.policy]

  while (policy_end <= int(args.end)):
    aggregate(aggregate_details, db, policy_start, policy_end, args.sensor_type, args.execute)
    policy_start += policy_map[args.policy]
    policy_end += policy_map[args.policy]