pyxllib 0.3.197__py3-none-any.whl → 3.201.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.
- pyxllib/__init__.py +14 -21
- pyxllib/algo/__init__.py +8 -8
- pyxllib/algo/disjoint.py +54 -54
- pyxllib/algo/geo.py +537 -541
- pyxllib/algo/intervals.py +964 -964
- pyxllib/algo/matcher.py +389 -389
- pyxllib/algo/newbie.py +166 -166
- pyxllib/algo/pupil.py +629 -629
- pyxllib/algo/shapelylib.py +67 -67
- pyxllib/algo/specialist.py +241 -241
- pyxllib/algo/stat.py +494 -494
- pyxllib/algo/treelib.py +145 -149
- pyxllib/algo/unitlib.py +62 -66
- pyxllib/autogui/__init__.py +5 -5
- pyxllib/autogui/activewin.py +246 -246
- pyxllib/autogui/all.py +9 -9
- pyxllib/autogui/autogui.py +846 -852
- pyxllib/autogui/uiautolib.py +362 -362
- pyxllib/autogui/virtualkey.py +102 -102
- pyxllib/autogui/wechat.py +827 -827
- pyxllib/autogui/wechat_msg.py +421 -421
- pyxllib/autogui/wxautolib.py +84 -84
- pyxllib/cv/__init__.py +5 -5
- pyxllib/cv/expert.py +267 -267
- pyxllib/cv/imfile.py +159 -159
- pyxllib/cv/imhash.py +39 -39
- pyxllib/cv/pupil.py +9 -9
- pyxllib/cv/rgbfmt.py +1525 -1525
- pyxllib/cv/slidercaptcha.py +137 -137
- pyxllib/cv/trackbartools.py +251 -251
- pyxllib/cv/xlcvlib.py +1040 -1040
- pyxllib/cv/xlpillib.py +423 -423
- pyxllib/data/echarts.py +236 -240
- pyxllib/data/jsonlib.py +85 -89
- pyxllib/data/oss.py +72 -72
- pyxllib/data/pglib.py +1111 -1127
- pyxllib/data/sqlite.py +568 -568
- pyxllib/data/sqllib.py +297 -297
- pyxllib/ext/JLineViewer.py +505 -505
- pyxllib/ext/__init__.py +6 -6
- pyxllib/ext/demolib.py +251 -246
- pyxllib/ext/drissionlib.py +277 -277
- pyxllib/ext/kq5034lib.py +12 -12
- pyxllib/ext/qt.py +449 -449
- pyxllib/ext/robustprocfile.py +493 -497
- pyxllib/ext/seleniumlib.py +76 -76
- pyxllib/ext/tk.py +173 -173
- pyxllib/ext/unixlib.py +821 -827
- pyxllib/ext/utools.py +345 -351
- pyxllib/ext/webhook.py +124 -119
- pyxllib/ext/win32lib.py +40 -40
- pyxllib/ext/wjxlib.py +91 -88
- pyxllib/ext/wpsapi.py +124 -124
- pyxllib/ext/xlwork.py +9 -9
- pyxllib/ext/yuquelib.py +1110 -1105
- pyxllib/file/__init__.py +17 -17
- pyxllib/file/docxlib.py +757 -761
- pyxllib/file/gitlib.py +309 -309
- pyxllib/file/libreoffice.py +165 -165
- pyxllib/file/movielib.py +144 -148
- pyxllib/file/newbie.py +10 -10
- pyxllib/file/onenotelib.py +1469 -1469
- pyxllib/file/packlib/__init__.py +330 -330
- pyxllib/file/packlib/zipfile.py +2441 -2441
- pyxllib/file/pdflib.py +422 -426
- pyxllib/file/pupil.py +185 -185
- pyxllib/file/specialist/__init__.py +681 -685
- pyxllib/file/specialist/dirlib.py +799 -799
- pyxllib/file/specialist/download.py +193 -193
- pyxllib/file/specialist/filelib.py +2825 -2829
- pyxllib/file/xlsxlib.py +3122 -3131
- pyxllib/file/xlsyncfile.py +341 -341
- pyxllib/prog/__init__.py +5 -5
- pyxllib/prog/cachetools.py +58 -64
- pyxllib/prog/deprecatedlib.py +233 -233
- pyxllib/prog/filelock.py +42 -42
- pyxllib/prog/ipyexec.py +253 -253
- pyxllib/prog/multiprogs.py +940 -940
- pyxllib/prog/newbie.py +451 -451
- pyxllib/prog/pupil.py +1208 -1197
- pyxllib/prog/sitepackages.py +33 -33
- pyxllib/prog/specialist/__init__.py +348 -391
- pyxllib/prog/specialist/bc.py +203 -203
- pyxllib/prog/specialist/browser.py +497 -497
- pyxllib/prog/specialist/common.py +347 -347
- pyxllib/prog/specialist/datetime.py +198 -198
- pyxllib/prog/specialist/tictoc.py +240 -240
- pyxllib/prog/specialist/xllog.py +180 -180
- pyxllib/prog/xlosenv.py +110 -108
- pyxllib/stdlib/__init__.py +17 -17
- pyxllib/stdlib/tablepyxl/__init__.py +10 -10
- pyxllib/stdlib/tablepyxl/style.py +303 -303
- pyxllib/stdlib/tablepyxl/tablepyxl.py +130 -130
- pyxllib/text/__init__.py +8 -8
- pyxllib/text/ahocorasick.py +36 -39
- pyxllib/text/airscript.js +754 -744
- pyxllib/text/charclasslib.py +121 -121
- pyxllib/text/jiebalib.py +267 -267
- pyxllib/text/jinjalib.py +27 -32
- pyxllib/text/jsa_ai_prompt.md +271 -271
- pyxllib/text/jscode.py +922 -922
- pyxllib/text/latex/__init__.py +158 -158
- pyxllib/text/levenshtein.py +303 -303
- pyxllib/text/nestenv.py +1215 -1215
- pyxllib/text/newbie.py +300 -300
- pyxllib/text/pupil/__init__.py +8 -8
- pyxllib/text/pupil/common.py +1121 -1121
- pyxllib/text/pupil/xlalign.py +326 -326
- pyxllib/text/pycode.py +47 -47
- pyxllib/text/specialist/__init__.py +8 -8
- pyxllib/text/specialist/common.py +112 -112
- pyxllib/text/specialist/ptag.py +186 -186
- pyxllib/text/spellchecker.py +172 -172
- pyxllib/text/templates/echart_base.html +10 -10
- pyxllib/text/templates/highlight_code.html +16 -16
- pyxllib/text/templates/latex_editor.html +102 -102
- pyxllib/text/vbacode.py +17 -17
- pyxllib/text/xmllib.py +741 -747
- pyxllib/xl.py +42 -39
- pyxllib/xlcv.py +17 -17
- pyxllib-3.201.1.dist-info/METADATA +296 -0
- pyxllib-3.201.1.dist-info/RECORD +125 -0
- {pyxllib-0.3.197.dist-info → pyxllib-3.201.1.dist-info}/licenses/LICENSE +190 -190
- pyxllib/ext/old.py +0 -663
- pyxllib-0.3.197.dist-info/METADATA +0 -48
- pyxllib-0.3.197.dist-info/RECORD +0 -126
- {pyxllib-0.3.197.dist-info → pyxllib-3.201.1.dist-info}/WHEEL +0 -0
pyxllib/data/pglib.py
CHANGED
@@ -1,1127 +1,1111 @@
|
|
1
|
-
#!/usr/bin/env python3
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
# @Author : 陈坤泽
|
4
|
-
# @Email : 877362867@qq.com
|
5
|
-
# @Date : 2022/06/09 16:26
|
6
|
-
|
7
|
-
"""
|
8
|
-
针对PostgreSQL封装的工具
|
9
|
-
"""
|
10
|
-
|
11
|
-
import
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
import
|
32
|
-
from
|
33
|
-
import
|
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
|
-
self.
|
60
|
-
|
61
|
-
def
|
62
|
-
pass
|
63
|
-
|
64
|
-
def
|
65
|
-
""
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
#
|
116
|
-
if
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
WHERE
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
self.
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
return
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
def
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
"""
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
if
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
row
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
"""
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
"""
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
if
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
self.commit()
|
577
|
-
|
578
|
-
def
|
579
|
-
"""
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
def
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
def
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
self.
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
return self._create_stack_chart(title, ls, yaxis_name=yaxis_name)
|
744
|
-
|
745
|
-
def
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
for
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
htmltexts
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
htmltexts
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
htmltexts
|
1018
|
-
|
1019
|
-
self.
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
:
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
status2['update_time'] = update_time
|
1113
|
-
|
1114
|
-
# 3 添加历史记录
|
1115
|
-
if can_merge is None:
|
1116
|
-
def can_merge(status1, status2):
|
1117
|
-
for k in backup_keys:
|
1118
|
-
if status1.get(k) != status2.get(k):
|
1119
|
-
return False
|
1120
|
-
return True
|
1121
|
-
|
1122
|
-
if historys and can_merge(status1, status2):
|
1123
|
-
historys[-1] = status2
|
1124
|
-
else:
|
1125
|
-
historys.append(status2)
|
1126
|
-
|
1127
|
-
self.update_row(table_name, {'historys': historys}, where, commit=commit)
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Author : 陈坤泽
|
4
|
+
# @Email : 877362867@qq.com
|
5
|
+
# @Date : 2022/06/09 16:26
|
6
|
+
|
7
|
+
"""
|
8
|
+
针对PostgreSQL封装的工具
|
9
|
+
"""
|
10
|
+
|
11
|
+
import sys
|
12
|
+
|
13
|
+
from pyxllib.file.specialist import XlPath
|
14
|
+
|
15
|
+
import io
|
16
|
+
from collections import Counter
|
17
|
+
import json
|
18
|
+
import json
|
19
|
+
import textwrap
|
20
|
+
import datetime
|
21
|
+
import re
|
22
|
+
|
23
|
+
from tqdm import tqdm
|
24
|
+
|
25
|
+
import psycopg
|
26
|
+
import psycopg.rows
|
27
|
+
|
28
|
+
from pyxllib.prog.newbie import round_int, human_readable_number
|
29
|
+
from pyxllib.prog.pupil import utc_now, utc_timestamp, is_valid_identifier
|
30
|
+
from pyxllib.prog.specialist import XlOsEnv
|
31
|
+
from pyxllib.algo.pupil import ValuesStat2
|
32
|
+
from pyxllib.file.specialist import get_etag, StreamJsonlWriter
|
33
|
+
from pyxllib.data.sqlite import SqlBase, SqlBuilder
|
34
|
+
|
35
|
+
|
36
|
+
class Connection(psycopg.Connection, SqlBase):
|
37
|
+
|
38
|
+
def __init__(self, *args, **kwargs):
|
39
|
+
psycopg.Connection.__init__(self, *args, **kwargs)
|
40
|
+
SqlBase.__init__(self, *args, **kwargs)
|
41
|
+
|
42
|
+
def __del__(self):
|
43
|
+
self.close()
|
44
|
+
|
45
|
+
def __1_库(self):
|
46
|
+
pass
|
47
|
+
|
48
|
+
def get_db_activities(self, datname=None):
|
49
|
+
"""
|
50
|
+
检索当前数据库的活动信息。
|
51
|
+
|
52
|
+
:param datname: 这个字段理论上应该要能自动检测出来才对,但这会急着没空钻研,先手动输入吧
|
53
|
+
"""
|
54
|
+
sql = SqlBuilder('pg_stat_activity')
|
55
|
+
sql.select('pid', 'datname', 'usename', 'state', 'query', 'age(now(), query_start) AS "query_age"')
|
56
|
+
sql.where("state = 'active'")
|
57
|
+
if datname:
|
58
|
+
sql.where(f"datname = '{datname}'")
|
59
|
+
return self.exec2dict(sql.build_select()).fetchall()
|
60
|
+
|
61
|
+
def __2_表格(self):
|
62
|
+
pass
|
63
|
+
|
64
|
+
def get_table_names(self):
|
65
|
+
# cmd = "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'"
|
66
|
+
cmd = "SELECT tablename FROM pg_tables WHERE schemaname='public'"
|
67
|
+
return [x[0] for x in self.execute(cmd)]
|
68
|
+
|
69
|
+
def has_table(self, table_name, schemaname='public'):
|
70
|
+
query = ["SELECT EXISTS (SELECT FROM pg_tables ",
|
71
|
+
f"WHERE schemaname='{schemaname}' AND tablename='{table_name}')"]
|
72
|
+
res = self.execute(' '.join(query))
|
73
|
+
return res.fetchone()[0]
|
74
|
+
|
75
|
+
def get_column_names(self, table_name):
|
76
|
+
""" 【查】表格有哪些字段
|
77
|
+
"""
|
78
|
+
cmd = f"SELECT column_name FROM information_schema.columns WHERE table_name='{table_name}'"
|
79
|
+
return self.exec2col(cmd)
|
80
|
+
|
81
|
+
def ensure_column(self, table_name, col_name, *args, comment=None, **kwargs):
|
82
|
+
super(Connection, self).ensure_column(table_name, col_name, *args, **kwargs)
|
83
|
+
if comment:
|
84
|
+
self.set_column_comment(table_name, col_name, comment)
|
85
|
+
|
86
|
+
def set_column_comment(self, table_name, col_name, comment):
|
87
|
+
# 这里comment按%s填充会报错。好像是psycopg库的问题,这种情况因为没有表格字段参照类型,会不知道要把comment转成字符串。
|
88
|
+
# 然后使用py的f字符串填充的话,要避免comment有引号等特殊字符,就用了特殊的转义标记$zyzf$
|
89
|
+
# 暂时找不到更优雅的方式~~ 而且这个问题,可能以后其他特殊的sql语句也会遇到。。。
|
90
|
+
self.execute(f"COMMENT ON COLUMN {table_name}.{col_name} IS $zyzf${comment}$zyzf$")
|
91
|
+
self.commit()
|
92
|
+
|
93
|
+
def reset_table_item_id(self, table_name, item_id_name=None, counter_name=None):
|
94
|
+
""" 重置表格的数据的id值,也重置计数器
|
95
|
+
|
96
|
+
:param item_id_name: 表格的自增id字段名,有一套我自己风格的自动推算算法
|
97
|
+
:param counter_name: 计数器字段名,如果有的话,也会重置
|
98
|
+
"""
|
99
|
+
# 1 重置数据的编号
|
100
|
+
if item_id_name is None:
|
101
|
+
m = re.match(r'(.+?)(_table)?$', table_name)
|
102
|
+
item_id_name = m.group(1) + '_id'
|
103
|
+
|
104
|
+
sql = f"""WITH cte AS (
|
105
|
+
SELECT {item_id_name}, ROW_NUMBER() OVER (ORDER BY {item_id_name}) AS new_{item_id_name}
|
106
|
+
FROM {table_name}
|
107
|
+
)
|
108
|
+
UPDATE {table_name}
|
109
|
+
SET {item_id_name} = cte.new_{item_id_name}
|
110
|
+
FROM cte
|
111
|
+
WHERE {table_name}.{item_id_name} = cte.{item_id_name}"""
|
112
|
+
self.execute(sql) # todo 这种sql写法好像偶尔有bug会出问题
|
113
|
+
self.commit()
|
114
|
+
|
115
|
+
# 2 重置计数器
|
116
|
+
if counter_name is None:
|
117
|
+
counter_name = f'{table_name}_{item_id_name}_seq'
|
118
|
+
|
119
|
+
# 找到目前最大的id值
|
120
|
+
max_id = self.exec2one(f'SELECT MAX({item_id_name}) FROM {table_name}')
|
121
|
+
# self.execute(f'ALTER SEQUENCE {counter_name} RESTART WITH {max_id + 1}')
|
122
|
+
# 检查序列是否存在,如果不存在则创建序列,然后重置序列
|
123
|
+
# 检查序列是否存在,如果不存在则创建序列,并将其关联到指定的表和字段
|
124
|
+
self.execute(f"""
|
125
|
+
DO $$
|
126
|
+
BEGIN
|
127
|
+
IF NOT EXISTS (SELECT FROM pg_class WHERE relkind = 'S' AND relname = '{counter_name}') THEN
|
128
|
+
EXECUTE format('CREATE SEQUENCE %I', '{counter_name}');
|
129
|
+
EXECUTE format('ALTER TABLE %I ALTER COLUMN %I SET DEFAULT nextval(''%I'')', '{table_name}', '{item_id_name}', '{counter_name}');
|
130
|
+
END IF;
|
131
|
+
EXECUTE format('ALTER SEQUENCE %I RESTART WITH %s', '{counter_name}', {max_id + 1});
|
132
|
+
END $$;
|
133
|
+
""")
|
134
|
+
self.commit()
|
135
|
+
|
136
|
+
def __3_execute(self):
|
137
|
+
pass
|
138
|
+
|
139
|
+
def exec_nametuple(self, *args, **kwargs):
|
140
|
+
cur = self.cursor(row_factory=psycopg.rows.namedtuple_row)
|
141
|
+
data = cur.execute(*args, **kwargs)
|
142
|
+
# cur.close()
|
143
|
+
return data
|
144
|
+
|
145
|
+
def exec2dict(self, *args, **kwargs):
|
146
|
+
cur = self.cursor(row_factory=psycopg.rows.dict_row)
|
147
|
+
data = cur.execute(*args, **kwargs)
|
148
|
+
# cur.close()
|
149
|
+
return data
|
150
|
+
|
151
|
+
def exec2dict_batch(self, sql, batch_size=1000, use_offset=None, **kwargs):
|
152
|
+
""" 分批返回数据的版本
|
153
|
+
|
154
|
+
:param use_offset: 是否使用offset分页,会根据sql中是否含有where自动判断,但有时候最好明确指定以防错误
|
155
|
+
如果外部sql每次操作,会改变数据库的情况,导致sql的where规则虽然没变,但是数据本身发生变化,则offset应该要关闭
|
156
|
+
每次取对应的满足条件的数据即可
|
157
|
+
这种情况,也需要本函数内部主动执行commit_all的
|
158
|
+
否则,只是一种遍历查询,没有where或者where获取的数据情况是不会变化的,则要使用offset
|
159
|
+
:return:
|
160
|
+
第1个值,是一个迭代器,看起来仍然能一条一条返回,实际后台是按照batch_size打包获取的
|
161
|
+
第2个值,是数据总数
|
162
|
+
"""
|
163
|
+
if not isinstance(sql, SqlBuilder):
|
164
|
+
raise ValueError('暂时只能搭配SQLBuilder使用')
|
165
|
+
|
166
|
+
if use_offset is None:
|
167
|
+
use_offset = not sql._where
|
168
|
+
|
169
|
+
num = self.exec2one(sql.build_count())
|
170
|
+
offset = 0
|
171
|
+
|
172
|
+
def yield_row():
|
173
|
+
nonlocal offset
|
174
|
+
while True:
|
175
|
+
sql2 = sql.copy()
|
176
|
+
if not use_offset: # 如果不使用offset,那么缓存的sql操作需要全部提交,确保数据都更新后,再提取数据
|
177
|
+
self.commit_all()
|
178
|
+
sql2.limit(batch_size, offset)
|
179
|
+
rows = self.exec2dict(sql2.build_select(), **kwargs).fetchall()
|
180
|
+
if use_offset:
|
181
|
+
offset += len(rows)
|
182
|
+
if not rows:
|
183
|
+
break
|
184
|
+
yield from rows
|
185
|
+
|
186
|
+
return yield_row(), num
|
187
|
+
|
188
|
+
exec_dict = exec2dict
|
189
|
+
|
190
|
+
def __4_数据类型(self):
|
191
|
+
pass
|
192
|
+
|
193
|
+
@classmethod
|
194
|
+
def cvt_type(cls, val):
|
195
|
+
if isinstance(val, (dict, list)):
|
196
|
+
val = json.dumps(val, ensure_ascii=False, default=str)
|
197
|
+
# 注意list数组类型读、写都会自动适配py
|
198
|
+
return val
|
199
|
+
|
200
|
+
@classmethod
|
201
|
+
def autotype(cls, val):
|
202
|
+
if isinstance(val, str):
|
203
|
+
return 'text'
|
204
|
+
elif isinstance(val, int):
|
205
|
+
return 'int4' # int2、int8
|
206
|
+
elif isinstance(val, bool):
|
207
|
+
return 'boolean'
|
208
|
+
elif isinstance(val, float):
|
209
|
+
return 'float4'
|
210
|
+
elif isinstance(val, (dict, list)):
|
211
|
+
return 'jsonb'
|
212
|
+
elif isinstance(val, datetime.datetime):
|
213
|
+
return 'timestamp'
|
214
|
+
else: # 其他list等类型,可以用json.dumps或str转文本存储
|
215
|
+
return 'text'
|
216
|
+
|
217
|
+
def __5_增删改查(self):
|
218
|
+
pass
|
219
|
+
|
220
|
+
def insert_row(self, table_name, cols, *, on_conflict='DO NOTHING', commit=False):
|
221
|
+
""" 【增】插入新数据
|
222
|
+
|
223
|
+
:param dict cols: 用字典表示的要插入的值
|
224
|
+
:param on_conflict: 如果已存在的处理策略
|
225
|
+
DO NOTHING,跳过不处理
|
226
|
+
REPLACE,这是个特殊标记,会转为主键冲突也仍然更新所有值
|
227
|
+
(id) DO UPDATE SET host_name=EXCLUDED.host_name, nick_name='abc'
|
228
|
+
也可以写复杂的处理算法规则,详见 http://postgres.cn/docs/12/sql-insert.html
|
229
|
+
比如这里是插入的id重复的话,就把host_name替换掉,还可以指定nick_name替换为'abc'
|
230
|
+
注意前面的(id)是必须要输入的
|
231
|
+
|
232
|
+
注意:有个常见需求,是想插入后返回对应的id,但是这样就需要知道这张表自增的id字段名
|
233
|
+
以及还是很难获得插入后的id值,可以默认刚插入的id是最大的,但是这样并不安全,有风险
|
234
|
+
建议还是外部自己先计算全表最大的id值,自己实现自增,就能知道插入的这条数据的id了
|
235
|
+
"""
|
236
|
+
ks = ','.join(cols.keys())
|
237
|
+
vs = ','.join(['%s'] * (len(cols.keys())))
|
238
|
+
query = f'INSERT INTO {table_name}({ks}) VALUES ({vs})'
|
239
|
+
params = self.cvt_types(cols.values())
|
240
|
+
|
241
|
+
if on_conflict == 'REPLACE':
|
242
|
+
on_conflict = f'ON CONFLICT ON CONSTRAINT {table_name}_pkey DO UPDATE SET ' + \
|
243
|
+
','.join([f'{k}=EXCLUDED.{k}' for k in cols.keys()])
|
244
|
+
else:
|
245
|
+
on_conflict = f'ON CONFLICT {on_conflict}'
|
246
|
+
query += f' {on_conflict}'
|
247
|
+
|
248
|
+
self.commit_base(commit, query, params)
|
249
|
+
|
250
|
+
def insert_rows(self, table_name, keys, ls, *, on_conflict='DO NOTHING', commit=False):
|
251
|
+
""" 【增】插入新数据
|
252
|
+
|
253
|
+
:param str keys: 要插入的字段名,一个字符串,逗号,隔开属性值
|
254
|
+
:param list[list] ls: n行m列的数组
|
255
|
+
|
256
|
+
>> con.insert_rows('hosts2', 'id,host_name,nick_name', [[1, 'test5', 'dcba'], [11, 'test', 'aabb']])
|
257
|
+
"""
|
258
|
+
n, m = len(ls), len(keys.split(','))
|
259
|
+
vs = ','.join(['%s'] * m)
|
260
|
+
query = f'INSERT INTO {table_name}({keys}) VALUES ' + ','.join([f'({vs})'] * n)
|
261
|
+
params = []
|
262
|
+
for cols in ls:
|
263
|
+
params += self.cvt_types(cols)
|
264
|
+
|
265
|
+
if on_conflict == 'REPLACE':
|
266
|
+
on_conflict = f'ON CONFLICT ON CONSTRAINT {table_name}_pkey DO UPDATE SET ' + \
|
267
|
+
','.join([f'{k.strip()}=EXCLUDED.{k.strip()}' for k in keys.split(',')])
|
268
|
+
else:
|
269
|
+
on_conflict = f'ON CONFLICT {on_conflict}'
|
270
|
+
query += f' {on_conflict}'
|
271
|
+
|
272
|
+
self.commit_base(commit, query, params)
|
273
|
+
|
274
|
+
def __6_高级统计(self):
|
275
|
+
pass
|
276
|
+
|
277
|
+
def get_column_valuesstat(self, table_name, column, filter_condition=None,
|
278
|
+
percentile_count=5,
|
279
|
+
by_data=False, data_type=None):
|
280
|
+
""" 获得指定表格的某个字段的统计特征ValuesStat2对象
|
281
|
+
|
282
|
+
:param table_name: 表名
|
283
|
+
:param column: 用于计算统计数据的字段名
|
284
|
+
不一定是标准的字段名
|
285
|
+
:param percentile_count: 分位数的数量,例如 3 表示只计算中位数
|
286
|
+
:param by_data: 是否获得原始数据
|
287
|
+
默认只获得统计特征,不获得原始数据
|
288
|
+
"""
|
289
|
+
|
290
|
+
def init_from_db_data():
|
291
|
+
sql = SqlBuilder(table_name)
|
292
|
+
if filter_condition:
|
293
|
+
sql.where(filter_condition)
|
294
|
+
values = self.exec2col(sql.build_select(column))
|
295
|
+
if data_type == 'numeric':
|
296
|
+
values = [x and float(x) for x in values]
|
297
|
+
vs = ValuesStat2(raw_values=values, data_type=data_type)
|
298
|
+
|
299
|
+
if data_type == 'text' and is_valid_identifier(column):
|
300
|
+
vs0 = self.get_column_valuesstat(table_name, column, filter_condition=filter_condition,
|
301
|
+
percentile_count=percentile_count, by_data=False)
|
302
|
+
vs.n = vs0.n
|
303
|
+
vs.dist = vs0.dist
|
304
|
+
|
305
|
+
return vs
|
306
|
+
|
307
|
+
def init_from_db():
|
308
|
+
# 1 构建基础的 SQL 查询
|
309
|
+
sql = SqlBuilder(table_name)
|
310
|
+
sql.select("COUNT(*) AS total_count")
|
311
|
+
sql.select(f"COUNT({column}) AS non_null_count")
|
312
|
+
sql.select(f"MIN({column}) AS min_value")
|
313
|
+
sql.select(f"MAX({column}) AS max_value")
|
314
|
+
if data_type and 'timestamp' in data_type:
|
315
|
+
percentile_type = 'PERCENTILE_DISC'
|
316
|
+
# todo 其实时间类也可以"泛化"一种平均值、标准差算法的,这需要获取全量数据,然后自己计算
|
317
|
+
elif data_type == 'text':
|
318
|
+
percentile_type = 'PERCENTILE_DISC'
|
319
|
+
else: # 默认是正常的数值类型
|
320
|
+
sql.select(f"SUM({column}) AS total_sum")
|
321
|
+
sql.select(f"AVG({column}) AS average")
|
322
|
+
sql.select(f"STDDEV({column}) AS standard_deviation")
|
323
|
+
percentile_type = 'PERCENTILE_CONT'
|
324
|
+
|
325
|
+
percentiles = []
|
326
|
+
# 根据分位点的数量动态添加分位数计算
|
327
|
+
if percentile_count > 2:
|
328
|
+
step = 1 / (percentile_count - 1)
|
329
|
+
percentiles = [(i * step) for i in range(1, percentile_count - 1)]
|
330
|
+
for p in percentiles:
|
331
|
+
sql.select(f"{percentile_type}({p:.2f}) WITHIN GROUP (ORDER BY {column}) "
|
332
|
+
f"AS percentile_{int(p * 100)}")
|
333
|
+
|
334
|
+
if filter_condition:
|
335
|
+
sql.where(filter_condition)
|
336
|
+
|
337
|
+
row = self.exec2dict(sql.build_select()).fetchone()
|
338
|
+
|
339
|
+
# 2 统计展示
|
340
|
+
x = ValuesStat2(data_type=data_type)
|
341
|
+
x.raw_n = row['total_count']
|
342
|
+
x.n = row['non_null_count']
|
343
|
+
if not x.n:
|
344
|
+
return x
|
345
|
+
|
346
|
+
x.sum = row.get('total_sum', None)
|
347
|
+
x.mean = row.get('average', None)
|
348
|
+
x.std = row.get('standard_deviation', None)
|
349
|
+
|
350
|
+
# 如果计算了分位数,填充相应属性
|
351
|
+
x.dist = [row['min_value']] + [row[f"percentile_{int(p * 100)}"] for p in percentiles] + [row['max_value']]
|
352
|
+
if data_type == 'numeric':
|
353
|
+
x.dist = [float(x) for x in x.dist]
|
354
|
+
|
355
|
+
return x
|
356
|
+
|
357
|
+
data_type = data_type or self.get_column_data_type(table_name, column)
|
358
|
+
|
359
|
+
# 如果不是标准的列名,强制获取数据
|
360
|
+
if not is_valid_identifier(column):
|
361
|
+
by_data = True
|
362
|
+
|
363
|
+
if by_data:
|
364
|
+
vs = init_from_db_data()
|
365
|
+
else:
|
366
|
+
vs = init_from_db()
|
367
|
+
|
368
|
+
return vs
|
369
|
+
|
370
|
+
def export_jsonl(self, file_path, table_name, key_col=None, batch_size=1000, print_mode=0):
|
371
|
+
""" 将某个表导出为本地jsonl文件
|
372
|
+
|
373
|
+
:param str|SqlBuilder table_name: 表名
|
374
|
+
支持传入SqlBuilder对象,这样可以更灵活的控制导出的数据规则
|
375
|
+
:param file_path: 导出的文件路径
|
376
|
+
:param batch_size: 每次读取的行数和保存的行数
|
377
|
+
:param key_col: 作为主键的列名,如果有的话,会自动去重
|
378
|
+
强烈推荐要设置
|
379
|
+
实际不一定要用主键,只要是有顺序值的列就行
|
380
|
+
|
381
|
+
todo 暴力最简单的版本不难写,我纠结的是缓存机制,还有bytes类型数据会有点大等问题
|
382
|
+
还需要先支持一个通用的缓存写文件功能
|
383
|
+
"""
|
384
|
+
# 1 sql
|
385
|
+
if isinstance(table_name, str):
|
386
|
+
sql = SqlBuilder(table_name)
|
387
|
+
sql.select('*')
|
388
|
+
else:
|
389
|
+
sql = table_name
|
390
|
+
m = re.search(r'FROM (\w+)', sql.build_select())
|
391
|
+
table_name = m.group(1) if m else 'table'
|
392
|
+
assert isinstance(sql, SqlBuilder)
|
393
|
+
|
394
|
+
file_path = XlPath(file_path)
|
395
|
+
if key_col:
|
396
|
+
sql.order_by(key_col)
|
397
|
+
if file_path.is_file():
|
398
|
+
# 读取现有数据,找出主键最大值
|
399
|
+
data = file_path.read_jsonl(batch_size=1000)
|
400
|
+
if data:
|
401
|
+
max_val = max([x[key_col] for x in data]) if data else None
|
402
|
+
if max_val is not None:
|
403
|
+
sql.where(f'{key_col} > {max_val}')
|
404
|
+
|
405
|
+
# 2 获取数据
|
406
|
+
file = StreamJsonlWriter(file_path, batch_size=batch_size) # 流式存储
|
407
|
+
rows, total = self.exec2dict_batch(sql, batch_size=batch_size, use_offset=True)
|
408
|
+
for row in tqdm(rows, total=total, desc=f'从{table_name}表导出数据', disable=not print_mode):
|
409
|
+
file.append_line(row)
|
410
|
+
file.flush()
|
411
|
+
|
412
|
+
def check_db_tables_size(self, db_name=None):
|
413
|
+
""" 查看指定数据下所有表格的大小 """
|
414
|
+
from datetime import datetime
|
415
|
+
import pandas as pd
|
416
|
+
|
417
|
+
if db_name is None:
|
418
|
+
# 使用sql获取当前self所在数据库
|
419
|
+
db_name = self.exec2one("SELECT current_database()")
|
420
|
+
|
421
|
+
data = []
|
422
|
+
tables = self.exec2col("SELECT table_name FROM information_schema.tables WHERE table_schema='public'")
|
423
|
+
for table_name in tables:
|
424
|
+
row = {
|
425
|
+
'database': db_name,
|
426
|
+
'table_name': table_name,
|
427
|
+
}
|
428
|
+
sz = self.exec2one(f"SELECT pg_total_relation_size('public.{table_name}')")
|
429
|
+
if not sz:
|
430
|
+
continue
|
431
|
+
lines = self.exec2one(f"SELECT COUNT(*) FROM {table_name}")
|
432
|
+
row['size'], row['lines'] = sz, lines
|
433
|
+
row['readable_size'] = human_readable_number(sz, 'KB')
|
434
|
+
row['perline_size'] = human_readable_number(sz / lines, 'KB') if lines else -1
|
435
|
+
row['update_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
436
|
+
data.append(row)
|
437
|
+
|
438
|
+
df = pd.DataFrame.from_dict(data)
|
439
|
+
if len(df):
|
440
|
+
df.sort_values(['size'], ascending=False, inplace=True)
|
441
|
+
df.reset_index(drop=True, inplace=True)
|
442
|
+
return df
|
443
|
+
|
444
|
+
def check_multi_db_size(self, db_list):
|
445
|
+
""" 这个功能一般要用postgres账号,才有权限处理所有数据库 """
|
446
|
+
from datetime import datetime
|
447
|
+
import pandas as pd
|
448
|
+
|
449
|
+
data = []
|
450
|
+
for db in db_list:
|
451
|
+
row = {
|
452
|
+
'name': db,
|
453
|
+
}
|
454
|
+
sz = self.exec2one(f"SELECT pg_database_size('{db}')")
|
455
|
+
row['size'] = sz
|
456
|
+
row['readable_size'] = human_readable_number(sz, 'KB')
|
457
|
+
row['update_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
458
|
+
|
459
|
+
data.append(row)
|
460
|
+
|
461
|
+
df = pd.DataFrame.from_dict(data)
|
462
|
+
df.sort_values(['size'], ascending=False, inplace=True)
|
463
|
+
df.reset_index(drop=True, inplace=True)
|
464
|
+
return df
|
465
|
+
|
466
|
+
|
467
|
+
"""
|
468
|
+
【关于为什么XlprDb要和pglib合一个文件】
|
469
|
+
好处
|
470
|
+
1、代码集中管理,方便开发效率
|
471
|
+
|
472
|
+
坏处 - 对治
|
473
|
+
1、导入代码多,效率低
|
474
|
+
- 导入代码其实很快的,不差这一点点
|
475
|
+
2、仅用简单功能的时候,会引入多余复杂功能
|
476
|
+
- 如果单拉一个xlprdb.py文件,依然不能头部import复杂库,因为XlprDb也存在需要精简使用的场景
|
477
|
+
既然都要有精简的部分,干脆都把代码弄到一起得了
|
478
|
+
然后复杂包都在成员函数里单独import
|
479
|
+
"""
|
480
|
+
|
481
|
+
|
482
|
+
class XlprDb(Connection):
|
483
|
+
""" xlpr统一集中管理的一个数据库
|
484
|
+
|
485
|
+
为了一些基础的数据库功能操作干净,尽量不要在全局导入过于复杂的包,可以在每个特定功能里导特定包
|
486
|
+
"""
|
487
|
+
|
488
|
+
def __init__(self, *args, **kwargs):
|
489
|
+
super().__init__(*args, **kwargs)
|
490
|
+
self.seckey = ''
|
491
|
+
|
492
|
+
@classmethod
|
493
|
+
def set_conninfo(cls, conninfo, seckey=''):
|
494
|
+
""" 提前将登录信息加密写到环境变量中,这样代码中就没有明文的账号密码信息
|
495
|
+
|
496
|
+
:param conninfo:
|
497
|
+
:param seckey: 如果要获得数据库里较重要的密码等信息,需要配置key值,否则默认可以不设
|
498
|
+
|
499
|
+
使用后,需要重启IDE重新加载环境变量
|
500
|
+
并且本句明文代码需要删除
|
501
|
+
"""
|
502
|
+
# TODO 目前只设一个账号,后续可以扩展支持多个账号指定配置
|
503
|
+
# conninfo = 'postgresql://postgres:yourpassword@172.16.170.110/xlpr'
|
504
|
+
return XlOsEnv.persist_set('XlprDbAccount', {'conninfo': conninfo, 'seckey': seckey}, encoding=True)
|
505
|
+
|
506
|
+
@classmethod
|
507
|
+
def connect(cls, conninfo='', seckey='', *,
|
508
|
+
autocommit=False, row_factory=None, context=None, **kwargs) -> 'XlprDb':
|
509
|
+
""" 因为要标记 -> XlprDb,IDE才会识别出类别,有自动补全功能
|
510
|
+
但在类中写@classmethod,无法标记 -> XlprDb,所以就放外面单独写一个方法了
|
511
|
+
"""
|
512
|
+
d = XlOsEnv.get('XlprDbAccount', decoding=True)
|
513
|
+
if conninfo == '':
|
514
|
+
conninfo = d['conninfo']
|
515
|
+
if seckey == '' and isinstance(d, dict) and 'seckey' in d:
|
516
|
+
seckey = d['seckey']
|
517
|
+
# 注意这里获取的是XlprDb类型
|
518
|
+
con = super(XlprDb, cls).connect(conninfo, autocommit=autocommit, row_factory=row_factory, context=context,
|
519
|
+
**kwargs)
|
520
|
+
con.seckey = seckey
|
521
|
+
return con
|
522
|
+
|
523
|
+
@classmethod
|
524
|
+
def connect2(cls, name, passwd, database=None, ip_list=None, **kwargs):
|
525
|
+
""" 简化的登录方式,并且自带优先尝试局域网连接,再公网连接
|
526
|
+
|
527
|
+
一般用在jupyter等对启动速度没有太高要求的场合,因为不断尝试localhost等需要耗费不少时间
|
528
|
+
"""
|
529
|
+
if database is None:
|
530
|
+
database = name
|
531
|
+
|
532
|
+
xldb = None
|
533
|
+
if ip_list is None:
|
534
|
+
ip_list = ['localhost', '172.16.170.136', 'xmutpriu.com']
|
535
|
+
for ip in ip_list:
|
536
|
+
try:
|
537
|
+
xldb = cls.connect(f'postgresql://{name}:{passwd}@{ip}/{database}', **kwargs)
|
538
|
+
break
|
539
|
+
except psycopg.OperationalError:
|
540
|
+
pass
|
541
|
+
|
542
|
+
return xldb
|
543
|
+
|
544
|
+
def __1_hosts相关数据表操作(self):
|
545
|
+
pass
|
546
|
+
|
547
|
+
def update_host(self, host_name, accounts=None, **kwargs):
|
548
|
+
""" 更新一台服务器的信息
|
549
|
+
|
550
|
+
:param dict accounts: 账号信息,记得要列全,比如
|
551
|
+
{'root': '123456', 'chenkunze': '654321'}
|
552
|
+
"""
|
553
|
+
if not self.execute('SELECT EXISTS (SELECT FROM hosts WHERE host_name=%s)', (host_name,)).fetchone()[0]:
|
554
|
+
self.insert_row('hosts', {'host_name': host_name})
|
555
|
+
if kwargs:
|
556
|
+
self.update_row('hosts', kwargs, {'host_name': host_name})
|
557
|
+
if accounts:
|
558
|
+
self.execute('UPDATE hosts SET accounts=pgp_sym_encrypt(%s, %s) WHERE host_name=%s',
|
559
|
+
(json.dumps(accounts, ensure_ascii=False), self.seckey, host_name))
|
560
|
+
self.commit()
|
561
|
+
|
562
|
+
def set_host_account(self, host_name, user_name, passwd):
|
563
|
+
""" 设置某台服务器的一个账号密码
|
564
|
+
|
565
|
+
>> xldb.set_host_account('titan2', 'chenkunze', '123456')
|
566
|
+
|
567
|
+
"""
|
568
|
+
# 读取旧的账号密码字典数据
|
569
|
+
d = self.execute("SELECT pgp_sym_decrypt(accounts, %s)::jsonb FROM hosts WHERE host_name=%s",
|
570
|
+
(self.seckey, host_name)).fetchone()[0]
|
571
|
+
# 修改字典数据
|
572
|
+
d[user_name] = str(passwd)
|
573
|
+
# 将新的字典数据写回数据库
|
574
|
+
self.execute('UPDATE hosts SET accounts=pgp_sym_encrypt(%s, %s) WHERE host_name=%s',
|
575
|
+
(json.dumps(d, ensure_ascii=False), self.seckey, host_name))
|
576
|
+
self.commit()
|
577
|
+
|
578
|
+
def login_ssh(self, host_name, user_name, map_path=None, **kwargs) -> 'XlSSHClient':
|
579
|
+
""" 通过数据库里的服务器数据记录,直接登录服务器 """
|
580
|
+
from pyxllib.ext.unixlib import XlSSHClient
|
581
|
+
|
582
|
+
if host_name.startswith('g_'):
|
583
|
+
host_ip = self.execute("SELECT host_ip FROM hosts WHERE host_name='xlpr0'").fetchone()[0]
|
584
|
+
pw, port = self.execute('SELECT (pgp_sym_decrypt(accounts, %s)::jsonb)[%s]::text, frpc_port'
|
585
|
+
' FROM hosts WHERE host_name=%s',
|
586
|
+
(self.seckey, user_name, host_name[2:])).fetchone()
|
587
|
+
else:
|
588
|
+
port = 22
|
589
|
+
host_ip, pw = self.execute('SELECT host_ip, (pgp_sym_decrypt(accounts, %s)::jsonb)[%s]::text'
|
590
|
+
' FROM hosts WHERE host_name=%s',
|
591
|
+
(self.seckey, user_name, host_name)).fetchone()
|
592
|
+
|
593
|
+
if map_path is None:
|
594
|
+
if sys.platform == 'win32':
|
595
|
+
map_path = {'C:/': '/'}
|
596
|
+
else:
|
597
|
+
map_path = {'/': '/'}
|
598
|
+
|
599
|
+
return XlSSHClient(host_ip, user_name, pw[1:-1], port=port, map_path=map_path, **kwargs)
|
600
|
+
|
601
|
+
def __2_xlapi相关数据表操作(self):
|
602
|
+
"""
|
603
|
+
files,存储二进制文件的表
|
604
|
+
etag,文件、二进制对应的etag值
|
605
|
+
meta,可以存储不同数据类型一些特殊的属性,比如图片可以存储高、宽
|
606
|
+
但因为用户使用中,为了提高速度,以及减少PIL等依赖,执行中不做计算
|
607
|
+
可以其他途径使用定期脚本自动处理
|
608
|
+
xlapi,底层api调用的记录统计
|
609
|
+
input,所有输入的参数
|
610
|
+
mode,使用的算法接口
|
611
|
+
etag,涉及到大文件数据的,以打包后的文件的etag做记录
|
612
|
+
output,运行结果
|
613
|
+
elapse_ms,调用函数的用时,不含后处理转换格式的时间
|
614
|
+
xlserver,服务端记录的使用情况
|
615
|
+
"""
|
616
|
+
pass
|
617
|
+
|
618
|
+
def insert_row2files(self, buffer, *, etag=None, **kwargs):
|
619
|
+
"""
|
620
|
+
|
621
|
+
为了运算效率考虑,除了etag需要用于去重,是必填字段
|
622
|
+
其他字段默认先不填充计算
|
623
|
+
"""
|
624
|
+
# 1 已经有的不重复存储
|
625
|
+
if etag is None:
|
626
|
+
etag = get_etag(buffer)
|
627
|
+
|
628
|
+
res = self.execute('SELECT etag FROM files WHERE etag=%s', (etag,)).fetchone()
|
629
|
+
if res: # 已经做过记录的,不再重复记录
|
630
|
+
return
|
631
|
+
|
632
|
+
# 2 没有的图,做个备份
|
633
|
+
kwargs['etag'] = etag
|
634
|
+
kwargs['data'] = buffer
|
635
|
+
kwargs['fsize_kb'] = round_int(len(buffer) / 1024)
|
636
|
+
|
637
|
+
self.insert_row('files', kwargs)
|
638
|
+
self.commit()
|
639
|
+
|
640
|
+
def get_xlapi_record(self, **input):
|
641
|
+
res = self.execute('SELECT id, output FROM xlapi WHERE input=%s', (self.cvt_type(input),)).fetchone()
|
642
|
+
if res:
|
643
|
+
_id, output = res
|
644
|
+
output['xlapi_id'] = _id
|
645
|
+
return output
|
646
|
+
|
647
|
+
def insert_row2xlapi(self, input, output, elapse_ms, *, on_conflict='(input) DO NOTHING'):
|
648
|
+
""" 往数据库记录当前操作内容
|
649
|
+
|
650
|
+
:return: 这个函数比较特殊,需要返回插入的条目的id值
|
651
|
+
"""
|
652
|
+
if on_conflict == 'REPLACE':
|
653
|
+
on_conflict = "(input) DO UPDATE " \
|
654
|
+
"SET output=EXCLUDED.output, elapse_ms=EXCLUDED.elapse_ms, update_time=EXCLUDED.update_time"
|
655
|
+
|
656
|
+
input = json.dumps(input, ensure_ascii=False)
|
657
|
+
self.insert_row('xlapi', {'input': input, 'output': output,
|
658
|
+
'elapse_ms': elapse_ms, 'update_time': utc_timestamp(8)},
|
659
|
+
on_conflict=on_conflict)
|
660
|
+
self.commit()
|
661
|
+
return self.execute('SELECT id FROM xlapi WHERE input=%s', (input,)).fetchone()[0]
|
662
|
+
|
663
|
+
def insert_row2xlserver(self, request, xlapi_id=0, **kwargs):
|
664
|
+
kw = {'remote_addr': request.headers.get('X-Real-IP', request.remote_addr),
|
665
|
+
'route': '/'.join(request.base_url.split('/')[3:]),
|
666
|
+
'update_time': utc_timestamp(8),
|
667
|
+
'xlapi_id': xlapi_id}
|
668
|
+
kw.update(kwargs)
|
669
|
+
print(kw) # 监控谁在用api
|
670
|
+
self.insert_row('xlserver', kw, commit=True)
|
671
|
+
|
672
|
+
def __3_host_trace相关可视化(self):
|
673
|
+
""" TODO dbview 改名 host_trace """
|
674
|
+
pass
|
675
|
+
|
676
|
+
def __dbtool(self):
|
677
|
+
pass
|
678
|
+
|
679
|
+
def record_host_usage(self, cpu=True, gpu=True, disk=False):
|
680
|
+
""" 记录服务器各种状况,存储到PG数据库
|
681
|
+
|
682
|
+
TODO 并行处理
|
683
|
+
TODO 功能还可以增加:gpu显卡温度、硬盘读写速率检查、网络上传下载带宽
|
684
|
+
"""
|
685
|
+
# 1 服务器列表
|
686
|
+
host_names = self.exec2col('SELECT host_name FROM hosts WHERE id > 1 ORDER BY id')
|
687
|
+
host_cpu_gb = {h: v for h, v in self.execute('SELECT host_name, cpu_gb FROM hosts')}
|
688
|
+
|
689
|
+
# 2 去所有服务器取使用情况
|
690
|
+
for i, host_name in enumerate(host_names, start=1):
|
691
|
+
print('-' * 20, i, host_name, '-' * 20)
|
692
|
+
try:
|
693
|
+
ssh = self.login_ssh(host_name, 'root', relogin=5, relogin_interval=0.2)
|
694
|
+
status = {}
|
695
|
+
if cpu:
|
696
|
+
data = ssh.check_cpu_usage(print_mode=True)
|
697
|
+
status['cpu'] = {k: v[0] for k, v in data.items()}
|
698
|
+
status['cpu_memory'] = {k: round(v[1] * host_cpu_gb[host_name] / 100, 2) for k, v in data.items()}
|
699
|
+
if gpu:
|
700
|
+
status['gpu_memory'] = ssh.check_gpu_usage(print_mode=True)
|
701
|
+
if disk:
|
702
|
+
# 检查磁盘空间会很慢,如果超时可以跳过。(设置超时6小时)
|
703
|
+
status['disk_memory'] = ssh.check_disk_usage(print_mode=True, timeout=60 * 60 * 6)
|
704
|
+
except Exception as e:
|
705
|
+
status = {'error': f'{str(type(e))[8:-2]}: {e}'}
|
706
|
+
print(status)
|
707
|
+
|
708
|
+
if status:
|
709
|
+
self.insert_row('host_trace',
|
710
|
+
{'host_name': host_name, 'status': status, 'update_time': utc_timestamp(8)},
|
711
|
+
commit=True)
|
712
|
+
print()
|
713
|
+
|
714
|
+
def _get_host_trace_total(self, mode, title, yaxis_name, date_trunc, recent, host_attr):
|
715
|
+
# CREATE INDEX ON gpu_trace (update_time); -- update_time最好建个索引
|
716
|
+
ls = self.execute(textwrap.dedent(f"""\
|
717
|
+
WITH cte1 AS ( -- 筛选近期内的数据,并且时间做trunc处理
|
718
|
+
SELECT host_name, (status)['{mode}'], date_trunc('{date_trunc}', update_time) ttime
|
719
|
+
FROM host_trace WHERE update_time > %s AND (status ? '{mode}')
|
720
|
+
), cte2 AS ( -- 每个时间里每个服务器的多条记录取平均
|
721
|
+
SELECT ttime, cte1.host_name, jsonb_div(jsonb_deep_sum(status), count(*)) status
|
722
|
+
FROM cte1 GROUP BY ttime, cte1.host_name
|
723
|
+
) -- 接上每台服务器显存总值,并且分组得到每个时刻总情况
|
724
|
+
SELECT ttime, {host_attr}, jsonb_deep_sum(status)
|
725
|
+
FROM cte2 JOIN hosts ON cte2.host_name = hosts.host_name
|
726
|
+
GROUP BY ttime ORDER BY ttime"""), ((utc_now(8) - recent).isoformat(timespec='seconds'),)).fetchall()
|
727
|
+
return self._create_stack_chart(title, ls, yaxis_name=yaxis_name)
|
728
|
+
|
729
|
+
def _get_host_trace_per_host(self, hostname, mode, title, yaxis_name, date_trunc, recent, host_attr):
|
730
|
+
ls = self.execute(textwrap.dedent(f"""\
|
731
|
+
WITH cte1 AS (
|
732
|
+
SELECT (status)['{mode}'], date_trunc('{date_trunc}', update_time) ttime
|
733
|
+
FROM host_trace WHERE host_name='{hostname}' AND update_time > %s AND (status ? '{mode}')
|
734
|
+
), cte2 AS (
|
735
|
+
SELECT ttime, jsonb_div(jsonb_deep_sum(status), count(*)) status
|
736
|
+
FROM cte1 GROUP BY ttime
|
737
|
+
)
|
738
|
+
SELECT ttime, {host_attr}, jsonb_deep_sum(status)
|
739
|
+
FROM cte2 JOIN hosts ON hosts.host_name='{hostname}'
|
740
|
+
GROUP BY ttime ORDER BY ttime"""), ((utc_now(8) - recent).isoformat(timespec='seconds'),)).fetchall()
|
741
|
+
if not ls: # 有的服务器可能数据是空的
|
742
|
+
return '', 0
|
743
|
+
return self._create_stack_chart(title, ls, yaxis_name=yaxis_name)
|
744
|
+
|
745
|
+
def _create_stack_chart(self, title, ls, *, yaxis_name=''):
|
746
|
+
""" 创建展示表
|
747
|
+
|
748
|
+
:param title: 表格标题
|
749
|
+
:param list ls: n*3,第1列是时间,第2列是总值,第3列是每个用户具体的数据
|
750
|
+
"""
|
751
|
+
from pyecharts.charts import Line
|
752
|
+
|
753
|
+
map_user_name = {}
|
754
|
+
for ks, v in self.execute('SELECT account_names, name FROM users'):
|
755
|
+
for k in ks:
|
756
|
+
map_user_name[k] = v
|
757
|
+
|
758
|
+
# 1 计算涉及的所有用户以及使用总量
|
759
|
+
all_users_usaged = Counter()
|
760
|
+
last_time = None
|
761
|
+
for x in ls:
|
762
|
+
hours = 0 if last_time is None else ((x[0] - last_time).total_seconds() / 3600)
|
763
|
+
last_time = x[0]
|
764
|
+
for k, v in x[2].items():
|
765
|
+
if k == '_total':
|
766
|
+
continue
|
767
|
+
all_users_usaged[map_user_name.get(k, k)] += v * hours
|
768
|
+
|
769
|
+
# ls里的姓名也要跟着更新
|
770
|
+
for i, x in enumerate(ls):
|
771
|
+
ct = Counter()
|
772
|
+
for k, v in x[2].items():
|
773
|
+
ct[map_user_name.get(k, k)] += v
|
774
|
+
ls[i] = (x[0], x[1], ct)
|
775
|
+
|
776
|
+
# 2 转图表可视化
|
777
|
+
def to_list(values):
|
778
|
+
return [(x[0], v) for x, v in zip(ls, values)]
|
779
|
+
|
780
|
+
def pretty_val(v):
|
781
|
+
return round_int(v) if v > 100 else round(v, 2)
|
782
|
+
|
783
|
+
try:
|
784
|
+
chart = Line()
|
785
|
+
chart.set_title(title)
|
786
|
+
chart.options['xAxis'][0].update({'min': ls[0][0], 'type': 'time',
|
787
|
+
# 'minInterval': 3600 * 1000 * 24,
|
788
|
+
'name': '时间', 'nameGap': 50, 'nameLocation': 'middle'})
|
789
|
+
chart.options['yAxis'][0].update({'name': yaxis_name, 'nameGap': 50, 'nameLocation': 'middle'})
|
790
|
+
# 目前是比较暴力的方法调整排版,后续要研究是不是能更自动灵活些
|
791
|
+
chart.options['legend'][0].update({'top': '6%', 'icon': 'pin'})
|
792
|
+
chart.options['grid'] = [{'top': 55 + len(all_users_usaged) * 4, 'containLabel': True}]
|
793
|
+
chart.options['tooltip'].opts.update({'axisPointer': {'type': 'cross'}, 'trigger': 'item'})
|
794
|
+
|
795
|
+
chart.add_series(f'total{pretty_val(ls[0][1]):g}', to_list([x[1] for x in ls]), areaStyle={})
|
796
|
+
for user, usaged in all_users_usaged.most_common():
|
797
|
+
usaged = usaged / ((ls[-1][0] - ls[0][0]).total_seconds() / 3600 + 1e-9)
|
798
|
+
chart.add_series(f'{user}{pretty_val(usaged):g}',
|
799
|
+
to_list([x[2].get(user, 0) for x in ls]),
|
800
|
+
areaStyle={}, stack='Total', emphasis={'focus': 'series'})
|
801
|
+
|
802
|
+
return '<body>' + chart.render_embed() + '</body>', sum(all_users_usaged.values())
|
803
|
+
except Exception as e:
|
804
|
+
return str(e), 0
|
805
|
+
|
806
|
+
# cdx_edit
|
807
|
+
def _get_database_trace_total(self, title, yaxis_name, date_trunc, recent, link_name):
|
808
|
+
ls = self.execute(textwrap.dedent(f"""\
|
809
|
+
WITH cte1 AS(
|
810
|
+
SELECT link_name, jsonb_each(status::jsonb) AS db_data, date_trunc('{date_trunc}', update_time) ttime
|
811
|
+
FROM database_trace WHERE update_time > %s AND link_name = '{link_name}'
|
812
|
+
), cte2 AS(
|
813
|
+
SELECT ttime, link_name, (db_data).key AS table_name, ((db_data).value->> '_total')::bigint AS total
|
814
|
+
FROM cte1
|
815
|
+
)
|
816
|
+
SELECT ttime, jsonb_object_agg(table_name, total) AS aggregated_json,SUM(total) as total
|
817
|
+
FROM cte2
|
818
|
+
GROUP BY ttime
|
819
|
+
ORDER BY ttime"""), ((utc_now(8) - recent).isoformat(timespec='seconds'),)).fetchall()
|
820
|
+
return self.database_create_stack_chart(title, ls, yaxis_name=yaxis_name)
|
821
|
+
|
822
|
+
def _get_database_trace_per_host(self, db, title, yaxis_name, date_trunc, recent, link_name):
|
823
|
+
ls = self.execute(textwrap.dedent(f"""\
|
824
|
+
WITH cte1 AS (
|
825
|
+
SELECT link_name, jsonb_each(status::jsonb) AS db_data, date_trunc('{date_trunc}', update_time) ttime
|
826
|
+
FROM database_trace WHERE update_time > %s AND link_name = '{link_name}'
|
827
|
+
), cte2 AS (
|
828
|
+
SELECT ttime, link_name, (db_data).key AS table_name, (db_data).value AS size_text
|
829
|
+
FROM cte1
|
830
|
+
), cte3 AS (
|
831
|
+
SELECT ttime, table_name, each.key AS key, each.value AS value
|
832
|
+
FROM cte2, jsonb_each_text(size_text) AS each(key, value)
|
833
|
+
)
|
834
|
+
SELECT ttime, jsonb_object_agg(key,
|
835
|
+
CASE
|
836
|
+
WHEN key = '_total' THEN NULL
|
837
|
+
ELSE (value::jsonb ->> 'size')::bigint -- Handle other keys as usual
|
838
|
+
END
|
839
|
+
) FILTER (WHERE key != '_total') AS aggregated_result, -- 确保 _total 不在 aggregated_result 中
|
840
|
+
MAX(CASE WHEN key = '_total' THEN value::bigint ELSE NULL END) AS total -- 单独提取 _total 的值
|
841
|
+
FROM cte3
|
842
|
+
WHERE (key = '_total' OR value::jsonb ? 'size') -- Ensure that '_total' is included
|
843
|
+
AND table_name = '{db}'
|
844
|
+
GROUP BY ttime
|
845
|
+
ORDER BY ttime"""), ((utc_now(8) - recent).isoformat(timespec='seconds'),)).fetchall()
|
846
|
+
return self.database_create_stack_chart(title, ls, yaxis_name=yaxis_name)
|
847
|
+
|
848
|
+
def database_create_stack_chart(self, title, ls, *, yaxis_name=''):
|
849
|
+
""" 创建展示表
|
850
|
+
|
851
|
+
:param title: 表格标题
|
852
|
+
:param list ls: n*3,第1列是时间,第3列是总值,第2列是每个用户具体的数据
|
853
|
+
"""
|
854
|
+
from pyecharts.charts import Line
|
855
|
+
all_database_usaged = Counter()
|
856
|
+
last_time = None
|
857
|
+
for x in ls:
|
858
|
+
hours = 0 if last_time is None else ((x[0] - last_time).total_seconds() / 3600)
|
859
|
+
last_time = x[0]
|
860
|
+
for k, v in x[1].items():
|
861
|
+
if k == '_total':
|
862
|
+
continue
|
863
|
+
all_database_usaged[k] += v * hours
|
864
|
+
|
865
|
+
for i, x in enumerate(ls):
|
866
|
+
ct = Counter()
|
867
|
+
for k, v in x[1].items():
|
868
|
+
ct[k] += v
|
869
|
+
ls[i] = (x[0], ct, int(x[2]))
|
870
|
+
|
871
|
+
# 2 转图表可视化
|
872
|
+
def to_list(values):
|
873
|
+
return [(x[0], v) for x, v in zip(ls, values)]
|
874
|
+
|
875
|
+
def pretty_val(v):
|
876
|
+
return round_int(v) if v > 100 else round(v, 2)
|
877
|
+
|
878
|
+
chart = Line()
|
879
|
+
chart.set_title(title)
|
880
|
+
chart.options['xAxis'][0].update({'min': ls[0][0], 'type': 'time',
|
881
|
+
# 'minInterval': 3600 * 1000 * 24,
|
882
|
+
'name': '时间', 'nameGap': 50, 'nameLocation': 'middle'})
|
883
|
+
chart.options['yAxis'][0].update({'name': yaxis_name, 'nameGap': 50, 'nameLocation': 'middle'})
|
884
|
+
# 目前是比较暴力的方法调整排版,后续要研究是不是能更自动灵活些
|
885
|
+
chart.options['legend'][0].update({'top': '6%', 'icon': 'pin'})
|
886
|
+
chart.options['grid'] = [{'top': 55 + len(all_database_usaged) * 4, 'containLabel': True}]
|
887
|
+
chart.options['tooltip'].opts.update({'axisPointer': {'type': 'cross'}, 'trigger': 'item'})
|
888
|
+
|
889
|
+
# chart.add_series(f'total {pretty_val(ls[0][2] / 1024 / 1024 / 1024):g}',
|
890
|
+
# to_list([x[2] / 1024 / 1024 / 1024 for x in ls]), areaStyle={})
|
891
|
+
for database, usaged in all_database_usaged.most_common():
|
892
|
+
usaged = usaged / ((ls[-1][0] - ls[0][0]).total_seconds() / 3600 + 1e-9)
|
893
|
+
chart.add_series(f'{database} {pretty_val(usaged / 1024 / 1024 / 1024):g}',
|
894
|
+
to_list([x[1].get(database, 0) / 1024 / 1024 / 1024 for x in ls]),
|
895
|
+
areaStyle={}, stack='Total', emphasis={'focus': 'series'})
|
896
|
+
return '<body>' + chart.render_embed() + '</body>'
|
897
|
+
|
898
|
+
def dbview_xldb1_memory(self, recent=datetime.timedelta(days=180), date_trunc='day'):
|
899
|
+
from pyxllib.data.echarts import render_echart_html
|
900
|
+
|
901
|
+
db_list = ['stdata', 'xlpr', 'st', 'ckz']
|
902
|
+
args = ['数据库大小(GB)', date_trunc, recent, 'xldb1']
|
903
|
+
htmltexts = []
|
904
|
+
|
905
|
+
res = self._get_database_trace_total('xldb1数据库使用近况', *args)
|
906
|
+
htmltexts.append(res)
|
907
|
+
|
908
|
+
data_stats = []
|
909
|
+
for idx, db in enumerate(db_list, start=1):
|
910
|
+
data_stats.append(self._get_database_trace_per_host(db, f'{db}', *args))
|
911
|
+
htmltexts += data_stats
|
912
|
+
|
913
|
+
self.commit()
|
914
|
+
h = render_echart_html('database_cdx', body='<br/>'.join(htmltexts))
|
915
|
+
return h
|
916
|
+
|
917
|
+
def dbview_xldb2_memory(self, recent=datetime.timedelta(days=180), date_trunc='day'):
|
918
|
+
from pyxllib.data.echarts import render_echart_html
|
919
|
+
|
920
|
+
db_list = ['ragdata', 'kq5034']
|
921
|
+
args = ['数据库大小(GB)', date_trunc, recent, 'xldb2']
|
922
|
+
htmltexts = []
|
923
|
+
|
924
|
+
res = self._get_database_trace_total('xldb2数据库使用近况', *args)
|
925
|
+
htmltexts.append(res)
|
926
|
+
|
927
|
+
data_stats = []
|
928
|
+
for idx, db in enumerate(db_list, start=1):
|
929
|
+
data_stats.append(self._get_database_trace_per_host(db, f'{db}', *args))
|
930
|
+
htmltexts += data_stats
|
931
|
+
|
932
|
+
self.commit()
|
933
|
+
h = render_echart_html('database_cdx', body='<br/>'.join(htmltexts))
|
934
|
+
return h
|
935
|
+
|
936
|
+
def dbview_cpu(self, recent=datetime.timedelta(days=1), date_trunc='hour'):
|
937
|
+
from pyxllib.data.echarts import render_echart_html
|
938
|
+
|
939
|
+
args = ['CPU核心数(比如4核显示是400%)', date_trunc, recent, 'sum(hosts.cpu_number)*100']
|
940
|
+
|
941
|
+
htmltexts = [
|
942
|
+
'<a target="_blank" href="https://www.yuque.com/xlpr/data/hnpb2g?singleDoc#"> 《服务器监控》工具使用文档 </a>']
|
943
|
+
res = self._get_host_trace_total('cpu', 'XLPR服务器 CPU 使用近况', *args)
|
944
|
+
htmltexts.append(res[0])
|
945
|
+
|
946
|
+
hosts = self.execute('SELECT host_name, nick_name FROM hosts WHERE gpu_gb > 0').fetchall()
|
947
|
+
host_stats = []
|
948
|
+
for i, (hn, nick_name) in enumerate(hosts, start=1):
|
949
|
+
name = f'{hn},{nick_name}' if nick_name else hn
|
950
|
+
host_stats.append(self._get_host_trace_per_host(hn, 'cpu', f'{name}', *args))
|
951
|
+
host_stats.sort(key=lambda x: -x[1]) # 按使用量,从多到少排序
|
952
|
+
htmltexts += [x[0] for x in host_stats]
|
953
|
+
|
954
|
+
self.commit()
|
955
|
+
|
956
|
+
h = render_echart_html('cpu', body='<br/>'.join(htmltexts))
|
957
|
+
return h
|
958
|
+
|
959
|
+
def dbview_cpu_memory(self, recent=datetime.timedelta(days=1), date_trunc='hour'):
|
960
|
+
from pyxllib.data.echarts import render_echart_html
|
961
|
+
|
962
|
+
args = ['内存(单位:GB)', date_trunc, recent, 'sum(hosts.cpu_gb)']
|
963
|
+
|
964
|
+
htmltexts = [
|
965
|
+
'<a target="_blank" href="https://www.yuque.com/xlpr/data/hnpb2g?singleDoc#"> 《服务器监控》工具使用文档 </a>']
|
966
|
+
res = self._get_host_trace_total('cpu_memory', 'XLPR服务器 内存 使用近况', *args)
|
967
|
+
htmltexts.append(res[0])
|
968
|
+
|
969
|
+
hosts = self.execute('SELECT host_name, nick_name FROM hosts WHERE gpu_gb > 0').fetchall()
|
970
|
+
host_stats = []
|
971
|
+
for i, (hn, nick_name) in enumerate(hosts, start=1):
|
972
|
+
name = f'{hn},{nick_name}' if nick_name else hn
|
973
|
+
host_stats.append(self._get_host_trace_per_host(hn, 'cpu_memory', f'{name}', *args))
|
974
|
+
host_stats.sort(key=lambda x: -x[1]) # 按使用量,从多到少排序
|
975
|
+
htmltexts += [x[0] for x in host_stats]
|
976
|
+
|
977
|
+
self.commit()
|
978
|
+
|
979
|
+
h = render_echart_html('cpu_memory', body='<br/>'.join(htmltexts))
|
980
|
+
return h
|
981
|
+
|
982
|
+
def dbview_disk_memory(self, recent=datetime.timedelta(days=360), date_trunc='day'):
|
983
|
+
""" 查看disk硬盘使用近况
|
984
|
+
"""
|
985
|
+
from pyxllib.data.echarts import render_echart_html
|
986
|
+
|
987
|
+
args = ['硬盘(单位:GB)', date_trunc, recent, 'sum(hosts.disk_gb)']
|
988
|
+
|
989
|
+
htmltexts = [
|
990
|
+
'<a target="_blank" href="https://www.yuque.com/xlpr/data/hnpb2g?singleDoc#"> 《服务器监控》工具使用文档 </a>']
|
991
|
+
res = self._get_host_trace_total('disk_memory', 'XLPR服务器 DISK硬盘 使用近况', *args)
|
992
|
+
htmltexts.append(res[0])
|
993
|
+
htmltexts.append('注:xlpr4(四卡)服务器使用du计算/home大小有问题,未统计在列<br/>')
|
994
|
+
|
995
|
+
hosts = self.execute('SELECT host_name, nick_name FROM hosts WHERE gpu_gb > 0').fetchall()
|
996
|
+
host_stats = []
|
997
|
+
for i, (hn, nick_name) in enumerate(hosts, start=1):
|
998
|
+
name = f'{hn},{nick_name}' if nick_name else hn
|
999
|
+
host_stats.append(self._get_host_trace_per_host(hn, 'disk_memory', f'{name}', *args))
|
1000
|
+
host_stats.sort(key=lambda x: -x[1]) # 按使用量,从多到少排序
|
1001
|
+
htmltexts += [x[0] for x in host_stats]
|
1002
|
+
|
1003
|
+
self.commit()
|
1004
|
+
|
1005
|
+
h = render_echart_html('disk_memory', body='<br/>'.join(htmltexts))
|
1006
|
+
return h
|
1007
|
+
|
1008
|
+
def dbview_gpu_memory(self, recent=datetime.timedelta(days=30), date_trunc='day'):
|
1009
|
+
""" 查看gpu近使用近况
|
1010
|
+
|
1011
|
+
TODO 这里有可以并行处理的地方,但这个方法不是很重要,不需要特地去做加速占用cpu资源
|
1012
|
+
"""
|
1013
|
+
from pyxllib.data.echarts import render_echart_html
|
1014
|
+
|
1015
|
+
args = ['显存(单位:GB)', date_trunc, recent, 'sum(hosts.gpu_gb)']
|
1016
|
+
|
1017
|
+
htmltexts = [
|
1018
|
+
'<a target="_blank" href="https://www.yuque.com/xlpr/data/hnpb2g?singleDoc#"> 《服务器监控》工具使用文档 </a>']
|
1019
|
+
res = self._get_host_trace_total('gpu_memory', 'XLPR八台服务器 GPU显存 使用近况', *args)
|
1020
|
+
htmltexts.append(res[0])
|
1021
|
+
|
1022
|
+
hosts = self.execute('SELECT host_name, nick_name FROM hosts WHERE gpu_gb > 0').fetchall()
|
1023
|
+
host_stats = []
|
1024
|
+
for i, (hn, nick_name) in enumerate(hosts, start=1):
|
1025
|
+
name = f'{hn},{nick_name}' if nick_name else hn
|
1026
|
+
host_stats.append(self._get_host_trace_per_host(hn, 'gpu_memory', f'{name}', *args))
|
1027
|
+
host_stats.sort(key=lambda x: -x[1]) # 按使用量,从多到少排序
|
1028
|
+
htmltexts += [x[0] for x in host_stats]
|
1029
|
+
|
1030
|
+
self.commit()
|
1031
|
+
|
1032
|
+
h = render_echart_html('gpu_memory', body='<br/>'.join(htmltexts))
|
1033
|
+
return h
|
1034
|
+
|
1035
|
+
def __4_一些数据更新操作(self):
|
1036
|
+
""" 比如一些扩展字段,在调用api的时候为了性能并没有进行计算,则可以这里补充更新 """
|
1037
|
+
|
1038
|
+
def update_files_dhash(self, print_mode=True):
|
1039
|
+
""" 更新files表中的dhash字段值 """
|
1040
|
+
from pyxllib.cv.imhash import dhash
|
1041
|
+
from pyxllib.xlcv import xlpil
|
1042
|
+
|
1043
|
+
# 获取总图片数
|
1044
|
+
total_count = self.execute("SELECT COUNT(*) FROM files WHERE dhash IS NULL").fetchall()[0][0]
|
1045
|
+
|
1046
|
+
# 执行查询语句,获取dhash为NULL的记录
|
1047
|
+
|
1048
|
+
# 初始化进度条
|
1049
|
+
progress_bar = tqdm(total=total_count, disable=not print_mode)
|
1050
|
+
|
1051
|
+
while True:
|
1052
|
+
result = self.execute("SELECT id, data FROM files WHERE dhash IS NULL LIMIT 5")
|
1053
|
+
if not result:
|
1054
|
+
break
|
1055
|
+
for row in result:
|
1056
|
+
file_id = row[0]
|
1057
|
+
im = xlpil.read_from_buffer(row[1])
|
1058
|
+
computed_dhash = str(dhash(im))
|
1059
|
+
self.update_row('files', {'dhash': computed_dhash}, {'id': file_id})
|
1060
|
+
progress_bar.update(1)
|
1061
|
+
self.commit()
|
1062
|
+
|
1063
|
+
def append_history(self, table_name, where, backup_keys, *,
|
1064
|
+
can_merge=None,
|
1065
|
+
update_time=None,
|
1066
|
+
commit=False):
|
1067
|
+
""" 为表格添加历史记录,请确保这个表有一个jsonb格式的historys字段
|
1068
|
+
|
1069
|
+
这里每次都会对关键字段进行全量备份,没有进行高级的优化。
|
1070
|
+
所以只适用于一些历史记录功能场景。更复杂的还是需要另外自己定制。
|
1071
|
+
|
1072
|
+
:param table_name: 表名
|
1073
|
+
:param where: 要记录的id的规则,请确保筛选后记录是唯一的
|
1074
|
+
:param backup_keys: 需要备份的字段名
|
1075
|
+
:param can_merge: 在某些情况下,history不需要非常冗余地记录,可以给定与上一条合并的规则
|
1076
|
+
def can_merge(last, now):
|
1077
|
+
"last是上一条字典记录,now是当前要记录的字典数据,
|
1078
|
+
返回True,则用now替换last,并不新增记录"
|
1079
|
+
...
|
1080
|
+
|
1081
|
+
:param update_time: 更新时间,如果不指定则使用当前时间
|
1082
|
+
"""
|
1083
|
+
# 1 获得历史记录
|
1084
|
+
ops = ' AND '.join([f'{k}=%s' for k in where.keys()])
|
1085
|
+
historys = self.exec2one(f'SELECT historys FROM {table_name} WHERE {ops}', list(where.values())) or []
|
1086
|
+
if historys:
|
1087
|
+
status1 = historys[-1]
|
1088
|
+
else:
|
1089
|
+
status1 = {}
|
1090
|
+
|
1091
|
+
# 2 获得新记录
|
1092
|
+
if update_time is None:
|
1093
|
+
update_time = utc_timestamp()
|
1094
|
+
status2 = self.exec2dict(f'SELECT {",".join(backup_keys)} FROM {table_name} WHERE {ops}',
|
1095
|
+
list(where.values())).fetchone()
|
1096
|
+
status2['update_time'] = update_time
|
1097
|
+
|
1098
|
+
# 3 添加历史记录
|
1099
|
+
if can_merge is None:
|
1100
|
+
def can_merge(status1, status2):
|
1101
|
+
for k in backup_keys:
|
1102
|
+
if status1.get(k) != status2.get(k):
|
1103
|
+
return False
|
1104
|
+
return True
|
1105
|
+
|
1106
|
+
if historys and can_merge(status1, status2):
|
1107
|
+
historys[-1] = status2
|
1108
|
+
else:
|
1109
|
+
historys.append(status2)
|
1110
|
+
|
1111
|
+
self.update_row(table_name, {'historys': historys}, where, commit=commit)
|