def report_observation_with_db(ndb, service, fp): """Insert or update an Observation record in the notary database.""" cur_time = int(time.time()) obs = ndb.get_observations(service) most_recent_time_by_key = {} most_recent_key = None most_recent_time = 0 # calculate the most recently seen key for (service, key, start, end) in obs: if key not in most_recent_time_by_key or end > most_recent_time_by_key[key]: most_recent_time_by_key[key] = end for k in most_recent_time_by_key: if most_recent_time_by_key[k] > most_recent_time: most_recent_key = k most_recent_time = most_recent_time_by_key[k] ndb.close_session() if most_recent_key == fp: # this key matches the most recently seen key before this observation. # just update the observation 'end' time. ndb.update_observation_end_time(service, fp, most_recent_time, cur_time) ndb.report_metric('ServiceScanKeyUpdated', service) else: # the key has changed or no observations exist yet for this service. # add a new entry for this key with start and end set to the current time ndb.insert_observation(service, fp, cur_time, cur_time) if most_recent_key != None: # if there was a previous key, set its 'end' timespan value to be # the current time minus one second (ending just before the new key) ndb.update_observation_end_time(service, most_recent_key, most_recent_time, cur_time -1) ndb.report_metric('ServiceScanPrevKeyUpdated', service)
# pass ndb the args so it can use any relevant ones from its own parser ndb = ndb(args) output_file = args.output_file # set a default action if (args.all == False and args.newer == None and args.older == None): args.all = True ids = None if args.all: ids = ndb.get_all_services() else: cur_time = int(time.time()) if args.older: ids = ndb.get_oldest_services(int(cur_time - (3600 * 24 * args.older))) else: ids = ndb.get_newest_services(int(cur_time - (3600 * 24 * args.newer))) ndb.close_session() if (ids != None): for (name) in ids: # print as string instead of tuple, to make it easier to use elsewhere print >> output_file, name[0]