-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
52 lines (36 loc) · 1.3 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import redis
from datetime import datetime, date
from typing import List
r = redis.Redis(host='redis')
class Product():
def __init__(self, id: str, brand: str, colors: str, dateAdded: str = None):
self.id = id
self.brand = brand
self.colors = colors
self.dateAdded = dateAdded
def as_json(self):
return json.dumps(self.__dict__)
# TODO: from_json
class RecentProduct():
def __init__(self, date: str):
self.date = date
def set(self, product: Product):
r.set(f'products:date:{self.date}', product.as_json())
def get(self):
return r.get(f'products:date:{self.date}')
class RecentProductsByColor():
def __init__(self, color: str):
self.color = color.lower()
def add(self, product: Product, timestamp: float):
# TODO: ensure max cap on the list
r.zadd(f'products:color:{self.color}', {product.as_json(): timestamp})
def get(self, count=10):
return r.zrevrange(f'products:color:{self.color}', 0, count)
class BrandCount():
def __init__(self, date: str):
self.date = date
def set(self, brand: str, count: int):
r.zadd(f'brands:date:{self.date}', {brand: count})
def get(self):
return r.zrevrange(f'brands:date:{self.date}', 0, -1, withscores=True)