-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
171 lines (137 loc) · 5.63 KB
/
helper.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from math import floor
from random import random, choice, choices, randint
from datetime import datetime
from scipy.stats import gamma, uniform
from os import path
from yaml import load, Loader
import redis
import pprint
# TODO:
# ANSI Colors
W = "\033[0m" # white
R = "\033[31m" # red
G = "\033[32m" # green
O = "\033[33m" # orange
B = "\033[34m" # blue
P = "\033[35m" # purple
C = "\033[36m" # cyan
M = "\033[35m" # magenta
conf = load(open('config.yaml'), Loader=Loader)
# load constants from config.yaml file
ACTIONS = conf["actions"]["names"]
DEBIT_CARDS = conf["cards"]["names"]
UID_LIMIT = conf["limit"]["users"]
AID_LIMIT = conf["limit"]["articles"]
APP_NAME = conf["app"]
PROMOTIONS = conf["promotions"]["names"]
KAFKA_BOOTSTRAP_SERVERS = ":".join(conf["servers"]["kafka"].values())
JARS_DIR = path.join(path.abspath("") + "/" + conf["jars"])
REDIS_SERVER = conf["servers"]["redis"]
METRICS_SOCKETS = conf["servers"]["graphing_sockets"]
class Event:
def __init__(self):
'''
:params uid: user identificator
:params aid: article identificator
:params created_at: event emitted time
:params action: bind to kafka topic
'''
# action has the same name as the name of the class from where
# it (as attribut) is initiated
self.action = self.__class__.__name__.lower()
self.uid = Generator.get_gamma(UID_LIMIT)
self.aid = Generator.get_gamma(AID_LIMIT)
self.created_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
def __str__(self):
return f"( action={self.action}, time={self.created_at}, uid={self.uid}, aid={self.aid}"
class Purchasse(Event):
def __init__(self):
'''
:params number: number of article the user is about to buy
:params promotion: promotion that the user is using for this purchasse
:params card: card used by the current for purchassing
'''
super().__init__()
# self.action = "purchasse"
self.number = Generator.get_quantity()
self.promotion = Generator.get_promo()
self.card = Generator.get_card()
def __str__(self):
return super().__str__() + f", promotion={self.promotion}, card={self.card}, number={self.number} )"
class Basket(Event):
def __init__(self):
'''
:params number: number of article the user is about to buy
:params promotion: promotion that the user is using for this purchasse
'''
super().__init__()
self.number = Generator.get_quantity()
self.promotion = Generator.get_promo()
def __str__(self):
return super().__str__() + f", promotion={self.promotion}, number={self.number}" + f" )"
class Comment(Event):
def __init__(self):
super().__init__()
# get_message returns a comment about the article self.aid
self.body = Generator.get_message()
self.promotion = Generator.get_promo()
def __str__(self):
return super().__str__() + f", body={self.body}" + f" )"
def __str__(self):
return super().__str__() + f", body={self.body}" + f" )"
class Bookmark(Event):
def __init__(self):
super().__init__()
def __str__(self):
return super().__str__() + f" )"
class Click(Event):
def __init__(self):
super().__init__()
# self.body = Generator.get_message()
# self.promotion = Generator.get_promo()
def __str__(self):
return super().__str__() + f" )"
class Generator:
@staticmethod
def get_gamma(lim):
return int(gamma.rvs(a=5)/12*lim)
@staticmethod
def get_promo():
return choices(PROMOTIONS, weights=conf["promotions"]["dist"], k=1)[0]
@staticmethod
def get_quantity():
# generate the quantity of article the user is about to buy or
# have bought regarding some action, as choice used uniform distribution
if conf["quantities"]["static"] == False:
liminf = len(conf["quantities"]["values"])
conf["quantities"]["values"].pop(liminf-1) # pop out the last element
conf["quantities"]["values"].append(randint(liminf,conf["quantities"]["limsup"]))
return choices(conf["quantities"]["values"], weights=conf["quantities"]["dist"], k=1)[0]
@staticmethod
def get_message():
rconn = redis.Redis(
host=REDIS_SERVER["host"],
port=REDIS_SERVER["port"],
db=REDIS_SERVER["db"]
)
body = rconn.get(randint(1,conf["data_comment"]["size"]))
if body != None:
body = body.decode('ASCII')
return body
@staticmethod
def get_card():
return choices(DEBIT_CARDS, weights=conf["cards"]["dist"], k=1)[0]
@staticmethod
def emit():
# map each action to an event with the same name
events = list(
map(lambda action: eval(action), ACTIONS)
)
event = choices(events, weights=conf["actions"]["dist"], k=1)[0]()
return event
# ----------------------------------------------------------------------------
# Further informations
# ----------------------------------------------------------------------------
# Random fonctions can be manipulated/reseed using seed provided by the library
# from where it has been picked. Random function use a sequence variable in memory
# to keep track of (already) returned or not values.