问题描述
callTimestamp | httpStatus | 端点 |
---|---|---|
2021-04-01 06:00:00 | 200 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 10:21:11 | 200 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 10:25:00 | 500 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 11:33:15 | 200 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 11:34:31 | 200 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 11:35:22 | 500 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 12:22:54 | 200 | https://someserver/someapi/v1/endpoint1 |
2021-04-01 10:21:11 | 200 | https://someserver/someapi/v1/endpoint2 |
2021-04-01 10:25:32 | 500 | https://someserver/someapi/v1/endpoint2 |
2021-04-01 10:59:12 | 200 | https://someserver/someapi/v1/endpoint2 |
我需要计算正常运行时间率,即:
-
以秒为单位的总正常运行时间率 (100%) 为 TIME_TO_SEC(TIMEDIFF(Now(),MIN(callTimestamp)))
-
需要用 httpStatus 500 计算记录之间的(停机时间)差异,然后用按端点分组的 httpStatus 200 计算。 从上面端点 1 的示例中,我有两次出现:
2021-04-01 11:33:15 - 2021-04-01 10:25:00 = 4095 秒
2021-04-01 12:22:54 - 2021-04-01 11:35:22 = 2852 秒
预期结果示例
考虑
MIN(callTimestamp) = 2021-04-01 06:00:00
现在() = 2021-04-01 22:00:00
TIME_TO_SEC(TIMEDIFF(Now(),MIN(callTimestamp))) = 57600 秒(这是我的 100%)
端点 1 = 57600 - 6947 = 50563 秒正常运行时间
端点 2 = 57600 - 2020 = 55580 秒正常运行时间
uptimeRate | 端点 |
---|---|
87.93 | https://someserver/someapi/v1/endpoint1 |
96.49 | https://someserver/someapi/v1/endpoint2 |
我不知道如何按端点计算停机时间。
对我如何做到这一点有任何想法吗?
P.S:我使用的是 MysqL 8.0.20
解决方法
您可以使用lead()over()
计算下一个callTimestamp
和min()over()
窗口函数来选择最小值callTimestamp
。然后通过分组和聚合,您可以获得所需的内容。
架构和插入语句:
create table mytable(callTimestamp datetime,httpStatus int,endpoint varchar(100));
insert into mytable values('2021-04-01 10:21:11',200,'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 10:25:00',500,'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:33:15','https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:34:31','https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:35:22','https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 12:22:54','https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 10:21:11','https://someserver/someapi/v1/endpoint2');
insert into mytable values('2021-04-01 10:25:32','https://someserver/someapi/v1/endpoint2');
insert into mytable values('2021-04-01 10:59:12','https://someserver/someapi/v1/endpoint2');
查询:
select endpoint,100*(TIME_TO_SEC(TIMEDIFF(NOW(),max(mincalltime)))-sum(TIME_TO_SEC(TIMEDIFF(nexttime,calltimestamp))))/ TIME_TO_SEC(TIMEDIFF(NOW(),max(mincalltime))) uptimeRate
from
(select *,lead(calltimestamp)over (partition by endpoint order by calltimestamp)nexttime,min(calltimestamp)over(partition by endpoint order by calltimestamp) mincalltime
from mytable
)t
where httpstatus=500
group by endpoint
输出:
端点 | uptimeRate |
---|---|
https://someserver/someapi/v1/endpoint1 | 98.8895 |
https://someserver/someapi/v1/endpoint2 | 99.6771 |
db