pidatametrics1 0.3.4__py2.py3-none-any.whl → 0.3.6__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pidatametrics/manager.py
CHANGED
|
@@ -5,6 +5,24 @@ import datetime
|
|
|
5
5
|
from dateutil.relativedelta import relativedelta
|
|
6
6
|
|
|
7
7
|
class PiReportManager(PiDataMetrics):
|
|
8
|
+
|
|
9
|
+
# --- HELPER: Generate Unique Name ---
|
|
10
|
+
def _generate_unique_name(self, base_name, workspace_ref=None):
|
|
11
|
+
"""
|
|
12
|
+
Creates a unique name like: Hist_51780_0502_1430
|
|
13
|
+
(Base_WorkspaceID_Date_Time)
|
|
14
|
+
"""
|
|
15
|
+
now = datetime.datetime.now()
|
|
16
|
+
timestamp = now.strftime("%d%m_%H%M") # e.g., 0502_1430
|
|
17
|
+
|
|
18
|
+
ws_part = f"_{workspace_ref}" if workspace_ref else ""
|
|
19
|
+
|
|
20
|
+
# Combine parts
|
|
21
|
+
full_name = f"{base_name}{ws_part}_{timestamp}"
|
|
22
|
+
|
|
23
|
+
# Truncate to 31 chars (Google Sheets limit) just in case
|
|
24
|
+
return full_name[:31]
|
|
25
|
+
|
|
8
26
|
def _resolve_workspaces(self, ids_str=None, name_pattern=None):
|
|
9
27
|
all_ws = self.get_workspaces()
|
|
10
28
|
targets = {}
|
|
@@ -43,21 +61,34 @@ class PiReportManager(PiDataMetrics):
|
|
|
43
61
|
current_date -= relativedelta(months=1)
|
|
44
62
|
return dates
|
|
45
63
|
|
|
46
|
-
# ---
|
|
47
|
-
def _export_data(self, data, output_mode,
|
|
64
|
+
# --- UPDATED EXPORT LOGIC ---
|
|
65
|
+
def _export_data(self, data, output_mode, bq_config, spreadsheet_name, unique_name):
|
|
66
|
+
"""
|
|
67
|
+
Handles export routing.
|
|
68
|
+
unique_name is used for:
|
|
69
|
+
- CSV Filename
|
|
70
|
+
- Excel Filename
|
|
71
|
+
- Google Sheet Tab Name
|
|
72
|
+
"""
|
|
48
73
|
if not data:
|
|
49
74
|
print("No data to export.")
|
|
50
75
|
return
|
|
51
76
|
|
|
52
77
|
if output_mode == 'bigquery' and bq_config:
|
|
78
|
+
# BigQuery doesn't use filenames, it uses the Table ID in config
|
|
53
79
|
PiExporter.to_bigquery(data, bq_config['project'], bq_config['dataset'], bq_config['table'])
|
|
80
|
+
|
|
54
81
|
elif output_mode == 'excel':
|
|
55
|
-
|
|
82
|
+
# Use unique_name as filename
|
|
83
|
+
PiExporter.to_excel(data, unique_name)
|
|
84
|
+
|
|
56
85
|
elif output_mode == 'gsheet' and spreadsheet_name:
|
|
57
|
-
|
|
86
|
+
# Use unique_name as Tab Name
|
|
87
|
+
PiExporter.to_google_sheet(data, spreadsheet_name, tab_name=unique_name)
|
|
88
|
+
|
|
58
89
|
else:
|
|
59
|
-
# Default to CSV
|
|
60
|
-
PiExporter.to_csv(data,
|
|
90
|
+
# Default to CSV, use unique_name as filename
|
|
91
|
+
PiExporter.to_csv(data, unique_name)
|
|
61
92
|
|
|
62
93
|
def run_volume_report(self, filename, workspace_ids=None, workspace_name=None, output_mode='csv', bq_config=None, spreadsheet_name=None):
|
|
63
94
|
targets = self._resolve_workspaces(workspace_ids, workspace_name)
|
|
@@ -71,7 +102,11 @@ class PiReportManager(PiDataMetrics):
|
|
|
71
102
|
rows = PiParsers.parse_volume_data(vol_data, stg['name'], terms, ws_name)
|
|
72
103
|
all_rows.extend(rows)
|
|
73
104
|
|
|
74
|
-
|
|
105
|
+
# Generate Unique Name
|
|
106
|
+
ws_ref = list(targets.keys())[0] if targets else "Multi"
|
|
107
|
+
unique_name = self._generate_unique_name("Vol", ws_ref)
|
|
108
|
+
|
|
109
|
+
self._export_data(all_rows, output_mode, bq_config, spreadsheet_name, unique_name)
|
|
75
110
|
|
|
76
111
|
def run_serp_report(self, data_sources, output_mode='csv', bq_config=None, filename=None, manual_duplication=None, spreadsheet_name=None):
|
|
77
112
|
yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
|
|
@@ -83,7 +118,11 @@ class PiReportManager(PiDataMetrics):
|
|
|
83
118
|
rows = PiParsers.parse_serp_response(raw_data, market, w_name, se_name, yesterday, cat_map, manual_duplication)
|
|
84
119
|
all_rows.extend(rows)
|
|
85
120
|
|
|
86
|
-
|
|
121
|
+
# Generate Unique Name
|
|
122
|
+
ws_ref = data_sources[0][1] if data_sources else "All"
|
|
123
|
+
unique_name = self._generate_unique_name("SERP", ws_ref)
|
|
124
|
+
|
|
125
|
+
self._export_data(all_rows, output_mode, bq_config, spreadsheet_name, unique_name)
|
|
87
126
|
|
|
88
127
|
def run_historical_serp_report(self, data_sources, duration, frequency, start_date=None, features=None, num_results=25, output_mode='csv', bq_config=None, filename="historical_data", spreadsheet_name=None):
|
|
89
128
|
if features is None:
|
|
@@ -96,12 +135,11 @@ class PiReportManager(PiDataMetrics):
|
|
|
96
135
|
|
|
97
136
|
print(f"Starting Historical Report ({frequency}) for last {duration} periods...")
|
|
98
137
|
|
|
99
|
-
all_file_rows = []
|
|
138
|
+
all_file_rows = []
|
|
100
139
|
|
|
101
140
|
for i, date in enumerate(target_dates):
|
|
102
141
|
print(f"[{i+1}/{len(target_dates)}] Processing Date: {date}...")
|
|
103
|
-
|
|
104
|
-
daily_rows = [] # Reset container for this specific date
|
|
142
|
+
daily_rows = []
|
|
105
143
|
|
|
106
144
|
for source in data_sources:
|
|
107
145
|
market, w_id, w_name, se_id, se_name = source
|
|
@@ -111,31 +149,26 @@ class PiReportManager(PiDataMetrics):
|
|
|
111
149
|
'serp-feature[]': features
|
|
112
150
|
}
|
|
113
151
|
raw_data = self.get_bulk_serp_data(w_id, se_id, date, **params)
|
|
114
|
-
|
|
115
|
-
rows = PiParsers.parse_serp_response(
|
|
116
|
-
raw_data, market, w_name, se_name, date, category_map=None
|
|
117
|
-
)
|
|
118
|
-
|
|
152
|
+
rows = PiParsers.parse_serp_response(raw_data, market, w_name, se_name, date, category_map=None)
|
|
119
153
|
daily_rows.extend(rows)
|
|
120
|
-
|
|
121
154
|
except Exception as e:
|
|
122
155
|
print(f"Failed to fetch {w_name} on {date}: {e}")
|
|
123
156
|
|
|
124
|
-
#
|
|
157
|
+
# BigQuery uploads immediately per day
|
|
125
158
|
if output_mode == 'bigquery' and bq_config:
|
|
126
159
|
if daily_rows:
|
|
127
160
|
print(f"Uploading {len(daily_rows)} rows for {date} to BigQuery...")
|
|
128
161
|
PiExporter.to_bigquery(daily_rows, bq_config['project'], bq_config['dataset'], bq_config['table'])
|
|
129
|
-
|
|
130
|
-
print(f"No data found for {date}, skipping upload.")
|
|
131
|
-
|
|
132
|
-
# --- FILE LOGIC: ACCUMULATE (CSV, Excel, GSheet) ---
|
|
162
|
+
# Others accumulate
|
|
133
163
|
elif output_mode in ['csv', 'excel', 'gsheet']:
|
|
134
164
|
all_file_rows.extend(daily_rows)
|
|
135
165
|
|
|
136
|
-
# Final
|
|
166
|
+
# Final Export for Files
|
|
137
167
|
if output_mode in ['csv', 'excel', 'gsheet']:
|
|
138
|
-
|
|
168
|
+
ws_ref = data_sources[0][1] if data_sources else "All"
|
|
169
|
+
unique_name = self._generate_unique_name("Hist", ws_ref)
|
|
170
|
+
|
|
171
|
+
self._export_data(all_file_rows, output_mode, bq_config, spreadsheet_name, unique_name)
|
|
139
172
|
|
|
140
173
|
def run_llm_report(self, data_sources, start_period, end_period, stg_ids=None, output_mode='csv', bq_config=None, filename="llm_output", spreadsheet_name=None):
|
|
141
174
|
all_rows = []
|
|
@@ -146,11 +179,14 @@ class PiReportManager(PiDataMetrics):
|
|
|
146
179
|
try:
|
|
147
180
|
print(f"Fetching LLM data for {w_name} ({se_name})...")
|
|
148
181
|
raw_data = self.get_llm_mentions(w_id, se_id, start_period, end_period, stg_ids)
|
|
149
|
-
|
|
150
182
|
rows = PiParsers.parse_llm_response(raw_data, market, w_name, se_name)
|
|
151
183
|
all_rows.extend(rows)
|
|
152
184
|
print(f"Found {len(rows)} mentions/queries.")
|
|
153
185
|
except Exception as e:
|
|
154
186
|
print(f"Failed to fetch LLM data for {w_name}: {e}")
|
|
155
187
|
|
|
156
|
-
|
|
188
|
+
# Generate Unique Name
|
|
189
|
+
ws_ref = data_sources[0][1] if data_sources else "All"
|
|
190
|
+
unique_name = self._generate_unique_name("LLM", ws_ref)
|
|
191
|
+
|
|
192
|
+
self._export_data(all_rows, output_mode, bq_config, spreadsheet_name, unique_name)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pidatametrics1
|
|
3
|
-
Version: 0.3.
|
|
4
|
-
Summary: A wrapper for Pi Datametrics API with CSV and BigQuery support.
|
|
3
|
+
Version: 0.3.6
|
|
4
|
+
Summary: A test wrapper for Pi Datametrics API with CSV and BigQuery support.
|
|
5
5
|
Requires-Dist: google-auth
|
|
6
6
|
Requires-Dist: google-cloud-bigquery
|
|
7
7
|
Requires-Dist: gspread
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
pidatametrics/__init__.py,sha256=cmNSHvjvMsYO1tMv0Nf-7LGjIJ8AFXmUIwiv8jQ34BI,137
|
|
2
2
|
pidatametrics/client.py,sha256=tHH0GV0rk2SizVqRdKepjdDQevkfdWlHOJHwsPR2PCk,4399
|
|
3
3
|
pidatametrics/exporter.py,sha256=CcsdVhxI6rXi0zlQaYzFEGX0GL3ZaNV94Pj5r_WrZc4,4226
|
|
4
|
-
pidatametrics/manager.py,sha256=
|
|
4
|
+
pidatametrics/manager.py,sha256=tUeeJ-wKAlhpWsaZEAjxtZCtA2EbQcTBB1JkXPEVV50,9101
|
|
5
5
|
pidatametrics/parsers.py,sha256=fiLx3080wNubT1VqSIeDvlrKT85KdqlKhY6FaB2XuC8,5989
|
|
6
|
-
pidatametrics1-0.3.
|
|
7
|
-
pidatametrics1-0.3.
|
|
8
|
-
pidatametrics1-0.3.
|
|
6
|
+
pidatametrics1-0.3.6.dist-info/METADATA,sha256=DOlktdH-uHRwem6nbXWDIn3NLXmw67iENpiiX4f7--o,293
|
|
7
|
+
pidatametrics1-0.3.6.dist-info/WHEEL,sha256=aha0VrrYvgDJ3Xxl3db_g_MDIW-ZexDdrc_m-Hk8YY4,105
|
|
8
|
+
pidatametrics1-0.3.6.dist-info/RECORD,,
|
|
File without changes
|