Skip to content

Instantly share code, notes, and snippets.

@miromasat
Last active September 22, 2017 09:27
Show Gist options
  • Save miromasat/e9a33928e72fa7dbac16551208d9281d to your computer and use it in GitHub Desktop.
Save miromasat/e9a33928e72fa7dbac16551208d9281d to your computer and use it in GitHub Desktop.
from __future__ import print_function
import boto3
import time
import random
import threading
import sys
cloudwatch = boto3.client(
'cloudwatch',
region_name = 'eu-west-1',
aws_access_key_id='XXXXX',
aws_secret_access_key='XXXXX',
)
# Initial random value
sensor_values = {}
sensor_values['temperature'] = random.randrange(-20,200)
sensor_values['pressure'] = random.randrange(800,1200)
sensor_values['windspeed'] = random.randrange(100,110)
sensor_values['rainfall'] = random.randrange(0,200)
sensor_values['humidity'] = random.randrange(10,80)
sensor_values['windspeed_cat1'] = 150
def mutate_metric(arr):
metricValue = arr[0]
metricEvolve = arr[1]
metricSentiment = arr[2]
metricMin = arr[3]
metricMax = arr[4]
metricConsistency = random.uniform(-metricMax+metricEvolve, metricMax-metricEvolve)
metricWithin = random.uniform(metricSentiment, 100) + metricEvolve
if metricWithin > 90:
metricValue = random.uniform(metricMin+metricEvolve, metricMax-metricEvolve)
if metricEvolve < 9:
metricEvolve = metricEvolve + 1
elif metricConsistency > 0:
metricValue = random.uniform(metricMax, metricMax+metricConsistency)
else:
metricValue = random.uniform(metricMin+metricConsistency, metricMin)
return [metricValue, metricEvolve, metricSentiment, metricMin, metricMax]
def mutate_altitude(value):
value += random.randrange(-2,8)
blow_up = random.randrange(0,500)
if blow_up == 1:
value = -1
return value
def mutate_up(value, factor, metricMin, metricMax):
v = value + random.randrange(-factor/5,factor)
if v < metricMin:
return value
elif v > metricMax:
return value
else:
return v
def mutate_down(value, factor, metricMin, metricMax):
v = value - random.randrange(-factor/5,factor)
if v < metricMin:
return value
elif v > metricMax:
return value
else:
return v
def mutate_even(value, factor, metricMin, metricMax):
v = value + random.randrange(-factor,factor)
if v < metricMin:
return value
elif v > metricMax:
return value
else:
return v
def mutate_fuel(value):
value -= random.randrange(0,10)
if value <= 0:
value = -1
return value
def mutate_oxygen(value):
value -= random.randrange(0,35)
if value <= 0:
value = -1
return value
def submit_metric (rocket, sensor_name, sensor_value):
print(time.time(),rocket, sensor_name, sensor_value)
sensor_timestamp = time.time()
response = cloudwatch.put_metric_data(
Namespace=rocket,
MetricData=[
{
'MetricName': sensor_name,
'Timestamp': sensor_timestamp,
'Value': sensor_value,
'Unit': 'None',
'StorageResolution': 1
},
]
)
if __name__ == "__main__":
try:
city = sys.argv[1]
print(city)
starttime=time.time()
#temperature = [0, 1, random.uniform(5, 50), 10, 25]
#pressure = [0, 2, random.uniform(15, 30), 10, 90]
while True:
#temperature = mutate_metric(temperature)
#sensor_values['temperature'] = temperature[0]
#pressure = mutate_metric(pressure)
#sensor_values['pressure'] = pressure[0]
sensor_values['temperature'] = mutate_even(sensor_values['temperature'], 3, -10, 60);
sensor_values['pressure'] = mutate_up(sensor_values['pressure'], 2, 800, 1200);
sensor_values['humidity'] = mutate_even(sensor_values['humidity'], 5, 0, 100)
sensor_values['rainfall'] = mutate_up(sensor_values['rainfall'], 7, 0, 3500)
sensor_values['windspeed'] = mutate_up(sensor_values['windspeed'], 3, 0, 300)
if (sensor_values['windspeed'] < sensor_values['windspeed_cat1']):
temp = threading.Thread(target=submit_metric, args=[city, 'temperature', sensor_values['temperature']])
press = threading.Thread(target=submit_metric, args=[city, 'pressure', sensor_values['pressure']])
hum = threading.Thread(target=submit_metric, args=[city, 'humidity', sensor_values['humidity']])
rain = threading.Thread(target=submit_metric, args=[city, 'rainfall', sensor_values['rainfall']])
wind = threading.Thread(target=submit_metric, args=[city, 'windspeed', sensor_values['windspeed']])
wind_cat1 = threading.Thread(target=submit_metric, args=[city, 'windspeed_cat1', sensor_values['windspeed_cat1']])
else:
#Hurricane happened
print('Hurricane!')
break
temp.start()
press.start()
hum.start()
rain.start()
wind.start()
wind_cat1.start()
time.sleep(1.0 - ((time.time() - starttime) % 1.0))
except KeyboardInterrupt:
print('Exiting due to keyboard interrupt', file=sys.stderr)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment