Skip to main content

DynamoDB utils

Project description

DynamoDB Package for detech.ai

This is detech.ai's package to access Dynamodb.

Imports

import detech_query_pkg

from detech_query_pkg import dynamodb_queries as db_queries

from detech_query_pkg.utils import dynamodb_utils as db_utils

#Start DynamoDB Client
db_utils.create_dynamodb_client(aws_access_key_id=AWS_ACCESS_KEY_ID, 
                      aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=REGION_NAME)

Initialize Client

def create_dynamodb_client(aws_access_key_id,aws_secret_access_key, region_name)

Functions

dynamodb

insert_alert
def insert_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)

#Example
insert_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')
get_alert_item_by_key
def get_alert_item_by_key(anom_id, dynamodb)
update_alert_with_related_anomalies
def update_alert_with_related_anomalies(alert_id,start_time, corr_anoms_dict, related_prev_anomalies, dynamodb)
terminate_alert
def terminate_alert(alert_id,start_time, end_timestamp, dynamodb)
create_metric
def create_metric(metric_id, date_bucket, metric_name, provider, namespace,
agent, org_id, app_id, alignment, groupby, dimensions, data_points_list, dynamodb)

#Example
create_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
)
batch_insert_metric_objects
def batch_insert_metric_objects(list_of_metric_objects, dynamodb)
#Inserts list of metrics objects in batch into Dynamodb
get_metric_details
def get_metric_details(metric_id, dynamodb)
#Fetches all the details for a specific metric_id
get_metric_item_by_key
def get_metric_item_by_key(metric_id, curr_date, dynamodb)
scan_metrics_by_encrypted_id
def scan_metrics_by_encrypted_id(anom_alarm_id, dynamodb)
query_alerts_configs_by_key
def query_alerts_configs_by_key(metric_id, dynamodb)
insert_alert_config
def insert_alert_config(metric_id, alert_title, severity, alert_type, alert_direction, description, duration, duration_unit, rule_dict, recipients_list, owner_dict, dynamodb)

#Example
insert_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
def query_most_recent_metric_fetching_log(component_id, dynamodb)
#Fetches the log with the highest timestamp, from all the logs between start & end ts

dynamodb_utils

put_item
def put_item(item_dict, table_name, dynamodb)
#Inserts json item into DynamoDB table

#Example
item_dict = {
  "attr" : "value",
  "attr2" : "value2"
}
table_name = "alerts"
batch_insert
def batch_insert(list_of_item_dicts, table_name, dynamodb)
#Inserts a list of item_dicts in batch to dynamodb
get_item
def get_item(key_dict, table_name, dynamodb)
#Retrieves item from DynamoDB table

#Example
key_dict = {
  "prim_key" = "value",
  "sort_key" = "value"
}
get_item_and_retrieve_specific_attributes
def get_item_and_retrieve_specific_attributes(key_dict, attr_list, table_name, dynamodb)
#Retrieves item from DynamoDB table and retrieve specific attributes

#Example
key_dict = {
  "prim_key" :"value",
  "sort_key" : "value"
}
attr_list = ['attr1', 'attr2']
update_item
def update_item(key_dict, update_expression, expression_attr_values, table_name, dynamodb)
#Retrieves item from DynamoDB table

#Example
key_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 list
UpdateExpression="SET some_attr = list_append(if_not_exists(some_attr, :empty_list), :i)",
ExpressionAttributeValues={
  ':i': [some_value],
  "empty_list" : []
}
update_item_conditionally
def update_item_conditionally(key_dict, condition_expression, update_expression, expression_attr_values, table_name, dynamodb)
#Retrieves item from DynamoDB table

#Example
key_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)
}
condition_expression = "significance_score <= :val"
delete_item_conditionally
def delete_item_conditionally(key_dict, condition_expression, expression_attr_values, table_name, dynamodb)

#Example
condition_expression = "significance_score <= :val"
expression_attr_values = {
  ":val": Decimal(50)
}
key_dict = {
  'org_id': 'Aptoide',
  'start_time': '2020-09-03 12:00:00'
}
'''
query_by_key
def query_by_key(key_condition, table_name, dynamodb)
#Queries from DynamoDB table by key condition

#Example
key_condition = Key('org_id').eq('Aptoide')
query_and_project_by_key_condition
def query_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

#Example
key_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"}
scan_table
def scan_table(scan_kwargs, table_name, dynamodb)
#Scans entire table looking for items that match the filter expression

#Example
scan_kwargs = {
  'FilterExpression': Key('year').between(*year_range),
  'ProjectionExpression': "#yr, title, info.rating",
  'ExpressionAttributeNames': {"#yr": "year"}
}
query_by_key_min_max
def query_by_key_min_max(key_condition, table_name, is_min, dynamodb)
#Queries from DynamoDB table by key condition

#Example
key_condition = Key('part_id').eq(partId) & Key('range_key').between(start, end)
#or 
key_condition = Key('part_id').eq(partId)
get_all_items_in_table
def get_all_items_in_table(table_name, dynamodb)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamodb_detech_ai-0.0.24.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamodb_detech_ai-0.0.24-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file dynamodb_detech_ai-0.0.24.tar.gz.

File metadata

  • Download URL: dynamodb_detech_ai-0.0.24.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for dynamodb_detech_ai-0.0.24.tar.gz
Algorithm Hash digest
SHA256 288041d274797552ac7c05aaa3ba3ada09b68134e8c2ee3adf2f14ee6c26ef7f
MD5 6f5e12adae05a11e5f90b7c2bb268a74
BLAKE2b-256 9c7910f9850fd3f38b887adb73077ef5837135ae21a747605366f064c694d87f

See more details on using hashes here.

File details

Details for the file dynamodb_detech_ai-0.0.24-py3-none-any.whl.

File metadata

  • Download URL: dynamodb_detech_ai-0.0.24-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for dynamodb_detech_ai-0.0.24-py3-none-any.whl
Algorithm Hash digest
SHA256 8e13eedb5ed7add56b6529caa37107d4e1194ce25c7c4b5e48865ef60f97162b
MD5 dbd03a1c15c3dbe0ed10449cb9c049ad
BLAKE2b-256 18da99f96a3701651c4e5bee77bf28509e2b06811565a0928f1711c30c292bf8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page