目录
前言
前文已经介绍C/C++使用ProcessDB的基本操作,本文将针对历史统计数据的相关操作进行介绍
一、历史统计数据字段介绍
字段 | 注释 |
tmTag | 历史数据时间 |
nQuality | 质量是否完好 |
dSum | 累计值 |
dMax | 最大值 |
dMin | 最小值 |
dFirst | 第一值 |
dLast | 最后值 |
tmFirst | 第一值时间 |
tmLast | 最后时间 |
nUltraLimitCount | 超限制数 |
nUltraLimitDuration | 超限制时间 |
dOutput | 产量 |
dFlow | 流量值 |
nCount | 统计数据数量 |
Duration | 持续时间 |
tmMin | 最小值时间 |
tmMax | 最大值时间 |
二、查询历史统计数据
// ProcessDB_API_TEST.cpp : ProcessDB数据库示例
//
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include "processdb_v2.h"
using namespace std;
int main(int argc, char* argv[])
{
float val = 0;
EBASE_RES result = NULL;
int res = 0;
EBASE ebase = { 0 };
/* 初始化连接控制块 */
res = ebase2_init(&ebase);
if (0 != res)
{
cout << "ebase2_init failed !" << endl;
getchar();
exit(1);
}
/*设置超时时间*/
int timeout = 30;
ebase2_set_options(&ebase, EBASE_CONNECTION_TIMEOUT, &timeout);
/* 建立连接 */
res = ebase2_connect(&ebase, "127.0.0.1", 8301, "root", "root");
if (0 != res)
{
cout << "login failed, \n";
getchar();
exit(1);
}
/* 设置统计数据起止时间 */
char fmt[] = "%Y-%m-%d %H:%M:%S";
char startTimeStr[] = "2022-11-24 00:00:00";
char stopTimeStr[] = "2022-11-24 12:00:00";
struct tm tb = { 0 };
time_t startTime;
time_t stopTime;
if (ebase2_strptime(startTimeStr, fmt, &tb))
{
startTime = mktime(&tb);
if (startTime == -1)
{
printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
}
}
if (ebase2_strptime(stopTimeStr, fmt, &tb))
{
stopTime = mktime(&tb);
if (stopTime == -1)
{
printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
}
}
TimeRegion timeRegion = { 0 };
timeRegion.tmBegin = startTime;
timeRegion.tmEnd = stopTime;
/*
* 设置数据统计条件
*/
float val_flow = 0;
float val_max = 0;
float val_min = 0;
int val_seconds = 0;
int static_interval = 600;
EBASE_RES ebase2_res;
/* 查询历史的统计信息 */
res = ebase2_exec_his_stat_query(&ebase, "PUBLIC.SYS.PDB_OS_CPU_USAGE", &timeRegion, static_interval, &ebase2_res);
if (0 != res)
{
printf("ebase2_exec_his_stat_query failed, error code: %d\n", res);
getchar();
exit(1);
}
printf("History stat query ---------------begin-----------------\n");
int count = 0;
ebase2_get_result_count(ebase2_res, &count);
MemStatRecord memStatRecord = { 0 };
for (int dwLoop = 0; dwLoop < count; dwLoop++)
{
ebase2_get_stat_record(ebase2_res, dwLoop, &memStatRecord);
char time_str[32] = { 0 };
ebase2_decode_time_str(memStatRecord.tmTag, time_str);
printf("%s, Max: %.4lf, Min: %.4lf, Sum: %.4lf Count: %d, \n", time_str, memStatRecord.dMax, memStatRecord.dMin, memStatRecord.dSum, memStatRecord.nCount);
}
ebase2_free_result(ebase2_res);
printf("History stat query ---------------end-------------------\n");
printf("His stat record AllCount: %d\n", count);
/* 关闭连接 */
ebase2_close(&ebase);
return 0;
}
示例运行如下:
History stat query ---------------begin-----------------
2022-11-24 08:50:00, Max: 99.9987, Min: 0.9091, Sum: 3808.3886 Count: 200,
2022-11-24 09:00:00, Max: 69.0205, Min: 0.0000, Sum: 6573.5281 Count: 600,
2022-11-24 09:10:00, Max: 30.8310, Min: 0.0000, Sum: 2547.7053 Count: 600,
2022-11-24 09:20:00, Max: 25.6472, Min: 0.0000, Sum: 1843.4202 Count: 599,
2022-11-24 09:30:00, Max: 22.4311, Min: 0.0000, Sum: 2320.1618 Count: 599,
2022-11-24 09:40:00, Max: 41.5001, Min: 0.0000, Sum: 2113.0711 Count: 599,
2022-11-24 09:50:00, Max: 50.0257, Min: 0.0000, Sum: 2277.5432 Count: 600,
2022-11-24 10:00:00, Max: 58.6962, Min: 0.0000, Sum: 2586.3394 Count: 596,
2022-11-24 10:10:00, Max: 50.7822, Min: 0.0000, Sum: 3916.0321 Count: 600,
2022-11-24 10:20:00, Max: 32.9876, Min: 0.0000, Sum: 1741.4006 Count: 599,
2022-11-24 10:30:00, Max: 13.1706, Min: 0.0000, Sum: 826.4296 Count: 594,
2022-11-24 10:40:00, Max: 22.3458, Min: 0.0000, Sum: 1323.7160 Count: 598,
2022-11-24 10:50:00, Max: 37.7127, Min: 0.0000, Sum: 1679.5743 Count: 598,
2022-11-24 11:00:00, Max: 52.8623, Min: 0.0000, Sum: 2460.2730 Count: 599,
2022-11-24 11:10:00, Max: 41.1986, Min: 0.0000, Sum: 2229.8151 Count: 600,
2022-11-24 11:20:00, Max: 48.2265, Min: 0.0000, Sum: 2061.7923 Count: 600,
2022-11-24 11:30:00, Max: 37.4905, Min: 0.0000, Sum: 1755.6014 Count: 599,
2022-11-24 11:40:00, Max: 25.4237, Min: 0.0000, Sum: 2054.8398 Count: 599,
2022-11-24 11:50:00, Max: 20.5717, Min: 0.0000, Sum: 1728.9972 Count: 598,
History stat query ---------------end-------------------
His stat record AllCount: 19