/*
 * cm_errfilter_dbio.pgc - cm 모듈 DB 접근 함수 (cm_errfilter_dbio), archived into libcmdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 전체 대비 오류(E) 로그 비율(bps) - FILTER 절.
 */
#include <string.h>
#include <userlog.h>
#include "cm_errfilter_dbio.h"

long cmdb_log_err_filter(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_err = 0, h_tot = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) FILTER (WHERE log_level = 'E'), count(*)
        INTO :h_err, :h_tot FROM cm_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_tot <= 0) return 0;
    return h_err * 10000 / h_tot;
}
