-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
365 lines (291 loc) · 14.4 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/python3
import datetime
import sys
import getopt
import time
import pymysql
class Clear:
def __init__(self, cursor, conn):
# Connect to the database
self.cursor = cursor
self.conn = conn
def partner_record(self, lt_day):
querySql = "select * from partner_record where id < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from partner_record where id < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
# 返佣流水表
def partner_flow(self, lt_day):
querySql = "select * from partner_flow where day < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from partner_flow where day < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def dp_rebate_freeze_log(self, lt_day):
querySql = "select * from dp_rebate_freeze_log where create_time < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from dp_rebate_freeze_log where create_time < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def dp_report_incurred(self, lt_day):
querySql = "select * from dp_report_incurred where create_time < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from dp_report_incurred where create_time< %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def super_statistics(self, lt_day):
querySql = "select * from super_statistics where day < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from super_statistics where day < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def agent_sketch_list(self, lt_day):
querySql = "select * from agent_sketch_list where day < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from agent_sketch_list where day < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def rs_dwd_user_account_currency_detail_h(self, lt_day):
querySql = "select * from rs_dwd_user_account_currency_detail_h where date < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from rs_dwd_user_account_currency_detail_h where date < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def dp_edm_send_log(self, lt_day):
querySql = "select * from dp_edm_send_log where create_time < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from dp_edm_send_log where create_time < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def contract_record(self, lt_day):
querySql = "select * from contract_record where current_time < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from contract_record where current_time < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def t_D_Trade(self, lt_day):
querySql = "select * from t_D_Trade where TradeTime < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from t_D_Trade where TradeTime < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def t_D_AccountDetail(self, lt_day):
querySql = "select * from t_D_AccountDetail where UpdateTime < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from t_D_AccountDetail where UpdateTime < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def t_D_FinishOrder(self, lt_day):
querySql = "select * from t_D_FinishOrder where UpdateTime < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from t_D_FinishOrder where UpdateTime < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def t_D_FinishTriggerOrder(self, lt_day):
querySql = "select * from t_D_FinishTriggerOrder where UpdateTime < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from t_D_FinishTriggerOrder where UpdateTime < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def t_D_FinishPosition(self, lt_day):
querySql = "select * from t_D_FinishPosition where UpdateTime < %s limit 1;"
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
while result is not None:
clearSql = "delete from t_D_FinishPosition where UpdateTime < %s limit 500000;"
cursor.execute(clearSql, lt_day)
self.conn.commit()
self.cursor.execute(querySql, lt_day)
result = self.cursor.fetchone()
def clear_time_format(current_time, cleaning_days, format):
if format == "%Y%m%d":
# format为"%Y%m%d"适配时间格式为20230910这种格式
past_time = current_time - datetime.timedelta(days=cleaning_days)
past_time_string = past_time.strftime("%Y-%m-%d")
past_time_int = int(past_time_string.replace("-", ""))
return past_time_int
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "",
["host=", "user=", "password=", "port=", "database=", "cleaning_method="])
except getopt.GetoptError:
print("%s --host <host> --user <user> --password <password> --port <port> --database <database> "
"--cleaning_method <cleaning_method>" % (sys.argv[0]))
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("%s --host <host> --user <user> --password <password> --port <port> --database <database> "
"--cleaning_method <cleaning_method> --cleaning_days <cleaning_days>" % (sys.argv[0]))
sys.exit()
elif opt in "--host":
host = arg
elif opt in "--user":
user = arg
elif opt in "--password":
password = arg
elif opt in "--port":
port = int(arg)
elif opt in "--database":
database = arg
elif opt in "--cleaning_method":
cleaning_method = arg
conn = pymysql.connect(host=host,
user=user,
password=password,
port=port,
database=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
current_time = datetime.datetime.now()
# clear = Clear(conn)
with conn:
with conn.cursor() as cursor:
clear = Clear(cursor, conn)
if cleaning_method == "partner_flow":
# partner_flow表,默认清理60天前的
cleaning_days = 60
cleaning_time = clear_time_format(current_time, cleaning_days, "%Y%m%d")
print(cleaning_time)
clear.partner_flow(cleaning_time)
elif cleaning_method == "dp_rebate_freeze_log":
# dp_rebate_freeze_log表,默认清理60天前的
cleaning_days = 60
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.dp_rebate_freeze_log(cleaning_time)
elif cleaning_method == "dp_report_incurred":
# dp_report_incurred表,默认清理30天前的
cleaning_days = 30
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.dp_report_incurred(cleaning_time)
elif cleaning_method == "super_statistics":
# super_statistics表,默认清理180天前的
cleaning_days = 180
cleaning_time = clear_time_format(current_time, cleaning_days, "%Y%m%d")
print(cleaning_time)
clear.super_statistics(cleaning_time)
elif cleaning_method == "agent_sketch_list":
# agent_sketch_list,默认清理90天前的
cleaning_days = 90
cleaning_time = clear_time_format(current_time, cleaning_days, "%Y%m%d")
print(cleaning_time)
clear.agent_sketch_list(cleaning_time)
elif cleaning_method == "rs_dwd_user_account_currency_detail_h":
# rs_dwd_user_account_currency_detail_h,默认清理365天前的
cleaning_days = 365
cleaning_time = clear_time_format(current_time, cleaning_days, "%Y%m%d")
print(cleaning_time)
clear.rs_dwd_user_account_currency_detail_h(cleaning_time)
elif cleaning_method == "dp_edm_send_log":
# dp_edm_send_log,默认清理90天前的
cleaning_days = 90
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.dp_edm_send_log(cleaning_time)
elif cleaning_method == "contract_record":
# contract_record,默认清理180天前的
cleaning_days = 180
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.contract_record(cleaning_time)
elif cleaning_method == "t_D_Trade":
# t_D_Trade,默认清理180天前的
cleaning_days = 180
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.t_D_Trade(cleaning_time)
elif cleaning_method == "t_D_AccountDetail":
# t_D_AccountDetail,默认清理180天前的
cleaning_days = 180
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.t_D_AccountDetail(cleaning_time)
elif cleaning_method == "t_D_FinishOrder":
# t_D_FinishOrder,默认清理180天前的
cleaning_days = 180
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.t_D_FinishOrder(cleaning_time)
elif cleaning_method == "t_D_FinishTriggerOrder":
# t_D_FinishTriggerOrder,默认清理180天前的
cleaning_days = 180
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.t_D_FinishTriggerOrder(cleaning_time)
elif cleaning_method == "t_D_FinishPosition":
# t_D_FinishPosition,默认清理180天前的
cleaning_days = 180
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
print(cleaning_time)
clear.t_D_FinishPosition(cleaning_time)
elif cleaning_method == "partner_record":
# partner_record,默认清理60天前的
cleaning_days = 60
cleaning_time = int(time.time() - cleaning_days * 24 * 60 * 60)
result = None
start_temp_cleaning_time = cleaning_time - 300
end_temp_cleaning_time = cleaning_time
while result is None:
querySql = "select * from partner_record where create_time >= %s and create_time <= %s limit 1;"
cursor.execute(querySql, (start_temp_cleaning_time, end_temp_cleaning_time))
result = cursor.fetchone()
end_temp_cleaning_time = start_temp_cleaning_time
start_temp_cleaning_time = start_temp_cleaning_time - 300
if result["create_time"] <= cleaning_time:
if result is not None:
print("result", result["id"], result["create_time"], cleaning_time)
clear.partner_record(result["id"])