Files
svc.ems/src/accountMgr/c_program/cdrCollector/cdrdb.c
2025-01-22 21:53:19 +08:00

1538 lines
45 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "cdr.h"
#include "../../../omcLib/c_program/ftp/ftp.h"
#include <pwd.h>
#include <grp.h>
extern cdr_sys cdr_sys_set[MAX_SYS_NUM];
extern int cdr_sys_num;
extern cdr_src cdr_src_set[MAX_SRC_NUM];
extern int cdr_src_num;
extern void cdr_log(const char *fmt, ...);
#define CDR_ALARM_FLAG 0
#define MNG_STATE_IDLE 0
#define MNG_STATE_PREPARE 1
#define MNG_STATE_GENERATE_CSV 2
#define MNG_STATE_CHECK_CSV 3
#define MNG_STATE_GET_SYS 4
static char mng_sqlstr[8192];
#define MNG_CLEAR_STATE_IDLE 0
#define MNG_CLEAR_STATE_PREPARE 1
#define MNG_CLEAR_STATE_GET_TABLE 2
#define MNG_CLEAR_STATE_CLEAR_TABLE 3
typedef struct _cdr_file_mng
{
int time_instance;
int cur_generate_timeout;
int generate_timeout; //4 -> 60 minutes generate a file
//2 -> 30 minutes generate a file
//1 -> 15 minutes generate a file
char sending_dir[128];
// Zed baicells requirement by simon --
char dailycdr_sending_dir[128];
// ---end
int backup_flag;
// Zed baicells requirement by simon --
int dailycdr_backup_flag;
// ---end
int state;
unsigned long prepare_time;
unsigned long start_time;
int cdr_src_index;
/* Fixed for Norfolk EMS CDR lost issue by simon
* Store second number when time to be changed
* --begin
*/
int tc_sec;
/* --end
*
*/
char last_create_file[256];
}_cdr_file_mng;
_cdr_file_mng cdr_file_mng;
typedef struct _table_mng
{
int state;
int table_count;
int cur_table_index;
char table_name[512][32];
unsigned long prepare_time;
unsigned long start_time;
}_table_mng;
_table_mng table_mng;
#define MAX_SMS_NOTE_SYS 32
typedef struct _sms_note_sys
{
int sysTypeNo;
char sysTypeName[32];
int need_alarm;
int need_sms;
int msisdn_count;
char msisdn[4][32];
}_sms_note_sys;
#define ONE_DAY_SECONDS (24*60*60)
#define ONE_HOUR_SECONDS (60*60)
#define ONE_QUARTER_SECONDS (15*60)
#define ONE_MIN_SECONDS 60
#define HALF_MIN_SECONDS 30
#define TEN_SECONDS 10
#define TWO_SECONDS 2
static unsigned long day_countdown;
static unsigned long hour_countdown;
static unsigned long quarter_countdown;
static unsigned long minute_countdown;
static unsigned long half_minute_countdown;
static unsigned long tensec_countdown;
static unsigned long twosec_countdown;
static unsigned long prev_time;
static unsigned long next_time;
unsigned long current_time=0;
void cdrdb_init();
void cdrdb_timer();
extern void insertSeriousAlarmLog(int compCode,int alarmCode);
int check_csv_file(char *fileName);
void read_sms_conf();
int insert_sm(int index,char *fileName);
_sms_note_sys sms_note_sys[MAX_SMS_NOTE_SYS];
int sms_note_sys_count=0;
void cdr_clock_init();
unsigned long cdr_clock_timer();
void get_parameter();
void create_csv(cdr_src *psrc);
void create_csv_tc(cdr_src *psrc);
void create_dat_for_wxc1(cdr_src *psrc);
int get_time_condition(char *time_condition,char *time_field,char *time_table);
int get_time_condition_tc(char *time_condition,char *time_field,char *time_table);
void get_tables();
void clear_table(char *table_name);
void read_sms_conf()
{
char sqlstr[256];
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL *dbConn;
char *pstr;
cdr_log("[INFO][read_sms_conf]: Read the SMS conf");
dbConn=mysql_conn("localhost","OMC_PUB");
if(dbConn == NULL)
cdr_log("[ERR][read_sms_conf]:mysql_conn OMC_PUB\n");
//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>cdr_store_server<65><72>ֵ
sprintf(sqlstr,"select sysTypeNo,sysTypeName,need_alarm,need_sms,sms_receiver FROM OMC_PUB.cdrNotifyConf;");
res=mysql_getres(dbConn,sqlstr);
if(res == NULL)
{
cdr_log("[ERR %d][read_sms_conf]:%s\n",mysql_errno(dbConn),sqlstr);
mysql_close(dbConn);
return;
}
memset(sms_note_sys,0,sizeof(sms_note_sys));
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
sms_note_sys_count=0;
while((row = mysql_fetch_row(res)) != NULL)
{
sms_note_sys[sms_note_sys_count].sysTypeNo=atoi(row[0]);
sprintf(sms_note_sys[sms_note_sys_count].sysTypeName,"%s",row[1]);
sms_note_sys[sms_note_sys_count].need_alarm=atoi(row[2]);
sms_note_sys[sms_note_sys_count].need_sms=atoi(row[3]);
sms_note_sys[sms_note_sys_count].msisdn_count=0;
if(sms_note_sys[sms_note_sys_count].need_sms)
{
pstr=strtok(row[4],",");
sprintf(sms_note_sys[sms_note_sys_count].msisdn[sms_note_sys[sms_note_sys_count].msisdn_count],"%s",pstr);
sms_note_sys[sms_note_sys_count].msisdn_count++;
while((pstr=strtok(NULL,",")))
{
sprintf(sms_note_sys[sms_note_sys_count].msisdn[sms_note_sys[sms_note_sys_count].msisdn_count],"%s",pstr);
sms_note_sys[sms_note_sys_count].msisdn_count++;
}
}
sms_note_sys_count++;
}
mysql_free_result(res);
mysql_close(dbConn);
}
int insert_sm(int index,char *fileName)
{
char sqlstr[512];
MYSQL *dbConn;
char content[256];
int i;
dbConn=mysql_conn("localhost","OMC_PUB");
if(dbConn == NULL)
{
cdr_log("[ERR][insert_sm]:mysql_conn OMC_PUB\n");
return 0;
}
for(i=0;i<sms_note_sys[index].msisdn_count;i++)
{
sprintf(content,"EMS generates empty CDR file %s",fileName);
sprintf(sqlstr,"insert into OMC_PUB.sendSMSList(sender,receiver,content,issue_time,send_time)values('EMS','%s','%s',FROM_UNIXTIME(%ld),0)",sms_note_sys[index].msisdn[i],content,time(NULL));
if(mysql_getnores(dbConn,sqlstr) != 0)
cdr_log("mysql fail,%s",sqlstr);
}
mysql_close(dbConn);
return 1;
}
int check_csv_file(char *fileName)
{
char checkstr[64];
int index;
struct stat fileStat;
char fullfilename[128];
sprintf(fullfilename,"%s",fileName);
cdr_log("###check csv file %s\n",fullfilename);
for(index=0;index<sms_note_sys_count;index++)
{
sprintf(checkstr,"cdrFrom%s",sms_note_sys[index].sysTypeName);
if(strstr(fileName,checkstr))
break;
}
if(index == sms_note_sys_count)
{
cdr_log("###check csv file %s, not configure\n",fullfilename);
return 0;
}
if(sms_note_sys[index].need_alarm || sms_note_sys[index].need_sms)
{
stat(fullfilename,&fileStat);
}
//printf("%s sms_note_sys[%d].need_alarm=%d,need_sm=%d,%s\n",sms_note_sys[index].sysTypeName,index,sms_note_sys[index].need_alarm,sms_note_sys[index].need_sms,fileName);
if(fileStat.st_size == 0)
{
if(sms_note_sys[index].need_alarm)
{
cdr_log("###csv file %s is empty,insert alarm\n",fileName);
switch(sms_note_sys[index].sysTypeNo)
{
case 320:
insertSeriousAlarmLog(3,1);break;
case 330:
insertSeriousAlarmLog(3,2);break;
case 325:
insertSeriousAlarmLog(3,3);break;
case 360:
insertSeriousAlarmLog(3,4);break;
case 350:
insertSeriousAlarmLog(3,5);break;
case 375:
insertSeriousAlarmLog(3,6);break;
case 380:
insertSeriousAlarmLog(3,9);break;
case 386:
insertSeriousAlarmLog(3,10);break;
default:
insertSeriousAlarmLog(3,99);break;
}
}
if(sms_note_sys[index].need_sms)
{
cdr_log("###csv file %s is empty,insert sm\n",fileName);
insert_sm(index,fullfilename);
}
}
return 1;
}
void get_parameter()
{
MYSQL *pubConn;
MYSQL_RES *res;
MYSQL_ROW row;
char sqlstr[256];
int value;
memset(&cdr_file_mng,0,sizeof(_cdr_file_mng));
pubConn=mysql_conn("localhost","OMC_PUB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][get_parameter]:Can not connect to OMC_PUB",mysql_errno(pubConn));
return;
}
sprintf(sqlstr,"select pubVarValue from OMC_PUB.omcPubVarConf where pubVarName='GenerateCDRFileInterval';");
res=mysql_getres(pubConn,sqlstr);
if(res == NULL)
{
cdr_log("[ERR %d][get_parameter]:%s\n",mysql_errno(pubConn),sqlstr);
mysql_close(pubConn);
return;
}
row=mysql_fetch_row(res);
if(row == NULL)
{
cdr_log("[ERR %d][get_parameter]:no row1\n",mysql_errno(pubConn));
mysql_free_result(res);
cdr_file_mng.generate_timeout=4;
//mysql_close(pubConn);
//return;
}
else
{
value=atoi(row[0]);
if(value == 15)
{
cdr_file_mng.generate_timeout=1;
}
else if(value == 30)
{
cdr_file_mng.generate_timeout=2;
}
else if(value == 60)
{
cdr_file_mng.generate_timeout=4;
}
else
{
cdr_file_mng.generate_timeout=4;
cdr_log("wrong value for Interval of genrating CDR CSV");
}
mysql_free_result(res);
}
//get cdr_store_server
sprintf(sqlstr,"SELECT pubVarValue FROM OMC_PUB.omcPubVarConf WHERE pubVarName='cdr_store_server'");
res=mysql_getres(pubConn,sqlstr);
if(res == NULL)
{
cdr_log("[ERR %d][get_parameter]:%s\n",mysql_errno(pubConn),sqlstr);
mysql_close(pubConn);
return;
}
row=mysql_fetch_row(res);
if(row == NULL)
{
cdr_log("[ERR %d][get_parameter]:no row2\n",mysql_errno(pubConn));
mysql_free_result(res);
mysql_close(pubConn);
return;
}
if(strlen(row[0]) == 0)
cdr_file_mng.backup_flag=0;
else
cdr_file_mng.backup_flag=1;
// Zed baicells daily cdr store, by simon ---
// get dailycdr_store_server
sprintf(sqlstr, "SELECT pubVarValue FROM OMC_PUB.omcPubVarConf WHERE pubVarName='dailycdr_store_server'");
res = mysql_getres(pubConn, sqlstr);
if (res == NULL)
{
cdr_log("[ERR %d][get_parameter]:%s\n", mysql_errno(pubConn), sqlstr);
mysql_close(pubConn);
return;
}
row = mysql_fetch_row(res);
if (row == NULL)
{
cdr_log("[ERR %d][get_parameter]:no row2\n", mysql_errno(pubConn));
mysql_free_result(res);
mysql_close(pubConn);
return;
}
if (strlen(row[0]) == 0)
cdr_file_mng.dailycdr_backup_flag = 0;
else
cdr_file_mng.dailycdr_backup_flag = 1;
// ---end
mysql_free_result(res);
mysql_close(pubConn);
//sending_dir
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][get_parameter]:Can not connect to CDR_DB",mysql_errno(pubConn));
return;
}
sprintf(sqlstr,"SELECT pubVarValue FROM CDR_DB.cdrPubVarConf WHERE pubVarName='sending_directory';");
res=mysql_getres(pubConn,sqlstr);
if(res == NULL)
{
cdr_log("[ERR %d][get_parameter]:%s\n",mysql_errno(pubConn),sqlstr);
mysql_close(pubConn);
return;
}
row=mysql_fetch_row(res);
if(row == NULL)
{
cdr_log("[ERR %d][get_parameter]:no row3\n",mysql_errno(pubConn));
mysql_free_result(res);
mysql_close(pubConn);
}
strcpy(cdr_file_mng.sending_dir,row[0]);
mysql_free_result(res);
mysql_close(pubConn);
// sending_dir
pubConn = mysql_conn("localhost", "CDR_DB");
if (pubConn == NULL)
{
cdr_log("[ERR %d][get_parameter]:Can not connect to CDR_DB", mysql_errno(pubConn));
return;
}
// Zed requirement CDR daily merge --
sprintf(sqlstr, "SELECT pubVarValue FROM CDR_DB.cdrPubVarConf WHERE pubVarName='dailycdr_sending_directory';");
res = mysql_getres(pubConn, sqlstr);
if (res == NULL)
{
cdr_log("[ERR %d][get_parameter]:%s\n", mysql_errno(pubConn), sqlstr);
mysql_close(pubConn);
return;
}
row = mysql_fetch_row(res);
if (row == NULL)
{
cdr_log("[ERR %d][get_parameter]:no row\n", mysql_errno(pubConn));
mysql_free_result(res);
mysql_close(pubConn);
return;
}
strcpy(cdr_file_mng.dailycdr_sending_dir, row[0]);
// ---end
mysql_free_result(res);
mysql_close(pubConn);
}
void cdrdb_init()
{
get_parameter();
cdr_clock_init(0);
#if CDR_ALARM_FLAG
read_sms_conf();
#endif
cdr_log("[INFO][cdrdb_init]:time_instance=%d",cdr_file_mng.time_instance);
cdr_log("[INFO][cdrdb_init]:cur_generate_timeout=%d",cdr_file_mng.cur_generate_timeout);
cdr_log("[INFO][cdrdb_init]:generate_timeout=%d",cdr_file_mng.generate_timeout);
cdr_log("[INFO][cdrdb_init]:cdrsending_dir=%s",cdr_file_mng.sending_dir);
cdr_log("[INFO][cdrdb_init]:dailycdr_sending_dir=%s", cdr_file_mng.dailycdr_sending_dir);
cdr_log("[INFO][cdrdb_init]:backup_flag=%d",cdr_file_mng.backup_flag);
cdr_log("[INFO][cdrdb_init]:dailycdr_backup_flag=%d", cdr_file_mng.dailycdr_backup_flag);
}
void cdrdb_timer()
{
unsigned long time_past;
time_past=cdr_clock_timer();
switch(cdr_file_mng.state)
{
case MNG_STATE_IDLE:
break;
case MNG_STATE_PREPARE:
cdr_file_mng.start_time+=time_past;
if(cdr_file_mng.start_time - cdr_file_mng.prepare_time > 5*60)
{
cdr_file_mng.cdr_src_index=0;
cdr_file_mng.state=MNG_STATE_GENERATE_CSV;
cdr_log("[INFO][cdrdb_timer]:start to create csv file\n");
}
break;
case MNG_STATE_GENERATE_CSV:
//TC Modified
//printf("cdr_file_mng.cdr_src_index=%d,sysTypeNo=%d,version=%d\n",cdr_file_mng.cdr_src_index,cdr_src_set[cdr_file_mng.cdr_src_index].sysTypeNo,cdr_src_set[cdr_file_mng.cdr_src_index].version);
if(cdr_src_set[cdr_file_mng.cdr_src_index].version == 1 && cdr_src_set[cdr_file_mng.cdr_src_index].sysTypeNo == 320){
create_dat_for_wxc1(&cdr_src_set[cdr_file_mng.cdr_src_index]);
//printf("sysTypeNo=%d\n",cdr_src_set[cdr_file_mng.cdr_src_index].sysTypeNo);
}
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
else
{
create_csv(&cdr_src_set[cdr_file_mng.cdr_src_index]);
if (cdr_file_mng.tc_sec >= ((1*60*60) - (5*60)) )
{
create_csv_tc(&cdr_src_set[cdr_file_mng.cdr_src_index]);
}
// for Zeb baicell daily CDR ---
if (cdr_src_set[cdr_file_mng.cdr_src_index].version == 3 && cdr_src_set[cdr_file_mng.cdr_src_index].sysTypeNo == 320)
{
create_dailycdr_csv(&cdr_src_set[cdr_file_mng.cdr_src_index]);
}
// ---end
}
/* --End
*
*/
cdr_file_mng.state = MNG_STATE_CHECK_CSV;
break;
case MNG_STATE_CHECK_CSV:
#if CDR_ALARM_FLAG == 1
check_csv_file(cdr_file_mng.last_create_file);
#endif
cdr_file_mng.state=MNG_STATE_GET_SYS;
break;
case MNG_STATE_GET_SYS:
cdr_file_mng.cdr_src_index++;
if(cdr_file_mng.cdr_src_index == cdr_src_num)
{
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
cdr_file_mng.tc_sec = 0;
/* --End
*
*/
cdr_file_mng.state=MNG_STATE_IDLE;
}
else
{
cdr_file_mng.state=MNG_STATE_GENERATE_CSV;
}
break;
default:
cdr_file_mng.state=MNG_STATE_IDLE;
break;
}
switch(table_mng.state)
{
case MNG_CLEAR_STATE_IDLE:
break;
case MNG_CLEAR_STATE_PREPARE:
table_mng.start_time+=time_past;
if(table_mng.start_time - table_mng.prepare_time > 300)
{
table_mng.state=MNG_CLEAR_STATE_GET_TABLE;
cdr_log("[INFO][cdrdb_timer]:start to clear table\n");
}
break;
case MNG_CLEAR_STATE_GET_TABLE:
get_tables();
table_mng.state=MNG_CLEAR_STATE_CLEAR_TABLE;
break;
case MNG_CLEAR_STATE_CLEAR_TABLE:
if(table_mng.cur_table_index == table_mng.table_count)
{
table_mng.state=MNG_CLEAR_STATE_IDLE;
break;
}
clear_table(table_mng.table_name[table_mng.cur_table_index]);
table_mng.cur_table_index++;
break;
default:
table_mng.state=MNG_CLEAR_STATE_IDLE;
break;
}
}
void cdr_clock_init(int tc_sec)
{
time_t l_time;
struct tm *t;
int hour,minute,second;
l_time=time(NULL);
t=localtime(&l_time);
hour=t->tm_hour;
minute=t->tm_min;
second=t->tm_sec;
day_countdown=hour*ONE_HOUR_SECONDS + minute*ONE_MIN_SECONDS + second;
hour_countdown=minute*ONE_MIN_SECONDS + second;
quarter_countdown=(minute*ONE_MIN_SECONDS + second)%ONE_QUARTER_SECONDS;
minute_countdown=second;
half_minute_countdown=second%HALF_MIN_SECONDS;
tensec_countdown=second%TEN_SECONDS;
twosec_countdown=second%TWO_SECONDS;
current_time=prev_time=next_time=l_time;
cdr_file_mng.time_instance=hour*4 + minute/15;
cdr_file_mng.cur_generate_timeout=minute/15;
cdr_file_mng.cur_generate_timeout%=cdr_file_mng.generate_timeout;
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
cdr_file_mng.tc_sec = tc_sec;
/* --End
*
*/
cdr_log("[INFO][cdr_clock_init]:time_instance=%d",cdr_file_mng.time_instance);
cdr_log("[INFO][cdr_clock_init]:cur_generate_timeout=%d",cdr_file_mng.cur_generate_timeout);
cdr_log("[INFO][cdr_clock_init]:generate_timeout=%d",cdr_file_mng.generate_timeout);
cdr_log("[INFO][cdr_clock_init]:tc_sec=%d",cdr_file_mng.tc_sec);
}
unsigned long cdr_clock_timer()
{
struct tm *t;
unsigned long time_past,ret_past;
next_time=time(NULL);
current_time=next_time;
t=localtime((time_t*)&next_time);
if(prev_time > next_time)
{
int hour,minute,second;
time_t l_time;
cdr_log("[WARN][cdr_clock_timer]:system time has been changed");
l_time=prev_time=next_time=time(NULL);
t=localtime(&l_time);
hour=t->tm_hour;
minute=t->tm_min;
second=t->tm_sec;
day_countdown=hour*ONE_HOUR_SECONDS + minute*ONE_MIN_SECONDS + second;
hour_countdown=minute*ONE_MIN_SECONDS + second;
quarter_countdown=(minute*ONE_MIN_SECONDS + second)%ONE_QUARTER_SECONDS;
minute_countdown=second;
half_minute_countdown=second%HALF_MIN_SECONDS;
tensec_countdown=second%TEN_SECONDS;
twosec_countdown=second%TWO_SECONDS;
}
time_past=next_time-prev_time;
if(time_past < 0)
time_past=0;
ret_past=time_past;
day_countdown+=time_past;
hour_countdown+=time_past;
quarter_countdown+=time_past;
minute_countdown+=time_past;
half_minute_countdown+=time_past;
tensec_countdown+=time_past;
twosec_countdown+=time_past;
if(day_countdown >= ONE_DAY_SECONDS)
{
time_past=day_countdown % ONE_DAY_SECONDS;
day_countdown=time_past;
//call the day clock function here
}
if(hour_countdown >= ONE_HOUR_SECONDS)
{
time_past=hour_countdown % ONE_HOUR_SECONDS;
hour_countdown=time_past;
//call the hour clock function here
table_mng.state=MNG_CLEAR_STATE_PREPARE;
table_mng.prepare_time=time(NULL);
table_mng.start_time=table_mng.prepare_time;
cdr_log("[INFO][cdr_clock_timer]:prepare to clear table\n");
}
if(quarter_countdown >= ONE_QUARTER_SECONDS)
{
time_past=quarter_countdown % ONE_QUARTER_SECONDS;
quarter_countdown=time_past;
//call the quarter clock function here
cdr_file_mng.time_instance++;
cdr_file_mng.time_instance%=96;
cdr_file_mng.cur_generate_timeout++;
if(cdr_file_mng.cur_generate_timeout == cdr_file_mng.generate_timeout)
{
cdr_file_mng.cur_generate_timeout=0;
cdr_file_mng.state=MNG_STATE_PREPARE;
cdr_file_mng.prepare_time=time(NULL);
cdr_file_mng.start_time=cdr_file_mng.prepare_time;
cdr_log("[INFO][cdr_clock_timer]:prepare to create csv file\n");
}
}
if(minute_countdown >= ONE_MIN_SECONDS)
{
time_past=minute_countdown % ONE_MIN_SECONDS;
minute_countdown=time_past;
//call the minute clock function here
}
if(half_minute_countdown >= HALF_MIN_SECONDS)
{
time_past=half_minute_countdown % HALF_MIN_SECONDS;
half_minute_countdown=time_past;
//call the half-minute clock function here
}
if(tensec_countdown >= TEN_SECONDS)
{
time_past=tensec_countdown % TEN_SECONDS;
tensec_countdown=time_past;
//call the ten-seconds clock function here
}
if(twosec_countdown >= TWO_SECONDS)
{
time_past=twosec_countdown % TWO_SECONDS;
twosec_countdown=time_past;
//call the two-seconds clock function here
}
prev_time=next_time;
return ret_past;
}
int get_time_condition(char *time_condition,char *time_field,char *time_table)
{
int start_sec;
int end_sec;
int start_instance;
int end_instance;
char end_time_str[64];
char start_time_str[64];
struct tm *end_t,*start_t;
unsigned long start_time;
end_t=localtime((time_t*)&current_time);
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
int end_year = end_t->tm_year+1900;
int end_mon = end_t->tm_mon + 1;
int end_day = end_t->tm_mday;
int local_time_hour = end_t->tm_hour;
/* --End
*
*/
end_instance=cdr_file_mng.time_instance;
end_sec=end_instance * 15;
cdr_log("[DEBUG][get_time_condition]:end_instance=%02d,end_sec=%02d",end_instance,end_sec);
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
int state_time_end_hour = end_sec/60;
int state_time_start_hour = 0;
cdr_log("[DEBUG][get_time_condition]:state_time_end_hour=%02d,local_time_hour=%02d",state_time_end_hour,local_time_hour);
if ( state_time_end_hour != local_time_hour)
{
cdr_log("[ERROR][get_time_condition]:error for cdr table target! state_time_start_hour=%02d, local_time_hour=%02d", state_time_end_hour, local_time_hour);
state_time_end_hour = local_time_hour ;
}
sprintf(end_time_str,"%04d-%02d-%02d %02d:%02d:00",end_year,end_mon,end_day,state_time_end_hour,end_sec%60);
/* --End
*
*/
start_instance=cdr_file_mng.time_instance - cdr_file_mng.generate_timeout;
start_sec=start_instance * 15;
cdr_log("[DEBUG][get_time_condition]:cdr_file_mng.generate_timeout=%d,start_sec=%d",cdr_file_mng.generate_timeout,start_sec);
if(start_instance < 0)
{
start_instance+=96;
start_sec=start_instance * 15;
start_time=current_time - 12*60*60;
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
struct tm start_tb;
start_t=localtime_r((time_t*)&start_time, &start_tb);
state_time_start_hour = start_sec/60;
cdr_log("[DEBUG][get_time_condition]:state_time_start_hour=%02d,local_time_hour=%02d",state_time_start_hour, local_time_hour);
if ( ((state_time_start_hour + 1) % 24) != local_time_hour)
{
cdr_log("[ERROR][get_time_condition]:error for cdr table target!state_time_start_hour=%02d, local_time_hour=%02d", state_time_start_hour, local_time_hour);
state_time_start_hour = (local_time_hour == 0) ? 23 : (local_time_hour - 1);
}
sprintf(start_time_str,"%04d-%02d-%02d %02d:%02d:00",start_t->tm_year+1900,start_t->tm_mon + 1,start_t->tm_mday,state_time_start_hour,start_sec%60);
//when create csv file per hour, keep the old filename
if(cdr_file_mng.generate_timeout == 4)
sprintf(time_table,"%04d_%02d_%02d_%02d",start_t->tm_year+1900,start_t->tm_mon + 1,start_t->tm_mday,state_time_start_hour);
else
sprintf(time_table,"%04d_%02d_%02d_%02d_%02d",start_t->tm_year+1900,start_t->tm_mon + 1,start_t->tm_mday,state_time_start_hour,start_sec%60);
cdr_log("[DEBUG][get_time_condition]:time_table=%s.",time_table);
/* --End
*
*/
}
else
{
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
state_time_start_hour = start_sec/60;
cdr_log("[DEBUG][get_time_condition]:state_time_start_hour=%02d,local_time_hour=%02d",state_time_start_hour,local_time_hour);
if ( ((state_time_start_hour + 1) % 24) != local_time_hour)
{
cdr_log("[ERROR][get_time_condition]:error for cdr table target!state_time_start_hour=%02d, local_time_hour=%02d", state_time_start_hour, local_time_hour);
state_time_start_hour = (local_time_hour == 0) ? 23 : (local_time_hour - 1);
}
sprintf(start_time_str,"%04d-%02d-%02d %02d:%02d:00",end_t->tm_year+1900,end_t->tm_mon + 1,end_t->tm_mday,state_time_start_hour,start_sec%60);
//when create csv file per hour, keep the old filename
if(cdr_file_mng.generate_timeout == 4)
sprintf(time_table,"%04d_%02d_%02d_%02d",end_year,end_mon,end_day,state_time_start_hour);
else
sprintf(time_table,"%04d_%02d_%02d_%02d_%02d",end_year,end_mon,end_day,state_time_start_hour,start_sec%60);
cdr_log("[DEBUG][get_time_condition]:time_table=%s",time_table);
/* --End
*
*/
}
sprintf(time_condition,"%s >= '%s' and %s < '%s'",time_field,start_time_str,time_field,end_time_str);
cdr_log("[DEBUG][get_time_condition]:time_condition=%s",time_condition);
return state_time_start_hour;
}
/* Fixed for Norfolk EMS CDR lost issue by simon
* --Begin
*/
int get_time_condition_tc(char *time_condition,char *time_field,char *time_table)
{
int start_sec;
int end_sec;
int start_instance;
int end_instance;
char end_time_str[64];
char start_time_str[64];
struct tm *end_t,*start_t;
struct tm time_tm;
unsigned long start_time;
unsigned long pre_time = current_time - cdr_file_mng.tc_sec + (60 *60);
end_t=localtime_r((time_t*)&pre_time, &time_tm);
int tc_instance = cdr_file_mng.time_instance - (((cdr_file_mng.tc_sec + 5 * 60) / (60*60)) * 4);
end_instance = tc_instance;
end_sec=end_instance * 15;
int end_year = end_t->tm_year+1900;
int end_mon = end_t->tm_mon + 1;
int end_day = end_t->tm_mday;
int end_hour = end_t->tm_hour;
int state_time_end_hour = end_sec/60;
int state_time_start_hour = 0;
cdr_log("[DEBUG][get_time_condition_tc]:pre_time=%d, tc_sec=%d, tc_instance=%d,end_hour=%2d", pre_time, cdr_file_mng.tc_sec, tc_instance, end_hour);
cdr_log("[DEBUG][get_time_condition_tc]:end_instance=%02d,end_sec=%02d, state_time_end_hour=%02d",end_instance,end_sec, state_time_end_hour);
if (state_time_end_hour < 0)
{
state_time_end_hour += 24;
}
sprintf(end_time_str,"%04d-%02d-%02d %02d:%02d:00",end_year,end_mon,end_day,state_time_end_hour,end_sec%60);
start_instance=tc_instance - cdr_file_mng.generate_timeout;
start_sec=start_instance * 15;
cdr_log("[DEBUG][get_time_condition_tc]:cdr_file_mng.generate_timeout=%d,start_sec=%d",cdr_file_mng.generate_timeout,start_sec);
if(start_instance < 0)
{
start_instance+=96;
start_sec=start_instance * 15;
start_time=pre_time - 12*60*60;
struct tm start_tb;
start_t=localtime_r((time_t*)&start_time, &start_tb);
state_time_start_hour = start_sec/60;
cdr_log("[DEBUG][get_time_condition_tc]:state_time_start_hour=%02d",state_time_start_hour);
if ( ((state_time_start_hour + 1) % 24) != state_time_end_hour)
{
cdr_log("[ERROR][get_time_condition_tc]:error for cdr table target!state_time_start_hour=%02d, state_time_end_hour=%02d", state_time_start_hour, state_time_end_hour);
state_time_start_hour = (state_time_end_hour == 0) ? 23 : (state_time_end_hour - 1);
}
sprintf(start_time_str,"%04d-%02d-%02d %02d:%02d:00",start_t->tm_year+1900,start_t->tm_mon + 1,start_t->tm_mday,state_time_start_hour,start_sec%60);
//when create csv file per hour, keep the old filename
if(cdr_file_mng.generate_timeout == 4)
sprintf(time_table,"%04d_%02d_%02d_%02d",start_t->tm_year+1900,start_t->tm_mon + 1,start_t->tm_mday,state_time_start_hour);
else
sprintf(time_table,"%04d_%02d_%02d_%02d_%02d",start_t->tm_year+1900,start_t->tm_mon + 1,start_t->tm_mday,state_time_start_hour,start_sec%60);
cdr_log("[DEBUG][get_time_condition_tc]:time_table=%s.",time_table);
}
else
{
state_time_start_hour = start_sec/60;
cdr_log("[DEBUG][get_time_condition_tc]:state_time_start_hour=%02d",state_time_start_hour);
if ( ((state_time_start_hour + 1) % 24) != state_time_end_hour)
{
cdr_log("[ERROR][get_time_condition_tc]:error for cdr table target!state_time_start_hour=%02d, state_time_end_hour=%02d", state_time_start_hour, state_time_end_hour);
state_time_start_hour = (state_time_end_hour == 0) ? 23 : (state_time_end_hour - 1);
}
sprintf(start_time_str,"%04d-%02d-%02d %02d:%02d:00",end_t->tm_year+1900,end_t->tm_mon + 1,end_t->tm_mday,state_time_start_hour,start_sec%60);
//when create csv file per hour, keep the old filename
if(cdr_file_mng.generate_timeout == 4)
sprintf(time_table,"%04d_%02d_%02d_%02d",end_year,end_mon,end_day,state_time_start_hour);
else
sprintf(time_table,"%04d_%02d_%02d_%02d_%02d",end_year,end_mon,end_day,state_time_start_hour,start_sec%60);
cdr_log("[DEBUG][get_time_condition_tc]:time_table=%s",time_table);
}
sprintf(time_condition,"%s >= '%s' and %s < '%s'",time_field,start_time_str,time_field,end_time_str);
cdr_log("[DEBUG][get_time_condition_tc]:time_condition=%s",time_condition);
return state_time_start_hour;
}
void create_csv_tc(cdr_src *psrc)
{
MYSQL *pubConn;
char fullfilename[128];
char tablename[64];
char time_table[64];
char time_cond[128];
char cmdstr[256];
struct tm *t;
int hour;
int create_success=0;
t=localtime((time_t*)&current_time);
hour=get_time_condition_tc(time_cond,"releaseTime",time_table);
sprintf(fullfilename,"%s/%s_%s.csv",psrc->csvPath,psrc->recordTable,time_table);
sprintf(tablename,"CDR_DB.%s_%02d",psrc->recordTable,hour);
cdr_log("[INFO][create_csv_tc]:prepare to create %s from %s at %02d:%02d:%02d\n",fullfilename,tablename,t->tm_hour,t->tm_min,t->tm_sec);
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][create_csv_tc]:Can not connect to CDR_DB\n",mysql_errno(pubConn));
return;
}
sprintf(mng_sqlstr,"select key_sys_id,instance%s,srcCode into outfile '%s' from %s where %s",psrc->define.outFieldList,fullfilename,tablename,time_cond);
if(mysql_getnores(pubConn,mng_sqlstr) != 0)
{
cdr_log("[ERR %d][create_csv_tc]:Create %s fail,SQL=%s\n",mysql_errno(pubConn),fullfilename,mng_sqlstr);
create_success=0;
}
else
{
cdr_log("[SQL][create_csv_tc]:%s\n",mng_sqlstr);
cdr_log("[INFO][create_csv_tc]:create csv file %s successfully,%s\n",fullfilename,mng_sqlstr);
sprintf(cmdstr,"chmod 644 %s",fullfilename);
system(cmdstr);
create_success=1;
}
sprintf(cdr_file_mng.last_create_file,"%s",fullfilename);
if(cdr_file_mng.backup_flag && create_success)
{
if(chdir(cdr_file_mng.sending_dir) == -1)
{
sprintf(cmdstr,"mkdir -p -m755 %s",cdr_file_mng.sending_dir);
cdr_log("[INFO][create_csv_tc]:%s\n", cmdstr);
system(cmdstr);
if(chdir(cdr_file_mng.sending_dir) == -1)
return;
}
sprintf(cmdstr,"cp -f %s %s/",fullfilename,cdr_file_mng.sending_dir);
system(cmdstr);
}
mysql_close(pubConn);
}
/* --End
*
*/
void create_csv(cdr_src *psrc)
{
MYSQL *pubConn;
char fullfilename[128];
char tablename[64];
char time_table[64];
char time_cond[128];
char cmdstr[256];
struct tm *t;
int hour;
int create_success=0;
t=localtime((time_t*)&current_time);
hour=get_time_condition(time_cond,"releaseTime",time_table);
sprintf(fullfilename,"%s/%s_%s.csv",psrc->csvPath,psrc->recordTable,time_table);
sprintf(tablename,"CDR_DB.%s_%02d",psrc->recordTable,hour);
cdr_log("[INFO][create_csv]:prepare to create %s from %s at %02d:%02d:%02d\n",fullfilename,tablename,t->tm_hour,t->tm_min,t->tm_sec);
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][create_csv]:Can not connect to CDR_DB\n",mysql_errno(pubConn));
return;
}
sprintf(mng_sqlstr,"select key_sys_id,instance%s,srcCode into outfile '%s' from %s where %s",psrc->define.outFieldList,fullfilename,tablename,time_cond);
if(mysql_getnores(pubConn,mng_sqlstr) != 0)
{
cdr_log("[ERR %d][create_csv]:Create %s fail,SQL=%s\n",mysql_errno(pubConn),fullfilename,mng_sqlstr);
create_success=0;
}
else
{
cdr_log("[SQL]:%s\n",mng_sqlstr);
cdr_log("[INFO][create_csv]:create csv file %s successfully,%s\n",fullfilename,mng_sqlstr);
sprintf(cmdstr,"chmod 644 %s",fullfilename);
system(cmdstr);
create_success=1;
}
sprintf(cdr_file_mng.last_create_file,"%s",fullfilename);
if(cdr_file_mng.backup_flag && create_success)
{
if(chdir(cdr_file_mng.sending_dir) == -1)
{
sprintf(cmdstr,"mkdir -p -m755 %s",cdr_file_mng.sending_dir);
cdr_log("[INFO][create_csv]:%s\n", cmdstr);
system(cmdstr);
if(chdir(cdr_file_mng.sending_dir) == -1)
return;
}
sprintf(cmdstr,"cp -f %s %s/",fullfilename,cdr_file_mng.sending_dir);
system(cmdstr);
}
mysql_close(pubConn);
}
void create_dat_for_wxc1(cdr_src *psrc)
{
MYSQL *pubConn;
MYSQL_RES *res;
MYSQL_ROW row;
char fullfilename[128];
char tablename[64];
char time_table[64];
char time_cond[128];
char str[512];
struct tm *t;
int hour;
FILE *handle;
int res_num,i,wcount;
int p_width[27]={8,18,23,23,23,23,23,11,11,16,7,7,22,23,23,23,15,9,7,23,7,7,7,23,23,11,9};
int y=0,m=0,d=0;
char *pchr;
t=localtime((time_t*)&current_time);
hour=get_time_condition(time_cond,"releaseTime",time_table);
sprintf(fullfilename,"%s/%s_%s.csv",psrc->csvPath,psrc->recordTable,time_table);
sprintf(tablename,"CDR_DB.%s_%02d",psrc->recordTable,hour);
cdr_log("[INFO][create_dat_for_wxc1]:prepare to create %s version-1 from %s at %02d:%02d:%02d\n",fullfilename,tablename,t->tm_hour,t->tm_min,t->tm_sec);
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][create_dat_for_wxc1]:Can not connect to CDR_DB\n",mysql_errno(pubConn));
return;
}
//TC modified
sprintf(mng_sqlstr,"select %s from %s where %s",psrc->define.outFieldList+1,tablename,time_cond);
res=mysql_getres(pubConn,mng_sqlstr);
if(res == NULL)
{
cdr_log("[ERR %d][create_dat_for_wxc1]:%s\n",mysql_errno(pubConn),mng_sqlstr);
mysql_close(pubConn);
return;
}
handle=fopen(fullfilename,"w");
if(handle == NULL)
{
cdr_log("[ERR][create_dat_for_wxc1]:create %s version-1 fail\n",fullfilename);
mysql_close(pubConn);
return;
}
res_num=mysql_num_rows(res);
fprintf(handle,"FILE NAME:%s NO. OF RECORDS:%-10d\n",fullfilename,res_num);
fprintf(handle,"START daily: %02d/%02d/%d START TIME: %02d:%02d:%02d\n\n",t->tm_mon+1,t->tm_mday,t->tm_year+1900,t->tm_hour,t->tm_min,t->tm_sec);
fprintf(handle,"Type Imsi Msisdn Msrn Calling # Called # RE In.TkGp Ou.TkGp Location BS SS Mob. ClassMark Seizure Time Answer Time Release Time Duration TCos SSA SS Parm. SSR CR RCU SM Delivery Time SC Address SM Rslt MsgRf \n\n");
while((row=mysql_fetch_row(res)) != NULL)
{
//not sms
if(atoi(row[0]) != 6 && atoi(row[0]) != 7)
{
////Duration=0 skip
if(row[16]==NULL || atoi(row[16])==0)
continue;
}
for(i=0;i<27;i++)
{
if(row[i]==NULL)
{
if(i == 7 || i == 8)
{
;//In.TkGp,Ou.TkGpr skip
}
else
{
fprintf(handle,"%-*s",p_width[i],"--");
continue;
}
}
switch(i)
{
case 0: // Type
case 10:// basicService
case 11:// supplServicesUsed
case 18:// SSA
case 20:// SSR
case 26:// MsgRf
sprintf(str,"%03d",atoi(row[i]));
break;
case 7:// In.TkGp
case 8:// Ou.TkGp
//sms
if(atoi(row[0]) == 6 || atoi(row[0]) == 7)
sprintf(str,"--");
else
sprintf(str,"00 00 00");
break;
case 13:// seizureTime
case 14:// answerTime
case 15:// releaseTime
case 23:// SmDeliveryTime
// 2001-06-07 11:12:22->
// 06/07/2001 11:12:22
if(i==15)
{
if(atoi(row[0]) == 6 || atoi(row[0]) == 7)
{
fprintf(handle,"%-*s",p_width[i],"--");
continue;
}
}
pchr=row[i];
y=atoi(pchr);
pchr=strchr(pchr,'-');
if(pchr==NULL)
{
sprintf(str,"%02d/%02d/%02d %s",m,d,y,pchr);
break;
}
pchr++;
m=atoi(pchr);
pchr=strchr(pchr,'-');
pchr++;
if(pchr==NULL)
{
sprintf(str,"%02d/%02d/%02d %s",m,d,y,pchr);
break;
}
d=atoi(pchr);
pchr=strchr(pchr,' ');
sprintf(str,"%02d/%02d/%02d %s",m,d,y,pchr);
break;
case 16://callDuration
{
int d,h,m,s,total;
total =atoi(row[i]);
d=total/86400;
h=(total%86400)/3600;
m=(total%3600)/60;
s=total%60;
sprintf(str,"%02d:%02d:%02d:%02d",d,h,m,s);
break;
}
//case 9: // location_mscNumber
//case 10:// location_mscNumber_2
case 17:// TCos
sprintf(str,"%05d",atoi(row[i]));
break;
case 21:// CR
sprintf(str,"%04d",atoi(row[i]));
break;
case 12:// Mob. ClassMark
sprintf(str,"1f %s",row[i]);
break;
default:
sprintf(str,"%s",row[i]);
break;
}
fprintf(handle,"%-*s",p_width[i],str);
}
fprintf(handle,"\n");
wcount++;
}
fseek(handle,0,SEEK_SET);
fprintf(handle,"FILE NAME: %s NO. OF RECORDS:%-10d\n",fullfilename,wcount);
mysql_free_result(res);
fclose(handle);
mysql_close(pubConn);
sprintf(cdr_file_mng.last_create_file,"%s",fullfilename);
sprintf(str,"chmod 644 %s",fullfilename);
system(str);
if(cdr_file_mng.backup_flag)
{
if(chdir(cdr_file_mng.sending_dir) == -1)
{
sprintf(str,"mkdir -p -m755 %s",cdr_file_mng.sending_dir);
cdr_log("[INFO][create_dat_for_wxc1]:%s\n",str);
system(str);
if(chdir(cdr_file_mng.sending_dir) == -1)
return;
}
sprintf(str,"cp -f %s %s/",fullfilename,cdr_file_mng.sending_dir);
system(str);
}
}
void get_tables()
{
MYSQL *pubConn;
MYSQL_RES *res;
MYSQL_ROW row;
int index;
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][get_tables]:Can not connect to CDR_DB",mysql_errno(pubConn));
return;
}
sprintf(mng_sqlstr,"show tables like 'cdrFrom%%\\___';");
res=mysql_getres(pubConn,mng_sqlstr);
if(res == NULL)
{
cdr_log("[ERR %d][get_tables]:%s\n",mysql_errno(pubConn),mng_sqlstr);
mysql_close(pubConn);
return;
}
table_mng.table_count=0;
table_mng.cur_table_index=0;
index=0;
while((row=mysql_fetch_row(res)))
{
sprintf(table_mng.table_name[index],"%s",row[0]);
index++;
}
table_mng.table_count=index;
mysql_free_result(res);
mysql_close(pubConn);
cdr_log("[INFO][get_tables]:Get clear table count=%d\n",table_mng.table_count);
}
void clear_table(char *table_name)
{
MYSQL *pubConn;
cdr_log("[INFO][clear_table]:Clear table %s\n",table_name);
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][clear_table]:Can not connect to CDR_DB",mysql_errno(pubConn));
return;
}
sprintf(mng_sqlstr,"delete from CDR_DB.%s WHERE TO_DAYS(releaseTime)<TO_DAYS(NOW());",table_name);
if(mysql_getnores(pubConn,mng_sqlstr) != 0)
cdr_log("[ERR %d][clear_table]:%s",mysql_errno(pubConn),mng_sqlstr);
else
cdr_log("[INFO][clear_table]:%s",mng_sqlstr);
//sprintf(mng_sqlstr,"repair table CDR_DB.%s",table_name);
//if(mysql_getnores(pubConn,mng_sqlstr) != 0)
// cdr_log("[ERR %d][clear_table]:%s\n",mysql_errno(pubConn),mng_sqlstr);
mysql_close(pubConn);
}
// Zed baicells CDR merge and split requirement, by simon ---
int get_dailycdr_time_condition(char *time_condition, char *time_field, char *time_table)
{
int start_sec;
int end_sec;
int start_instance;
int end_instance;
char end_time_str[64];
char start_time_str[64];
struct tm *end_t, *start_t;
unsigned long start_time;
end_t = localtime((time_t *)&current_time);
int end_year = end_t->tm_year + 1900;
int end_mon = end_t->tm_mon + 1;
int end_day = end_t->tm_mday;
int local_time_hour = end_t->tm_hour;
end_instance = cdr_file_mng.time_instance;
end_sec = end_instance * 15;
cdr_log("[DEBUG][get_time_condition]:end_instance=%02d,end_sec=%02d", end_instance, end_sec);
int state_time_end_hour = end_sec / 60;
int state_time_start_hour = 0;
cdr_log("[DEBUG][get_time_condition]:state_time_end_hour=%02d,local_time_hour=%02d", state_time_end_hour, local_time_hour);
if (state_time_end_hour != local_time_hour)
{
cdr_log("[ERROR][get_time_condition]:error for cdr table target! state_time_start_hour=%02d, local_time_hour=%02d", state_time_end_hour, local_time_hour);
state_time_end_hour = local_time_hour;
}
sprintf(end_time_str, "%04d-%02d-%02d 00:00:00", end_year, end_mon, end_day);
start_instance = cdr_file_mng.time_instance - cdr_file_mng.generate_timeout;
start_sec = start_instance * 15;
cdr_log("[DEBUG][get_time_condition]:cdr_file_mng.generate_timeout=%d,start_sec=%d", cdr_file_mng.generate_timeout, start_sec);
if (start_instance < 0)
{
start_instance += 96;
start_sec = start_instance * 15;
start_time = current_time - 12 * 60 * 60;
struct tm start_tb;
start_t = localtime_r((time_t *)&start_time, &start_tb);
state_time_start_hour = start_sec / 60;
cdr_log("[DEBUG][get_time_condition]:state_time_start_hour=%02d,local_time_hour=%02d", state_time_start_hour, local_time_hour);
if (((state_time_start_hour + 1) % 24) != local_time_hour)
{
cdr_log("[ERROR][get_time_condition]:error for cdr table target!state_time_start_hour=%02d, local_time_hour=%02d", state_time_start_hour, local_time_hour);
state_time_start_hour = (local_time_hour == 0) ? 23 : (local_time_hour - 1);
}
sprintf(start_time_str, "%04d-%02d-%02d 00:00:00", start_t->tm_year + 1900, start_t->tm_mon + 1, start_t->tm_mday);
sprintf(time_table, "%04d_%02d_%02d", start_t->tm_year + 1900, start_t->tm_mon + 1, start_t->tm_mday);
}
else
{
state_time_start_hour = start_sec / 60;
cdr_log("[DEBUG][get_time_condition]:state_time_start_hour=%02d,local_time_hour=%02d", state_time_start_hour, local_time_hour);
if (((state_time_start_hour + 1) % 24) != local_time_hour)
{
cdr_log("[ERROR][get_time_condition]:error for cdr table target!state_time_start_hour=%02d, local_time_hour=%02d", state_time_start_hour, local_time_hour);
state_time_start_hour = (local_time_hour == 0) ? 23 : (local_time_hour - 1);
}
sprintf(start_time_str, "%04d-%02d-%02d 00:00:00", end_t->tm_year + 1900, end_t->tm_mon + 1, end_t->tm_mday);
sprintf(time_table, "%04d_%02d_%02d", end_year, end_mon, end_day);
}
sprintf(time_condition, "%s >= '%s' and %s < '%s'", time_field, start_time_str, time_field, end_time_str);
cdr_log("[DEBUG][get_time_condition]:time_condition=%s", time_condition);
return state_time_start_hour;
}
void copy_to_dailycdr_table(cdr_src *psrc)
{
MYSQL *pubConn;
char tablename[64];
char time_table[64];
char time_cond[128];
char cmdstr[256];
struct tm *t;
int hour;
int create_success=0;
char dailytable[64];
t=localtime((time_t*)&current_time);
hour=get_time_condition(time_cond,"releaseTime",time_table);
strcpy(dailytable, psrc->recordTable);
sprintf(tablename,"CDR_DB.%s_%02d",psrc->recordTable,hour);
cdr_log("[INFO][copy_to_dailycdr_table]:prepare to copy from %s to %s at %02d:%02d:%02d\n", tablename, dailytable, t->tm_hour, t->tm_min, t->tm_sec);
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][copy_to_dailycdr_table]:Can not connect to CDR_DB\n", mysql_errno(pubConn));
return;
}
sprintf(mng_sqlstr, "INSERT INTO %s SELECT * FROM %s where %s", dailytable, tablename, time_cond);
if(mysql_getnores(pubConn,mng_sqlstr) != 0)
{
cdr_log("[ERR %d][copy_to_dailycdr_table]:SQL=%s\n", mysql_errno(pubConn), mng_sqlstr);
create_success=0;
}
else
{
cdr_log("[SQL]:%s\n",mng_sqlstr);
cdr_log("[INFO][copy_to_dailycdr_table]:copy table %s successfully,%s\n", dailytable, mng_sqlstr);
create_success=1;
}
mysql_close(pubConn);
}
void create_dailycdr_csv(cdr_src *psrc)
{
MYSQL *pubConn;
char voice_filename[128];
char sms_filename[128];
char tablename[64];
char time_table[64];
char time_cond[128];
char cmdstr[256];
struct tm *t;
int hour;
int voice_create_success=0;
int sms_create_success=0;
char calltype_condition[128];
t=localtime((time_t*)&current_time);
hour = get_dailycdr_time_condition(time_cond, "releaseTime", time_table);
copy_to_dailycdr_table(psrc);
// the last hour of one day
if (hour == 23)
{
char dailycdr_csv_path[128];
// judge directory cdrdaily
struct stat st = {0};
sprintf(dailycdr_csv_path, "%sdaily", psrc->csvPath);
// if the dir not exist
if (stat(dailycdr_csv_path, &st) == -1)
{
// not found dir
if (mkdir(dailycdr_csv_path, 0770) != 0)
{
cdr_log("[ERROR][create_dailycdr_csv]:failed to make directory %s\n", dailycdr_csv_path);
return;
}
// <20><>ȡ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID
struct passwd *pwd = getpwnam("mysql");
struct group *grp = getgrnam("mysql");
if (pwd == NULL || grp == NULL)
{
cdr_log("[ERROR][create_dailycdr_csv]:failed to get uid or gid\n");
return;
}
// <20>޸<EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߺ<EFBFBD><DFBA><EFBFBD><EFBFBD><EFBFBD>
if (chown(dailycdr_csv_path, pwd->pw_uid, grp->gr_gid) != 0)
{
cdr_log("[ERROR][create_dailycdr_csv]:failed to chown directory %s\n", dailycdr_csv_path);
return;
}
}
sprintf(voice_filename, "%s/%s_voice_%s.csv", dailycdr_csv_path, psrc->recordTable, time_table);
sprintf(sms_filename, "%s/%s_sms_%s.csv", dailycdr_csv_path, psrc->recordTable, time_table);
sprintf(tablename, "CDR_DB.%s", psrc->recordTable);
cdr_log("[INFO][create_dailycdr_csv]:prepare to create %s from %s at %02d:%02d:%02d\n", voice_filename, tablename, t->tm_hour, t->tm_min, t->tm_sec);
pubConn=mysql_conn("localhost","CDR_DB");
if(pubConn == NULL)
{
cdr_log("[ERR %d][create_dailycdr_csv]:Can not connect to CDR_DB\n", mysql_errno(pubConn));
return;
}
// export msc voice cdr file
strcpy(calltype_condition, "(recordType!=6 and recordType!=7)");
sprintf(mng_sqlstr, "select key_sys_id,instance%s,srcCode into outfile '%s' from %s where %s and %s",
psrc->define.outFieldList, voice_filename, tablename, time_cond, calltype_condition);
if(mysql_getnores(pubConn,mng_sqlstr) != 0)
{
cdr_log("[ERR %d][create_dailycdr_csv]:Create %s fail,SQL=%s\n", mysql_errno(pubConn), voice_filename, mng_sqlstr);
voice_create_success = 0;
}
else
{
cdr_log("[SQL]:%s\n",mng_sqlstr);
cdr_log("[INFO][create_dailycdr_csv]:create csv file %s successfully,%s\n", voice_filename, mng_sqlstr);
sprintf(cmdstr, "chmod 644 %s", voice_filename);
system(cmdstr);
voice_create_success = 1;
}
if (cdr_file_mng.dailycdr_backup_flag && voice_create_success)
{
if (chdir(cdr_file_mng.dailycdr_sending_dir) == -1)
{
sprintf(cmdstr, "mkdir -p -m755 %s", cdr_file_mng.dailycdr_sending_dir);
cdr_log("[INFO][create_dailycdr_csv]:%s\n", cmdstr);
system(cmdstr);
if (chdir(cdr_file_mng.dailycdr_sending_dir) == -1)
return;
}
sprintf(cmdstr, "cp -f %s %s/", voice_filename, cdr_file_mng.dailycdr_sending_dir);
system(cmdstr);
}
// export msc sms cdr file
strcpy(calltype_condition, "(recordType=6 or recordType=7)");
sprintf(mng_sqlstr, "select key_sys_id,instance%s,srcCode into outfile '%s' from %s where %s and %s",
psrc->define.outFieldList, sms_filename, tablename, time_cond, calltype_condition);
if (mysql_getnores(pubConn, mng_sqlstr) != 0)
{
cdr_log("[ERR %d][create_dailycdr_csv]:Create %s fail,SQL=%s\n", mysql_errno(pubConn), sms_filename, mng_sqlstr);
sms_create_success = 0;
}
else
{
cdr_log("[SQL]:%s\n", mng_sqlstr);
cdr_log("[INFO][create_dailycdr_csv]:create csv file %s successfully,%s\n", sms_filename, mng_sqlstr);
sprintf(cmdstr, "chmod 644 %s", sms_filename);
system(cmdstr);
sms_create_success = 1;
}
if (cdr_file_mng.dailycdr_backup_flag && sms_create_success)
{
if(chdir(cdr_file_mng.dailycdr_sending_dir) == -1)
{
sprintf(cmdstr, "mkdir -p -m755 %s", cdr_file_mng.dailycdr_sending_dir);
cdr_log("[INFO][create_dailycdr_csv]:%s\n", cmdstr);
system(cmdstr);
if (chdir(cdr_file_mng.dailycdr_sending_dir) == -1)
return;
}
sprintf(cmdstr, "cp -f %s %s/", sms_filename, cdr_file_mng.dailycdr_sending_dir);
system(cmdstr);
}
if (voice_create_success && sms_create_success)
{
clear_table(psrc->recordTable);
}
mysql_close(pubConn);
}
}
// ---end