Example #1
0
int kr_output_handle_process(T_KROutputHandle *ptOutputHandle, 
        T_KROutputDefine *ptOutputDefine, size_t *size, char **buff, 
        T_KRContext *ptContext)
{
    void *data = NULL;
    
    if (ptOutputHandle->pfOutputPre) data=ptOutputHandle->pfOutputPre();
    for (int fldno=0; fldno<ptOutputDefine->iFieldCnt; fldno++) {
        T_KROutputFieldDef *ptFieldDef = &ptOutputDefine->ptFieldDef[fldno];
        
        if (kr_calc_eval(ptFieldDef->calc, ptContext) != 0) {
            KR_LOG(KR_LOGERROR, "kr_calc_eval field:[%d] failed!", fldno);
            return -1;
        }

        void *fldval = kr_get_value(kr_calc_value(ptFieldDef->calc), 
                                    kr_calc_type(ptFieldDef->calc));
        ptOutputHandle->pfOutput(data, fldno, ptFieldDef->length, fldval);
    }
    if (ptOutputHandle->pfOutputPost) ptOutputHandle->pfOutputPost(data, size, buff);

    return 0;
}
Example #2
0
int kr_ddi_aggr_func(T_KRDDI *krddi, T_KRContext *krcontext)
{
    int iResult = -1;
    int iAbsLoc = -1;
    int iRelLoc = -1;
    
    time_t tCurrTransTime = kr_get_transtime(krcontext->ptCurrRec);
    time_t tRecTransTime;
    
    T_KRListNode *node = krcontext->ptRecList->tail;
    while(node)
    {
        krcontext->ptRecord = (T_KRRecord *)kr_list_value(node);
        
        iAbsLoc++; /*绝对位置加一*/
        
        /*当笔是否包含校验*/
        if ((krddi->ptShmDDIDef->caStatisticsType[0] == \
                               KR_DDI_STATISTICS_EXCLUDE) && 
            (krcontext->ptRecord == krcontext->ptCurrRec)) {
            node = node->prev;
            continue;
        }
        
        /*统计数据源校验*/
        if (((T_KRTable *)krcontext->ptRecord->ptTable)->iTableId != \
            krddi->ptShmDDIDef->lStatisticsDatasrc) {
            node = node->prev;
            continue;
        }
        
        /*时间窗口校验*/
        tRecTransTime = kr_get_transtime(krcontext->ptRecord);
        if ((tCurrTransTime - tRecTransTime) > krddi->ptShmDDIDef->lStatisticsValue ) {
            node = node->prev;
            continue;
        }
        
        /*过滤器校验*/
        iResult = kr_calc_eval(krddi->ptDDICalc, krcontext);
        if (iResult != 0) {
            KR_LOG(KR_LOGERROR, "kr_calc_eval[%ld] failed!", krddi->lDDIId);
            return -1;
        } else if (krddi->ptDDICalc->result_type != KR_TYPE_BOOL) {
            KR_LOG(KR_LOGERROR, "result_type of ddi_calc must be boolean!");
            return -1;
        } else if (krddi->ptDDICalc->result_ind != KR_VALUE_SETED ||
                   !krddi->ptDDICalc->result_value.b) {
            node = node->prev;
            continue;
        }
    
        iRelLoc++; /*相对位置加一*/
                        
        /*获取数据项值*/
        E_KRType type = kr_get_field_type(krcontext->ptRecord, krddi->ptShmDDIDef->lStatisticsField);
        void *val = kr_get_field_value(krcontext->ptRecord, krddi->ptShmDDIDef->lStatisticsField);
        U_KRValue stValue = {0};
        switch(type)
        {
            case KR_TYPE_INT:
                stValue.i = *(int *)val;
                break;
            case KR_TYPE_LONG:
                stValue.l = *(long *)val;
                break;
            case KR_TYPE_DOUBLE:
                stValue.d = *(double *)val;
                break;
            case KR_TYPE_STRING:
                stValue.s = (char *)val;
                break;
            default:
                KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", type);
                return -1;
        }
        
        switch(krddi->ptShmDDIDef->caStatisticsMethod[0])
        {
            case KR_DDI_METHOD_SUM:
                switch(krddi->eValueType)
                {
                    case KR_TYPE_INT:
                        krddi->uValue.i = krddi->uValue.i + stValue.i;
                        break;
                    case KR_TYPE_LONG:
                        krddi->uValue.l = krddi->uValue.l + stValue.l;
                        break;
                    case KR_TYPE_DOUBLE:
                        krddi->uValue.d = krddi->uValue.d + stValue.d;
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                krddi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_MIN:
                switch(krddi->eValueType)
                {
                    case KR_TYPE_INT:
                        krddi->uValue.i = MIN(krddi->uValue.i, stValue.i);
                        break;
                    case KR_TYPE_LONG:
                        krddi->uValue.l = MIN(krddi->uValue.l, stValue.l);
                        break;
                    case KR_TYPE_DOUBLE:
                        krddi->uValue.d = MIN(krddi->uValue.d, stValue.d);
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                krddi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_MAX:
                switch(krddi->eValueType)
                {
                    case KR_TYPE_INT:
                        krddi->uValue.i = MAX(krddi->uValue.i, stValue.i);
                        break;
                    case KR_TYPE_LONG:
                        krddi->uValue.l = MAX(krddi->uValue.l, stValue.l);
                        break;
                    case KR_TYPE_DOUBLE:
                        krddi->uValue.d = MAX(krddi->uValue.d, stValue.d);
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                krddi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_COUNT:
                switch(krddi->eValueType)
                {
                    case KR_TYPE_INT:
                        krddi->uValue.i = krddi->uValue.i + 1;
                        break;
                    case KR_TYPE_LONG:
                        krddi->uValue.l = krddi->uValue.l + 1;
                        break;
                    case KR_TYPE_DOUBLE:
                        krddi->uValue.d = krddi->uValue.d + 1;
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                krddi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_CON_INC:
                //TODO
                break;
            case KR_DDI_METHOD_CON_DEC:
                //TODO
                break;
            case KR_DDI_METHOD_CNT_DIS:
                //TODO
                break;
            default:
                KR_LOG(KR_LOGERROR, "Bad Method [%c]!", \
                       krddi->ptShmDDIDef->caStatisticsMethod[0]);
                return -1;    
        }
        
        /*add this record to related*/
        kr_hashtable_insert(krddi->ptRelated, \
                krcontext->ptRecord, krcontext->ptRecord);
    
        /* This is what the difference between SDI and DDI:
         * SDI only set once, while DDI still need to traversal all the list
         */
        krddi->eValueInd = KR_VALUE_SETED;
        
        node = node->prev;
    }

    return 0;
}
Example #3
0
int kr_sdi_aggr_func(T_KRSDI *krsdi, T_KRContext *krcontext)
{
    int iResult = -1;
    int iAbsLoc = -1;
    int iRelLoc = -1;
    
    T_KRListNode *node = krcontext->ptRecList->tail;
    while(node)
    {
        krcontext->ptRecord = (T_KRRecord *)kr_list_value(node);
        
        iAbsLoc++; /*绝对位置加一*/
                
        /*数据项绝对统计定位校验*/
        if (krsdi->ptShmSDIDef->caLocationProperty[0] == KR_LOC_ABSOLUTE) {     
            if (krsdi->ptShmSDIDef->lStatisticsLocation != iAbsLoc) {
                node = node->prev;
                continue;
            }
        }
        
        /*统计数据源校验*/
        if (((T_KRTable *)krcontext->ptRecord->ptTable)->iTableId != \
            krsdi->ptShmSDIDef->lStatisticsDatasrc) {
            node = node->prev;
            continue;
        }
        
        /*过滤器校验*/
        iResult = kr_calc_eval(krsdi->ptSDICalc, krcontext);
        if (iResult != 0) {
            KR_LOG(KR_LOGERROR, "kr_calc_eval [%ld] failed!", krsdi->lSDIId);
            return -1;
        } else if (krsdi->ptSDICalc->result_type != KR_TYPE_BOOL) {
            KR_LOG(KR_LOGERROR, "result_type of sdi_calc must be boolean!");
            return -1;
        } else if (krsdi->ptSDICalc->result_ind != KR_VALUE_SETED ||
                   !krsdi->ptSDICalc->result_value.b) {
            node = node->prev;
            continue;
        }
    
        iRelLoc++; /*相对位置加一*/
            
        /*数据项相对统计定位校验*/
        if (krsdi->ptShmSDIDef->caLocationProperty[0] == KR_LOC_RELATIVE) {     
            if (krsdi->ptShmSDIDef->lStatisticsLocation != iRelLoc) {
                node = node->prev;
                continue;
            }
        }
            
        /*获取数据项值*/
        void *val = kr_get_field_value(krcontext->ptRecord, \
                          krsdi->ptShmSDIDef->lStatisticsField);
        switch(krsdi->eValueType)
        {
            case KR_TYPE_INT:
                krsdi->uValue.i = *(int *)val;
                break;
            case KR_TYPE_LONG:
                krsdi->uValue.l = *(long *)val;
                break;
            case KR_TYPE_DOUBLE:
                krsdi->uValue.d = *(double *)val;
                break;
            case KR_TYPE_STRING:
                krsdi->uValue.s = kr_strdup(val);
                break;
            default:
                KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", krsdi->eValueType);
                return -1;
        }

        /*add this record to related*/
        kr_hashtable_insert(krsdi->ptRelated, \
                krcontext->ptRecord, krcontext->ptRecord);
    
        /* This is what the difference between SDI and DDI:
         * SDI only set once, while DDI still need to traversal all the list
         */
        krsdi->eValueInd = KR_VALUE_SETED;
        break;
        
        node = node->prev;
    }
    
    return 0;
}
int kr_data_item_ddi_aggr(T_KRDataItem *ptDataItem, T_KRContext *ptContext)
{
    T_KRParamDdi *ptParamDdi = (T_KRParamDdi *)ptDataItem->ptDataItemDef;
    T_KRDdi *ptDdi = (T_KRDdi *)ptDataItem->ptPrivate;

    T_KRRecord *ptCurrRec = kr_context_get_data(ptContext, "curr_rec");
    if (ptCurrRec == NULL) {
        KR_LOG(KR_LOGERROR, "no current record in context");
        return -1;
    }

    T_KRDB *ptDB = kr_context_get_data(ptContext, "db");
    if (ptDB == NULL) {
        KR_LOG(KR_LOGERROR, "no db in context");
        return -1;
    }

    //get record list 
    ptDdi->ptRecList = kr_db_select(ptDB,
            ptParamDdi->lStatisticsIndex, 
            NULL, //FIXME:key
            ptParamDdi->lDdiId, //FIXME:tBeginTime
            ptParamDdi->lDdiId, //FIXME:tEndTime
            KR_FIELDID_TRANSTIME //FIXME:iSortFieldId
            );

    int iResult = -1, iAbsLoc = -1, iRelLoc = -1;
    
    T_KRListNode *node = ptDdi->ptRecList->tail;
    while(node)
    {
        T_KRRecord *ptTravRec = (T_KRRecord *)kr_list_value(node);
        kr_context_add_data(ptContext, "trav_rec", ptTravRec);
        
        iAbsLoc++; 
        
        if ((ptParamDdi->caStatisticsType[0] == \
             KR_DDI_STATISTICS_EXCLUDE) && (ptTravRec == ptCurrRec)) {
            node = node->prev;
            continue;
        }
        
        if (ptParamDdi->lStatisticsDatasrc != \
            kr_record_get_input_id(ptTravRec)) {
            node = node->prev;
            continue;
        }
        
        time_t tCurrTransTime = kr_record_get_transtime(ptCurrRec);
        time_t tRecTransTime = kr_record_get_transtime(ptTravRec);
        if ((tCurrTransTime - tRecTransTime) > ptParamDdi->lStatisticsValue ) {
            node = node->prev;
            continue;
        }
        
        iResult = kr_calc_eval(ptDdi->ptFilterCalc, ptContext);
        if (iResult != 0) {
            KR_LOG(KR_LOGERROR, "kr_calc_eval filter failed!");
            return -1;
        } else if (kr_calc_type(ptDdi->ptFilterCalc) != KR_TYPE_BOOL) {
            KR_LOG(KR_LOGERROR, "filter calc type must be boolean!");
            return -1;
        } else if (kr_calc_ind(ptDdi->ptFilterCalc) != KR_VALUE_SETED ||
                   !kr_calc_value(ptDdi->ptFilterCalc)->b) {
            node = node->prev;
            continue;
        }
    
        iRelLoc++; 
                        
        E_KRType type = kr_record_get_field_type(ptTravRec, ptParamDdi->lStatisticsField);
        void *val = kr_record_get_field_value(ptTravRec, ptParamDdi->lStatisticsField);
        U_KRValue stValue = {0};
        switch(type)
        {
            case KR_TYPE_INT:
                stValue.i = *(int *)val;
                break;
            case KR_TYPE_LONG:
                stValue.l = *(long *)val;
                break;
            case KR_TYPE_DOUBLE:
                stValue.d = *(double *)val;
                break;
            case KR_TYPE_STRING:
                stValue.s = (char *)val;
                break;
            default:
                KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", type);
                return -1;
        }
        
        /*
        switch(ptParamDdi->caStatisticsMethod[0])
        {
            case KR_DDI_METHOD_SUM:
                switch(ptDataItem->eValueType)
                {
                    case KR_TYPE_INT:
                        ptDdi->uValue.i = ptDdi->uValue.i + stValue.i;
                        break;
                    case KR_TYPE_LONG:
                        ptDdi->uValue.l = ptDdi->uValue.l + stValue.l;
                        break;
                    case KR_TYPE_DOUBLE:
                        ptDdi->uValue.d = ptDdi->uValue.d + stValue.d;
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                ptDdi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_MIN:
                switch(ptDataItem->eValueType)
                {
                    case KR_TYPE_INT:
                        ptDdi->uValue.i = MIN(ptDdi->uValue.i, stValue.i);
                        break;
                    case KR_TYPE_LONG:
                        ptDdi->uValue.l = MIN(ptDdi->uValue.l, stValue.l);
                        break;
                    case KR_TYPE_DOUBLE:
                        ptDdi->uValue.d = MIN(ptDdi->uValue.d, stValue.d);
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                ptDdi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_MAX:
                switch(ptDataItem->eValueType)
                {
                    case KR_TYPE_INT:
                        ptDdi->uValue.i = MAX(ptDdi->uValue.i, stValue.i);
                        break;
                    case KR_TYPE_LONG:
                        ptDdi->uValue.l = MAX(ptDdi->uValue.l, stValue.l);
                        break;
                    case KR_TYPE_DOUBLE:
                        ptDdi->uValue.d = MAX(ptDdi->uValue.d, stValue.d);
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                ptDdi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_COUNT:
                switch(ptDataItem->eValueType)
                {
                    case KR_TYPE_INT:
                        ptDdi->uValue.i = ptDdi->uValue.i + 1;
                        break;
                    case KR_TYPE_LONG:
                        ptDdi->uValue.l = ptDdi->uValue.l + 1;
                        break;
                    case KR_TYPE_DOUBLE:
                        ptDdi->uValue.d = ptDdi->uValue.d + 1;
                        break;
                    default:
                        KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", 
                                ptDdi->eValueType);
                        return -1;
                }
                break;
            case KR_DDI_METHOD_CON_INC:
                //TODO
                break;
            case KR_DDI_METHOD_CON_DEC:
                //TODO
                break;
            case KR_DDI_METHOD_CNT_DIS:
                //TODO
                break;
            default:
                KR_LOG(KR_LOGERROR, "Bad Method [%c]!", \
                       ptDdi->ptParamDDIDef->caStatisticsMethod[0]);
                return -1;    
        }
        */
        
        /*add this record to related*/
        kr_hashtable_insert(ptDdi->ptRelated, ptTravRec, ptTravRec);
        
        node = node->prev;
    }

    /* This is what the difference between DDI and DDI:
     * DDI only set once, while DDI still need to traversal all the list
     */
    ptDataItem->eValueInd = KR_VALUE_SETED;

    return 0;
}