-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
60 lines (44 loc) · 1.56 KB
/
database.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
from datetime import date
from pymongo import MongoClient
from settings import MONGO_HOST, MONGO_PORT
class MongoDatabase:
def __init__(self):
self.mongo_client = MongoClient(MONGO_HOST, MONGO_PORT, connect=False)
self.mongo_db = self.mongo_client['nginx_stats']
self.stats_collection = self.mongo_db['per_uri_access_stats']
self.telegram_bot_collection = self.mongo_db['telegram_bot_data']
def write_cache_to_db(self, dict_to_write):
self.stats_collection.update_one(
{'_id': date.today().strftime("%d_%m_%y")},
{'$inc': dict_to_write},
upsert=True)
def write_telegram_user_to_db(self, telegram_user_id):
"""
:type telegram_user_id: int
"""
self.telegram_bot_collection.update_one(
{'_id': 'telegram_users'},
{'$addToSet': {'ids': telegram_user_id}},
upsert=True
)
def get_stat_data_from_db(self, query_filter):
"""
:rtype: dict
"""
return self.stats_collection.find_one(
{'_id': query_filter})
def get_telegram_data_from_db(self):
"""
:rtype: dict
"""
return self.telegram_bot_collection.find_one(
{'_id': 'telegram_users'})
def remove_telegram_user_from_db(self, telegram_user_id):
"""
:type telegram_user_id: int
"""
self.telegram_bot_collection.update_one(
{'_id': 'telegram_users'},
{'$pull': {'ids': telegram_user_id}}
)
MAIN_MONGO = MongoDatabase()