用于将 TClientDataSet 数据分组和聚合到另一个数据集的内置解决方案

问题描述

我可以访问带有请求时间和 URI 的 API 日志。我将数据加载到 TClientDataSet 组件中。

时间 请求
2021-05-01 10:01:01 /识别
2021-05-01 10:01:02 /识别
2021-05-01 10:01:03 /识别
2021-05-01 10:02:01 /add-bio
2021-05-01 10:02:01 /识别

现在我想在图表中显示每分钟的请求统计信息。为此,我必须像这样将数据聚合到另一个 TClientDataSet(我们称之为 cdsRequestsPerMinute)中:

分钟 请求 计数
2021-05-01 10:01:00 /识别 3
2021-05-01 10:02:00 /add-bio 1
2021-05-01 10:02:00 /识别 1

有很多关于 TClientDataSet 记录的分组和聚合的信息,但结果更像是 sql生成的“窗口函数”,而不是返回用于分组标准的唯一记录的分组。

我知道我可以逐条记录并将记录添加cdsRequestsPerMinute(如果尚不存在)或增加计数。是否有内置解决方案?

编辑 1

获取我使用此功能的时间:

function TruncSeconds(const AVal: TDateTime): TDateTime;
var
  _H,_M,_Sec,_MSec: Word;
begin
  DecodeTime(AVal,_H,_MSec);
  Result := Trunc( AVal ) + EncodeTime(_H,0);
end;

它只是从 TDateTime 值中删除秒数。

天真的方法适用于我的每个请求:

procedure TForm1.AddRequest(const _Req: TRequestInfo);
begin
  //request list
  cdsRequests.Insert;
  cdsRequestsstart.Value := _Req.Start;
  cdsRequestsuri.Value := _Req.Uri;
  cdsRequestsduration_ms.Value := _Req.DurationMs;
  cdsRequeststhread.Value := _Req.Thread;
  cdsRequestsclient.Value := _Req.Client;
  cdsRequests.Post;

  //statistics
  if cdsReqsPerMinute.Locate(
    cdsReqsPerMinuteminute.FieldName+';'+cdsReqsPerMinuterequest.FieldName,VararrayOf([TruncSeconds(_Req.Start),_Req.Uri]),[]) then
  begin
    cdsReqsPerMinute.Edit;
    cdsReqsPerMinutecount.Value := cdsReqsPerMinutecount.Value + 1;
    cdsReqsPerMinute.Post;
  end
  else
  begin
    cdsReqsPerMinute.Insert;
    cdsReqsPerMinuteminute.Value := TruncSeconds(_Req.Start);
    cdsReqsPerMinuterequest.Value := _Req.Uri;
    cdsReqsPerMinutecount.Value := 1;
    cdsReqsPerMinute.Post;
  end;
end;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)