-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
610 lines (537 loc) · 21.7 KB
/
main2.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
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
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
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
577
578
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
import copy
import csv
import dataclasses
import json
import os
from collections import defaultdict
from datetime import datetime, timedelta
from io import StringIO
from file_save_helper import FileSaveHelper
from src.util import discard_ones_digit
@dataclasses.dataclass(frozen=False)
class GoodsContext:
goods_sno: int = -1
goods_name: str = ''
thumbnail_price: int = -1
correct_price: int = -1
consumer_origin: int = -1
price_origin: int = -1
discount_type: int = -1
discount_rate: int = -1
discount_price: int = -1
updated_at: datetime = datetime(1970, 1, 1)
@dataclasses.dataclass(frozen=True)
class DatadogLog:
market_sno: int
goods_sno: int
consumer_origin: int
price_origin: int
discount_type: int
discount_rate: int
discount_price: int
discount_started_at: datetime
discount_ended_at: datetime
request_time: datetime
@dataclasses.dataclass(frozen=True)
class OrderItem:
item_sno: int
order_sno: int
market_sno: int
option_sno: int
goods_sno: int
goods_name: str
quantity: int
price: int
checked_at: datetime
@dataclasses.dataclass(frozen=True)
class InvalidData:
context: GoodsContext
log: DatadogLog
item: OrderItem
class ReaderUtil:
@classmethod
def parse_value(cls, value: str) -> str:
"""Parse a CSV value that might be wrapped in quotes or be a plain integer."""
# Strip quotes if present
if value.startswith('"') and value.endswith('"'):
return value[1:-1]
return value
@classmethod
def parse_int(cls, value: str) -> int:
"""Parse a value into an integer, handling quoted strings."""
if value == '':
return -1
cleaned_value: str = cls.parse_value(value)
try:
return int(cleaned_value.replace("'", ""))
except ValueError:
return int(float(cleaned_value))
@classmethod
def parse_csv_line_with_csv(cls, line: str) -> list[str]:
return next(csv.reader(StringIO(line)))
class GrafanaLogReader:
@classmethod
def read(cls, filepaths: list[str], limit: int | None) -> list[str]:
print('grafana read log starts...')
res: list[str] = []
for filepath in filepaths:
data: bytes = FileSaveHelper.read(filepath=filepath)
print('bytes loaded..')
decoded = data.decode('utf-8')
if limit:
lines: list[str] = decoded.splitlines()[:limit]
else:
lines: list[str] = decoded.splitlines()
print('lines: ', len(lines))
for line in lines:
res.append(cls._extract_log_data(line))
return res
@classmethod
def _extract_log_data(cls, line: str) -> str:
try:
data: dict = json.loads(line)
except json.JSONDecodeError:
return line
return data.get("line")
class SellerLogReader:
@classmethod
def read(cls, filepaths: list[str] = None, file_dir_path: str = None) -> list[str]:
if filepaths:
for filepath in filepaths:
data: bytes = FileSaveHelper.read(filepath=filepath)
decoded = data.decode('utf-8')
lines = decoded.splitlines()
return lines
return []
@classmethod
def parse(cls, lines: list[str]) -> list[DatadogLog]:
res: list[DatadogLog] = []
for line in lines[1:]:
columns: list[str] = ReaderUtil.parse_csv_line_with_csv(line=line)
data = cls._parse_csv_line(line=columns)
if data:
res.append(data)
return sorted(res, key=lambda x: x.request_time)
@classmethod
def _parse_csv_line(cls, line: list[str]) -> DatadogLog | None:
'''
Date, 0
ably_market_sno, 1
@ably_sno, 2
@prices, 3
consumer_origin, 4
@price_origin, 5
@consumer_price_adjustment.discount_type, 6
@consumer_price_adjustment.discount_rate, 7
@consumer_price_adjustment.discount_price, 8
@consumer_price_adjustment.ended_at, 9
@consumer_price_adjustment.started_at, 10
Message 11
"2025-01-02T23:47:16.270Z","18675","""37364075""",,"43000","14900","0","0","28100",,,"update_seller_goods"
"2025-01-08T02:09:39.868Z","32368","""21503547""","[{""app_type"":0,""discount_policy"":{""policy_type"":0,""policy_value"":111000},""consumer"":1800000},{""app_type"":1,""discount_policy"":{""policy_type"":0,""policy_value"":111000},""consumer"":1800000}]",,,,,,,,"update_seller_goods"
'''
dt: datetime = cls._parse_datetime(line[0]) + timedelta(hours=9)
started_at: datetime = cls._parse_datetime(line[9], datetime(1970, 1, 1))
ended_at: datetime = cls._parse_datetime(line[10], datetime(9999, 12, 31))
prices: list[dict] = json.loads(line[3]) if line[3] else []
if prices:
app_price: dict = prices[0]
discount_type: int = app_price.get("discount_policy", {}).get("policy_type", 0)
app_consumer: int = app_price.get("consumer")
discount_value: int = app_price.get("discount_policy", {}).get("policy_value", 0)
discount_price: int = discard_ones_digit(discount_value)
if discount_type == 1 and discount_value > 0:
# 정률
discount_price = app_consumer * discount_value
started_at: datetime = app_price.get("discount_policy", {}).get("started_at", datetime(1970, 1, 1))
ended_at: datetime = app_price.get("discount_policy", {}).get("ended_at", datetime(9999, 12, 31))
return DatadogLog(
market_sno=ReaderUtil.parse_int(line[1]),
goods_sno=ReaderUtil.parse_int(line[2]),
consumer_origin=app_consumer,
price_origin=ReaderUtil.parse_int(line[5]),
discount_type=0,
discount_rate=0,
discount_price=discount_price,
discount_started_at=cls._parse_datetime(started_at, datetime(1970, 1, 1)),
discount_ended_at=cls._parse_datetime(ended_at, datetime(9999, 12, 31)),
request_time=dt,
)
else:
return DatadogLog(
market_sno=ReaderUtil.parse_int(line[1]),
goods_sno=ReaderUtil.parse_int(line[2]),
consumer_origin=ReaderUtil.parse_int(line[4]),
price_origin=ReaderUtil.parse_int(line[5]),
discount_type=ReaderUtil.parse_int(line[6]) if line[6] else -1,
discount_rate=ReaderUtil.parse_int(line[7]) if line[7] else -1,
discount_price=ReaderUtil.parse_int(line[8]) if line[8] else -1,
discount_started_at=started_at,
discount_ended_at=ended_at,
request_time=dt,
)
@classmethod
def _parse_datetime(cls, datetime_str: str | datetime, default_dt: datetime = None) -> datetime | None:
'''
Parses datetime strings in formats:
- "2025-01-13 04:48:59.005" # With milliseconds, space separator
- "2025-01-13T04:48:59.005Z" # With milliseconds, ISO format
- "2025-01-03T06:50:06Z" # Without milliseconds, ISO format
'''
if isinstance(datetime_str, datetime):
return datetime_str
cleaned_str = datetime_str.strip('"')
if cleaned_str == "":
return default_dt
# Replace comma with period for proper datetime parsing
cleaned_str = cleaned_str.replace(',', '.')
formats_to_try = [
"%Y-%m-%d %H:%M:%S.%f", # With milliseconds, space separator
"%Y-%m-%dT%H:%M:%S.%fZ", # With milliseconds, ISO format
"%Y-%m-%dT%H:%M:%SZ" # Without milliseconds, ISO format
]
for date_format in formats_to_try:
try:
return datetime.strptime(cleaned_str, date_format)
except ValueError:
continue
return default_dt
class LogReader:
HEADERS: list[str] = [
"Date", # 0
"Host", # 1
"Service" # 2
"@ably_sno", # 3
"ably_market_sno", # 4
"consumer_origin", # 5
"@price_origin", # 6
"@consumer_price_adjustment.discount_type", # 7
"@consumer_price_adjustment.discount_rate", # 8
"@consumer_price_adjustment.discount_price", # 9
"@consumer_price_adjustment.started_at", # 10
"@consumer_price_adjustment.ended_at", # 11
"Message", # 12
]
@classmethod
def read(cls, filepaths: list[str] = None, file_dir_path: str = None) -> list[str]:
res: list[str] = []
if filepaths:
for filepath in filepaths:
data: bytes = FileSaveHelper.read(filepath=filepath)
decoded = data.decode('utf-8')
lines = decoded.splitlines()
# Skip header
res.extend(lines[1:])
elif file_dir_path:
for filename in os.listdir(f"{os.getcwd()}/{file_dir_path}"):
data: bytes = FileSaveHelper.read(filepath=f"{file_dir_path}{filename}")
print(f"{file_dir_path}{filename}")
decoded = data.decode('utf-8')
lines = decoded.splitlines()
res.extend(lines[1:])
return res
@classmethod
def parse(cls, lines: list[str]) -> list[DatadogLog]:
res: list[DatadogLog] = []
for line in lines:
columns: list[str] = ReaderUtil.parse_csv_line_with_csv(line=line)
data = cls._parse_csv_line(line=columns)
if data:
res.append(data)
return sorted(res, key=lambda x: x.request_time)
@classmethod
def _parse_csv_line(cls, line: list[str]) -> DatadogLog | None:
'''
33878,33056035,69000,62500,6500,,0,"2025-01-13T00:00:00Z","2999-12-31T23:59:59Z","2025-01-13 22:47:10,781",vendor_seller_update_goods
{{.ably_market_sno}}, 0
{{.ably_sno}}, 1
{{.consumer_origin}}, 2
{{.price_origin}}, 3
{{.consumer_price_adjustment_discount_price}}, 4
{{.consumer_price_adjustment_discount_rate}}, 5
{{.consumer_price_adjustment_discount_type}}, 6
"{{.consumer_price_adjustment_started_at}}", 7
"{{.consumer_price_adjustment_ended_at}}", 8
"{{.asctime}}", 9
{{.message}} 10
'''
message = line[10]
if message != "vendor_seller_update_goods":
return None
dt: datetime = cls._parse_datetime(line[9])
started_at: datetime = cls._parse_datetime(line[7], datetime(1970, 1, 1))
ended_at: datetime = cls._parse_datetime(line[8], datetime(9999, 12, 31))
return DatadogLog(
market_sno=ReaderUtil.parse_int(line[0]),
goods_sno=ReaderUtil.parse_int(line[1]),
consumer_origin=ReaderUtil.parse_int(line[2]),
price_origin=ReaderUtil.parse_int(line[3]),
discount_type=ReaderUtil.parse_int(line[6]) if line[6] else -1,
discount_rate=ReaderUtil.parse_int(line[5]) if line[5] else -1,
discount_price=ReaderUtil.parse_int(line[4]) if line[4] else -1,
discount_started_at=started_at,
discount_ended_at=ended_at,
request_time=dt,
)
@classmethod
def _parse_datetime(cls, datetime_str: str, default_dt: datetime = None) -> datetime | None:
cleaned_str = datetime_str.strip('"')
if cleaned_str == "":
return default_dt
# Replace comma with period for proper datetime parsing
cleaned_str = cleaned_str.replace(',', '.')
try:
return datetime.strptime(cleaned_str, "%Y-%m-%d %H:%M:%S.%f")
except ValueError:
return default_dt
class ItemReader:
@classmethod
def read(cls, filepaths: list[str]) -> list[str]:
res: list[str] = []
for filepath in filepaths:
print("filepath: ", filepath)
data: bytes = FileSaveHelper.read(filepath=filepath)
decoded = data.decode('utf-8')
lines = decoded.splitlines()
# Skip header
res.extend(lines[1:])
return res
@classmethod
def parse(cls, lines: list[str]) -> list[OrderItem]:
'''
sno, 0
ordno, 1
market_sno, 2
goods_option_sno, 3
goodsno, 4
goodsnm, 5
price, 6
memberdc,
emoney,
coupon,
ea, 10
reserve,
checked_at 12
'''
res: list[OrderItem] = []
for line in lines:
columns: list[str] = ReaderUtil.parse_csv_line_with_csv(line=line)
checked_at: datetime = datetime.strptime(columns[12], "%Y-%m-%d %H:%M:%S.%f")
res.append(
OrderItem(
item_sno=ReaderUtil.parse_int(columns[0]),
order_sno=ReaderUtil.parse_int(columns[1]),
market_sno=int(columns[2]),
option_sno=int(columns[3]),
goods_sno=ReaderUtil.parse_int(columns[4]),
goods_name=columns[5],
price=ReaderUtil.parse_int(columns[6]),
quantity=ReaderUtil.parse_int(columns[10]),
checked_at=checked_at,
)
)
return sorted(res, key=lambda x: x.checked_at)
class ItemAnalyzer:
@classmethod
def analyze(cls, logs: list[DatadogLog], items: list[OrderItem]) -> list[InvalidData]:
res: list[InvalidData] = []
context_map: dict[int, GoodsContext] = defaultdict(GoodsContext)
goods_map: dict[int, list[DatadogLog]] = defaultdict(list)
for log in logs:
goods_map[log.goods_sno].append(log)
for item in items:
checked_at: datetime = item.checked_at
ctx = cls._apply_updates(
ctx=context_map[item.goods_sno], logs=goods_map[item.goods_sno], checked_at=checked_at
)
ctx.goods_name = item.goods_name
context_map[item.goods_sno] = ctx
if ctx.correct_price != item.price and ctx.goods_sno != -1:
res.append(
InvalidData(
context=copy.deepcopy(ctx),
log=copy.deepcopy(log),
item=copy.deepcopy(item),
)
)
return res
@classmethod
def _apply_updates(cls, ctx: GoodsContext, logs: list[DatadogLog], checked_at: datetime) -> GoodsContext:
while logs and logs[0].request_time <= checked_at:
log: DatadogLog = logs.pop(0)
correct_price: int = cls.calc_correct_price(log=log)
ctx.goods_sno = log.goods_sno
ctx.correct_price = correct_price
ctx.price_origin = log.price_origin
ctx.consumer_origin = log.consumer_origin
ctx.discount_price = log.discount_price
ctx.discount_rate = log.discount_rate
ctx.discount_type = log.discount_type
ctx.updated_at = log.request_time
ctx.goods_sno = log.goods_sno
return ctx
@classmethod
def calc_correct_price(cls, log: DatadogLog) -> int:
'''
algorithm.
정가: [max(consumer_origin, price_origin)]
할인: [
정가 * discount_rate,
discount_price,
consumer_diff,
]
'''
consumer: int = cls._get_best_consumer(
consumer_origin=log.consumer_origin,
price_origin=log.price_origin,
)
discount: int = cls._get_best_discount(
consumer=consumer,
discount_price=log.discount_price,
discount_type=log.discount_type,
discount_rate=log.discount_rate,
diff=abs(log.consumer_origin - log.price_origin),
started_at=log.discount_started_at,
ended_at=log.discount_ended_at,
checked_at=log.request_time,
)
return consumer - discount
@classmethod
def _get_best_consumer(cls, consumer_origin: int, price_origin: int) -> int:
return max([consumer_origin, price_origin])
@classmethod
def _get_best_discount(
cls,
consumer: int,
discount_price: int,
discount_type: int,
discount_rate: int,
diff: int,
started_at: datetime,
ended_at: datetime,
checked_at: datetime,
) -> int:
adj_discount_price: int = cls._get_consumer_price_adj_discount(
consumer=consumer,
discount_price=discount_price,
discount_type=discount_type,
discount_rate=discount_rate,
started_at=started_at,
ended_at=ended_at,
checked_at=checked_at,
)
return adj_discount_price or diff
@classmethod
def _get_consumer_price_adj_discount(
cls,
consumer: int,
discount_price: int,
discount_type: int,
discount_rate: int,
started_at: datetime,
ended_at: datetime,
checked_at: datetime,
) -> int:
can_discount: bool = started_at <= checked_at <= ended_at
if discount_type == 1 and discount_rate > 0 and discount_price == 0:
# 정률
res = discard_ones_digit(
consumer * discount_rate
)
if can_discount:
return res if res > 0 else 0
if can_discount:
# 단가
res = discard_ones_digit(discount_price if discount_price > 0 else 0)
return res
return 0
class DataPrinter:
@classmethod
def print(cls, data: list[InvalidData], goods_set: set[int]) -> None:
# Print header
print("\n" + "=" * 150)
print(
f"{'Goods Name':<40} | {'SNO':>8} | {'Price':>10} | {'Correct':>10} | {'Diff':>8} | {'Checked At':^20} | {'Updated At':^20}")
print("-" * 150)
# Print each row
for invalid in data:
if goods_set and invalid.context.goods_sno not in goods_set:
continue
goods_name: str = invalid.context.goods_name[:37] + "..." if len(
invalid.context.goods_name) > 40 else invalid.context.goods_name
goods_sno: int = invalid.context.goods_sno
price: int = invalid.item.price
correct_price: int = invalid.context.correct_price
price_diff: int = correct_price - price
checked_at: datetime = invalid.item.checked_at
updated_at: datetime = invalid.context.updated_at
print(
f"{goods_name:<40} | "
f"{goods_sno:>8} | "
f"{price:>10,d} | "
f"{correct_price:>10,d} | "
f"{price_diff:>8,d} | "
f"{checked_at.strftime('%Y-%m-%d %H:%M')} | "
f"{updated_at.strftime('%Y-%m-%d %H:%M')}"
)
print("=" * 150 + "\n")
@classmethod
def map_csv(cls, data: list[InvalidData]) -> bytes:
# Define headers
headers = [
# 'goods_name',
'goods_sno',
'option_sno',
'price',
'correct_price',
'price_diff',
'checked_at',
'updated_at',
'market_sno',
'order_sno',
'item_sno'
]
# Create CSV content starting with headers
lines = [','.join(headers)]
# Add data rows
for invalid in data:
row = [
# f'"{invalid.context.goods_name}"', # Quote the name to handle commas
str(invalid.context.goods_sno),
str(invalid.item.option_sno),
str(invalid.item.price),
str(invalid.context.correct_price),
str(invalid.context.correct_price - invalid.item.price),
invalid.item.checked_at.strftime('%Y-%m-%d %H:%M:%S'),
invalid.context.updated_at.strftime('%Y-%m-%d %H:%M:%S'),
str(invalid.item.market_sno),
str(invalid.item.order_sno),
str(invalid.item.item_sno)
]
lines.append(','.join(row))
# Join with newlines and encode to bytes
return '\n'.join(lines).encode('utf-8')
@classmethod
def encode_bytes(cls, lines: list[str]) -> bytes:
return '\n'.join(lines).encode('utf-8')
if __name__ == '__main__':
print('program starts to parse..')
# lines: list[str] = GrafanaLogReader.read(filepaths=['data/logs/o.jsonl'], limit=None)
# lines: list[str] = GrafanaLogReader.read(filepaths=['data/logs/o.jsonl'], limit=None)
# FileSaveHelper.save(data=DataPrinter.encode_bytes(lines=lines), filepath='data/parsed_logs.csv')
# for line in lines:
# print('grafana-line: ', line)
vendor_logs: list[DatadogLog] = LogReader.parse(lines=LogReader.read(file_dir_path="data/logs/"))
seller_logs: list[DatadogLog] = SellerLogReader.parse(
lines=SellerLogReader.read(filepaths=["data/seller_logs.csv"])
)
logs: list[DatadogLog] = sorted(vendor_logs + seller_logs, key=lambda x: x.request_time)
items: list[OrderItem] = ItemReader.parse(lines=ItemReader.read(filepaths=['data/ably_gd_order_item.csv']))
print('logs: ', len(logs))
invalids: list[InvalidData] = ItemAnalyzer.analyze(logs=logs, items=items)
print('len(vendor_logs): ', len(vendor_logs))
print('len(items): ', len(items))
print('len(invalids): ', len(invalids))
print('unique goods: ', len(set([invalid.context.goods_sno for invalid in invalids])))
FileSaveHelper.save(data=DataPrinter.map_csv(data=invalids), filepath='data/invalids_order_items.csv')
# DataPrinter.print(data=invalids, goods_set=set())