Exemplo n.º 1
0
def listResultsWithResponse(sql):
    # logger.info("sql is: " + sql)
    my_query = query_db(sql)

    json_output = jsondumps(my_query)

    return getResponse(json_output)
Exemplo n.º 2
0
def combineResults2(key1, value1, key2, value2):

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

    # logger.info(resp)
    return resp
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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))
Exemplo n.º 5
0
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))
Exemplo n.º 6
0
def getRedirectResponse(url, resp={}):
    return getResponse({})
Exemplo n.º 7
0
def combineResults3(key1, value1, key2, value2, key3, value3):
    json_output = {key1: value1, key2: value2, key3: value3}
    return getResponse(jsondumps(json_output))
Exemplo n.º 8
0
def getSingleRecordResponseWithConn(sql, conn, args=()):
    return getResponse(getSingleRecordWithConn(sql, conn, args))
Exemplo n.º 9
0
def getSingleRecordResponse(sql, args=()):
    return getResponse(getSingleRecord(sql, args))
Exemplo n.º 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"}))