def testSimpleIntervals(self): td_days = timedelta(days=7) time_start = datetime.utcnow() - timedelta(days=365) time_end = datetime.utcnow() tree = IntervalNode(time_start.toordinal(), time_end.toordinal()) int_end = time_start + td_days int_start = time_start counter = 0 while int_end < time_end: tree.insert(int_start.toordinal(), int_end.toordinal(), other="week %d" % counter) int_start = int_end int_end = int_end+td_days # print "inserting week: %d" % counter counter = counter + 1 td_hours = timedelta(hours=4) start_check = time_start check_time = time_start day_count = 0 hour_count = 0 self.node_hits = 0 while check_time < time_end: def report_schedule(node): if node.other is not None: self.node_hits += 1 tree.intersect(check_time.toordinal(), check_time.toordinal(), report_schedule) check_time = check_time + td_hours if check_time > start_check+td_days: #print "node hits per interval: %d" % self.node_hits self.node_hits = 0 start_check = check_time
def get_schedule(cls, chw_username, override_date=None): """ Generate schedule object for a given username """ cached_schedules = None if override_date == None: nowdate = datetime.utcnow() else: nowdate = override_date day_intervaltree = {} if cached_schedules == None: #no documents, then we need to load them up db = CommCareCase.get_db() chw_schedules = db.view('pact/chw_dot_schedules', key=chw_username).all() to_cache = [] for item in chw_schedules: single_sched = item['value'] to_cache.append(single_sched) cache.set("%s_schedule" % (chw_username), json.dumps(to_cache), 3600) cached_arr = to_cache else: cached_arr = json.loads(cached_schedules) for single_sched in cached_arr: day_of_week = int(single_sched['day_of_week']) if day_of_week in day_intervaltree: daytree = day_intervaltree[day_of_week] else: #if there's no day of week indication for this, then it's just a null interval node. To start this node, we make it REALLY old. daytree = IntervalNode( get_seconds(datetime.min), get_seconds(nowdate + timedelta(days=10))) if single_sched['ended_date'] == None: enddate = nowdate + timedelta(days=9) else: enddate = iso_string_to_datetime(single_sched['ended_date']) startdate = iso_string_to_datetime(single_sched['active_date']) case_id = single_sched['case_id'] if 'error' in single_sched: #this is a non-showstopping issue due to quirks with older submissions logging.error("Error, no pactid: %s" % single_sched['error']) daytree.insert(get_seconds(startdate), get_seconds(enddate), other=case_id) day_intervaltree[day_of_week] = daytree return cls(chw_username, day_intervaltree, cached_arr)