|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pandas as pd |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +class UserBehaviorAnalytics: |
| 8 | + def __init__(self, activity_log_file='user_activity_log.json'): |
| 9 | + self.activity_log_file = activity_log_file |
| 10 | + self.activity_log = [] |
| 11 | + self.load_activity_log() |
| 12 | + |
| 13 | + def load_activity_log(self): |
| 14 | + """Load user activity log from a JSON file.""" |
| 15 | + if os.path.exists(self.activity_log_file): |
| 16 | + with open(self.activity_log_file, 'r') as file: |
| 17 | + self.activity_log = json.load(file) |
| 18 | + |
| 19 | + def save_activity_log(self): |
| 20 | + """Save user activity log to a JSON file.""" |
| 21 | + with open(self.activity_log_file, 'w') as file: |
| 22 | + json.dump(self.activity_log, file) |
| 23 | + |
| 24 | + def log_activity(self, user_id, activity_type, details): |
| 25 | + """Log user activity.""" |
| 26 | + activity_record = { |
| 27 | + 'user_id': user_id, |
| 28 | + 'activity_type': activity_type, |
| 29 | + 'details': details, |
| 30 | + 'timestamp': datetime.now().isoformat() |
| 31 | + } |
| 32 | + self.activity_log.append(activity_record) |
| 33 | + self.save_activity_log() |
| 34 | + |
| 35 | + def get_user_activity(self, user_id): |
| 36 | + """Get all activities for a specific user.""" |
| 37 | + user_activities = [activity for activity in self.activity_log if activity['user_id'] == user_id] |
| 38 | + return user_activities |
| 39 | + |
| 40 | + def generate_activity_report(self): |
| 41 | + """Generate a report of user activities.""" |
| 42 | + df = pd.DataFrame(self.activity_log) |
| 43 | + report = df.groupby(['user_id', 'activity_type']).size().reset_index(name='counts') |
| 44 | + return report |
| 45 | + |
| 46 | + def visualize_activity(self): |
| 47 | + """Visualize user activity data.""" |
| 48 | + df = pd.DataFrame(self.activity_log) |
| 49 | + if df.empty: |
| 50 | + print("No activity data available for visualization.") |
| 51 | + return |
| 52 | + |
| 53 | + activity_counts = df['activity_type'].value_counts() |
| 54 | + plt.figure(figsize=(10, 5)) |
| 55 | + activity_counts.plot(kind='bar', color='skyblue') |
| 56 | + plt.title('User Activity Distribution') |
| 57 | + plt.xlabel('Activity Type') |
| 58 | + plt.ylabel('Number of Activities') |
| 59 | + plt.xticks(rotation=45) |
| 60 | + plt.grid(axis='y') |
| 61 | + plt.tight_layout() |
| 62 | + plt.show() |
| 63 | + |
| 64 | +# Example usage |
| 65 | +if __name__ == "__main__": |
| 66 | + analytics = UserBehaviorAnalytics() |
| 67 | + |
| 68 | + # Log some user activities |
| 69 | + analytics.log_activity("user123", "Deposit", {"amount": 1000}) |
| 70 | + analytics.log_activity("user123", "Vote", {"proposal_id": 1, "vote": "for"}) |
| 71 | + analytics.log_activity("user456", "Withdraw", {"amount": 500}) |
| 72 | + |
| 73 | + # Get user activity |
| 74 | + user_activities = analytics.get_user_activity("user123") |
| 75 | + print(f"User Activities for user123: {user_activities}") |
| 76 | + |
| 77 | + # Generate activity report |
| 78 | + report = analytics.generate_activity_report() |
| 79 | + print("Activity Report:") |
| 80 | + print(report) |
| 81 | + |
| 82 | + # Visualize activity |
| 83 | + analytics.visualize_activity() |
0 commit comments