def test_data_presenter_cannot_be_initiated_twice(self): data_presenter = Data_Presenter() with pytest.raises(RuntimeError) as re: Data_Presenter() assert re
def test_data_from_different_get_instance(self): data_presenter1 = Data_Presenter.get_instance() data_presenter2 = Data_Presenter.get_instance() test_data = SentPackage("1") data_presenter1.append(test_data) from_different_object = data_presenter2.latest_packages().pop() assert from_different_object.sent_at == test_data.time
def update_graph_live(n): data = { TIME: [], LOSS: [], } # Collect some data data_presenter = Data_Presenter.get_instance() time_table = data_presenter.get_time_table() base = datetime.now() - timedelta(seconds=30) # 30 seconds behind timestamp_array = numpy.array([base - timedelta(seconds=i) for i in range(1, 180)]) for timestamp in timestamp_array: data[TIME].append(timestamp) loss_decimal = 0 entry = time_table[timestamp] if entry.sent != 0: loss_decimal = entry.loss / entry.sent loss_pct = loss_decimal * 100 data[LOSS].append(loss_pct) # Create the graph fig_2 = px.line(data, x=TIME, y=LOSS, title="Loss", labels={ LOSS: 'Package loss (%) ', TIME: 'Timestamp ', }) return fig_2
def test_data_presenter_can_take_an_argument(self): test_data = SentPackage("1") data_presenter = Data_Presenter.get_instance() data_presenter.append(test_data) assert data_presenter.latest_packages().pop().sent_at == test_data.time
def save_data(n_clicks, old_output): if n_clicks == 0 or n_clicks == 1: raise PreventUpdate data_presenter = Data_Presenter.get_instance() packet_table = data_presenter.get_packet_table() dataframe = pd.DataFrame(columns=['sent_at', 'received_at']) try: for x in [['a', 'b']]: # prep_data(packet_table): dataframe.append({'sent_at': x[0], 'received_at': x[1]}, ignore_index=True) csv_string = dataframe.to_csv(index=False, encoding='utf-8') csv_string = "data:text/csv;charset=utf-8," + urllib.parse.quote(csv_string) file_name = f'loss_data_{datetime.now().strftime("%Y-%m-%d_%H:%M:%S")}.csv' download_link = html.A( file_name, id='download-link', download=file_name + ".csv", href=csv_string, target="_blank" ) except RuntimeError as e: log(e) download_link = html.P('The test is not finished yet. Wait for the test to finish') list_item = html.Li(download_link) return old_output + [list_item]
def update_sent_lost(n): data = { TIME: [], PACKET_TYPE: [], PACKET_COUNT: [] } # Collect some data data_presenter = Data_Presenter.get_instance() time_table = data_presenter.get_time_table() base = datetime.now() - timedelta(seconds=30) # 30 seconds behind timestamp_array = numpy.array([base - timedelta(seconds=i) for i in range(1, 180)]) for timestamp in timestamp_array: entry = time_table[timestamp] data[TIME].append(timestamp) data[PACKET_COUNT].append(entry.sent - entry.loss) data[PACKET_TYPE].append('received') data[TIME].append(timestamp) data[PACKET_COUNT].append(entry.sent) data[PACKET_TYPE].append('sent') # Create the graph fig_2 = px.line(data, x=TIME, y=PACKET_COUNT, title="Sent/Received", color=PACKET_TYPE, labels={ PACKET_COUNT: 'Packets: ', TIME: 'Timestamp ', PACKET_TYPE: 'Type: ' }) return fig_2
def test_get_package_info_at_timestamp(self): data_presenter = Data_Presenter.get_instance() time = datetime.now() data_presenter.append(SentPackage("1", time)) data_presenter.append(SentPackage("2", time)) # get time table package_info = data_presenter.get_time_table() assert package_info[time].sent == 2
def test_get_latest_data_by_amount_of_entries(self): data_presenter = Data_Presenter.get_instance() test_data_1 = SentPackage("1") test_data_2 = SentPackage("2") data_presenter.append(test_data_1) data_presenter.append(test_data_2) latest_package = data_presenter.latest_packages(1).pop() assert latest_package.sent_at == test_data_2.time
def test_append_method_appends_data(self): data_presenter = Data_Presenter.get_instance() test_data_1 = SentPackage("1") test_data_2 = ReceivePackage("1", "1") data_presenter.append(test_data_1) data_presenter.append(test_data_2) latest = data_presenter.latest_packages().pop() assert latest.sent_at == test_data_1.time assert latest.received_at == test_data_2.time
def update_metrics(n): style = {'padding': '5px', 'fontSize': '16px'} data_presenter = Data_Presenter.get_instance() time_table = data_presenter.get_time_table() timestamp = datetime.now() - timedelta(seconds=15) time_entry = time_table[timestamp] speed = size(time_entry.sent * 1024, system=verbose) return [ html.Span(f"Actual package speed: {speed}/s", style=style), ]
def __init__(self, speed=megabyte, ip='0.0.0.0', port=7070, runtime_of_test=ONE_SECOND * 60 * 30): self.last_sent_packet = 0 self.last_received_packet = 0 self.time_of_sample_size = runtime_of_test self.socket_timeout = 60 * 5 # seconds self.log_interval = 1 self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.listening_address = (ip, port) self.id = 0 self.__interval = 1 self.speed = speed self.data_presenter = Data_Presenter.get_instance()
def teardown_method(self): Data_Presenter.clear_instance()
def test_get_instance_initiates_the_class_if_not_initiated(self): data_presenter = Data_Presenter.get_instance() assert data_presenter is not None
def test_data_presenter_instance_is_stored_as_a_class_variable(self): data_presenter = Data_Presenter() assert Data_Presenter.get_instance() is not None
def test_data_presenter_can_be_instantiated(self): my_singleton = Data_Presenter() assert my_singleton is not None