コード例 #1
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def getSingleRecord(sql, args=()):
    # logger.info("sql is: " + sql)
    my_query = query_db_once(sql, args)
    json_output = jsondumps(my_query)
    # logger.info("record is: " + json_output)

    return json_output
コード例 #2
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def listResultsWithResponseWithConn(sql, conn, args=(), response=True):
    # logger.info("sql is: " + sql)
    my_query = query_db_with_conn(conn, sql, args)

    json_output = jsondumps(my_query)

    return getResponse(json_output)
コード例 #3
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def listResultsWithResponse(sql):
    # logger.info("sql is: " + sql)
    my_query = query_db(sql)

    json_output = jsondumps(my_query)

    return getResponse(json_output)
コード例 #4
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def combineResults2(key1, value1, key2, value2):

    json_output = {key1: value1, key2: value2}
    resp = getResponse(jsondumps(json_output))

    # logger.info(resp)
    return resp
コード例 #5
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def getSingleRecordWithConn(sql, conn, args=()):
    # logger.info("sql is: " + sql)
    my_query = query_db_once_with_conn(conn, sql, args)
    json_output = jsondumps(my_query)
    # logger.info("record is: " + json_output)

    return json_output
コード例 #6
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def combineResults5(key1, value1, key2, value2, key3, value3, key4, value4,
                    key5, value5):
    json_output = {
        key1: value1,
        key2: value2,
        key3: value3,
        key4: value4,
        key5: value5
    }
    return getResponse(jsondumps(json_output))
コード例 #7
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def combineResults7(key1, value1, key2, value2, key3, value3, key4, value4,
                    key5, value5, key6, value6, key7, value7):
    json_output = {
        key1: value1,
        key2: value2,
        key3: value3,
        key4: value4,
        key5: value5,
        key6: value6,
        key7: value7
    }
    return getResponse(jsondumps(json_output))
コード例 #8
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def getErrorResponse(resp={}):
    json_output = jsondumps(resp)

    return getResponseWithStatus(json_output, "500")
コード例 #9
0
ファイル: dbUtil.py プロジェクト: SmartStake/harmonyanalytics
def combineResults3(key1, value1, key2, value2, key3, value3):
    json_output = {key1: value1, key2: value2, key3: value3}
    return getResponse(jsondumps(json_output))
