如何按状态比较时间戳来计算正常运行率
How to calculate uptime rate comparing timestamps by status
我有一个 table 用于记录 http 调用。这是我的 table 的样子:
callTimestamp
httpStatus
endpoint
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(呼叫时间戳)= 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 秒正常运行时间
endpoint2 = 57600 - 2020 = 55580 秒正常运行时间
uptimeRate
endpoint
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()
window函数到select最小值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',200, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:34:31',200, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:35:22',500, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 12:22:54',200, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 10:21:11',200, 'https://someserver/someapi/v1/endpoint2');
insert into mytable values('2021-04-01 10:25:32',500, 'https://someserver/someapi/v1/endpoint2');
insert into mytable values('2021-04-01 10:59:12',200, '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
输出:
endpoint
uptimeRate
https://someserver/someapi/v1/endpoint1
98.8895
https://someserver/someapi/v1/endpoint2
99.6771
dbhere
我有一个 table 用于记录 http 调用。这是我的 table 的样子:
callTimestamp | httpStatus | endpoint |
---|---|---|
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(呼叫时间戳)= 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 秒正常运行时间
endpoint2 = 57600 - 2020 = 55580 秒正常运行时间
uptimeRate | endpoint |
---|---|
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()
window函数到select最小值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',200, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:34:31',200, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 11:35:22',500, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 12:22:54',200, 'https://someserver/someapi/v1/endpoint1');
insert into mytable values('2021-04-01 10:21:11',200, 'https://someserver/someapi/v1/endpoint2');
insert into mytable values('2021-04-01 10:25:32',500, 'https://someserver/someapi/v1/endpoint2');
insert into mytable values('2021-04-01 10:59:12',200, '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
输出:
endpoint | uptimeRate |
---|---|
https://someserver/someapi/v1/endpoint1 | 98.8895 |
https://someserver/someapi/v1/endpoint2 | 99.6771 |
db