def get(cls, ip, column_start=None, column_finish=None, column_count=100, column_reversed=True): """Get the times an IP address has accessed various account IDs. Returns a list of dicts of the times an IP address has accessed various account IDs, most recent first: Example: >>> AccountsByIP.get('127.0.0.1') [ {datetime.datetime(2016, 1, 22, 23, 28, 21, 286000, tzinfo=<UTC>): 52}, {datetime.datetime(2016, 1, 22, 23, 28, 24, 301000, tzinfo=<UTC>): 53}, ] Pagination is also supported. See the documentation for ``IPsByAccount.get``. """ column_start = column_start or "" column_finish = column_finish or "" results = [] query = tdb_cassandra.ColumnQuery( cls, (ip,), column_start=column_start, column_finish=column_finish, column_count=column_count, column_reversed=column_reversed) for date_account in query: for dt, account in date_account.iteritems(): results.append({dt.replace(tzinfo=pytz.utc): int(account)}) return results
def for_sr(cls, srid36, count=5): """ Return the subreddits ID36s recommended by the sr whose id36 is passed """ cq = tdb_cassandra.ColumnQuery(cls, [srid36], column_count = count+1, column_reversed = True) recs = [ r.values()[0] for r in cq if r.values()[0] != srid36 ][:count] return recs
def get_recent(cls, sr, page_name, count=100): rowkey = WikiPage.id_for(sr, page_name) q = tdb_cassandra.ColumnQuery(cls, (rowkey, ), column_count=count) q = list(q) if not q: return [] ret = [] for col in q: for uuid, entry in col.iteritems(): entry = json.loads(entry) entry['uuid'] = str(uuid) ts = datetime.fromtimestamp(entry['timestamp']) ts = pytz.UTC.localize(ts) entry['timestamp'] = ts ret.append(entry) return ret
def get(cls, account_id, column_start=None, column_finish=None, column_count=100, column_reversed=True): """Get the last accessed times of an account by IP address. Returns a list of dicts of the last accessed times of an account by IP address, most recent first. Example: >>> IPsByAccount.get(52) [ {datetime.datetime(2016, 1, 24, 6, 23, 0, 326000, tzinfo=<UTC>): '127.0.0.3'}, {datetime.datetime(2016, 1, 24, 6, 22, 58, 983000, tzinfo=<UTC>): '127.0.0.2'}, ] Pagination is done based on the date of the entry. For instance, to continue getting results from the previous set: >>> IPsByAccount.get(52, column_start=datetime.datetime( 2016, 1, 24, 6, 22, 58, 983000)) [ {datetime.datetime(2016, 1, 24, 6, 21, 50, 121000, tzinfo=<UTC>): '127.0.0.1'}, ] """ column_start = column_start or "" column_finish = column_finish or "" results = [] query = tdb_cassandra.ColumnQuery( cls, (str(account_id),), column_start=column_start, column_finish=column_finish, column_count=column_count, column_reversed=column_reversed) for date_ip in query: for dt, ip in date_ip.iteritems(): results.append({dt.replace(tzinfo=pytz.utc): ip}) return results