Skip to content

Commit 7bf2e70

Browse files
committed
add access count metrics
1 parent 6a1e3da commit 7bf2e70

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

web/index.html

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
width: 75%;
3333
}
3434

35-
a::after {
36-
content: attr(title);
37-
display: none;
38-
}
35+
/*a::after {*/
36+
/* content: attr(title);*/
37+
/* display: none;*/
38+
/*}*/
3939

40-
a:hover::after {
41-
display: block;
42-
}
40+
/*a:hover::after {*/
41+
/* display: block;*/
42+
/*}*/
4343

4444
</style>
4545
</head>
@@ -67,6 +67,7 @@ <h2>有问题联系 <a style="text-decoration: none;color: green" href="https://
6767
</div>
6868
</body>
6969
<script src="js/axios.min.js"></script>
70+
<script src="js/analytics.js"></script>
7071

7172
<script>
7273
// get top
@@ -99,5 +100,7 @@ <h2>有问题联系 <a style="text-decoration: none;color: green" href="https://
99100

100101
}
101102

103+
accessMetrics();
104+
102105
</script>
103106
</html>

web/js/analytics.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function accessMetrics() {
2+
axios.post('/api/metrics')
3+
.then(function (response) {
4+
// console.log(response);
5+
})
6+
.catch(function (error) {
7+
// console.log(error);
8+
});
9+
}

web/resource.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@
660660
getData: function getData() {
661661
var me = this;
662662
let resource_id = document.URL.split("id=")[1]
663-
$.get("/api/resource?id="+resource_id, function (json) {
663+
$.get("/api/resource?id=" + resource_id, function (json) {
664664
// json.data.info.expire < (Date.now()/1000)
665665
if (json.status != 1 || !json.data) {
666666
return location.href = location.origin
@@ -854,6 +854,9 @@
854854
// });
855855
}
856856

857+
$.post("/api/metrics", function (data) {
858+
});
859+
857860
</script>
858861
</body>
859862
</html>

web/search.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<link rel="icon" type="image/x-icon" href="favicon.ico">
77
<link href="https://fonts.googleapis.com/css?family=Pacifico&display=swap" rel="stylesheet">
88
<link href="/css/normalize.min.css" rel="stylesheet">
9-
<script src="js/axios.min.js"></script>
109
<style>
1110
h1 {
1211
font-family: Pacifico, sans-serif;
@@ -54,6 +53,9 @@ <h2>在 <a style="text-decoration: none;color: deepskyblue" href="https://t.me/y
5453
<h2>有问题请联系 <a style="text-decoration: none;color: green" href="https://t.me/BennyThink">Benny小可爱</a></h2>
5554
</div>
5655
</body>
56+
<script src="js/axios.min.js"></script>
57+
<script src="js/analytics.js"></script>
58+
5759
<script>
5860
let kwe = document.URL.split("kw=")[1];
5961
let kw = decodeURI(kwe).toLowerCase().replace(" ", "");
@@ -93,5 +95,7 @@ <h2>有问题请联系 <a style="text-decoration: none;color: green" href="https
9395
div.innerHTML = `<h2>没有搜索到结果 (ノへ ̄、)</h2>`
9496
}
9597
}
98+
99+
accessMetrics();
96100
</script>
97101
</html>

web/server.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from platform import uname
1313
import os
1414
import contextlib
15+
from http import HTTPStatus
1516
from concurrent.futures import ThreadPoolExecutor
1617
from tornado import web, ioloop, httpserver, gen, options
1718
from tornado.log import enable_pretty_logging
@@ -98,17 +99,31 @@ def get(self):
9899
self.write(resp)
99100

100101

101-
class PingHandler(BaseHandler):
102+
class MetricsHandler(BaseHandler):
102103
executor = ThreadPoolExecutor(50)
103104

104105
@run_on_executor()
105-
def ping(self):
106-
os.system('ping z.cn -n 10')
107-
return 'Ping complete'
106+
def set_metrics(self):
107+
metrics_name = self.get_query_argument("type", "access")
108+
db['metrics'].find_one_and_update(
109+
{'type': metrics_name}, {'$inc': {'count': 1}}
110+
)
111+
self.set_status(HTTPStatus.CREATED)
112+
return {}
113+
114+
@run_on_executor()
115+
def get_metrics(self):
116+
metrics_name = self.get_query_argument("type", "access")
117+
return db['metrics'].find_one({'type': metrics_name}, {'_id': False})
108118

109119
@gen.coroutine
110120
def get(self):
111-
resp = yield self.ping()
121+
resp = yield self.get_metrics()
122+
self.write(resp)
123+
124+
@gen.coroutine
125+
def post(self):
126+
resp = yield self.set_metrics()
112127
self.write(resp)
113128

114129

@@ -118,6 +133,7 @@ class RunServer:
118133
handlers = [
119134
(r'/api/resource', ResourceHandler),
120135
(r'/api/top', TopHandler),
136+
(r'/api/metrics', MetricsHandler),
121137
(r'/', IndexHandler),
122138
(r'/(.*\.html|.*\.js|.*\.css|.*\.png|.*\.jpg|.*\.ico|.*\.gif|.*\.woff2)', web.StaticFileHandler,
123139
{'path': static_path}),

0 commit comments

Comments
 (0)