From 4b8e5e6fc8f01ac6a26f137f32d79e5cf72670ea Mon Sep 17 00:00:00 2001 From: hugetoad Date: Sat, 29 Nov 2003 21:39:57 +0000 Subject: Support of trending.. git-svn-id: svn://svn.zabbix.com/trunk@1072 97f52cf1-0a1b-0410-bd0e-c28be96e8082 --- include/db.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/db.c b/include/db.c index 2024c400..91c46046 100644 --- a/include/db.c +++ b/include/db.c @@ -775,13 +775,40 @@ int DBupdate_item_status_to_notsupported(int itemid) int DBadd_trend(int itemid, double value, int clock) { + DB_RESULT *result; char sql[MAX_STRING_LEN+1]; + int hour; + int num; + double value_min, value_avg, value_max; zabbix_log(LOG_LEVEL_DEBUG,"In add_trend()"); - sprintf(sql,"insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d,%d,%d,%g,%g%g)", clock, itemid, 1, value, value, value); + hour=clock-clock%3600; + + sprintf(sql,"select num,value_min,value_avg,value_max from trends where itemid=%d and clock=%d", itemid, hour); + zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); + result = DBselect(sql); + + if(DBnum_rows(result) == 1) + { + num=atoi(DBget_field(result,0,0)); + value_min=atof(DBget_field(result,0,1)); + value_avg=atof(DBget_field(result,0,2)); + value_max=atof(DBget_field(result,0,3)); + if(valuevalue_avg) value_max=value; + value_avg=(num*value_avg+value)/(num+1); + num++; + sprintf(sql,"update trends set num=%d, value_min=%g, value_avg=%g, value_max=%g where itemid=%d and clock=%d", num, value_min, value_avg, value_max, itemid, hour); + } + else + { + sprintf(sql,"insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d,%d,%d,%g,%g,%g)", hour, itemid, 1, value, value, value); + } DBexecute(sql); + DBfree_result(result); + return SUCCEED; } -- cgit