Skip to content

Instantly share code, notes, and snippets.

View rbotzer's full-sized avatar
🦄
Productizing

Ronen Botzer rbotzer

🦄
Productizing
View GitHub Profile
@rbotzer
rbotzer / alertmanager-config.yaml
Created May 27, 2020 18:57
An example of configuring a slack channel for Alertmanager alerts
# This is an example alertmanager.yaml which sends alert notifications to a slack channel.
global:
slack_api_url: "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
route:
group_by: ['alertname', 'cluster', 'service']
receiver: slack_general
receivers:
- name: slack_general
# Clear all the segments that expire before today from the user u1
print("Clean the stale segments for user u1")
if options.interactive:
pause()
ops = [
mh.map_remove_by_value_range(
"u",
[0, aerospike.null()],
[end_ttl, aerospike.null()],
aerospike.MAP_RETURN_COUNT,
# Count all the segments in the segment ID range 8000-9000
print("\nCount how many segments u1 has in the segment ID range 8000-9000")
if options.interactive:
pause()
ops = [mh.map_get_by_key_range("u", 8000, 9000, aerospike.MAP_RETURN_COUNT)]
_, _, b = client.operate(key, ops)
print(b["u"])
print(spacer)
# Fetch the user's segments that are not going to expire today
today = datetime.datetime(now.year, now.month, now.day)
end_ttl = int((today - epoch).total_seconds() / 3600)
print("\nGet only user segments that are not going to expire today")
if options.interactive:
pause()
ops = [
mh.map_get_by_value_range(
"u",
[0, aerospike.null()],
# Update a segment's TTL by 5 extra hours
# First, the context for the list increment (path to the TTL)
ctx = [ctxh.cdt_ctx_map_key(segment_id)]
ops = [
lh.list_increment("u", 0, 5, ctx=ctx),
mh.map_get_by_key("u", segment_id, aerospike.MAP_RETURN_KEY_VALUE),
]
print("\nAdd 5 hours to the TTL of user u1's segment {}".format(segment_id))
if options.interactive:
pause()
# Update multiple segments of a user profile
segments = {}
ttl_dt = now + datetime.timedelta(days=30)
segment_ttl = int((ttl_dt - epoch).total_seconds() / 3600)
for i in range(8):
segment_id = random.randint(0, 81999)
segments[segment_id] = [segment_ttl, {}]
print("\nUpdating multiple segments for user u1")
pp.pprint(segments)
if options.interactive:
# Upsert a single segment in the user profile
key = (namespace, set, "u1")
segment_id = random.randint(0, 81999)
ttl_dt = now + datetime.timedelta(days=30)
segment_ttl = int((ttl_dt - epoch).total_seconds() / 3600)
ops = [
mh.map_get_by_key("u", segment_id, aerospike.MAP_RETURN_KEY_VALUE),
mh.map_put("u", segment_id, [segment_ttl, {}]),
mh.map_get_by_key("u", segment_id, aerospike.MAP_RETURN_KEY_VALUE),
]
@rbotzer
rbotzer / scan-the-dataset.py
Created December 4, 2019 04:56
Scan the entire dataset for a random sample
def print_sensor_data(rec, pp):
k, _, b = rec
print(k[2])
print(b['t'])
print("=" * 30)
print("\nScan for a random sampling (about 0.25%) of all the sensor data")
if options.interactive:
pause()
@rbotzer
rbotzer / one-day-all-sensors.py
Created December 4, 2019 04:48
One day of data for all 1000 sensors
print("\nGet the data from all sensors for June 19")
if options.interactive:
pause()
dt = datetime.datetime(2018, 1, 1, 0, 0, 0)
keys = []
for i in range(1, 1001):
keys.append((namespace, set,"sensor{}-06-19".format(i)))
one_day_all_sensors = client.get_many(keys)
for rec in one_day_all_sensors:
k, _, b = rec
@rbotzer
rbotzer / one-sensor-one-year.py
Created December 4, 2019 04:42
One year of data for one sensor
print("\nGet a year's data for sensor{}".format(sensor_id))
if options.interactive:
pause()
dt = datetime.datetime(2018, 1, 1, 0, 0, 0)
keys = []
for i in range(1, 366):
keys.append((namespace, set,"sensor{}-{:02d}-{:02d}".format(sensor_id, dt.month, dt.day)))
dt = dt + timedelta(days=1)
sensor_year = client.get_many(keys)
for rec in sensor_year: