Codex模型IP分身实践:3亿token消耗下的权限配置与成本优化
2026/7/13 2:20:35
本文深入讲解DolphinDB告警分析技术。从告警统计到趋势分析,从根因定位到效果评估,从告警报告到持续改进,全面介绍告警分析的核心方法。通过丰富的代码示例,帮助读者掌握告警统计与趋势分析的核心技能。
| 维度 | 说明 |
|---|---|
| 时间维度 | 按时间统计告警 |
| 设备维度 | 按设备统计告警 |
| 类型维度 | 按告警类型统计 |
| 级别维度 | 按告警级别统计 |
| 指标 | 计算方式 |
|---|---|
| 告警数量 | 告警总数 |
| 告警率 | 告警数/设备数 |
| 响应时间 | 平均响应时间 |
| 处理时间 | 平均处理时间 |
//按小时统计defhourlyAlertStats(date){returnselect bar(alert_time,1h)ashour,count(*)asalert_count,count(distinct device_id)asdevice_countfromalert_log where date(alert_time)=date group by bar(alert_time,1h)}//按天统计defdailyAlertStats(startDate,endDate){returnselect date(alert_time)asdate,count(*)asalert_count,sum(iif(level=1,1,0))ascritical_count,sum(iif(level=2,1,0))aswarning_countfromalert_log where date(alert_time)between startDateandendDate group by date(alert_time)}//按周统计defweeklyAlertStats(weeks=4){returnselect week(alert_time)asweek,count(*)asalert_countfromalert_log where alert_time>now()-weeks*7*86400000group by week(alert_time)}//按设备统计defdeviceAlertStats(startTime,endTime){returnselect device_id,count(*)asalert_count,avg(level)asavg_level,min(alert_time)asfirst_alert,max(alert_time)aslast_alertfromalert_log where alert_time between startTimeandendTime group by device_id order by alert_count desc}//Top N告警设备deftopAlertDevices(limit=10){returnselect device_id,count(*)asalert_countfromalert_log where alert_time>now()-86400000group by device_id order by alert_count desc limit limit}//按告警类型统计deftypeAlertStats(startTime,endTime){returnselect rule_id,count(*)asalert_count,avg(value)asavg_value,max(value)asmax_valuefromalert_log where alert_time between startTimeandendTime group by rule_id order by alert_count desc}//按级别统计deflevelAlertStats(startTime,endTime){returnselect level,count(*)asalert_count,count(*)*100.0/(select count(*)fromalert_log where alert_time between startTimeandendTime)aspercentagefromalert_log where alert_time between startTimeandendTime group by level}//告警趋势defalertTrend(days=7){returnselect date(alert_time)asdate,count(*)asalert_count,mavg(count(*),7)over(order by date(alert_time))asmoving_avgfromalert_log where alert_time>now()-days*86400000group by date(alert_time)}//同比分析defyearOverYear(metric){now=now()current=getAlertCount(now-86400000,now)lastYear=getAlertCount(now-365*86400000,now-364*86400000)returndict(STRING,ANY,[["current",current],["lastYear",lastYear],["change",(current-lastYear)*100.0/lastYear]])}//环比分析defmonthOverMonth(){now=now()current=getAlertCount(now-86400000,now)lastMonth=getAlertCount(now-60*86400000,now-59*86400000)returndict(STRING,ANY,[["current",current],["lastMonth",lastMonth],["change",(current-lastMonth)*100.0/lastMonth]])}defgetAlertCount(startTime,endTime){returnexeccount(*)fromalert_log where alert_time between startTimeandendTime}//告警预测defpredictAlerts(days=7){//获取历史数据 data=select date(alert_time)asdate,count(*)ascountfromalert_log where alert_time>now()-30*86400000group by date(alert_time)order by dateif(data.rows()<7){return0}//简单移动平均预测returnavg(data.count[-7:])}//告警关联分析defcorrelationAnalysis(timeWindow=300){alerts=select*fromalert_log where alert_time>now()-timeWindow*1000order by alert_time correlations=dict(STRING,INT)for(iin0..alerts.rows()-1){for(jini+1..alerts.rows()-1){if(alerts[j].alert_time-alerts[i].alert_time<60000){key=alerts[i].rule_id+"_"+alerts[j].rule_id correlations[key]=correlations.get(key,0)+1}}}returncorrelations}//识别根因defidentifyRootCause(timeWindow=600){alerts=select*fromalert_log where alert_time>now()-timeWindow*1000order by alert_time rootCauses=array(STRING,0)for(alertinalerts){//检查是否触发其他告警 subsequentAlerts=select count(*)fromalerts where alert_time>alert.alert_timeandalert_time<alert.alert_time+60000if(subsequentAlerts>2){rootCauses.append!(alert.alert_id)}}returnrootCauses}//影响范围分析defimpactAnalysis(alertId){alert=select*fromalert_log where alert_id=alertIdif(alert.rows()==0){returnnull}//查找后续告警 subsequent=select*fromalert_log where alert_time>alert.alert_time[0]andalert_time<alert.alert_time[0]+600000returndict(STRING,ANY,[["sourceAlert",alert[0]],["affectedDevices",distinct(subsequent.device_id)],["affectedCount",subsequent.rows()]])}//响应时间统计defresponseTimeStats(startTime,endTime){returnselect avg(ack_time-alert_time)asavg_response_time,min(ack_time-alert_time)asmin_response_time,max(ack_time-alert_time)asmax_response_timefromalert_log where alert_time between startTimeandendTimeandack_timeisnotnull}//处理时间统计defresolutionTimeStats(startTime,endTime){returnselect avg(resolve_time-alert_time)asavg_resolution_time,min(resolve_time-alert_time)asmin_resolution_time,max(resolve_time-alert_time)asmax_resolution_timefromalert_log where alert_time between startTimeandendTimeandresolve_timeisnotnull}//告警质量评估defalertQualityAssessment(){total=execcount(*)fromalert_log where alert_time>now()-86400000acknowledged=execcount(*)fromalert_log where alert_time>now()-86400000andstatus!="new"resolved=execcount(*)fromalert_log where alert_time>now()-86400000andstatus="resolved"returndict(STRING,ANY,[["total",total],["acknowledged",acknowledged],["resolved",resolved],["ackRate",acknowledged*100.0/total],["resolveRate",resolved*100.0/total]])}//生成日报defgenerateDailyReport(date){stats=dailyAlertStats(date,date)topDevices=topAlertDevices(10)quality=alertQualityAssessment()returndict(STRING,ANY,[["date",date],["stats",stats],["topDevices",topDevices],["quality",quality]])}//生成周报defgenerateWeeklyReport(weekStart){weekEnd=weekStart+6stats=dailyAlertStats(weekStart,weekEnd)trend=alertTrend(7)topTypes=typeAlertStats(weekStart,weekEnd)returndict(STRING,ANY,[["weekStart",weekStart],["weekEnd",weekEnd],["stats",stats],["trend",trend],["topTypes",topTypes]])}//生成月报defgenerateMonthlyReport(month){stats=levelAlertStats(month[0],month[-1])devices=deviceAlertStats(month[0],month[-1])yoy=yearOverYear("alerts")returndict(STRING,ANY,[["month",month],["stats",stats],["devices",devices],["yoy",yoy]])}//==========告警分析系统==========//1.创建告警表 share table(1:0,`alert_id`rule_id`device_id`alert_time`metric`value`threshold`level`status`ack_time`resolve_time,[STRING,STRING,SYMBOL,TIMESTAMP,STRING,DOUBLE,DOUBLE,INT,STRING,TIMESTAMP,TIMESTAMP])asalert_log//2.统计接口defgetAlertDashboard(){now=now()returndict(STRING,ANY,[["today",execcount(*)fromalert_log where date(alert_time)=date(now)],["week",execcount(*)fromalert_log where alert_time>now-7*86400000],["month",execcount(*)fromalert_log where alert_time>now-30*86400000],["critical",execcount(*)fromalert_log where level=1andstatus="new"],["warning",execcount(*)fromalert_log where level=2andstatus="new"]])}addFunctionView(getAlertDashboard)//3.趋势接口defgetAlertTrend(days=7){returnselect date(alert_time)asdate,count(*)ascountfromalert_log where alert_time>now()-days*86400000group by date(alert_time)}addFunctionView(getAlertTrend)//4.Top设备defgetTopDevices(limit=10){returnselect device_id,count(*)asalert_countfromalert_log where alert_time>now()-86400000group by device_id order by alert_count desc limit limit}addFunctionView(getTopDevices)print("告警分析系统启动完成")本文详细介绍了DolphinDB告警分析:
思考题: