ProcessDB实时/时序数据库——C/C++查询历史统计数据

目录

前言

一、历史统计数据字段介绍

二、查询历史统计数据


前言

前文已经介绍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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值