Ejemplo n.º 1
0
def save_processed_data(result):
    data_path = config_service.get_directory_path_for_aircraft()
    date_as_folders = dom_utils.get_date_as_folders_linux()
    Path("{}/{}".format(data_path, date_as_folders)).mkdir(parents=True,
                                                           exist_ok=True)
    airport_processed_data = DEFAULT_PATH.format(
        data_path, date_as_folders,
        dom_utils.get_date_as_filename("aircraft-processed", "csv",
                                       datetime.now()))
    timestamp = datetime.now()
    try:
        with open(airport_processed_data, 'a+', encoding=ENCODING,
                  newline='') as aircraft_processed_file:
            csv_writer = csv.writer(aircraft_processed_file)

            for entry in result:
                if entry['flight'] != '':
                    csv_writer.writerow([
                        timestamp, entry['hex'], entry['squawk'],
                        entry['flight'].strip(), entry['lat'], entry['lon'],
                        entry['validposition'], entry['altitude'],
                        entry['vert_rate'], entry['track'],
                        entry['validtrack'], entry['speed'], entry['messages'],
                        entry['seen']
                    ])
    except Exception as exception:
        logger.error(
            'Unable to save processed reading due to {}'.format(exception),
            exc_info=True)
Ejemplo n.º 2
0
def load_report_on_server_on(report_date: datetime):
    report_file_path = '{}/{}'.format(
        config_service.get_report_path_at_server(),
        dom_utils.get_date_as_filename('report', 'json', report_date))
    logger.info('Loading report from {}'.format(report_file_path))
    with open(report_file_path, READ) as report_file:
        return json.load(report_file)
Ejemplo n.º 3
0
def generate_enviro_report_for_yesterday() -> dict:
    yesterday = dom_utils.get_yesterday_date()
    logger.info('Getting  report for enviro for {}'.format(yesterday))
    path = dom_utils.get_date_as_filename('report-enviro', 'json', yesterday)
    if data_files.check_if_report_was_generated(path):
        logger.info(
            'Report was generated. Getting report from file using path: {}'.
            format(path))
        return data_files.load_report(path)
    else:
        logger.info('Generating report')
        report = report_generator.generate_enviro_report_for_yesterday()
        email_sender_service.send(report, 'Report')
        data_files.save_report(
            report,
            dom_utils.get_date_as_filename('report-enviro', 'json', yesterday))
        return report
Ejemplo n.º 4
0
def save_report_at_server(report: dict):
    try:
        report_file_path = '{}/{}'.format(
            config_service.get_report_path_at_server(),
            dom_utils.get_date_as_filename('report', 'json',
                                           dom_utils.get_yesterday_date()))
        logger.info('Saving report to {}'.format(report_file_path))
        with open(report_file_path, 'w+', encoding=ENCODING) as report_file:
            json.dump(report, report_file, ensure_ascii=False, indent=4)
    except Exception as exception:
        logger.error('Unable to save report due to {}'.format(exception))
Ejemplo n.º 5
0
    def test_get_date_as_filename(self):
        # given
        random_time = datetime(2020, 1, 2, 3, 4, 5)
        expected_result = 'zeroeight-results-2020-01-02.txt'

        # when
        result = dom_utils.get_date_as_filename('zeroeight-results', 'txt',
                                                random_time)

        # debug
        print(result)

        # then
        self.assertEqual(expected_result, result)
Ejemplo n.º 6
0
def generate_for_yesterday() -> dict:
    logger.info('Getting report for yesterday...')
    path = dom_utils.get_date_as_filename('report', 'json',
                                          dom_utils.get_yesterday_date())
    try:
        if data_files.check_if_report_was_generated(path):
            logger.info('Report was generated. Getting report from file.')
            return data_files.load_report(path)
        else:
            logger.info('Generating report')
            report = report_generator.generate_for_yesterday()
            email_sender_service.send(report, 'Report')
            data_files.save_report(
                report,
                dom_utils.get_date_as_filename('report', 'json',
                                               dom_utils.get_yesterday_date()))
            return report
    except Exception as e:
        logger.error(
            'Unable to generate report due to {}.Returning empty report'.
            format(e),
            exc_info=True)
        return {'error': str(e)}
Ejemplo n.º 7
0
def save_raw_reading(reading):
    data_path = config_service.get_directory_path_for_aircraft()
    date_as_folders = dom_utils.get_date_as_folders_linux()
    Path("{}/{}".format(data_path, date_as_folders)).mkdir(parents=True,
                                                           exist_ok=True)
    airport_raw_data = DEFAULT_PATH.format(
        data_path, date_as_folders,
        dom_utils.get_date_as_filename("aircraft", "txt", datetime.now()))
    try:
        with open(airport_raw_data, 'a+',
                  encoding=ENCODING) as aircraft_raw_file:
            json.dump(reading, aircraft_raw_file, ensure_ascii=False, indent=4)
    except Exception as exception:
        logger.error('Unable to save raw reading due to {}'.format(exception),
                     exc_info=True)
Ejemplo n.º 8
0
def load_processed_data_for(specified_data: date) -> list:
    data_path = config_service.get_directory_path_for_aircraft()
    date_as_folders = dom_utils.get_date_as_folders_for(specified_data)
    airport_processed_data = DEFAULT_PATH.format(
        data_path, date_as_folders,
        dom_utils.get_date_as_filename("aircraft-processed", "csv",
                                       dom_utils.to_datetime(specified_data)))
    try:
        with open(airport_processed_data) as csv_file:
            aircraft_csv = csv.reader(csv_file)
            return list(aircraft_csv)
    except Exception as exception:
        logger.error(
            'Unable to load processed reading due to {}'.format(exception),
            exc_info=True)
        return []
Ejemplo n.º 9
0
def is_report_file_exists_for(report_date: datetime) -> bool:
    report_file_path = '{}/{}'.format(
        config_service.get_report_path_at_server(),
        dom_utils.get_date_as_filename('report', 'json', report_date))
    return os.path.exists(report_file_path)
Ejemplo n.º 10
0
def is_report_file_exists() -> bool:
    report_file_path = '{}/{}'.format(
        config_service.get_report_path_at_server(),
        dom_utils.get_date_as_filename('report', 'json',
                                       dom_utils.get_yesterday_date()))
    return os.path.exists(report_file_path)
Ejemplo n.º 11
0
def get_sensor_log_file_at_server() -> str:
    return config.PI_SENSORS_DATA_PATH + 'denva/' + dom_utils.get_date_as_filename(
        'sensor-log', 'csv', datetime.now())
Ejemplo n.º 12
0
def get_sensor_log_file():
    return config.PI_LOGS_PATH + dom_utils.get_date_as_filename(
        'sensor-log', 'csv', datetime.now())