79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
import random
|
|
import json
|
|
from datetime import datetime, timedelta
|
|
|
|
class TestDataGenerator:
|
|
def __init__(self):
|
|
self.categories = ['科幻', '文学', '历史', '科技', '哲学', '经济', '教育']
|
|
self.authors = ['刘慈欣', '余华', '东野圭吾', '村上春树', '张三', '李四', '王五']
|
|
self.event_types = ['READ', 'RATE', 'BOOKMARK']
|
|
|
|
def generate_book(self):
|
|
"""生成一本书的基本信息"""
|
|
return {
|
|
'bookId': str(random.randint(1000, 9999)),
|
|
'title': f'测试书籍_{random.randint(1, 100)}',
|
|
'author': random.choice(self.authors),
|
|
'category': random.choice(self.categories),
|
|
'pageCount': random.randint(100, 500)
|
|
}
|
|
|
|
def generate_random_time(self):
|
|
"""生成随机时间"""
|
|
# 随机生成过去30天内的某一天
|
|
days_ago = random.randint(0, 30)
|
|
base_date = datetime.now() - timedelta(days=days_ago)
|
|
|
|
# 随机生成一天中的时间
|
|
hour = random.randint(0, 23)
|
|
minute = random.randint(0, 59)
|
|
second = random.randint(0, 59)
|
|
|
|
# 组合日期和时间
|
|
random_time = base_date.replace(
|
|
hour=hour,
|
|
minute=minute,
|
|
second=second
|
|
)
|
|
|
|
return random_time
|
|
|
|
def generate_event(self, user_id=None):
|
|
"""生成一个随机事件"""
|
|
if user_id is None:
|
|
user_id = str(random.randint(1, 3))
|
|
|
|
event_type = random.choice(self.event_types)
|
|
book = self.generate_book()
|
|
timestamp = self.generate_random_time()
|
|
|
|
event = {
|
|
'eventId': str(random.randint(10000, 99999)),
|
|
'eventType': event_type,
|
|
'userId': user_id,
|
|
'timestamp': timestamp.isoformat(),
|
|
'bookId': book['bookId'],
|
|
'bookInfo': book
|
|
}
|
|
|
|
# 根据事件类型添加特定字段
|
|
if event_type == 'READ':
|
|
event['readDuration'] = random.randint(10, 180) # 阅读时长(分钟)
|
|
event['progress'] = random.randint(1, 100) # 阅读进度
|
|
elif event_type == 'RATE':
|
|
event['rating'] = random.randint(1, 5) # 评分 1-5
|
|
|
|
return event
|
|
|
|
def generate_events(self, num_events=10):
|
|
"""生成多个事件"""
|
|
events = []
|
|
# 为每个用户生成一些事件
|
|
for user_id in range(1, 4): # 假设有3个用户
|
|
user_events = [self.generate_event(str(user_id))
|
|
for _ in range(num_events // 3)]
|
|
events.extend(user_events)
|
|
|
|
# 随机打乱事件顺序
|
|
random.shuffle(events)
|
|
return events |