Ejemplo n.º 1
0
def main():
    CONFIG = lib.get_config()
    LOGGER = lib.get_logger(PROCESS)
    LOGGER.warn("=== Starting {}".format(PROCESS))
    # Connect to DB
    database = lib.get_db()
    atexit.register(lib.teardown_db)

    check_interval = float(CONFIG[PROCESS]["check_interval"])
    avg_over_range = int(CONFIG[PROCESS]["avg_over_range"])

    # Find the height of the latest stats record
    last_height = 0
    latest_stat = Grin_stats.get_latest()
    print("latest_stat = {}".format(latest_stat))

    if latest_stat == None:
        LOGGER.warn("Initializing Grin_stats")
        grinstats.initialize(avg_over_range, LOGGER)
        latest_stat = Grin_stats.get_latest()
        print("Finished initializing, latest_stat height = {}".format(
            latest_stat.height))
    last_height = latest_stat.height
    height = last_height + 1
    LOGGER.warn(
        "grinStats service starting at block height: {}".format(height))

    # Generate grin stats records - one per grin block
    while True:
        #latest_db_block = Blocks.get_latest()
        latest = Blocks.get_latest().height
        while latest >= height:
            try:
                new_stats = grinstats.calculate(height, avg_over_range)
                # Batch new stats when possible, but commit at reasonable intervals
                database.db.getSession().add(new_stats)
                #                if( (height % BATCHSZ == 0) or (height >= (latest-10)) ):
                database.db.getSession().commit()
                LOGGER.warn(
                    "Added Grin_stats for block: {} - gps:{} diff:{}".format(
                        new_stats.height, new_stats.gps, new_stats.difficulty))
                height = height + 1
            except AssertionError as e:
                LOGGER.error("Something went wrong: {}".format(e))
                sleep(check_interval)
        sys.stdout.flush()
        sleep(check_interval)
    LOGGER.warn("=== Completed {}".format(PROCESS))
Ejemplo n.º 2
0
    def get(self, height=None, range=None, fields=None):
        LOGGER = lib.get_logger(PROCESS)
        LOGGER.warn("GrinAPI_stats get height:{} range:{} fields:{}".format(
            height, range, fields))
        fields = lib.fields_to_list(fields)
        if height is None or height == 0:
            stats = Grin_stats.get_latest(range)
        else:
            stats = Grin_stats.get_by_height(height, range)
        #pp.pprint(stats)

        if range == None:
            if stats is None:
                return None
            return stats.to_json(fields)
        else:
            st = []
            for stat in stats:
                st.append(stat.to_json(fields))
            return st