Skip to content

Instantly share code, notes, and snippets.

@DustinAlandzes
Created June 13, 2017 21:07
Show Gist options
  • Save DustinAlandzes/9457af17d5dceddd2f0f6232a23ad81a to your computer and use it in GitHub Desktop.
Save DustinAlandzes/9457af17d5dceddd2f0f6232a23ad81a to your computer and use it in GitHub Desktop.
def build_dwell_times(pings, max_dwell, min_dwell):
"""
Builds a collection of dwell times from a collection of discrete pings.
@param pings: array of dicts with these attributes: {"mac":, "signal_strength:, "first_seen":, "last_seen":, "count":}
@param max_dwell: max time between pings before a new dwell time is added
@param min_dwell: min time between pings before they're considered sessions
@return an array of dwell times
"""
dwell_times = []
for index, this_ping in enumerate(pings):
# Append the first ping and move on the second
if index == 0:
dwell_times.append(this_ping)
continue
# Grab the last dwell_time
last_ping = dwell_times[-1]
# Compare locations.
if this_ping['node_mac'] == last_ping['node_mac']:
# if this ping's first seen - last ping's last_seen is less than
# the amount of time we set for max_dwell,
# then make the last ping's last_seen = this ping's last_seen
if this_ping["first_seen"] - last_ping["last_seen"] < max_dwell:
last_ping["last_seen"] = this_ping["last_seen"]
last_ping["count"] += this_ping["count"]
# Create a new dwell time if a user leaves and come back.
else:
dwell_times.append(this_ping)
# last ping isn't from the same device as this ping
# if last ping is stronger than this ping
# or if this ping's first seen - last ping's last_seen is greater than the amount we set for min_dwell
# then create a new dwell time
else:
current_signal_stronger = last_ping["signal_strength"] < this_ping["signal_strength"]
minimum_dwell_elapsed = this_ping["first_seen"] - last_ping["last_seen"] >= min_dwell
# Create a new dwell time if the user moves to a different location.
if current_signal_stronger or minimum_dwell_elapsed:
dwell_times.append(this_ping)
return dwell_times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment