@@ -90,6 +90,8 @@ def get_resource_data(self):
90
90
{"data.info.id" : param },
91
91
{'$inc' : {'data.info.views' : 1 }},
92
92
{'_id' : False })
93
+
94
+ MetricsHandler .add ("resource" )
93
95
return data
94
96
95
97
@run_on_executor ()
@@ -107,6 +109,7 @@ def search_resource(self):
107
109
]},
108
110
projection
109
111
)
112
+ MetricsHandler .add ("search" )
110
113
return dict (data = list (data ))
111
114
112
115
@gen .coroutine
@@ -149,17 +152,17 @@ def get(self):
149
152
class MetricsHandler (BaseHandler ):
150
153
executor = ThreadPoolExecutor (50 )
151
154
152
- @run_on_executor ()
153
- def set_metrics (self ):
154
- self .mongo .db ['metrics' ].update_one (
155
- {'type' : "access" }, {'$inc' : {'count' : 1 }},
156
- upsert = True
157
- )
158
- # today
159
- self .mongo .db ['metrics' ].update_one (
160
- {'type' : "today" }, {'$inc' : {'count' : 1 }},
155
+ @classmethod
156
+ def add (cls , type_name ):
157
+ cls .mongo .db ['metrics' ].update_one (
158
+ {'type' : type_name }, {'$inc' : {'count' : 1 }},
161
159
upsert = True
162
160
)
161
+
162
+ @run_on_executor ()
163
+ def set_metrics (self ):
164
+ self .add ("access" )
165
+ self .add ("today" )
163
166
self .set_status (HTTPStatus .CREATED )
164
167
return {}
165
168
@@ -209,7 +212,8 @@ def run_server(port, host, **kwargs):
209
212
210
213
def reset_day ():
211
214
m = Mongo ()
212
- m .db ["metrics" ].delete_one ({"type" : "today" })
215
+ query = {"$or" : [{"type" : "today" }, {"type" : "resource" }, {"type" : "search" }]}
216
+ m .db ["metrics" ].delete_many (query )
213
217
214
218
215
219
if __name__ == "__main__" :
0 commit comments