コード例 #10
0
def syncValidators(conn, app, data, event):
    # logger.info("in harmony validator sync")
    startTime = commonUtils.getCurrentTime()
    startTimeAudit = datetime.datetime.now()
    # conn = dbUtil.getConnection()
    # currentTime = datetime.datetime.now()
    currentHour = datetime.datetime.now().replace(microsecond=0,
                                                  second=0,
                                                  minute=0)
    previousHour = currentHour - datetime.timedelta(hours=1)
    # logger.info("currentTime: {}".format(currentTime))

    # logger.info("loading request")
    # body = json.loads(event["body"])

    validators = data["allValidators"]
    stakingInfo = data["stakingInfo"]
    epochInfo = data["epochInfo"]
    currentEpoch = epochInfo["currentEpoch"]

    logger.info("total number of validators returned is: " +
                str(len(validators)))
    if len(validators) <= 100:
        logger.info(
            "number of validators returned is much less than what was expected. skipping the processing for now."
        )
        return getResponse(jsondumps({"result": "successful"}))

    currentEpochValMap = getCurrentValMap(conn, currentEpoch, None)
    previousEpochValMap = getCurrentValMap(conn, currentEpoch - 1, None)
    currentHourValMap = getCurrentValMap(conn, currentEpoch, currentHour)
    previousHourValMap = getCurrentValMap(conn, None, previousHour)

    commonUtils.logTimeDiff(startTime, "before coinstats")
    currentCoinStat = commonUtils.getCoinStat(conn, app)
    commonUtils.logTimeDiff(startTime, "before getBlockCompletionFactor")
    completionFactor, enoughDataForCurrentApr = getBlockCompletionFactor(
        epochInfo, stakingInfo, currentCoinStat, currentEpoch)
    # logger.info("is enough data captured for current apr: {}".format(enoughDataForCurrentApr))

    valMap = harmonyData.listHPoolsAsMap(conn)
    networkStake = getNetworkStake(validators)
    commonUtils.logTimeDiff(
        startTime, "time spent before starting validators processing")
    i = 0
    deltaInserts, deltaUpdates = [], []
    stakeWeights = []
    for validator in validators:
        address = validator["address"]
        # logger.info("{} - processing validator: {}".format(i, address))
        # logger.info(validator)
        i += 1
        commonUtils.logTimeDiff(startTime,
                                "processing validator #: {}".format(i))

        blsKeyCount = len(validator["blsPublicKeys"])
        optimalBlsKeyCount, bidPerSeat = getOptimalBlsKeyCount(
            stakingInfo, blsKeyCount, validator)

        commonUtils.logTimeDiff(
            startTime, "before processing syncValidator #: {}".format(i))
        stakeWeight = getStakeWeight(validator, networkStake)
        stakeWeights.append(stakeWeight)

        dbValidator = None
        if address in valMap:
            dbValidator = valMap[address]
        hPoolId = syncValidator(conn, validator, blsKeyCount,
                                optimalBlsKeyCount, bidPerSeat, stakeWeight,
                                currentEpoch, dbValidator)
        commonUtils.logTimeDiff(
            startTime, "after processing syncValidator #: {}".format(i))

        currEpochSummary = getValidatorDetails(currentEpochValMap, hPoolId)
        commonUtils.logTimeDiff(
            startTime, "after getting currEpochSummary #: {}".format(i))
        prevEpochSummary = getValidatorDetails(previousEpochValMap, hPoolId)
        commonUtils.logTimeDiff(
            startTime, "after getting prevEpochSummary #: {}".format(i))

        syncValidatorEpochSummary(conn, validator, blsKeyCount, bidPerSeat,
                                  hPoolId, currEpochSummary, prevEpochSummary,
                                  currentEpoch, completionFactor,
                                  enoughDataForCurrentApr)

        commonUtils.logTimeDiff(
            startTime,
            "after processing syncValidatorEpochSummary #: {}".format(i))
        # logger.info("processing hourly data")
        currHourValDetails = getValidatorDetails(currentHourValMap, hPoolId)
        commonUtils.logTimeDiff(
            startTime, "after obtaining currHourValDetails #: {}".format(i))
        prevHourValDetails = getValidatorDetails(previousHourValMap, hPoolId)
        # logger.info("current hour existing details")
        # logger.info(currHourValDetails)
        # logger.info("previous hour existing details")
        # logger.info(prevHourValDetails)
        # logger.info("previous hourly delta performance")
        commonUtils.logTimeDiff(
            startTime,
            "before delta perf - processing validator #: {}".format(i))
        processDeltaPerf(conn, validator, hPoolId, currHourValDetails,
                         prevHourValDetails, currentEpoch, currentHour,
                         deltaInserts, deltaUpdates)
        commonUtils.logTimeDiff(startTime,
                                "after processDeltaPerf #: {}".format(i))
        # logger.info("after processing deltas")

    commonUtils.logTimeDiff(startTime,
                            "before processing hpoolperf inserts and updates")
    batchCreateHPoolPerf(conn, deltaInserts)
    batchUpdateHPoolPerf(conn, deltaUpdates)

    conn.commit()

    # processDeltaPerf(conn, app, validators, currentHour)
    commonUtils.logTimeDiff(startTime, "before processing perf index")
    if enoughDataForCurrentApr:
        processPerfIndex(conn, app, epochInfo)
        conn.commit()
    # uniqueDelegates = len(delegates)
    commonUtils.logTimeDiff(startTime, "before processing perf index")
    processEpochPerf(conn, app, epochInfo, currentCoinStat,
                     enoughDataForCurrentApr)

    commonUtils.logTimeDiff(startTime, "before update coinstat")
    valForNetworkHalt = getValCountForNetworkHalt(stakeWeights)
    updateCoinStats(conn, app, stakingInfo, epochInfo, len(validators),
                    valForNetworkHalt)
    conn.commit()

    harmonyValEvents.generateValSyncEvents(conn, currentEpoch,
                                           enoughDataForCurrentApr)

    commonUtils.logTimeDiff(startTime, "before creating event and audit")
    auditUtils.createEvent(conn, app, eventName.syncHarmonyValidators)
    auditUtils.audit(conn, app, event, eventName.syncHarmonyValidators,
                     "service", startTimeAudit)
    # conn.close()

    commonUtils.logTimeDiff(startTime, "total time spent")
    return getResponse(jsondumps({"result": "successful"}))