-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
76 lines (61 loc) · 2.47 KB
/
service.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
"""service.py
Usage:
service.py -C <config-path>
service.py -h | --help
Options:
-h --help Show this screen
-C --config-path <config-path> path to config file
"""
import cachetools
import pandas as pd
from docopt import docopt
from redis import StrictRedis
from thriftpy.rpc import make_server
from bt_rts.services.allocations import allocation
from bt_rts.services.allocations import feedback
from bt_rts.services.allocations.experiments import DefaultExp
from bt_rts.thrift.gen.allocations_thrift import TAllocation, Allocations
from typing import Set, Dict
class AllocationService(object):
def __init__(self):
pass
@classmethod
def serve(cls, config):
dispatcher = cls()
server = make_server(Allocations, dispatcher, **config['thrift'])
server.serve()
def select_recommender(self, site_id: str,
bsin: str,
allowed_recommenders: Set[str]) -> TAllocation:
"""Returns an recommender allocation decision based on historic feedback."""
if type(allowed_recommenders) != set:
allowed_recommenders = set(allowed_recommenders)
print('needed to cast `allowed_recommenders` to set..')
decision = _select_recommender(site_id, bsin, allowed_recommenders)
print(decision)
return TAllocation(allocator=decision['allocator'], recommender=decision['arm'])
def _select_recommender(site_id: str,
bsin: str,
allowed_recommenders: Set[str]) -> Dict[str, str]:
# get users experiment params
exp_params = DefaultExp(bsin=bsin).get_params()
# get existing arms data (trials & successes)
arms = get_existing_arms_data(site_id)
return allocation.select_arm(arms, exp_params['allocator'], bsin, allowed_recommenders)
@cachetools.cached(cache=cachetools.TTLCache(maxsize=999, ttl=300))
def get_existing_arms_data(site_id: str) -> pd.DataFrame:
"""Query Allocator DB, return all recommender trials per site_id
TODO: implement real DB query
"""
return feedback.enumerate_keys(site_ids=list(site_id), experiments=['default'], recommender_ids=['fmv1', 'collb'])
if __name__ == '__main__':
print('running allocations service')
# CLI arg parsing
arguments = docopt(__doc__)
# config = {
# 'thrift': {
# 'host': 'localhost',
# 'port': 7070
# }
# }
AllocationService.serve(config)