Advertisement
hoangreal

Build graph from log system

Oct 21st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | Source Code | 0 0
  1. """
  2. now we have data with 3 properties
  3. user_id, timestamp, action
  4. 100, 1000, A. 留学申请论坛-⼀亩三分地
  5. 200, 1003, A
  6. 300, 1009, B
  7. 100, 1026, B
  8. 100, 1030, C. 1point3acres
  9. 200, 1109, B
  10. 200, 1503, A
  11. We want to output a graph to visualize it
  12. Graph from input:
  13. |---A (2)
  14. | |---B (2)
  15. | | |---C (1)
  16. | | |---A (1)
  17. |---B (1)
  18. aggregate by user_id:
  19. 100 : A->B->C
  20. 200: A->B->A
  21. 300: B
  22. """
  23.  
  24. def build_log_system(logs):
  25.     user_id, time_stamp, action = zip(*logs)
  26.     user_id = sorted(list(set(user_id)))
  27.  
  28.     # construct a trie
  29.     root = {'#': '#'}
  30.     for uid in user_id:
  31.         t = root
  32.         for log in logs:
  33.             if log[0] == uid:
  34.                 if log[2] not in t:
  35.                     t[log[2]] = {'counter': 1}
  36.                 else:
  37.                     t[log[2]]['counter'] += 1
  38.                 t = t[log[2]]
  39.         t['#'] = '#'
  40.  
  41.     # traversal
  42.     def dfs(node, level_so_far=''):
  43.         if node == '#':
  44.             return
  45.         for next_action in node:
  46.             if next_action not in ['counter', '#']:
  47.                 print('{}|---{}({})'.format(level_so_far, next_action, node[next_action]['counter']))
  48.                 dfs(node[next_action], level_so_far + '|\t')
  49.  
  50.     dfs(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement