-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path61_update-member-info-of-name-duplicated-member.py
104 lines (88 loc) · 4.14 KB
/
61_update-member-info-of-name-duplicated-member.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
60
61
62
63
64
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
import logging
from db import Session
from service import Service
from task import update_member
from core import TddError
from collections import defaultdict
from util import logging_init, get_ts_s, ts_s_to_str, format_ts_s, format_ts_ms, get_ts_ms, fullname
from serverchan import sc_send
script_id = '61'
script_name = 'update-member-info-of-name-duplicated-member'
script_fullname = fullname(script_id, script_name)
logger = logging.getLogger(script_id)
def update_member_info_of_name_duplicated_member():
logger.info(f'Now start {script_fullname}...')
start_ts = get_ts_s() # get start ts
session = Session()
service = Service(mode="worker", retry=20)
statistics = defaultdict(int)
logger.info('Now get duplicated names...')
results = session.execute(
'select `name`, count(`name`) as count from tdd_member where `name` != "账号已注销" '
'group by `name` having count > 1 order by count desc;'
)
duplicated_name_list = []
for r in results:
logger.info(f'{r[1]} members have duplicated name {r[0]}')
duplicated_name_list.append(r[0])
if len(duplicated_name_list) == 0:
logger.info('No duplicated name detected!')
else:
logger.info(f'{len(duplicated_name_list)} duplicated names detected!')
logger.info('Now get member mids with duplicated name...')
results = session.execute('select mid from tdd_member where `name` in (%s);'
% ', '.join(map(lambda name: '"%s"' % name, duplicated_name_list)))
name_duplicated_mids = []
for r in results:
name_duplicated_mids.append(r[0])
logger.info(f'{len(name_duplicated_mids)} name duplicated mids got!')
for idx, mid in enumerate(name_duplicated_mids, 1):
start_ts_ms = get_ts_ms()
try:
tdd_member_logs = update_member(mid, service, session)
except TddError as e:
logger.warning(f'Fail to update member info. mid: {mid}, error: {e}')
statistics['tdd_error_count'] += 1
except Exception as e:
logger.warning(f'Fail to update member info. mid: {mid}, error: {e}')
statistics['other_exception_count'] += 1
else:
if len(tdd_member_logs) == 0:
statistics['no_update_count'] += 1
else:
statistics['change_count'] += 1
for log in tdd_member_logs:
logger.info(f'Update member info. mid: {mid}, attr: {log.attr}, {log.oldval} -> {log.newval}')
statistics['change_log_count'] += 1
logger.debug(f'{tdd_member_logs} log(s) found. mid: {mid}')
end_ts_ms = get_ts_ms()
cost_ms = end_ts_ms - start_ts_ms
logger.debug(f'Finish update member info. mid: {mid}, cost: {format_ts_ms(cost_ms)}')
statistics['total_count'] += 1
statistics['total_cost_ms'] += cost_ms
# get end ts
end_ts = get_ts_s()
# make summary
summary = \
'update member info of name duplicated name done!\n\n' \
f'start: {ts_s_to_str(start_ts)}, ' \
f'end: {ts_s_to_str(end_ts)}, ' \
f'cost: {format_ts_s(end_ts - start_ts)}\n\n' \
f'total count: {statistics["total_count"]}, ' + \
f'average cost per service: {format_ts_ms(statistics["total_cost_ms"] // statistics["total_count"])}\n\n' \
f'tdd error count: {statistics["tdd_error_count"]}\n\n' \
f'other exception count: {statistics["other_exception_count"]}\n\n' \
f'no update count: {statistics["no_update_count"]}\n\n' \
f'change count: {statistics["change_count"]}\n\n' \
f'change log count: {statistics["change_log_count"]}\n\n' \
f'by.bunnyxt, {ts_s_to_str(get_ts_s())}'
logger.info('Finish update member info of name duplicated member!')
logger.warning(summary)
# send sc
sc_send('Finish update member info of name duplicated name!', summary)
session.close()
def main():
update_member_info_of_name_duplicated_member()
if __name__ == '__main__':
logging_init(file_prefix=script_id)
main()