Skip to content

Commit b50eebc

Browse files
committed
Multy user DB release
1 parent 8eca941 commit b50eebc

5 files changed

Lines changed: 59 additions & 15 deletions

File tree

main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def parse_args():
6060
default=1
6161
)
6262
parser.add_argument(
63-
"--serch_request_id",
63+
"--search_request_id",
6464
type=int,
6565
default=1
6666
)
@@ -69,10 +69,10 @@ def parse_args():
6969
def init_all_tables(debug: bool) -> None:
7070
init_database()
7171
create_app_users_table()
72-
create_articles_table()
73-
create_request_stats_table()
7472
create_search_requests_table()
73+
create_articles_table()
7574
create_user_news_table()
75+
create_request_stats_table()
7676
if debug:
7777
create_news_tables()
7878

@@ -85,7 +85,7 @@ def main()-> None:
8585
if args.debug:
8686
loaded = run_debug_pipeline(args.keyword, args.limit, args.page_size)
8787
else:
88-
loaded = run_pipeline_for_web_user(args.user_id, args.search_request_id, args.ketword, args.limit, args.page_size)
88+
loaded = run_pipeline_for_web_user(args.user_id, args.search_request_id, args.keyword, args.limit, args.page_size)
8989
logger.info("Pipline finished. loaded rows: %s", loaded)
9090
except Exception as e:
9191
logger.exception("pipeline failed: %s", e)

src/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .extract import make_extract_debug, make_extract_web
22
from .transform import transform_article_web, transform_article_debug
3-
from .load import load_news, load_web_pipeline
3+
from .load import load_news, load_web_pipeline, load_request_stats
44
from .db import init_database, create_news_tables, create_app_users_table, create_search_requests_table, create_articles_table, create_user_news_table, create_request_stats_table
55
from .pipeline import run_pipeline_for_web_user, run_debug_pipeline

src/load.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66
BASE_DIR = (Path(__file__).resolve().parent.parent)/"data"/"clean"
77

8+
89
def load_news(clean_news:str, max_rows: Optional[int] = None)-> int:
910
LOAD_DIR = BASE_DIR/clean_news
1011
num_of_news = 0
@@ -95,29 +96,39 @@ def load_user_news(cur, user_id: int, search_request_id: int, article_id: int, k
9596
cur.execute(query,(user_id, search_request_id, article_id, keyword, fetched_at))
9697
return cur.rowcount
9798

98-
def load_request_stats(cur, search_request_id: int, stats: dict) -> None:
99+
def load_request_stats(search_request_id: int, stats: dict) -> None:
99100
query = """
100101
INSERT INTO request_stats (
101102
search_request_id,
102103
income_articles,
103104
accepted_articles,
104105
rejected_articles,
105106
reasons_counts,
106-
prime_reason
107+
prime_reasons
107108
)
108109
VALUES (%s,%s,%s,%s,%s,%s)
110+
ON CONFLICT (search_request_id) DO UPDATE
111+
SET
112+
income_articles = EXCLUDED.income_articles,
113+
accepted_articles = EXCLUDED.accepted_articles,
114+
rejected_articles = EXCLUDED.rejected_articles,
115+
reasons_counts = EXCLUDED.reasons_counts,
116+
prime_reasons = EXCLUDED.prime_reasons
109117
"""
110-
cur.execute(query, (
118+
119+
with get_cursor(settings.db_news) as (conn, cur):
120+
cur.execute(query, (
111121
search_request_id,
112122
stats["income_articles"],
113123
stats["accepted_articles"],
114124
stats["rejected_articles"],
115125
json.dumps(stats["reasons_counts"]),
116126
json.dumps(stats["prime_reason"])
117127
))
118-
return None
128+
conn.commit()
129+
119130

120-
def load_web_pipeline(user_id: int, search_request_id: int, clean_data: list[dict], stats: dict) -> int:
131+
def load_web_pipeline(user_id: int, search_request_id: int, clean_data: list[dict]) -> int:
121132
loaded_count = 0
122133
with get_cursor(settings.db_news) as (conn, cur):
123134
for article in clean_data:
@@ -126,7 +137,6 @@ def load_web_pipeline(user_id: int, search_request_id: int, clean_data: list[dic
126137
article_id = upsert_article(cur,article)
127138
inserted = load_user_news(cur, user_id, search_request_id, article_id, keyword, fetched_at)
128139
loaded_count += inserted
129-
load_request_stats(cur, search_request_id, stats)
130140
conn.commit()
131141
return loaded_count
132142

src/pipeline.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,44 @@
55
transform_article_web,
66
transform_article_debug,
77
load_web_pipeline,
8-
load_news,
8+
load_news,
9+
load_request_stats
910
)
1011

1112
logger = logging.getLogger(__name__)
13+
def make_empty_stats()->dict:
14+
return {
15+
"income_articles" : 0,
16+
"accepted_articles" : 0,
17+
"rejected_articles" : 0,
18+
"reasons_counts" :{
19+
"no_author":0,
20+
"no_title":0,
21+
"no_description":0,
22+
"short_description":0,
23+
"no_url":0
24+
},
25+
"prime_reason":{
26+
"no_author":0,
27+
"no_title":0,
28+
"no_description":0,
29+
"short_description":0,
30+
"no_url":0
31+
}
32+
}
33+
34+
def merge_stats(stats: dict, page_stats: dict)-> None:
35+
stats["income_articles"] += page_stats["income_articles"]
36+
stats["accepted_articles"] += page_stats["accepted_articles"]
37+
stats["rejected_articles"] += page_stats["rejected_articles"]
38+
for k,v in page_stats["reasons_counts"].items():
39+
stats["reasons_counts"][k] += v
40+
41+
for k,v in page_stats["prime_reason"].items():
42+
stats["prime_reason"][k] += v
1243

1344
def run_pipeline_for_web_user(user_id: int, search_request_id: int, key_word: str, limit: int, page_size: int) -> int:
45+
statistic = make_empty_stats()
1446
num_of_news = 0
1547
page = 1
1648
while num_of_news < limit:
@@ -19,9 +51,11 @@ def run_pipeline_for_web_user(user_id: int, search_request_id: int, key_word: st
1951
logger.warning("there is no more artical")
2052
break
2153
clean_data, stats = transform_article_web(payload)
22-
result_num_of_news = load_web_pipeline(user_id, search_request_id, clean_data, stats)
54+
merge_stats(statistic, stats)
55+
result_num_of_news = load_web_pipeline(user_id, search_request_id, clean_data)
2356
num_of_news += result_num_of_news
2457
page += 1
58+
load_request_stats(search_request_id, statistic)
2559
logger.info(f"{num_of_news} news on key word {key_word} already aploaded")
2660
return num_of_news
2761

src/worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def mark_as_success(search_request_id: int) -> None:
4646
def mark_as_error(search_request_id: int, error_text: str) -> None:
4747
query = """UPDATE search_requests
4848
SET
49-
status = 'ERROR',
49+
status = 'failed',
5050
finished_at = NOW(),
5151
error_text = %s
5252
WHERE id = %s"""
@@ -74,7 +74,7 @@ def one_request() -> bool:
7474
logger.info(f"pushed {amount_of_articles} articles")
7575
except Exception as e:
7676
logger.exception(f"Pipeline for {user_id} on {search_request_id} by {keyword} failed: {e}")
77-
mark_as_error(search_request_id, e)
77+
mark_as_error(search_request_id, str(e))
7878
return True
7979

8080
def run_worker_loop(pull_interval: int = 3) -> None:

0 commit comments

Comments
 (0)