44
55from .matomo_campaign_helper import process_dataframe_for_campaign
66from .matomo_request_config import matomo_requests_config
7- from ..postgres_helper import (
8- get_postgres_connection ,
9- clean_data_in_postgres ,
10- dump_data_to_postgres ,
11- )
7+ from .matomo_postgres_helper import get_postgres_connection , clean_data_in_postgres , dump_data_to_postgres
128from .matomo_url import get_matomo_base_url , construct_url
139import logging
1410
1915
2016def parse_range_data (raw_data ):
2117 for entry in raw_data :
22- if entry .get (" subtable" ):
23- for sub_entry in entry [" subtable" ]:
24- sub_entry [" sub_type" ] = entry ["label" ]
18+ if entry .get (' subtable' ):
19+ for sub_entry in entry [' subtable' ]:
20+ sub_entry [' sub_type' ] = entry ["label" ]
2521 raw_data .append (sub_entry )
26- entry .pop (" subtable" )
27- if entry .get (" goals" ):
28- entry .pop (" goals" )
22+ entry .pop (' subtable' )
23+ if entry .get (' goals' ):
24+ entry .pop (' goals' )
2925 return raw_data
3026
3127
@@ -34,11 +30,11 @@ def fetch_data_for_day(base_url, report_name, config, day):
3430 """Fetches data from Matomo for a specific day and returns it as a DataFrame."""
3531 url = construct_url (base_url , config , day )
3632 try :
37- response = http .request (" GET" , url )
38- raw_data = json .loads (response .data .decode (" utf-8" ))
33+ response = http .request (' GET' , url )
34+ raw_data = json .loads (response .data .decode (' utf-8' ))
3935
4036 # Check if the response contains errors
41- if isinstance (raw_data , dict ) and raw_data .get (" result" ) == " error" :
37+ if isinstance (raw_data , dict ) and raw_data .get (' result' ) == ' error' :
4238 error_message = f"Error fetching data for { report_name } on { day } : { raw_data .get ('message' )} "
4339 raise Exception (error_message )
4440
@@ -49,58 +45,46 @@ def fetch_data_for_day(base_url, report_name, config, day):
4945 elif isinstance (raw_data , dict ):
5046 data = pd .json_normalize (raw_data )
5147 else :
52- print (
53- f"Unexpected data format for { report_name } on { day } : { type (raw_data )} "
54- )
48+ print (f"Unexpected data format for { report_name } on { day } : { type (raw_data )} " )
5549 return pd .DataFrame ()
5650 # Add the date field to each row
57- data [" date" ] = pd .to_datetime (day )
51+ data [' date' ] = pd .to_datetime (day )
5852 data_processed = process_dataframe_for_campaign (data )
5953 return data_processed
6054
6155 except Exception as e :
6256 error_message = f"Error fetching data for { report_name } on { day } : { str (e )} "
6357 raise Exception (error_message )
6458
65-
59+ # Fetch data from Matomo for each day in the date range and merge into a single DataFrame
6660def fetch_data_from_matomo (base_url , report_name , config , start_date , end_date ):
6761 """Fetches data from Matomo for each day in the specified range and merges it into a single DataFrame."""
68- date_range = (
69- pd .date_range (start = start_date , end = end_date ).strftime ("%Y-%m-%d" ).tolist ()
70- )
71- all_data = [
72- fetch_data_for_day (base_url , report_name , config , day ) for day in date_range
73- ]
62+ date_range = pd .date_range (start = start_date , end = end_date ).strftime ('%Y-%m-%d' ).tolist ()
63+ all_data = [fetch_data_for_day (base_url , report_name , config , day ) for day in date_range ]
7464
7565 # Combine all non-empty DataFrames into a single DataFrame
7666 valid_data = [df for df in all_data if not df .empty ]
7767 if valid_data :
7868 return pd .concat (valid_data , ignore_index = True )
7969 else :
80- logger .warning (
81- f"No data fetched for report '{ report_name } ' between { start_date } and { end_date } ."
82- )
70+ logger .warning (f"No data fetched for report '{ report_name } ' between { start_date } and { end_date } ." )
8371 return pd .DataFrame ()
8472
85-
73+ # Main function to fetch and dump data
8674def fetch_and_dump_data (matomo_site_id , database , day ):
87- """
88- Main function to fetch and dump data
89- """
90- start_date = (pd .to_datetime (day ) - pd .Timedelta (days = 1 )).strftime ("%Y-%m-%d" )
75+
76+ start_date = (pd .to_datetime (day ) - pd .Timedelta (days = 1 )).strftime ('%Y-%m-%d' )
9177 end_date = start_date
9278 base_url = get_matomo_base_url (matomo_site_id )
93- connection = get_postgres_connection ("matomo_postgres" , database )
79+ connection = get_postgres_connection (database )
9480
9581 if not connection :
9682 error_message = "Cannot proceed without database connection."
9783 raise Exception (error_message )
9884
9985 for report_name , config in matomo_requests_config .items ():
10086 print (f"Fetching data for { report_name } ..." )
101- data = fetch_data_from_matomo (
102- base_url , report_name , config , start_date , end_date
103- )
87+ data = fetch_data_from_matomo (base_url , report_name , config , start_date , end_date )
10488
10589 if data is not None and not data .empty :
10690 # Clean existing data in the table before dumping new data
@@ -110,4 +94,4 @@ def fetch_and_dump_data(matomo_site_id, database, day):
11094 else :
11195 print (f"No data fetched for { report_name } , skipping clean and dump." )
11296
113- connection .close ()
97+ connection .close ()
0 commit comments