-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Report Generator Functionallity #166
Merged
mircealungu
merged 17 commits into
zeeguu:master
from
tfnribeiro:user-feed-weekly-report
Jul 10, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f758c79
Added report generator
tfnribeiro e9d079b
Added a warning in case the CrawlReport doesn't contain the number of…
tfnribeiro 953d919
Added necessary changes to the support the article crawler.
tfnribeiro 922cbf4
Fixed the Tests, as the feeds expect the CrawlReport object
tfnribeiro 87473ee
Simplified CrawlReport, which can take a feed and extract the code + …
tfnribeiro fc8d8ca
Update to not allow broken articles.
tfnribeiro 9e2157a
Store URLs based on patterns + Reducing duplication
tfnribeiro ada9583
Added generating likely candidates of repeated sents.
tfnribeiro fce096f
Renaming method names
tfnribeiro 01ae26b
Added a table to track broken_codes
tfnribeiro e90fbdd
Update crawl_report.py
tfnribeiro a4e3886
Merge remote-tracking branch 'upstream/master' into user-feed-weekly-…
tfnribeiro d216454
Fixed tests, need a call that allows to not add to the crawl report.
tfnribeiro 1ac822c
Do not plot graphs for inactive languages
tfnribeiro 72fb785
Add environment variables to control the output of the reports
tfnribeiro f4663a3
Fixed Queries + Only Show Plots with relevant data
tfnribeiro 8ecfd48
Updated naming convention to include date + days reported, rather tha…
tfnribeiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
from collections import Counter | ||
import datetime | ||
import os | ||
import inspect | ||
import json | ||
import pathlib | ||
|
||
STR_DATETIME_FORMAT = "%d_%m_%y_%H_%M_%S" | ||
CRAWL_REPORT_DATA = os.environ.get( | ||
"CRAWL_REPORT_DATA", | ||
os.path.join(pathlib.Path(__file__).parent.resolve(), "crawl_data"), | ||
) | ||
|
||
|
||
class CrawlReport: | ||
def __init__(self) -> None: | ||
self.save_dir = CRAWL_REPORT_DATA | ||
self.data = {"lang": {}} | ||
self.crawl_report_date = datetime.datetime.now() | ||
|
||
def get_days_from_crawl_report_date(self): | ||
return (datetime.datetime.now() - self.crawl_report_date).days | ||
|
||
def __convert_str_to_dt(self, str_datetime): | ||
dt_parsed = datetime.datetime.strptime(str_datetime, STR_DATETIME_FORMAT) | ||
return dt_parsed | ||
|
||
def __convert_dt_to_str(self, datetime): | ||
return datetime.strftime(STR_DATETIME_FORMAT) | ||
|
||
def _get_feed_dict(self, feed): | ||
lang_code = feed.language.code | ||
feed_id = feed.id | ||
return self.data["lang"][lang_code]["feeds"][feed_id] | ||
|
||
def add_language(self, lang_code: str): | ||
self.data["lang"][lang_code] = {"feeds": {}, "total_time": None} | ||
|
||
def add_feed(self, feed): | ||
lang_code = feed.language.code | ||
feed_id = feed.id | ||
if lang_code not in self.data["lang"]: | ||
self.add_language(lang_code) | ||
self.data["lang"][lang_code]["feeds"][feed_id] = { | ||
"article_report": { | ||
"sents_removed": {}, | ||
"quality_error": {}, | ||
"quality_to_url": {}, | ||
"sents_to_url": {}, | ||
}, | ||
"last_article_date": None, | ||
"feed_errors": [], | ||
"crawl_time": None, | ||
"total_articles": None, | ||
"total_downloaded": None, | ||
"total_low_quality": None, | ||
"total_in_db": None, | ||
} | ||
|
||
def set_total_time(self, lang_code: str, total_time): | ||
self.data["lang"][lang_code]["total_time"] = total_time | ||
|
||
def add_feed_error(self, feed, error: str): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["feed_errors"].append(error) | ||
|
||
def set_feed_crawl_time(self, feed, crawl_time): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["crawl_time"] = crawl_time | ||
|
||
def set_feed_last_article_date(self, feed, last_article_date): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["last_article_date"] = self.__convert_dt_to_str(last_article_date) | ||
|
||
def set_feed_total_articles(self, feed, total_articles): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["total_articles"] = total_articles | ||
|
||
def set_feed_total_downloaded(self, feed, total_downloaded): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["total_downloaded"] = total_downloaded | ||
|
||
def set_feed_total_low_quality(self, feed, total_low_quality): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["total_low_quality"] = total_low_quality | ||
|
||
def set_feed_total_in_db(self, feed, total_in_db): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["total_in_db"] = total_in_db | ||
|
||
def set_non_quality_reason(self, feed, non_quality_reason_counts: dict): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["article_report"]["quality_error"] = Counter( | ||
non_quality_reason_counts | ||
) | ||
|
||
def set_sent_removed(self, feed, sent_removed_count: dict): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["article_report"]["sents_removed"] = Counter(sent_removed_count) | ||
|
||
def add_non_quality_reason(self, feed, non_quality_reason, url=None): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["article_report"]["quality_error"][non_quality_reason] = ( | ||
feed_dict["article_report"]["quality_error"].get(non_quality_reason, 0) + 1 | ||
) | ||
if url is not None: | ||
feed_dict["article_report"]["quality_to_url"][non_quality_reason] = ( | ||
feed_dict["article_report"]["quality_to_url"].get( | ||
non_quality_reason, [] | ||
) | ||
+ [url] | ||
) | ||
|
||
def add_sent_removed(self, feed, sent_removed, url=None): | ||
feed_dict = self._get_feed_dict(feed) | ||
feed_dict["article_report"]["sents_removed"][sent_removed] = ( | ||
feed_dict["article_report"]["sents_removed"].get(sent_removed, 0) + 1 | ||
) | ||
if url is not None: | ||
feed_dict["article_report"]["sents_to_url"][sent_removed] = feed_dict[ | ||
"article_report" | ||
]["sents_to_url"].get(sent_removed, []) + [url] | ||
|
||
def save_crawl_report(self): | ||
timestamp_str = self.__convert_dt_to_str(self.crawl_report_date) | ||
if not os.path.exists(self.save_dir): | ||
os.mkdir(self.save_dir) | ||
for lang in self.data["lang"]: | ||
filename = f"{lang}-crawl-{timestamp_str}.json" | ||
output_dir = os.path.join(self.save_dir, lang) | ||
if not os.path.exists(output_dir): | ||
os.mkdir(output_dir) | ||
with open(os.path.join(output_dir, filename), "w", encoding="utf-8") as f: | ||
json.dump(self.data["lang"][lang], f) | ||
|
||
def load_crawl_report_data(self, day_period: int, report_dir_path=None): | ||
if report_dir_path is None: | ||
report_dir_path = self.save_dir | ||
for lang in os.listdir(report_dir_path): | ||
for file in os.listdir(os.path.join(report_dir_path, lang)): | ||
lang, _, date = file.split(".")[0].split("-") | ||
date = self.__convert_str_to_dt(date) | ||
|
||
day_diff = (date.now() - date).days | ||
if day_diff > day_period: | ||
print( | ||
f"File '{file}' outside of day range of '{day_period}', was: '{day_diff}'" | ||
) | ||
continue | ||
try: | ||
self.crawl_report_date = min(self.crawl_report_date, date) | ||
with open( | ||
os.path.join(report_dir_path, lang, file), "r", encoding="utf-8" | ||
) as f: | ||
loaded_data = json.load(f) | ||
if lang not in self.data["lang"]: | ||
self.add_language(lang) | ||
lang_dict = self.data["lang"][lang] | ||
for feed in loaded_data["feeds"]: | ||
if feed not in lang_dict["feeds"]: | ||
# We have not loaded any feeds yet: | ||
lang_dict["feeds"][feed] = loaded_data["feeds"][feed] | ||
else: | ||
feed_dict = lang_dict["feeds"][feed] | ||
feed_dict["article_report"]["sents_removed"] = Counter( | ||
feed_dict["article_report"]["sents_removed"] | ||
) + Counter( | ||
loaded_data["feeds"][feed]["article_report"][ | ||
"sents_removed" | ||
] | ||
) | ||
feed_dict["article_report"]["quality_error"] = Counter( | ||
feed_dict["article_report"]["quality_error"] | ||
) + Counter( | ||
loaded_data["feeds"][feed]["article_report"][ | ||
"quality_error" | ||
] | ||
) | ||
print(f"LOADED File (d:{date}, l:{lang}): {file}") | ||
except Exception as e: | ||
print(f"Failed to load: '{file}', with: '{e} ({type(e)})'") | ||
|
||
def __validate_lang(self, lang: str): | ||
langs_available = set(self.data["lang"].keys()) | ||
if lang not in langs_available: | ||
raise ValueError( | ||
f"'{lang}' is not found in current loaded data. Available langs: '{list(langs_available)}'" | ||
) | ||
return True | ||
|
||
def get_total_non_quality_counts(self, langs_to_load: list[str] = None): | ||
if langs_to_load is None: | ||
langs_to_load = self.data["lang"].keys() | ||
else: | ||
for lang in langs_to_load: | ||
self.__validate_lang(lang) | ||
|
||
total_counts = Counter() | ||
for lang in langs_to_load: | ||
for feed in self.data["lang"][lang]["feeds"]: | ||
feed_dict = self.data["lang"][lang]["feeds"][feed] | ||
total_counts += Counter(feed_dict["article_report"]["quality_error"]) | ||
return total_counts | ||
|
||
def get_total_removed_sents_counts(self, langs_to_load: list[str] = None): | ||
if langs_to_load is None: | ||
langs_to_load = self.data["lang"].keys() | ||
else: | ||
for lang in langs_to_load: | ||
self.__validate_lang(lang) | ||
total_counts = Counter() | ||
|
||
for lang in langs_to_load: | ||
for feed in self.data["lang"][lang]["feeds"]: | ||
feed_dict = self.data["lang"][lang]["feeds"][feed] | ||
try: | ||
total_counts += Counter( | ||
feed_dict["article_report"]["sents_removed"] | ||
) | ||
except Exception as e: | ||
from pprint import pprint | ||
|
||
pprint(feed_dict) | ||
print(e, type(e)) | ||
input("Continue?") | ||
return total_counts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import traceback | ||
|
||
from sqlalchemy.exc import PendingRollbackError | ||
from time import time | ||
|
||
import zeeguu.core | ||
|
||
|
@@ -27,17 +28,19 @@ | |
|
||
from zeeguu.core.content_retriever.article_downloader import download_from_feed | ||
from zeeguu.core.model import Feed, Language | ||
from crawl_summary.crawl_report import CrawlReport | ||
|
||
db_session = zeeguu.core.model.db.session | ||
|
||
|
||
def download_for_feeds(list_of_feeds): | ||
def download_for_feeds(list_of_feeds, crawl_report): | ||
|
||
summary_stream = "" | ||
counter = 0 | ||
all_feeds_count = len(list_of_feeds) | ||
|
||
for feed in list_of_feeds: | ||
crawl_report.add_feed(feed) | ||
if feed.deactivated: | ||
continue | ||
|
||
|
@@ -48,7 +51,12 @@ def download_for_feeds(list_of_feeds): | |
log(f"{msg}") | ||
|
||
summary_stream += ( | ||
download_from_feed(feed, zeeguu.core.model.db.session) + "\n\n" | ||
download_from_feed( | ||
feed, | ||
zeeguu.core.model.db.session, | ||
crawl_report, | ||
) | ||
+ "\n\n" | ||
) | ||
|
||
except PendingRollbackError as e: | ||
|
@@ -57,27 +65,50 @@ def download_for_feeds(list_of_feeds): | |
"Something went wrong and we had to rollback a transaction; following is the full stack trace:" | ||
) | ||
traceback.print_exc() | ||
crawl_report.add_feed_error(feed, str(e)) | ||
|
||
except: | ||
except Exception as e: | ||
traceback.print_exc() | ||
crawl_report.add_feed_error(feed, str(e)) | ||
|
||
logp(f"Successfully finished processing {counter} feeds.") | ||
return summary_stream | ||
|
||
|
||
def retrieve_articles_for_language(language_code, send_email=False): | ||
|
||
start_time = time() | ||
language = Language.find(language_code) | ||
all_language_feeds = ( | ||
Feed.query.filter_by(language_id=language.id).filter_by(deactivated=False).all() | ||
) | ||
crawl_report = CrawlReport() | ||
crawl_report.add_language(language_code) | ||
|
||
summary_stream = download_for_feeds(all_language_feeds, crawl_report) | ||
if send_email: | ||
|
||
logp("sending summary email") | ||
|
||
summary_stream = download_for_feeds(all_language_feeds) | ||
import datetime | ||
|
||
mailer = ZeeguuMailer( | ||
f"{language.name} Crawl Summary " | ||
+ datetime.datetime.now().strftime("%H:%M"), | ||
summary_stream, | ||
"[email protected]", | ||
) | ||
mailer.send() | ||
crawl_report.set_total_time(language.code, round(time() - start_time, 2)) | ||
crawl_report.save_crawl_report() | ||
return crawl_report | ||
|
||
|
||
def retrieve_articles_from_all_feeds(): | ||
counter = 0 | ||
all_feeds = Feed.query.all() | ||
download_for_feeds(all_feeds) | ||
crawl_report = CrawlReport() | ||
download_for_feeds(all_feeds, crawl_report) | ||
crawl_report.save_crawl_report() | ||
|
||
|
||
if __name__ == "__main__": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE `zeeguu_test`.`article_broken_code_map` ( | ||
`article_id` INT NOT NULL, | ||
`broken_code` VARCHAR(45) NULL, | ||
INDEX `article_broken_code_map_ibfk_1_idx` (`article_id` ASC) VISIBLE, | ||
CONSTRAINT `article_broken_code_map_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `zeeguu_test`.`article` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION | ||
); |
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this as sub-method where it's used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to make it a private method for the class, but maybe I will make it a class method to reflect this. This is just to ensure it's converted in the expected format for the file output.
Let me know if you agree with it otherwise I can make it part of the methods that save and load, it's just in my mind I see it as a pair the dt_to_str and str_to_dt