简体   繁体   English

MySQL 查询防火墙策略统计

[英]MySQL query firewall policy stats

Sorry, really new to mysql and drawing a bit of a blank on where it start with creating a query.抱歉,对于 mysql 来说真的很新,并且在从创建查询开始的地方画了一点空白。

What are you trying to achieve?你想达到什么目的? To create a simple dashboard with 3 Gauges to display data from firewall policy stats.创建一个带有 3 个仪表的简单仪表板,以显示来自防火墙策略统计数据的数据。 Basically I want a gauge to display the number of policies last_used less than 3 months, between 3 and 6months and over 12 months.基本上我想要一个仪表来显示最近使用的保单数量少于 3 个月、3 到 6 个月以及超过 12 个月。

How are you trying to achieve it?你是如何努力实现它的? I have MySQL table containing columns我有包含列的 MySQL 表

policyid int last_used int - epoch timestamp - timestamp. policyid int last_used int - 纪元时间戳 - 时间戳。

I put the policy stats every 5mins so end up with this table我每 5 分钟放一次政策统计数据,所以最后得到这张表

policyid保单编号 last_used最后使用 timestamp时间戳
511 511 1662808082 1662808082 2022-09-10 12:08:08 2022-09-10 12:08:08
511 511 1662808562 1662808562 2022-09-10 12:16:44 2022-09-10 12:16:44
511 511 1662809702 1662809702 2022-09-10 12:35:04 2022-09-10 12:35:04
511 511 1662809942 1662809942 2022-09-10 12:40:02 2022-09-10 12:40:02
511 511 1662810302 1662810302 2022-09-10 12:45:02 2022-09-10 12:45:02
511 511 1662810602 1662810602 2022-09-10 12:50:02 2022-09-10 12:50:02
511 511 1662810842 1662810842 2022-09-10 12:55:02 2022-09-10 12:55:02
512 512 1661442932 1661442932 2022-08-25 16:55:32 2022-08-25 16:55:32
512 512 1662808054 1662808054 2022-09-10 12:08:08 2022-09-10 12:08:08
512 512 1662808599 1662808599 2022-09-10 12:16:44 2022-09-10 12:16:44
512 512 1662809702 1662809702 2022-09-10 12:35:04 2022-09-10 12:35:04
512 512 1662809987 1662809987 2022-09-10 12:40:02 2022-09-10 12:40:02
512 512 1662810298 1662810298 2022-09-10 12:45:02 2022-09-10 12:45:02
512 512 1662810592 1662810592 2022-09-10 12:50:02 2022-09-10 12:50:02
512 512 1662810860 1662810860 2022-09-10 12:55:02 2022-09-10 12:55:02
512 512 1662811198 1662811198 2022-09-10 13:00:02 2022-09-10 13:00:02
512 512 1662811449 1662811449 2022-09-10 13:05:02 2022-09-10 13:05:02

I think the logical is to workout the MAX value for last_used per policy.我认为合乎逻辑的是为每个策略锻炼 last_used 的 MAX 值。 Then just compare that to epoch times ( < 3months, between 6 and 9 months, 12 months) from current_time.然后将其与 current_time 的纪元时间(< 3 个月,6 到 9 个月,12 个月)进行比较。

So as a start I tried所以作为开始,我尝试了

select policyid, last_used 
FROM STATS 
WHERE last_used >= 1654867964 UNION 
SELECT policyid,MAX(last_used) 
FROM STATS 
ORDER BY policyid;

But not getting the desired output.但没有得到想要的 output。 Has anyone got any ideas how this could be achieved?有没有人知道如何实现这一目标?

Not entirely clear what are you trying to achieve, but based on不完全清楚您要达到的目标,但基于

to display the number of policies last_used less than 3 months, between 3 and 6months and over 12 months.显示最后使用的保单数量少于 3 个月、3 到 6 个月以及超过 12 个月。

You can use conditional aggregation to count the number of records based on last_used columns.您可以使用条件聚合来计算基于 last_used 列的记录数。

Try:尝试:

select  sum(case when FROM_UNIXTIME(last_used)  <= now() and FROM_UNIXTIME(last_used) >=(now() -INTERVAL 3 MONTH) then 1 end )  as last_3_months,
        sum(case when FROM_UNIXTIME(last_used)  <= (now() -INTERVAL 3 MONTH) and FROM_UNIXTIME(last_used) >=(now() -INTERVAL 6 MONTH) then 1 end ) as between_3_6_months,
        sum(case when FROM_UNIXTIME(last_used)  <= (now() -INTERVAL 12 MONTH) then 1 end )  as over_12_months
from firewall_policy_stats; 

https://dbfiddle.uk/Y-h60QD2 https://dbfiddle.uk/Y-h60QD2

Note .注意 I added some more data example just for testing purpose.我添加了更多数据示例只是为了测试目的。 If you want to display 0 instead of null you could use coalesce如果你想显示0而不是 null 你可以使用coalesce

select  coalesce(sum(case when FROM_UNIXTIME(last_used)  <= now() and FROM_UNIXTIME(last_used) >=(now() -INTERVAL 3 MONTH) then 1 end ),0)  as last_3_months,
        coalesce(sum(case when FROM_UNIXTIME(last_used)  <= (now() -INTERVAL 3 MONTH) and FROM_UNIXTIME(last_used) >=(now() -INTERVAL 6 MONTH) then 1 end ),0)  as between_3_6_months,
        coalesce(sum(case when FROM_UNIXTIME(last_used)  <= (now() -INTERVAL 12 MONTH) then 1 end ),0)   as over_12_months
from firewall_policy_stats;

https://dbfiddle.uk/R5JuoulF https://dbfiddle.uk/R5JuoulF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM