model.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2024 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import logging, time
  18. import datetime as dt
  19. from collections import defaultdict as dd
  20. class Model:
  21. def __init__(self):
  22. self.nodes = {}
  23. def add_eg(self, node):
  24. name = list(node.keys())[0]
  25. values = list(node.values())[0]
  26. info = values['result']['eventgraph_info']
  27. self.nodes[name] = {}
  28. self.nodes[name]['current_genesis'] = {}
  29. self.nodes[name]['broadcasted_ids'] = {}
  30. self.nodes[name]['synced'] = {}
  31. self.nodes[name]['event'] = {}
  32. self.nodes[name]['unreferenced_tips'] = {}
  33. self.nodes[name]['msgs'] = dd(list)
  34. if info['current_genesis']:
  35. self.nodes[name]['current_genesis'] = info['current_genesis']
  36. if info['broadcasted_ids']:
  37. self.nodes[name]['broadcasted_ids'] = info['broadcasted_ids']
  38. if info['synced']:
  39. self.nodes[name]['synced'] = info['synced']
  40. if info['unreferenced_tips']:
  41. self.nodes[name]['unreferenced_tips'] = info['unreferenced_tips']
  42. def add_offline(self, node):
  43. name = list(node.keys())[0]
  44. values = list(node.values())[0]
  45. self.nodes[name] = values
  46. def add_event(self, event):
  47. name = list(event.keys())[0]
  48. values = list(event.values())[0]
  49. params = values.get('params')
  50. event = params[0].get('event')
  51. info = params[0].get('info')
  52. t = time.localtime()
  53. current_time = time.strftime('%H:%M:%S', t)
  54. match event:
  55. case 'send':
  56. nano = info.get('time')
  57. cmd = info.get('cmd')
  58. ev_info = info.get('info')
  59. t = (dt.datetime
  60. .fromtimestamp(int(nano)/1000000000)
  61. .strftime('%H:%M:%S'))
  62. msgs = self.nodes[name]['msgs']
  63. msgs[name].append((t, "send", cmd, ev_info))
  64. case 'recv':
  65. nano = info.get('time')
  66. cmd = info.get('cmd')
  67. ev_info = info.get('info')
  68. t = (dt.datetime
  69. .fromtimestamp(int(nano)/1000000000)
  70. .strftime('%H:%M:%S'))
  71. msgs = self.nodes[name]['msgs']
  72. msgs[name].append((t, "recv", cmd, ev_info))
  73. def __repr__(self):
  74. return f'{self.nodes}'