Skip to content

Instantly share code, notes, and snippets.

@Git-on-my-level
Created October 18, 2019 19:41
Show Gist options
  • Select an option

  • Save Git-on-my-level/4ad710509aaf499eaeec19a429a77c7c to your computer and use it in GitHub Desktop.

Select an option

Save Git-on-my-level/4ad710509aaf499eaeec19a429a77c7c to your computer and use it in GitHub Desktop.
Harmony Node Earning Cloudwatch Metric Lambda Function
import json
from botocore.vendored import requests
import boto3
import datetime
HARMONY_NODE_1H_STATUS_URL = 'https://harmony.one/1h'
ADDRESS_TO_MONITOR = 'one17ffl9csu7ln3jw07fcvlmh2h8e5pv3ddk9p5sv'
cloudwatch_client = boto3.client('cloudwatch')
def lambda_handler(event, context):
earning_rate = get_earning_rate_for_address(ADDRESS_TO_MONITOR)
publish_earning_rate_for_address(ADDRESS_TO_MONITOR, earning_rate)
return
def get_earning_rate_for_address(address):
res = requests.get(HARMONY_NODE_1H_STATUS_URL)
for line in res.iter_lines():
if line.startswith(str.encode(address)):
earning_rate = float(line.split(b':')[1])
return earning_rate
def publish_earning_rate_for_address(address, earning_rate):
cloudwatch_client.put_metric_data(
Namespace='HarmonyStats',
MetricData=[
{
'MetricName': 'EarningRate',
'Dimensions': [
{
'Name': 'address',
'Value': address,
},
],
'Timestamp': datetime.datetime.now(),
'Value': earning_rate,
'Unit': 'Count',
'StorageResolution': 60
},
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment