Exemplo n.º 1
0
    def put(self, entity_id, entity_type, segmentation, total_count=None):
        timeslices = get_timeslices()
        for timeslice in timeslices:
            match_criteria = {
                "entity_id": entity_id,
                "entity_type": entity_type,
                "segmentation": segmentation,
                "timeslice": timeslice,
            }
            if total_count:
                #Note - This is non-atomic and so could cause inaccuracies
                #TODO: Implement locking
                existing = list(self._store.find(match_criteria))
                if existing:
                    existing = existing[0]
		    if existing.get("base_count"):
                        update_op = {
		            "$set": {
			        "count": total_count - existing["base_count"] + 1
			    }
	                }
		    else:
		        raise ValueError("Tried to increment doc with no base_count using total_count")
                else:
                    update_op = {
		        "$set": {
			    "base_count": total_count,
			    "count": 1
			}
	            }
            else:
                update_op = {
		    "$inc": {"count": 1}
		}
            self._store.update(match_criteria, update_op, upsert=True)
Exemplo n.º 2
0
 def test_get_timeslices(self):
     jan_1st_2013_midday = fixture.jan_1st_2013_midday
     fifteen_mins = 60*15
     actual = utils.get_timeslices(jan_1st_2013_midday)
     expected = [
         jan_1st_2013_midday - fifteen_mins * 8,
         jan_1st_2013_midday - fifteen_mins * 7,
         jan_1st_2013_midday - fifteen_mins * 6,
         jan_1st_2013_midday - fifteen_mins * 5,
         jan_1st_2013_midday - fifteen_mins * 4,
         jan_1st_2013_midday - fifteen_mins * 3,
         jan_1st_2013_midday - fifteen_mins * 2,
         jan_1st_2013_midday - fifteen_mins * 1,
         jan_1st_2013_midday - fifteen_mins * 0, 
     ]
     self.assertEquals(expected, actual)
     
     actual = utils.get_timeslices(jan_1st_2013_midday + 1)
     expected = [
         jan_1st_2013_midday - fifteen_mins * 8,
         jan_1st_2013_midday - fifteen_mins * 7,
         jan_1st_2013_midday - fifteen_mins * 6,
         jan_1st_2013_midday - fifteen_mins * 5,
         jan_1st_2013_midday - fifteen_mins * 4,
         jan_1st_2013_midday - fifteen_mins * 3,
         jan_1st_2013_midday - fifteen_mins * 2,
         jan_1st_2013_midday - fifteen_mins * 1,
         jan_1st_2013_midday - fifteen_mins * 0, 
     ]
     self.assertEquals(expected, actual)
     
     actual = utils.get_timeslices(jan_1st_2013_midday - 1)
     expected = [
         jan_1st_2013_midday - fifteen_mins * 9,
         jan_1st_2013_midday - fifteen_mins * 8,
         jan_1st_2013_midday - fifteen_mins * 7,
         jan_1st_2013_midday - fifteen_mins * 6,
         jan_1st_2013_midday - fifteen_mins * 5,
         jan_1st_2013_midday - fifteen_mins * 4,
         jan_1st_2013_midday - fifteen_mins * 3,
         jan_1st_2013_midday - fifteen_mins * 2,
         jan_1st_2013_midday - fifteen_mins * 1, 
     ]
     self.assertEquals(expected, actual)
Exemplo n.º 3
0
 def get_top(self, entity_type, segmentation, num_to_get, timestamp=None):
     """Get the top entities in this segmentation."""
     result = []
     timestamp = timestamp or int(time.time())
     #We use the latest COMPLETE timeslice, not the latest one
     timeslice = get_timeslices(timestamp)[-1]
     query = {
         "entity_type": entity_type,
         "segmentation": segmentation,
         "timeslice": timeslice,
     }
     fields = {
         "entity_id": 1,
         "count": 1,
     }
     docs = self._store.find(query, fields=fields).sort("count", -1).limit(num_to_get)
     for doc in docs:
         result.append((doc["entity_id"], doc["count"]))
     return result