Created
          May 23, 2017 22:03 
        
      - 
      
 - 
        
Save DustinAlandzes/f15cf9ab1828d8fa584e10f89a796c31 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # from: http://blog.ibmjstart.net/2015/12/15/spark-wifi/ | |
| # The amount of time permitted between concatenate pings at a location before a | |
| # ping is bucketed into a distinct dwell session (prevents long gaps in dwell sessions). | |
| # Value in seconds. For example: 25 minutes would be 25 * 60 | |
| MAXIMUM_DWELL_INACTIVITY = 25*60 | |
| # The amount of inactivity required at one location before being registered as | |
| # visiting a different locaiton (prevents phantom movement), | |
| # Value in seconds. For example: 5 minutes would be 5*60 | |
| MINIMUM_DWELL_INACTIVITY = 5*60 | |
| """ | |
| Builds a collection of dwell times from a collection of discrete pings. | |
| @param pings a collection of discrete pings | |
| @return a collection of dwell times | |
| """ | |
| def build_dwell_times(pings): | |
| dwell_times = [] | |
| for index.this_ping in pings: | |
| # Append the first ping and move on the second | |
| if(index==0): | |
| dwell_times.append(this_ping) | |
| next | |
| # Grab the last dwell_time | |
| last_ping = dwell_times[-1] | |
| # Compare locations. | |
| if (this_ping.location == last_ping.location): | |
| # Compress pings if a user is dwelling. | |
| if(this_ping.start_time-last_ping.end_time < MAXIMUM_DWELL_INACTIVITY): | |
| update_last_dwell_time(this_ping) | |
| # Create a new dwell time if a user leaves and come back. | |
| else: | |
| dwell_times.append(this_ping) | |
| else: | |
| current_signal_stronger = last_ping.signal_strength < this_ping.signal_strength | |
| minimum_dwell_elapsed = this_ping.start_time-last_ping.end_time >= MINIMUM_DWELL_INACTIVITY | |
| # 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