艾警探数据库编程函数和实用程序

detech-ai-db的Python项目详细描述


的数据库访问包艾警探在

这是艾警探的包,以编程方式访问Dynamodb&Timestream。在

进口

importdetech_query_pkg###############    DynamoDB Package    ##############################fromdetech_query_pkg.dynamodb_pkgimportdynamodb_queriesasdb_queriesfromdetech_query_pkg.dynamodb_pkg.utilsimportdynamodb_utilsasdb_utils#Start DynamoDB Clientdb_utils.create_dynamodb_client(aws_access_key_id=AWS_ACCESS_KEY_ID,aws_secret_access_key=AWS_SECRET_ACCESS_KEY,region_name=REGION_NAME)###############    Timestream Package    ##############################fromdetech_query_pkg.timestream_pkgimportts_queriesfromdetech_query_pkg.timestream_pkg.utilsimportts_utilsfromdetech_query_pkg.timestream_pkg.modelsimportmetrics_modelfromdetech_query_pkg.timestream_pkg.modelsimportmetrics_creator_utils

初始化客户端

^{pr2}$

功能

时间流软件包(ts_查询.py)在

<详细信息>从“度量”列表中插入“度量”
definsert_metrics_from_metric_list(client,metric_list)# Inserts metrics in batch to timestream#metric_list must have the following fieldsmetric_list=[{'org_id','region_name','namespace','component_id','period','agent','metric_alignment','unit','description','metric_id','metric_name','value','timestamp'},{'org_id','region_name','namespace','component_id','period','agent','metric_alignment','unit','description','metric_id','metric_name','value','timestamp'},...]
<详细信息>查询度量
defquery_metrics(client,sql_query)# Performs an SQL query to timestream and transforms the output to a more desirable format#Outputquery_response={'metric_id':'qgrdy1bXGeKSmAtW58CD','agent':'AWS.CloudWatch','component_id':'AWS/ApplicationELB.app/component','period':'60','unit':'None','org_id':'Organization','metric_alignment':'Sum','namespace':'AWS/ApplicationELB','description':'The total number of concurrent TCP connections active from clients to the load balancer and from the load balancer to targets.','region_name':'eu-west-1','value':'64.0','metric_name':'ActiveConnectionCount','timestamp':'2020-10-12 14:28:00.000000000'}

时间流_实用程序包(ts)_实用工具.py)在

<详细信息>准备度量记录
defprepare_metric_records(measure_name,measure_value,timestamp,dimensions)#The dimensions that need to be passed must be in the following formatdimensions=[{'Name':'org_id','Value':str(metric['org_id'])},{'Name':'region_name','Value':str(metric['region_name'])},{'Name':'namespace','Value':str(metric['namespace'])},{'Name':'component_id','Value':str(metric['component_id'])},{'Name':'period','Value':str(metric['period'])},{'Name':'agent','Value':str(metric['agent'])},{'Name':'metric_alignment','Value':str(metric['metric_alignment'])},{'Name':'unit','Value':str(metric['unit'])},{'Name':'description','Value':str(metric['description'])},{'Name':'metric_id','Value':str(metric['metric_id'])}]
<详细信息>写入\u timestream
defwrite_to_timestream(client,records,database_name,table_name)# Inserts metrics to timestream after they are in the correct format
<详细信息>从\u timestream查询
defquery_from_timestream(client,sql_query)# Queries metrics from timestream with a given sql_query

时间流_包装型号(公制创建者_实用工具.py&度量(amp;M)_模型.py)在

<详细信息>构建度量模型
#from metric_creator_utils.pydefbuild_metric_model(metric_id,metric_name,org_id,component_id,namespace,metric_alignment,agent,dimensions,region_name=None,is_default=False,description=None,period=60,unit=None,samples=[])#Queries metrics from timestream with a given sql_query
<详细信息>MetricModel
#from metrics_model.pyclassMetricModel(object):def__init__(self,id,name,org_id,component_id,namespace,alignment,region_name,data_center_id,agent,dimension_name=None,dimension_value=None,is_active=False,description=None,unit=None)defto_dict(self)#Queries metrics from timestream with a given sql_query

发电机包

<详细信息>插入警报
definsert_alert(alert_id,metric_id,org_id,app_id,team_id,assigned_to,start_time,end_time,alert_description,is_acknowledged,anomalies_dict,related_prev_anomalies,service_graph,significance_score,dynamodb)#Exampleinsert_alert(alert_id="256828",metric_id=123,org_id='org_id',app_id='app_id',team_id='team_id',assigned_to='Jorge', \
start_time='2020-09-03 12:00:00',end_time='2020-09-03 12:20:00',alert_description='Spike in costs',\
is_acknowledged='True',anomalies_dict={},related_prev_anomalies={},service_graph={},significance_score='34.3')
<详细信息>按键获取\u alert_item_
defget_alert_item_by_key(anom_id,dynamodb)
<详细信息>用异常更新警报
defupdate_alert_with_related_anomalies(alert_id,start_time,corr_anoms_dict,related_prev_anomalies,dynamodb)
<详细信息>终止警报
defterminate_alert(alert_id,start_time,end_timestamp,dynamodb)
<详细信息>创建度量
defcreate_metric(metric_id,date_bucket,metric_name,provider,namespace,agent,org_id,app_id,alignment,groupby,dimensions,data_points_list,dynamodb)#Examplecreate_metric(metric_id="test1",date_bucket="2020-10-02",metric_name="error_rate",provider="aws",namespace="dynamodb",agent="CloudWatch",org_id="test",app_id="app1",alignment="Sum",dimensions=[{"Name":"TableName","Value":"alerts.config"}],last=1535530432,data_points_list=[{'val':55,'time':1535530430},{'val':56,'time':1535530432}],dynamodb=dynamodb)
<详细信息>批处理插入度量对象
defbatch_insert_metric_details_objects(list_of_metric_objects,dynamodb)#Inserts list of metrics objects in batch into Dynamodb
<详细信息>批处理插入度量对象
defbatch_insert_metric_details_objects(list_of_metric_objects,dynamodb)#Inserts list of metrics objects in batch into Dynamodb
<详细信息>批处理插入度量对象
defbatch_insert_component_info_objects(list_of_component_objects,dynamodb)#Inserts list of component objects in batch into Dynamodb
<详细信息>获取度量详细信息
defget_metric_details(metric_id,dynamodb)#Fetches all the details for a specific metric_id
<详细信息>按关键字获取度量项目
defget_metric_item_by_key(metric_id,curr_date,dynamodb)
<详细信息>按加密的\u id扫描度量
defscan_metrics_by_encrypted_id(anom_alarm_id,dynamodb)
<详细信息>按键查询警报配置 ^{pr21}$ <详细信息>插入警报配置
definsert_alert_config(metric_id,alert_title,severity,alert_type,alert_direction,description,duration,duration_unit,rule_dict,recipients_list,owner_dict,dynamodb)#Exampleinsert_alert_config(metric_id="metric1245",alert_title="Anomaly by Cluster",severity="critical",alert_type="anomaly",alert_direction="spikes/drops",description="Relevant to Play Store billing user journey",duration=12,duration_unit="hours",rule_dict={},recipients_list=[{"channel":"webhook","contact":"j.velez2210@gmail.com"},{"channel":"slack","contact":"j.velez2210@gmail.com"}],owner_dict={"user_id":"user12341","user_name":"João Tótó",})
<详细信息>query_most_recent_metric_fetching_log
defquery_most_recent_metric_fetching_log(component_id,dynamodb)#Fetches the log with the highest timestamp, from all the logs between start & end ts
<详细信息>插入api请求日志
definsert_api_request_log(api_name,request_timestamp,response_status_code,request,response,dynamodb)# Exampleinsert_api_request_log(api_name='anomalarm_metrics',request_timestamp=1603466177,response_status_code='202',request={'key':'value'},response={'key':'value'},dynamodb=dynamodb)
<详细信息>插入新的异常
definsert_new_anomaly(id,timestamp,metric_id,value,dynamodb,is_dev_env=False):# Exampleinsert_new_anomaly(id="125123",timestamp=1599563224,metric_id="m412",value=123.44,dynamodb=dynamodb)
<详细信息>更新异常关系
defupdate_anomaly_relations(id,timestamp,cross_correlations,possible_related_anomalies,possible_related_matches,dynamodb,is_dev_env=False):# Exampleupdate_anomaly_relations(id="125123",timestamp=1599563224,cross_correlations={"web-server-1.cpu0.iowait":{"coefficient":0.95752,"shifted":0,"shifted_coefficient":0.95752},},possible_related_anomalies={"256826":{"metric_id":"web-server-1.mysql.counters.handlerRead_key","timestamp":1599563164},},possible_related_matches={"169560":{"timestamp":1599563230,"fp id":8821,"layer id":"None","metric_id":"web-server-2.mariadb.localhost:3306.mysql.bytes_sent"}},dynamodb=dynamodb)
<详细信息>插入“anomalarm”异常“webhook”日志
definsert_anomalarm_anomalies_webhook_log(anomalarm_id,timestamp,request,response,dynamodb,is_dev_env=False):# Exampleinsert_anomalarm_anomalies_webhook_log(anomalarm_id='256828',timestamp=1603466177,request={...},response={...},dynamodb=dynamodb)
<详细信息>终止异常
defterminate_anomaly(id,timestamp,end_timestamp,dynamodb,is_dev_env=False):# Exampleterminate_anomaly(id="125123",timestamp=1599563224,end_timestamp=1599663224,dynamodb=dynamodb)
<详细信息>插入错误日志
definsert_error_log(dynamodb,service_name,timestamp,msg,details,is_dev_env=False):# Exampleinsert_error_log(dynamodb=dynamodb,service_name="metric_to_db",timestamp=1599563224,msg="Error inserting value",details={'exception':'RejectedRecordsException','response':{...}})

发电机B_实用程序包在

<详细信息>放入项目
defput_item(item_dict,table_name,dynamodb)#Inserts json item into DynamoDB table#Exampleitem_dict={"attr":"value","attr2":"value2"}table_name="alerts"
<详细信息>批量插入 ^{pr31}$ <详细信息>获取项目
defget_item(key_dict,table_name,dynamodb)#Retrieves item from DynamoDB table#Examplekey_dict={"prim_key"="value","sort_key"="value"}
<详细信息>获取“项目”和“检索”特定属性
defget_item_and_retrieve_specific_attributes(key_dict,attr_list,table_name,dynamodb)#Retrieves item from DynamoDB table and retrieve specific attributes#Examplekey_dict={"prim_key":"value","sort_key":"value"}attr_list=['attr1','attr2']
<详细信息>更新项目
defupdate_item(key_dict,update_expression,expression_attr_values,table_name,dynamodb)#Retrieves item from DynamoDB table#Examplekey_dict={"prim_key"="value","sort_key"="value"}update_expression="set service_graph=:i, metric_list=:l, significance_score=:s"expression_attr_values={':i':{'s1':['s2','s3']},':l':['124','123'],':s':Decimal(35.5)}#example to append to listUpdateExpression="SET some_attr = list_append(if_not_exists(some_attr, :empty_list), :i)",ExpressionAttributeValues={':i':[some_value],"empty_list":[]}
<详细信息>有条件地更新项目 ^{pr35}$ <详细信息>有条件地删除项目
defdelete_item_conditionally(key_dict,condition_expression,expression_attr_values,table_name,dynamodb)#Examplecondition_expression="significance_score <= :val"expression_attr_values={":val":Decimal(50)}key_dict={'org_id':'Aptoide','start_time':'2020-09-03 12:00:00'}'''
<详细信息>按关键字查询
defquery_by_key(key_condition,table_name,dynamodb)#Queries from DynamoDB table by key condition#Examplekey_condition=Key('org_id').eq('Aptoide')
<详细信息>按“关键字”条件查询“u”和“u项目”
defquery_and_project_by_key_condition(projection_expr,expr_attr_names,key_condition,table_name,dynamodb)#Queries from DynamoDB table by key condition and only returns some attrs#Examplekey_condition=Key('year').eq(year)&Key('title').between(title_range[0],title_range[1])projection_expr="#yr, title, info.genres, info.actors[0]"expr_attr_names={"#yr":"year"}
<详细信息>扫描表
defscan_table(scan_kwargs,table_name,dynamodb)#Scans entire table looking for items that match the filter expression#Examplescan_kwargs={'FilterExpression':Key('year').between(*year_range),'ProjectionExpression':"#yr, title, info.rating",'ExpressionAttributeNames':{"#yr":"year"}}
<详细信息>按_key_min_max查询
defquery_by_key_min_max(key_condition,table_name,is_min,dynamodb)#Queries from DynamoDB table by key condition#Examplekey_condition=Key('part_id').eq(partId)&Key('range_key').between(start,end)#orkey_condition=Key('part_id').eq(partId)
<详细信息>获取表中的所有项目
defget_all_items_in_table(table_name,dynamodb)
<详细信息>递增原子计数器
defincrement_atomic_counter(counter_type,number_of_values,dynamodb)#Increments a counter and makes sure it is done atomically#Available counter types:#org_id#component_id#metric_id

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Spring引导jar文件可重用(本地maven存储库)   没有标题的java Webpush通知不会出现   本地类的优先级。java文件还是从java包中导入的类?   java Sparks enableHiveSupport   java通过AJAX调用我的WebService通常会导致服务器故障   java添加到链表末尾   java提供了对Spring数据Mongo存储库的限制   仅显示字符的程序显示“?”在爪哇   java Oracle 10.2.0.4.0和OJDBC1212。1.0.0.jar | |无法获取JDBC连接;嵌套的异常是ja│ │ ORA01882:未找到时区区域   使用Netflix Eureka的java JSONException   java我们如何为akka非类型处理器编写单元测试   java 安卓在触摸和移动时获取按钮文本   java将字符串转换为int数组bluej