das-research/DAS/observer.py

67 lines
2.6 KiB
Python
Raw Normal View History

2022-11-30 14:28:27 +00:00
#!/bin/python3
from DAS.block import *
class Observer:
"""This class gathers global data from the simulation, like an 'all-seen god'."""
2022-11-30 14:28:27 +00:00
2023-01-13 15:51:27 +00:00
def __init__(self, logger, config):
"""It initializes the observer with a logger and given configuration."""
2023-01-13 15:51:27 +00:00
self.config = config
2022-11-30 14:28:27 +00:00
self.format = {"entity": "Observer"}
self.logger = logger
2023-02-08 19:10:26 +00:00
self.block = []
self.rows = []
self.columns = []
self.goldenData = []
self.broadcasted = []
2022-11-30 14:28:27 +00:00
def reset(self):
"""It resets all the gathered data to zeros."""
2023-01-13 15:51:27 +00:00
self.block = [0] * self.config.blockSize * self.config.blockSize
self.goldenData = [0] * self.config.blockSize * self.config.blockSize
self.rows = [0] * self.config.blockSize
self.columns = [0] * self.config.blockSize
self.broadcasted = Block(self.config.blockSize)
2022-11-30 14:28:27 +00:00
def checkRowsColumns(self, validators):
"""It checks how many validators have been assigned to each row and column."""
2022-11-30 14:28:27 +00:00
for val in validators:
2023-01-13 15:51:27 +00:00
if val.amIproposer == 0:
2022-11-30 14:28:27 +00:00
for r in val.rowIDs:
self.rows[r] += 1
for c in val.columnIDs:
self.columns[c] += 1
2023-01-13 15:51:27 +00:00
for i in range(self.config.blockSize):
2022-11-30 14:28:27 +00:00
self.logger.debug("Row/Column %d have %d and %d validators assigned." % (i, self.rows[i], self.columns[i]), extra=self.format)
if self.rows[i] == 0 or self.columns[i] == 0:
self.logger.warning("There is a row/column that has not been assigned", extra=self.format)
def setGoldenData(self, block):
"""Stores the original real data to compare it with future situations."""
2023-01-13 15:51:27 +00:00
for i in range(self.config.blockSize*self.config.blockSize):
2022-11-30 14:28:27 +00:00
self.goldenData[i] = block.data[i]
def checkBroadcasted(self):
"""It checks how many broadcasted samples are still missing in the network."""
2022-11-30 14:28:27 +00:00
zeros = 0
for i in range(self.blockSize * self.blockSize):
if self.broadcasted.data[i] == 0:
zeros += 1
if zeros > 0:
self.logger.debug("There are %d missing samples in the network" % zeros, extra=self.format)
return zeros
def checkStatus(self, validators):
"""It checks the status of how many expected and arrived samples globally."""
arrived = 0
expected = 0
for val in validators:
2023-01-13 15:51:27 +00:00
if val.amIproposer == 0:
(a, e) = val.checkStatus()
arrived += a
expected += e
return (arrived, expected)