コード例 #1
0
 def test_should_report_analyze_data(self):
     report_file = f"{self.config['REPORT_DIR']}/report-{datetime.date.today().strftime('%Y.%m.%d')}.html"
     data = log_analyzer.analyze_log(logs_gen())
     log_analyzer.report_data(data, report_file)
     self.assertTrue(os.path.isfile(report_file))
     path = os.path.join(os.path.abspath(os.path.dirname(__file__)), report_file)
     os.remove(path)
コード例 #2
0
 def test_should_report_analyze_data(self):
     report_file = '{}/report-{}.html'.format(
         self.config['REPORT_DIR'],
         datetime.date.today().strftime("%Y.%m.%d"))
     data = log_analyzer.analyze_log(logs_gen())
     log_analyzer.report_data(data, report_file)
     report_data = log_analyzer.read_lines_from_file(report_file)
     self.assertGreater(len(report_data), 0)
コード例 #3
0
def main():
    try:
        # 1. Prepare
        set_logging(None)

        config_path = get_config_path_from_args(DEFAULT_CONFIG_PATH)
        config = parse_config(config_path, DEFAULT_CONFIG)

        set_logging(config["LOG_RESULT_DIR"])

        # 2. Parse log
        last_log_file_info = log_parser.get_newest_log_file(config["LOG_DIR"])
        if not last_log_file_info["filepath"]:
            log_msg = "No log file found in dir {}"
            logging.info(log_msg.format(config["LOG_DIR"]))
            return

        if report_generator.report_by_date_exists(last_log_file_info["date"],
                                                  config["REPORT_DIR"]):
            log_msg = "Report for {} already exists"
            logging.info(log_msg.format(last_log_file_info["date"]))
            return

        log_urls = log_parser.parse_log_file(last_log_file_info["filepath"])
        if not log_urls:
            log_msg = "Log file ({}) is empty"
            logging.info(log_msg.format(last_log_file_info["filepath"]))
            return

        log_msg = "Parsed {lines} lines, {urls} urls from log file"
        logging.info(
            log_msg.format(lines=log_analyzer.calc_log_rows_count(log_urls),
                           urls=len(log_urls["urls_times"])))

        # 3. Analyze log
        report_list = log_analyzer.analyze_log(
            log_urls, config["MAX_LOG_ERRORS_PERCENT"], config["REPORT_SIZE"])
        log_msg = "Log has been analyzed"
        logging.info(log_msg)

        # 4. Generate report
        report_path = report_generator.save_report_html(
            report_list, last_log_file_info["date"], config["REPORT_DIR"])

        update_ts(config["TS_DIR"])

        log_msg = "Log file ({log_path}) parsed succesfully. " \
                  "Created report file - {report_path}"
        logging.info(
            log_msg.format(log_path=last_log_file_info["filepath"],
                           report_path=report_path))
    except Exception as ex:
        msg = "{0}: {1}".format(type(ex).__name__, ex)
        logging.exception(msg, exc_info=True)
        raise
コード例 #4
0
 def test_report_data(self):
     report_file = './test_data/report.html'
     data = la.analyze_log(
         la.read_lines("./test_data/nginx-access-ui.log-20170703.txt"))
     la.report_data(data, report_file)
     try:
         with open(report_file, 'r') as f:
             report_data = f.read()
     except Exception as error:
         print(error)
     self.assertIn(dumps(data), report_data)
コード例 #5
0
 def test_should_not_build_analyze_data(self):
     self.assertRaises(log_analyzer.analyze_log(logs_gen(), 0.999))
コード例 #6
0
 def test_should_build_analyze_data(self):
     self.assertIsNotNone(log_analyzer.analyze_log(logs_gen()))
コード例 #7
0
 def test_analyze_log(self):
     data = la.analyze_log(
         la.read_lines("./test_data/nginx-access-ui.log-20170702.txt"))
     self.assertEquals(len(data), 6)
コード例 #8
0
 def test_return_data_after_analyze(self):
     data = la.analyze_log(
         la.read_lines("./test_data/nginx-access-ui.log-20170630.log"))
     self.assertIsNotNone(data)
コード例 #9
0
 def test_should_not_build_analyze_data(self):
     with self.assertRaises(Exception):
         log_analyzer.analyze_log(logs_gen(), 0.999)