flowdeck 0.2.1__py3-none-any.whl
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.
- flowdeck/__init__.py +65 -0
- flowdeck/_version.py +1 -0
- flowdeck/api/__init__.py +216 -0
- flowdeck/api/routers/__init__.py +1 -0
- flowdeck/api/routers/dashboard.py +73 -0
- flowdeck/api/routers/health.py +26 -0
- flowdeck/api/routers/notifications.py +92 -0
- flowdeck/api/routers/pipelines.py +274 -0
- flowdeck/api/routers/runs.py +227 -0
- flowdeck/api/routers/schedule_exceptions.py +141 -0
- flowdeck/api/routers/system.py +386 -0
- flowdeck/api/routers/ui_pipelines.py +507 -0
- flowdeck/api/routers/ui_task_tools.py +275 -0
- flowdeck/api/routers/webhooks.py +115 -0
- flowdeck/auth/__init__.py +0 -0
- flowdeck/auth/basic.py +65 -0
- flowdeck/cli.py +40 -0
- flowdeck/config.py +240 -0
- flowdeck/database/__init__.py +0 -0
- flowdeck/database/base.py +91 -0
- flowdeck/database/models.py +218 -0
- flowdeck/database/repository.py +1231 -0
- flowdeck/logger/__init__.py +122 -0
- flowdeck/logger/formatter.py +112 -0
- flowdeck/logger/web_socket_handler.py +85 -0
- flowdeck/notifications/__init__.py +13 -0
- flowdeck/notifications/email_sender.py +79 -0
- flowdeck/notifications/engine.py +412 -0
- flowdeck/notifications/rules.py +22 -0
- flowdeck/notifications/table_image.py +273 -0
- flowdeck/notifications/task_result.py +478 -0
- flowdeck/notifications/templates.py +224 -0
- flowdeck/orchestrator/__init__.py +380 -0
- flowdeck/orchestrator/executor.py +661 -0
- flowdeck/pipeline/__init__.py +5 -0
- flowdeck/pipeline/_utils.py +11 -0
- flowdeck/pipeline/context.py +13 -0
- flowdeck/pipeline/pipeline.py +40 -0
- flowdeck/pipeline/schedule.py +125 -0
- flowdeck/pipeline/task.py +104 -0
- flowdeck/pipeline/trigger.py +22 -0
- flowdeck/schemas.py +11 -0
- flowdeck/static/assets/BackfillModal-BViyo-00.js +1 -0
- flowdeck/static/assets/BackfillModal-D6je7CRx.css +1 -0
- flowdeck/static/assets/Btn-B77BQZvQ.js +1 -0
- flowdeck/static/assets/Btn-D6G_tsbF.css +1 -0
- flowdeck/static/assets/DashboardView-BM-S5j64.js +1 -0
- flowdeck/static/assets/DashboardView-CBu0kYw6.css +1 -0
- flowdeck/static/assets/DatePickerField-IOKwxOwp.js +830 -0
- flowdeck/static/assets/Empty-B5kRTX_h.css +1 -0
- flowdeck/static/assets/Empty-ScitfHnD.js +1 -0
- flowdeck/static/assets/Modal-7xA4aZKG.js +1 -0
- flowdeck/static/assets/Modal-BNFhtIvK.css +1 -0
- flowdeck/static/assets/NotificationsView-BTkWFeLt.css +1 -0
- flowdeck/static/assets/NotificationsView-BkofWdjj.js +1 -0
- flowdeck/static/assets/NotifySettings-BFdtzy6K.js +1 -0
- flowdeck/static/assets/NotifySettings-D9Nn2RLl.css +1 -0
- flowdeck/static/assets/NumInput--MSEekvd.js +582 -0
- flowdeck/static/assets/NumInput-DfqPA-uG.css +1 -0
- flowdeck/static/assets/PageHeader-3D30Zbvm.js +1 -0
- flowdeck/static/assets/PageHeader-BULVGwbD.css +1 -0
- flowdeck/static/assets/PipelineDetailView-89815vAO.js +1 -0
- flowdeck/static/assets/PipelineDetailView-B_iVBCYT.css +1 -0
- flowdeck/static/assets/PipelinesView-CtDub3qu.js +1 -0
- flowdeck/static/assets/PipelinesView-DC9QO4Wr.css +1 -0
- flowdeck/static/assets/PrettyJson-DjAv38hk.css +1 -0
- flowdeck/static/assets/PrettyJson-_gjY71Cb.js +1 -0
- flowdeck/static/assets/RecentFailuresView-CruW_Dj8.js +1 -0
- flowdeck/static/assets/RecentFailuresView-DjQsiRv9.css +1 -0
- flowdeck/static/assets/RunDetailView--NojY8JX.css +1 -0
- flowdeck/static/assets/RunDetailView-2ZVPmVgW.js +5 -0
- flowdeck/static/assets/ScheduleExceptionsView-DdccyMcD.js +2 -0
- flowdeck/static/assets/ScheduleExceptionsView-pWTB9mSE.css +1 -0
- flowdeck/static/assets/SettingsView-BwlizulV.js +4 -0
- flowdeck/static/assets/SettingsView-D4ybFG31.css +1 -0
- flowdeck/static/assets/StatCard-CdPYF8jZ.js +1 -0
- flowdeck/static/assets/StatCard-D06FdLL9.css +1 -0
- flowdeck/static/assets/StatsView-88bkL-AN.css +1 -0
- flowdeck/static/assets/StatsView-DONGQHCc.js +18 -0
- flowdeck/static/assets/StatusBadge-BDBPRgRa.css +1 -0
- flowdeck/static/assets/StatusBadge-DzUDQ0X_.js +1 -0
- flowdeck/static/assets/ToggleSwitch-KMgLwPsM.js +1 -0
- flowdeck/static/assets/ToggleSwitch-UNHeNS9K.css +1 -0
- flowdeck/static/assets/UiPipelinesView-BgzBgUYR.css +1 -0
- flowdeck/static/assets/UiPipelinesView-WTYqKxB5.js +2 -0
- flowdeck/static/assets/UiTaskEditorView-BgYyqqgT.js +10 -0
- flowdeck/static/assets/UiTaskEditorView-Dl6zmLDP.css +1 -0
- flowdeck/static/assets/_plugin-vue_export-helper-OBKhRxFI.js +12 -0
- flowdeck/static/assets/chakra-petch-latin-400-normal-D7EtbySE.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-400-normal-SafcrIr2.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-500-normal-BR1ody1F.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-500-normal-CnUQnZ4D.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-600-normal-DQKfcdKo.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-600-normal-DVQm9bgb.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-700-normal-CnDBPjkL.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-700-normal-D1s_c2du.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-400-normal-C8u6EFkq.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-400-normal-MNNNbyh_.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-500-normal-BCHeNDEx.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-500-normal-gA6791b0.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-600-normal-CdGvbdDU.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-600-normal-nL80L4xU.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-700-normal-BeviJPUl.woff +0 -0
- flowdeck/static/assets/chakra-petch-latin-ext-700-normal-DAkvJhej.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-thai-400-normal-Bw1Q-fVY.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-thai-400-normal-KdzBjiWs.woff +0 -0
- flowdeck/static/assets/chakra-petch-thai-500-normal-CJG_V2_m.woff +0 -0
- flowdeck/static/assets/chakra-petch-thai-500-normal-DeexCGiz.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-thai-600-normal-BiM5MXH8.woff +0 -0
- flowdeck/static/assets/chakra-petch-thai-600-normal-C620THcd.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-thai-700-normal-B7WL5pBr.woff2 +0 -0
- flowdeck/static/assets/chakra-petch-thai-700-normal-vZLZ_5L8.woff +0 -0
- flowdeck/static/assets/chakra-petch-vietnamese-400-normal-Cn7ya5On.woff +0 -0
- flowdeck/static/assets/chakra-petch-vietnamese-500-normal-BVzUBLGs.woff +0 -0
- flowdeck/static/assets/chakra-petch-vietnamese-600-normal-Pvj4qcw_.woff +0 -0
- flowdeck/static/assets/chakra-petch-vietnamese-700-normal-gQuUA8Wu.woff +0 -0
- flowdeck/static/assets/index-DpymzEZv.css +1 -0
- flowdeck/static/assets/index-DuXbXiXo.js +2 -0
- flowdeck/static/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-cyrillic-400-normal-ugxPyKxw.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-cyrillic-500-normal-DJqRU3vO.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-cyrillic-500-normal-DmUKJPL_.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-cyrillic-700-normal-BWTpRfYl.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-cyrillic-700-normal-CEoEElIJ.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-greek-400-normal-B9oWc5Lo.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-greek-500-normal-D7SFKleX.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-greek-500-normal-JpySY46c.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-greek-700-normal-C6CZE3T8.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-greek-700-normal-DEigVDxa.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-400-normal-6-qcROiO.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-500-normal-BWZEU5yA.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-500-normal-CJOVTJB7.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-700-normal-BYuf6tUa.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-700-normal-D3wTyLJW.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-ext-400-normal-fXTG6kC5.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-ext-500-normal-Cut-4mMH.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-ext-500-normal-ckzbgY84.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-ext-700-normal-CZipNAKV.woff2 +0 -0
- flowdeck/static/assets/jetbrains-mono-latin-ext-700-normal-CxPITLHs.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-vietnamese-400-normal-CqNFfHCs.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-vietnamese-500-normal-DNRqzVM1.woff +0 -0
- flowdeck/static/assets/jetbrains-mono-vietnamese-700-normal-BDLVIk2r.woff +0 -0
- flowdeck/static/assets/light-B0F-b5Z6.js +20 -0
- flowdeck/static/assets/runtime-dom.esm-bundler-DESsN8Jj.js +2 -0
- flowdeck/static/favicon.svg +13 -0
- flowdeck/static/index.html +18 -0
- flowdeck/storage.py +145 -0
- flowdeck/tasks/__init__.py +1 -0
- flowdeck/tasks/http_executor.py +985 -0
- flowdeck/tasks/paramdefs.py +170 -0
- flowdeck/tasks/paramextract.py +185 -0
- flowdeck/tasks/python_executor.py +234 -0
- flowdeck/tasks/sql_executor.py +683 -0
- flowdeck/tasks/timevars.py +125 -0
- flowdeck/tasks/ui_registry.py +394 -0
- flowdeck-0.2.1.dist-info/METADATA +228 -0
- flowdeck-0.2.1.dist-info/RECORD +163 -0
- flowdeck-0.2.1.dist-info/WHEEL +4 -0
- flowdeck-0.2.1.dist-info/entry_points.txt +2 -0
- flowdeck-0.2.1.dist-info/licenses/LICENSE +293 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"""结果表格 → 图片(企业微信 markdown 不支持表格,用图片承载明细)。
|
|
2
|
+
|
|
3
|
+
借用户既有方案(wkhtmltoimage 渲染 HTML)的思路,但内置实现改走 Pillow:
|
|
4
|
+
不依赖外部 exe、跨平台可跑,只在磁盘找不到中文字体时退化为文字通知。
|
|
5
|
+
|
|
6
|
+
- 只渲染纯内容(表头 + 数据行),不带任务名/时间/结果集标题等前置标语
|
|
7
|
+
- 列宽按内容测量(中文按全角宽度),**不截断文字**,列随内容拉长
|
|
8
|
+
- 行数 / 像素高度有上限,超出写「其余 N 行见通知文本」
|
|
9
|
+
- JPEG 输出,超过企微 2MB 限制时自动降质量,仍超限则放弃图片改发文字
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import base64
|
|
13
|
+
import functools
|
|
14
|
+
import hashlib
|
|
15
|
+
import logging
|
|
16
|
+
import os
|
|
17
|
+
import platform
|
|
18
|
+
import threading
|
|
19
|
+
from typing import Any, Dict, List, Optional
|
|
20
|
+
|
|
21
|
+
_logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
SCALE = 2 # 2 倍绘制再缩回,文字更锐利
|
|
24
|
+
FONT_SIZE = 14 # 1x 字号
|
|
25
|
+
PADDING_X = 10
|
|
26
|
+
PADDING_Y = 6
|
|
27
|
+
ROW_HEIGHT = 28
|
|
28
|
+
MARGIN = 10
|
|
29
|
+
MAX_ROWS_PER_SET = 200 # 单结果集最多渲染行数
|
|
30
|
+
MAX_IMAGE_HEIGHT = 9000 # 1x 像素上限,防超大图
|
|
31
|
+
MIN_COL_WIDTH = 60 # 1x 像素
|
|
32
|
+
MAX_IMAGE_BYTES = 1_800_000 # 企微群机器人图片上限 2MB,留余量
|
|
33
|
+
MAX_CANVAS_PIXELS = 40_000_000 # 画布像素总量上限(约 120MB RGB),超出直接用文字承载
|
|
34
|
+
_RENDER_SLOTS = threading.BoundedSemaphore(2) # 同时最多渲染 2 张,限制常驻进程的瞬时内存峰值
|
|
35
|
+
|
|
36
|
+
HEADER_BG = "#D6E4F0"
|
|
37
|
+
HEADER_FG = "#1F4E79"
|
|
38
|
+
ZEBRA_BG = "#F7F9FB"
|
|
39
|
+
BORDER = "#BFC9D4"
|
|
40
|
+
|
|
41
|
+
_FONT_CANDIDATES = [
|
|
42
|
+
# Windows
|
|
43
|
+
("C:/Windows/Fonts/msyh.ttc", 0),
|
|
44
|
+
("C:/Windows/Fonts/simhei.ttf", 0),
|
|
45
|
+
("C:/Windows/Fonts/simsun.ttc", 0),
|
|
46
|
+
# macOS
|
|
47
|
+
("/System/Library/Fonts/PingFang.ttc", 0),
|
|
48
|
+
# Linux
|
|
49
|
+
("/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc", 0),
|
|
50
|
+
("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc", 0),
|
|
51
|
+
("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", 0),
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@functools.lru_cache(maxsize=8)
|
|
56
|
+
def _load_font(size: int):
|
|
57
|
+
"""加载中文字体;Pillow 缺失或没有中文字体时返回 None(调用方改发文字)。"""
|
|
58
|
+
try:
|
|
59
|
+
from PIL import ImageFont
|
|
60
|
+
except ImportError:
|
|
61
|
+
return None
|
|
62
|
+
for path, index in _FONT_CANDIDATES:
|
|
63
|
+
if os.path.exists(path):
|
|
64
|
+
try:
|
|
65
|
+
return ImageFont.truetype(path, size, index=index)
|
|
66
|
+
except Exception: # noqa: BLE001 - 字体损坏时换下一个
|
|
67
|
+
continue
|
|
68
|
+
_logger.info(
|
|
69
|
+
"未找到可用中文字体(平台 %s),结果图片已退化为文字通知",
|
|
70
|
+
platform.system(),
|
|
71
|
+
)
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _flatten(value: Any) -> str:
|
|
76
|
+
"""单元格取值:只压缩空白,不截断(列宽随内容拉长,保证信息完整)。"""
|
|
77
|
+
text = "NULL" if value is None else str(value)
|
|
78
|
+
return " ".join(text.split())
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _has_rows(result_sets: List[Dict[str, Any]]) -> bool:
|
|
82
|
+
return any(item.get("rows") for item in result_sets or [])
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _headers_and_cells(columns: List[str], rows: List[List[Any]]) -> tuple:
|
|
86
|
+
headers = [(_flatten(col) or f"列{i + 1}") for i, col in enumerate(columns)]
|
|
87
|
+
if not headers:
|
|
88
|
+
headers = [f"列{i + 1}" for i in range(len(rows[0]) if rows else 0)]
|
|
89
|
+
return headers, [[_flatten(v) for v in row] for row in rows]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _column_widths(font, headers: List[str], cells: List[List[str]], scale: int) -> List[int]:
|
|
93
|
+
"""列宽(绘制像素)= 表头与数据中最宽文本,只设下限,不设上限(不截断文字)。"""
|
|
94
|
+
widths: List[int] = []
|
|
95
|
+
pad = PADDING_X * scale
|
|
96
|
+
for index, name in enumerate(headers):
|
|
97
|
+
max_w = int(font.getlength(name))
|
|
98
|
+
for row in cells:
|
|
99
|
+
if index < len(row):
|
|
100
|
+
max_w = max(max_w, int(font.getlength(row[index])))
|
|
101
|
+
widths.append(max(MIN_COL_WIDTH * scale, max_w + 2 * pad))
|
|
102
|
+
return widths
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _draw_table(
|
|
106
|
+
draw, font, x0: int, y0: int, headers: List[str], cells: List[List[str]], scale: int
|
|
107
|
+
) -> int:
|
|
108
|
+
"""绘制单个表格,返回表格底部 y 坐标(坐标均为绘制像素)。"""
|
|
109
|
+
widths = _column_widths(font, headers, cells, scale)
|
|
110
|
+
row_h = ROW_HEIGHT * scale
|
|
111
|
+
pad_x, pad_y = PADDING_X * scale, PADDING_Y * scale
|
|
112
|
+
table_w = sum(widths)
|
|
113
|
+
|
|
114
|
+
# 表头
|
|
115
|
+
draw.rectangle([x0, y0, x0 + table_w, y0 + row_h], fill=HEADER_BG, outline=BORDER)
|
|
116
|
+
x = x0
|
|
117
|
+
for index, name in enumerate(headers):
|
|
118
|
+
draw.text((x + pad_x, y0 + pad_y), name, font=font, fill=HEADER_FG)
|
|
119
|
+
draw.line([x, y0, x, y0 + row_h], fill=BORDER)
|
|
120
|
+
x += widths[index]
|
|
121
|
+
|
|
122
|
+
# 数据行(斑马纹)
|
|
123
|
+
y = y0 + row_h
|
|
124
|
+
for row_index, row in enumerate(cells):
|
|
125
|
+
bg = ZEBRA_BG if row_index % 2 else "#FFFFFF"
|
|
126
|
+
draw.rectangle([x0, y, x0 + table_w, y + row_h], fill=bg, outline=BORDER)
|
|
127
|
+
x = x0
|
|
128
|
+
for col_index, width in enumerate(widths):
|
|
129
|
+
text = row[col_index] if col_index < len(row) else ""
|
|
130
|
+
if text:
|
|
131
|
+
draw.text((x + pad_x, y + pad_y), text, font=font, fill="#000000")
|
|
132
|
+
draw.line([x, y, x, y + row_h], fill=BORDER)
|
|
133
|
+
x += width
|
|
134
|
+
y += row_h
|
|
135
|
+
return y
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def render_table_image(result_sets: List[Dict[str, Any]]) -> Optional[bytes]:
|
|
139
|
+
"""把结果集渲染成 JPEG 字节;并发渲染数受限,避免多任务同时完成时内存峰值叠加。"""
|
|
140
|
+
if not _has_rows(result_sets):
|
|
141
|
+
return None
|
|
142
|
+
with _RENDER_SLOTS:
|
|
143
|
+
return _render_table_image(result_sets)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _render_table_image(result_sets: List[Dict[str, Any]]) -> Optional[bytes]:
|
|
147
|
+
"""实际渲染逻辑(只有表头 + 数据行);不可用 / 无数据 / 超限时返回 None。"""
|
|
148
|
+
try:
|
|
149
|
+
from PIL import Image, ImageDraw
|
|
150
|
+
except ImportError:
|
|
151
|
+
return None
|
|
152
|
+
|
|
153
|
+
base_font = _load_font(FONT_SIZE)
|
|
154
|
+
if base_font is None:
|
|
155
|
+
return None
|
|
156
|
+
|
|
157
|
+
# 先算行预算:总高受 MAX_IMAGE_HEIGHT(1x 像素)限制
|
|
158
|
+
total_rows = sum(len(item.get("rows") or []) for item in result_sets)
|
|
159
|
+
row_budget = max(20, min(total_rows, (MAX_IMAGE_HEIGHT // ROW_HEIGHT) - 40))
|
|
160
|
+
|
|
161
|
+
blocks: List[Dict[str, Any]] = []
|
|
162
|
+
remaining = row_budget
|
|
163
|
+
for item in result_sets:
|
|
164
|
+
rows = item.get("rows") or []
|
|
165
|
+
if not rows:
|
|
166
|
+
continue
|
|
167
|
+
limit = min(len(rows), remaining, MAX_ROWS_PER_SET)
|
|
168
|
+
if limit <= 0:
|
|
169
|
+
blocks.append({"columns": [], "cells": [], "omitted": len(rows)})
|
|
170
|
+
continue
|
|
171
|
+
headers, cells = _headers_and_cells(item.get("columns") or [], rows[:limit])
|
|
172
|
+
blocks.append(
|
|
173
|
+
{"columns": headers, "cells": cells, "omitted": len(rows) - limit}
|
|
174
|
+
)
|
|
175
|
+
remaining -= limit
|
|
176
|
+
|
|
177
|
+
# 先按 1x 量出画布尺寸:宽高都随内容拉伸,但总量超过像素上限就放弃图片(改发文字),
|
|
178
|
+
# 避免长文本结果在常驻进程里瞬时占用几百 MB 内存
|
|
179
|
+
widths_1x = [
|
|
180
|
+
sum(_column_widths(base_font, block["columns"], block["cells"], 1))
|
|
181
|
+
for block in blocks
|
|
182
|
+
if block["cells"]
|
|
183
|
+
]
|
|
184
|
+
canvas_w_1x = max(widths_1x + [0]) + 2 * MARGIN
|
|
185
|
+
canvas_h_1x = MARGIN
|
|
186
|
+
for block in blocks:
|
|
187
|
+
canvas_h_1x += ROW_HEIGHT * (len(block["cells"]) + 1)
|
|
188
|
+
if block["omitted"] or not block["cells"]:
|
|
189
|
+
canvas_h_1x += ROW_HEIGHT
|
|
190
|
+
canvas_h_1x += MARGIN
|
|
191
|
+
|
|
192
|
+
pixels_1x = canvas_w_1x * canvas_h_1x
|
|
193
|
+
if pixels_1x > MAX_CANVAS_PIXELS:
|
|
194
|
+
_logger.warning(
|
|
195
|
+
"结果表格 %dx%d 像素超过上限,已放弃发送图片(改发文字明细)",
|
|
196
|
+
canvas_w_1x,
|
|
197
|
+
canvas_h_1x,
|
|
198
|
+
)
|
|
199
|
+
return None
|
|
200
|
+
# 2 倍绘制更清晰,但像素量翻 4 倍;大表自动降为 1 倍绘制
|
|
201
|
+
scale = SCALE if pixels_1x * SCALE * SCALE <= MAX_CANVAS_PIXELS else 1
|
|
202
|
+
font = base_font if scale == 1 else _load_font(FONT_SIZE * scale)
|
|
203
|
+
if font is None:
|
|
204
|
+
return None
|
|
205
|
+
|
|
206
|
+
image = Image.new(
|
|
207
|
+
"RGB", (max(canvas_w_1x * scale, 200), canvas_h_1x * scale), "#FFFFFF"
|
|
208
|
+
)
|
|
209
|
+
draw = ImageDraw.Draw(image)
|
|
210
|
+
|
|
211
|
+
x0 = MARGIN * scale
|
|
212
|
+
y = MARGIN * scale
|
|
213
|
+
for block in blocks:
|
|
214
|
+
if not block["cells"]:
|
|
215
|
+
draw.text(
|
|
216
|
+
(x0, y),
|
|
217
|
+
f"(省略 {block['omitted']} 行)",
|
|
218
|
+
font=font,
|
|
219
|
+
fill="#666666",
|
|
220
|
+
)
|
|
221
|
+
y += ROW_HEIGHT * scale
|
|
222
|
+
continue
|
|
223
|
+
y = _draw_table(draw, font, x0, y, block["columns"], block["cells"], scale)
|
|
224
|
+
if block["omitted"]:
|
|
225
|
+
draw.text(
|
|
226
|
+
(x0, y),
|
|
227
|
+
f"……其余 {block['omitted']} 行见通知文本",
|
|
228
|
+
font=font,
|
|
229
|
+
fill="#666666",
|
|
230
|
+
)
|
|
231
|
+
y += ROW_HEIGHT * scale
|
|
232
|
+
|
|
233
|
+
if scale != 1:
|
|
234
|
+
image = image.resize(
|
|
235
|
+
(image.width // scale, image.height // scale), Image.LANCZOS
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
for quality in (85, 70, 55):
|
|
239
|
+
buffer = _encode(image, quality)
|
|
240
|
+
if len(buffer) <= MAX_IMAGE_BYTES:
|
|
241
|
+
return buffer
|
|
242
|
+
_logger.warning("结果图片超过企微 2MB 限制,已放弃发送图片")
|
|
243
|
+
return None
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _encode(image, quality: int) -> bytes:
|
|
247
|
+
from io import BytesIO
|
|
248
|
+
|
|
249
|
+
buffer = BytesIO()
|
|
250
|
+
image.save(buffer, format="JPEG", quality=quality, optimize=True)
|
|
251
|
+
return buffer.getvalue()
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def build_image_payload(image_bytes: bytes) -> Optional[Dict[str, Any]]:
|
|
255
|
+
"""企微群机器人图片消息体(base64 + md5,格式 jpg)。"""
|
|
256
|
+
if not image_bytes:
|
|
257
|
+
return None
|
|
258
|
+
return {
|
|
259
|
+
"msgtype": "image",
|
|
260
|
+
"image": {
|
|
261
|
+
"base64": base64.b64encode(image_bytes).decode("ascii"),
|
|
262
|
+
"md5": hashlib.md5(image_bytes).hexdigest(),
|
|
263
|
+
},
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def is_available() -> bool:
|
|
268
|
+
"""当前环境能否生成结果图片(Pillow 已装且有中文字体)。"""
|
|
269
|
+
try:
|
|
270
|
+
from PIL import Image # noqa: F401
|
|
271
|
+
except ImportError:
|
|
272
|
+
return False
|
|
273
|
+
return _load_font(FONT_SIZE) is not None
|