def get_device_status():
    logger.info('Getting data for main page')
    try:
        data = {
            'warnings':
            local_data_gateway.get_current_warnings_for_all_services(),
            'denva': local_data_gateway.get_current_reading_for_denva(),
            'enviro': local_data_gateway.get_current_reading_for_enviro(),
            'aircraft': radar_service.get_aircraft_detected_today_count(),
            'system': get_current_system_information_for_all_services(),
            'links': get_links_for_gateway(),
            'welcome_text': data_files.load_text_to_display(),
            'transport': web_data_gateway.get_status(),
            'metrics': metrics_service.get_currents_metrics(),
            'log_count': local_data_gateway.get_current_log_counts(),
        }
        data['errors'] = get_errors_from_data(data)
    except Exception as exception:
        logger.error('Unable to get data due to {}'.format(exception))
        data = {
            'warnings': {},
            'denva': {},
            'enviro': {},
            'aircraft': {},
            'system': {},
            'links': get_links_for_gateway(),
            'welcome_text': f"Unable to load message due to ${exception}",
            'transport': [],
            'metrics': {},
            'log_count': {}
        }
    return data
    def test_reset_should_clear_metrics(self):
        # given
        metrics_service.reset()
        metrics_service.add(METRICS_NAME, ERRORS)
        metrics_service.add(METRICS_NAME, OK)
        setup = metrics_service.get_currents_metrics()

        # verify
        self.assertEqual(setup[ERRORS][METRICS_NAME], 1)
        self.assertEqual(setup[OK][METRICS_NAME], 1)
        self.assertEqual(setup[COUNT], 2)

        # when
        metrics_service.reset()
        result = metrics_service.get_currents_metrics()
        # then
        self.assertEqual(result[ERRORS][METRICS_NAME], 0)
        self.assertEqual(result[OK][METRICS_NAME], 0)
        self.assertEqual(result[COUNT], 0)
    def test_add_not_existing_metric_result_do_not_change_counts(self):
        # given
        metrics_service.reset()
        metrics_service.add(METRICS_NAME, ERRORS)
        metrics_service.add(METRICS_NAME, OK)
        setup = metrics_service.get_currents_metrics()

        # verify
        self.assertEqual(setup[ERRORS][METRICS_NAME], 1)
        self.assertEqual(setup[OK][METRICS_NAME], 1)
        self.assertEqual(setup[COUNT], 2)

        # when
        metrics_service.add(METRICS_NAME, "UFO")

        # then
        result = metrics_service.get_currents_metrics()
        self.assertEqual(result[ERRORS][METRICS_NAME], 1)
        self.assertEqual(result[OK][METRICS_NAME], 1)
        self.assertEqual(result[COUNT], 2)
    def test_add_error_metric_to_metrics(self):
        # given
        metrics_service.reset()

        # when
        metrics_service.add(METRICS_NAME, ERRORS)
        result = metrics_service.get_currents_metrics()

        # then
        self.assertEqual(result[ERRORS][METRICS_NAME], 1)
        self.assertEqual(result[COUNT], 1)
    def test_add_many_metrics(self):
        # given
        metrics_service.reset()

        # when
        metrics_service.add(METRICS_NAME, ERRORS)
        metrics_service.add(METRICS_NAME, OK)
        setup = metrics_service.get_currents_metrics()

        # then
        self.assertEqual(setup[ERRORS][METRICS_NAME], 1)
        self.assertEqual(setup[OK][METRICS_NAME], 1)
        self.assertEqual(setup[COUNT], 2)
    def test_should_save_backup(self):
        # given
        metrics_service.reset()
        metrics_service.add(METRICS_NAME, ERRORS)
        metrics_service.add(METRICS_NAME, OK)
        setup = metrics_service.get_currents_metrics()

        # verify
        self.assertEqual(setup[ERRORS][METRICS_NAME], 1)
        self.assertEqual(setup[OK][METRICS_NAME], 1)
        self.assertEqual(setup[COUNT], 2)

        # when
        result = metrics_service.save_metrics()

        # then
        self.assertEqual(result, 'saved')
def get_metrics():
    logger.info('getting current metrics')
    return jsonify(metrics_service.get_currents_metrics())