flask-pluginkit-accesscount 0.3.2__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: flask-pluginkit-accesscount
3
+ Version: 0.4.0
4
+ Summary: IP、PV、Endpoint Statistics with Response Time
5
+ Home-page: https://github.com/saintic/flask-pluginkit-accesscount
6
+ Download-URL: https://github.com/saintic/flask-pluginkit-accesscount
7
+ Author: Hiroshi.tao
8
+ Author-email: me@tcw.im
9
+ Keywords: flask-pluginkit
10
+ Classifier: Environment :: Web Environment
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: Implementation :: CPython
19
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
20
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: Flask-PluginKit>=3.8.0
25
+ Requires-Dist: prettytable>=3.16.0
26
+ Dynamic: author
27
+ Dynamic: author-email
28
+ Dynamic: classifier
29
+ Dynamic: description
30
+ Dynamic: description-content-type
31
+ Dynamic: download-url
32
+ Dynamic: home-page
33
+ Dynamic: keywords
34
+ Dynamic: license-file
35
+ Dynamic: requires-dist
36
+ Dynamic: summary
37
+
38
+ # AccessCount
39
+
40
+ PV、Endpoint 统计与接口耗时追踪插件
41
+
42
+ 基于 Flask-PluginKit 框架,采用**进程内存缓冲 + 批量刷入 Redis** 架构,兼顾统计准确性与写入性能。
43
+
44
+ ---
45
+
46
+ ## 功能
47
+
48
+ - **PV 统计**:按天统计页面浏览量
49
+ - **Endpoint 统计**:按天统计各 API 端点访问量
50
+ - **接口耗时**:记录每个请求耗时,查询各端点平均响应时间
51
+ - **表格打印**:CLI 工具,`print_pv` / `print_ep` / `print_ep_time`
52
+ - **数据接口**:`get_pv` / `get_ep` / `get_ep_time` 返回结构化 dict,供应用程序二次消费
53
+ - **自动过期**:Redis 中超过 90 天的数据自动清理(可配置)
54
+ - **优雅退出**:进程退出前自动 flush 内存中剩余数据,避免重启丢数
55
+ - **线程安全**:`threading.Lock` 保护内存统计,适配多线程 Worker
56
+
57
+ ---
58
+
59
+ ## Installation
60
+
61
+ ### Development
62
+
63
+ `pip install git+https://github.com/saintic/flask-pluginkit-accesscount@master`
64
+
65
+ ### PyPi Release
66
+
67
+ `pip install flask-pluginkit-accesscount`
68
+
69
+ ---
70
+
71
+ ## Usage
72
+
73
+ ```python
74
+ from flask_pluginkit import PluginManager
75
+
76
+ PluginManager(
77
+ plugin_packages=["flask_pluginkit_accesscount"],
78
+ install_packages=dict(pkgs=["flask-pluginkit-accesscount"]),
79
+ )
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Configuration
85
+
86
+ Flask Config 配置项:
87
+
88
+ | 配置键 | 类型 | 默认值 | 说明 |
89
+ |---|---|---|---|
90
+ | `PLUGINKIT_ACCESSCOUNT_FLUSH_TIMES` | int | 100 | 累计多少次请求后刷入 Redis |
91
+ | `PLUGINKIT_ACCESSCOUNT_FLUSH_INTERVAL` | int | 60 | 累计多少秒后刷入 Redis |
92
+ | `PLUGINKIT_ACCESSCOUNT_FLUSH_MIN_DATA` | int | 5 | 间隔触发刷入时最少累积多少条数据 |
93
+ | `PLUGINKIT_ACCESSCOUNT_KEY_PREFIX` | str | `pluginkit` | Redis key 前缀 |
94
+ | `PLUGINKIT_ACCESSCOUNT_REDIS_NAME` | str | `redis` | Redis 连接名称(从 Flask `g` 获取) |
95
+ | `PLUGINKIT_ACCESSCOUNT_REDIS_URL` | str | 无 | Redis 直连 URL(与 REDIS_NAME 二选一) |
96
+ | `PLUGINKIT_ACCESSCOUNT_DATA_RETENTION` | int | 90 | Redis 数据保留天数 |
97
+
98
+ ---
99
+
100
+ ## 内置路由
101
+
102
+ | 路由 | 说明 |
103
+ |---|---|
104
+ | `/_internal/access_count_stats` | 返回当前进程内存中的统计数据 JSON |
105
+
106
+ ---
107
+
108
+ ## CLI 查询工具
109
+
110
+ 用于命令行查询 Redis 中的统计数据:
111
+
112
+ ```python
113
+ from flask_pluginkit_accesscount import print_pv, print_ep, print_ep_time
114
+ from flask import current_app
115
+
116
+ # 查看最近 7 天的 PV
117
+ print_pv(7, redis_url="redis://localhost:6379/0")
118
+
119
+ # 查看最近 7 天的端点访问量
120
+ print_ep(7, redis_url="redis://localhost:6379/0")
121
+
122
+ # 查看最近 7 天的端点平均耗时
123
+ print_ep_time(7, redis_url="redis://localhost:6379/0")
124
+ ```
125
+
126
+ ---
127
+
128
+ ## 数据接口
129
+
130
+ 返回结构化 dict,供应用程序自定义展示、告警、导出:
131
+
132
+ ```python
133
+ from flask_pluginkit_accesscount import get_pv, get_ep, get_ep_time
134
+
135
+ # PV:返回 {"2026-12-06": 150, "2026-12-05": 200, ...}
136
+ data = get_pv(7, redis_url="redis://localhost:6379/0")
137
+
138
+ # 端点访问量:返回 {"2026-12-06": {"/api/users": 50, ...}, ...}
139
+ data = get_ep(7, redis_url="redis://localhost:6379/0")
140
+
141
+ # 平均耗时:返回 {"2026-12-06": {"/api/users": {"avg_ms": 45.2, "count": 100, "total_ms": 4520}, ...}, ...}
142
+ data = get_ep_time(7, redis_url="redis://localhost:6379/0")
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Redis 存储结构
148
+
149
+ ```
150
+ {prefix}:AccessCount:pv:hash → Hash: {YYYYMMDD: PV}
151
+ {prefix}:AccessCount:endpoint:hash → Hash: {YYYYMMDD:endpoint: 访问量}
152
+ {prefix}:AccessCount:endpoint:time:hash → Hash: {YYYYMMDD:endpoint: 总耗时(ms)}
153
+ {prefix}:AccessCount:endpoint:time_cnt:hash → Hash: {YYYYMMDD:endpoint: 请求次数}
154
+ ```
155
+
156
+ ---
157
+
158
+ ## 架构说明
159
+
160
+ 每个 HTTP 请求在 `after_request` 钩子中累加计数到进程内存,满足阈值(次数或时间)后通过 **Redis Pipeline** 批量写入。避免每次请求都直连 Redis,保证高并发下的写入性能。
161
+
162
+ 进程退出时(正常关闭、SIGTERM、Worker 回收),`atexit` 回调自动将内存中剩余数据刷入 Redis,避免重启丢数。
163
+
164
+ 超过 `PLUGINKIT_ACCESSCOUNT_DATA_RETENTION` 天(默认 90 天)的数据会自动清理 — 包括字段级精准删除和 key 级 TTL 兜底。
165
+
166
+ ---
167
+
168
+ ## License
169
+
170
+ BSD 3-Clause
@@ -0,0 +1,133 @@
1
+ # AccessCount
2
+
3
+ PV、Endpoint 统计与接口耗时追踪插件
4
+
5
+ 基于 Flask-PluginKit 框架,采用**进程内存缓冲 + 批量刷入 Redis** 架构,兼顾统计准确性与写入性能。
6
+
7
+ ---
8
+
9
+ ## 功能
10
+
11
+ - **PV 统计**:按天统计页面浏览量
12
+ - **Endpoint 统计**:按天统计各 API 端点访问量
13
+ - **接口耗时**:记录每个请求耗时,查询各端点平均响应时间
14
+ - **表格打印**:CLI 工具,`print_pv` / `print_ep` / `print_ep_time`
15
+ - **数据接口**:`get_pv` / `get_ep` / `get_ep_time` 返回结构化 dict,供应用程序二次消费
16
+ - **自动过期**:Redis 中超过 90 天的数据自动清理(可配置)
17
+ - **优雅退出**:进程退出前自动 flush 内存中剩余数据,避免重启丢数
18
+ - **线程安全**:`threading.Lock` 保护内存统计,适配多线程 Worker
19
+
20
+ ---
21
+
22
+ ## Installation
23
+
24
+ ### Development
25
+
26
+ `pip install git+https://github.com/saintic/flask-pluginkit-accesscount@master`
27
+
28
+ ### PyPi Release
29
+
30
+ `pip install flask-pluginkit-accesscount`
31
+
32
+ ---
33
+
34
+ ## Usage
35
+
36
+ ```python
37
+ from flask_pluginkit import PluginManager
38
+
39
+ PluginManager(
40
+ plugin_packages=["flask_pluginkit_accesscount"],
41
+ install_packages=dict(pkgs=["flask-pluginkit-accesscount"]),
42
+ )
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Configuration
48
+
49
+ Flask Config 配置项:
50
+
51
+ | 配置键 | 类型 | 默认值 | 说明 |
52
+ |---|---|---|---|
53
+ | `PLUGINKIT_ACCESSCOUNT_FLUSH_TIMES` | int | 100 | 累计多少次请求后刷入 Redis |
54
+ | `PLUGINKIT_ACCESSCOUNT_FLUSH_INTERVAL` | int | 60 | 累计多少秒后刷入 Redis |
55
+ | `PLUGINKIT_ACCESSCOUNT_FLUSH_MIN_DATA` | int | 5 | 间隔触发刷入时最少累积多少条数据 |
56
+ | `PLUGINKIT_ACCESSCOUNT_KEY_PREFIX` | str | `pluginkit` | Redis key 前缀 |
57
+ | `PLUGINKIT_ACCESSCOUNT_REDIS_NAME` | str | `redis` | Redis 连接名称(从 Flask `g` 获取) |
58
+ | `PLUGINKIT_ACCESSCOUNT_REDIS_URL` | str | 无 | Redis 直连 URL(与 REDIS_NAME 二选一) |
59
+ | `PLUGINKIT_ACCESSCOUNT_DATA_RETENTION` | int | 90 | Redis 数据保留天数 |
60
+
61
+ ---
62
+
63
+ ## 内置路由
64
+
65
+ | 路由 | 说明 |
66
+ |---|---|
67
+ | `/_internal/access_count_stats` | 返回当前进程内存中的统计数据 JSON |
68
+
69
+ ---
70
+
71
+ ## CLI 查询工具
72
+
73
+ 用于命令行查询 Redis 中的统计数据:
74
+
75
+ ```python
76
+ from flask_pluginkit_accesscount import print_pv, print_ep, print_ep_time
77
+ from flask import current_app
78
+
79
+ # 查看最近 7 天的 PV
80
+ print_pv(7, redis_url="redis://localhost:6379/0")
81
+
82
+ # 查看最近 7 天的端点访问量
83
+ print_ep(7, redis_url="redis://localhost:6379/0")
84
+
85
+ # 查看最近 7 天的端点平均耗时
86
+ print_ep_time(7, redis_url="redis://localhost:6379/0")
87
+ ```
88
+
89
+ ---
90
+
91
+ ## 数据接口
92
+
93
+ 返回结构化 dict,供应用程序自定义展示、告警、导出:
94
+
95
+ ```python
96
+ from flask_pluginkit_accesscount import get_pv, get_ep, get_ep_time
97
+
98
+ # PV:返回 {"2026-12-06": 150, "2026-12-05": 200, ...}
99
+ data = get_pv(7, redis_url="redis://localhost:6379/0")
100
+
101
+ # 端点访问量:返回 {"2026-12-06": {"/api/users": 50, ...}, ...}
102
+ data = get_ep(7, redis_url="redis://localhost:6379/0")
103
+
104
+ # 平均耗时:返回 {"2026-12-06": {"/api/users": {"avg_ms": 45.2, "count": 100, "total_ms": 4520}, ...}, ...}
105
+ data = get_ep_time(7, redis_url="redis://localhost:6379/0")
106
+ ```
107
+
108
+ ---
109
+
110
+ ## Redis 存储结构
111
+
112
+ ```
113
+ {prefix}:AccessCount:pv:hash → Hash: {YYYYMMDD: PV}
114
+ {prefix}:AccessCount:endpoint:hash → Hash: {YYYYMMDD:endpoint: 访问量}
115
+ {prefix}:AccessCount:endpoint:time:hash → Hash: {YYYYMMDD:endpoint: 总耗时(ms)}
116
+ {prefix}:AccessCount:endpoint:time_cnt:hash → Hash: {YYYYMMDD:endpoint: 请求次数}
117
+ ```
118
+
119
+ ---
120
+
121
+ ## 架构说明
122
+
123
+ 每个 HTTP 请求在 `after_request` 钩子中累加计数到进程内存,满足阈值(次数或时间)后通过 **Redis Pipeline** 批量写入。避免每次请求都直连 Redis,保证高并发下的写入性能。
124
+
125
+ 进程退出时(正常关闭、SIGTERM、Worker 回收),`atexit` 回调自动将内存中剩余数据刷入 Redis,避免重启丢数。
126
+
127
+ 超过 `PLUGINKIT_ACCESSCOUNT_DATA_RETENTION` 天(默认 90 天)的数据会自动清理 — 包括字段级精准删除和 key 级 TTL 兜底。
128
+
129
+ ---
130
+
131
+ ## License
132
+
133
+ BSD 3-Clause