/*
 * rc_lvlfilter_dbio.pgc - 경보 레벨 FILTER 집계 (rc_lvlfilter_dbio), librcdbio.a.
 * ECPG; count(*) FILTER (WHERE lvl=...) 로 WARN/CRIT 경보 수를 한 번에 집계.
 */
#include <string.h>
#include <userlog.h>
#include "rc_lvlfilter_dbio.h"

long rcdb_alert_levels(const char *bizdate, long *crit_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_warn = 0, h_crit = 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 lvl = 'WARN'), count(*) FILTER (WHERE lvl = 'CRIT')
        INTO :h_warn, :h_crit FROM rc_alert WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("rcdb_alert_levels FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    if (crit_out) *crit_out = h_crit;
    return h_warn;
}
