-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
294 lines (257 loc) · 9.41 KB
/
Copy pathmain.py
File metadata and controls
294 lines (257 loc) · 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""AutoProductCatalog — build a CleverTap product catalog from event data."""
from __future__ import annotations
import argparse
import re
import sys
from pathlib import Path
from rich.prompt import Prompt
from src.catalog_client import CatalogClient
from src.config import load_config
from src.event_utils import filter_properties_by_presence
from src.events_client import EventsClient, UnsupportedEventError
from src.llm_mapper import resolve_mapping
from src.manual_mapper import run_manual_mapping
from src.processor import deduplicate_products, write_csv
from src import ui
TOTAL_STEPS = 7
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Create a CleverTap product catalog from your existing event data.",
)
parser.add_argument(
"--mode",
choices=["llm", "manual"],
default="llm",
help="Mapping mode: llm (default) or manual",
)
parser.add_argument(
"--days",
type=int,
default=60,
help="Days of event history to fetch (default: 60)",
)
parser.add_argument(
"--max-events",
type=int,
default=10_000,
help="Maximum number of events to fetch (default: 10,000)",
)
parser.add_argument(
"--output",
type=Path,
default=Path("catalog.csv"),
help="Output CSV path (default: catalog.csv)",
)
parser.add_argument(
"--catalog-name",
type=str,
default=None,
help="Catalog name for CleverTap upload (prompted if omitted)",
)
parser.add_argument(
"--skip-upload",
action="store_true",
help="Only generate CSV; do not upload to CleverTap",
)
parser.add_argument(
"--min-property-presence",
type=float,
default=60.0,
metavar="PCT",
help=(
"Only map properties that appear in at least this %% of sampled events "
"(default: 60). Example: with 50 sample events, 60 means the property "
"must be present in at least 30 events."
),
)
return parser.parse_args()
def _slugify(name: str) -> str:
slug = re.sub(r"[^a-zA-Z0-9_-]+", "_", name.strip().lower())
slug = re.sub(r"_+", "_", slug).strip("_")
return slug or "event_catalog"
def _resolve_mode(requested: str, has_openai_key: bool) -> str:
if requested == "manual":
return "manual"
if has_openai_key:
return "llm"
ui.print_warning(
"OPENAI_API_KEY not set — falling back to manual mapping mode."
)
return "manual"
def main() -> None:
args = parse_args()
ui.print_banner()
ui.print_step(1, TOTAL_STEPS, "Load configuration")
config = load_config()
ui.console.print(
f" Account: [bold]{config.account_id}[/bold] "
f"Region: [bold]{config.region}[/bold]"
)
mode = _resolve_mode(args.mode, config.openai_api_key is not None)
ui.console.print(f" Mapping mode: [bold]{mode}[/bold]")
ui.print_step(2, TOTAL_STEPS, "Choose event")
event_name = Prompt.ask("Enter the CleverTap event name to export").strip()
if not event_name:
ui.print_error("Event name is required.")
sys.exit(1)
events_client = EventsClient(config, console=ui.console)
if args.min_property_presence < 0 or args.min_property_presence > 100:
ui.print_error("--min-property-presence must be between 0 and 100.")
sys.exit(1)
ui.print_step(3, TOTAL_STEPS, "Fetch sample events")
try:
samples, _all_keys, raw_samples = events_client.fetch_sample(event_name)
except UnsupportedEventError as exc:
ui.print_error(str(exc))
sys.exit(1)
except Exception as exc:
ui.print_error(f"Failed to fetch sample events: {exc}")
sys.exit(1)
if not samples:
ui.print_error(
f"No events found for '{event_name}' in the last few days. "
"Check the event name and try again."
)
sys.exit(1)
min_presence = args.min_property_presence / 100.0
property_keys, included_stats, excluded_stats = filter_properties_by_presence(
raw_samples,
min_presence,
)
if not property_keys:
ui.print_error(
f"No properties meet the {args.min_property_presence:g}% presence threshold "
f"across {len(raw_samples)} sampled events. "
"Try lowering --min-property-presence."
)
sys.exit(1)
has_items = any("Items" in r for r in raw_samples)
ui.console.print(
f" Found [bold]{len(raw_samples)}[/bold] sample event(s)"
+ (
f" → [bold]{len(samples)}[/bold] line item(s) after expanding Items"
if has_items
else ""
)
)
if has_items:
ui.console.print(
" [dim]Charged events store per-product fields (e.g. Product ID) "
"inside event_props.Items[]. They are expanded automatically.[/dim]"
)
ui.show_presence_threshold_note(
args.min_property_presence,
len(raw_samples),
len(property_keys),
excluded_stats,
)
ui.show_properties_table(
property_keys,
samples + raw_samples,
presence_stats=included_stats,
)
ui.show_excluded_properties(excluded_stats)
ui.print_step(4, TOTAL_STEPS, "Configure field mapping")
try:
if mode == "llm":
mapping = resolve_mapping(config, samples, property_keys)
else:
mapping = run_manual_mapping(samples, property_keys)
except Exception as exc:
ui.print_error(f"Mapping failed: {exc}")
sys.exit(1)
ui.print_step(5, TOTAL_STEPS, "Export events and build catalog CSV")
ui.console.print(
f" Fetching up to [bold]{args.max_events:,}[/bold] events "
f"from the last [bold]{args.days}[/bold] days "
)
try:
all_events = events_client.fetch_all(
event_name,
days=args.days,
max_events=args.max_events,
)
except Exception as exc:
ui.print_error(f"Failed to fetch events: {exc}")
sys.exit(1)
products, build_stats = deduplicate_products(all_events, mapping)
if not products:
ui.print_error("No products could be built from the events.")
ui.console.print(
f" Processed [bold]{build_stats.total_rows:,}[/bold] row(s) after "
f"expanding Items; [bold]{build_stats.rows_with_identity:,}[/bold] "
f"had a value for identity field [bold]'{mapping.identity}'[/bold]."
)
if build_stats.rows_with_identity == 0:
ui.console.print(
"\n [yellow]Likely cause:[/yellow] exported events use an older "
"Charged schema without per-product fields.\n"
" [dim]Try a shorter --days window.[/dim]"
)
else:
ui.console.print(
"\n [yellow]Rows had identity values but none produced valid "
"products — check mapping for name/image_url fields.[/yellow]"
)
sys.exit(1)
write_csv(products, mapping, args.output)
ui.print_success(
f"Saved [bold]{len(products):,}[/bold] unique products to "
f"[bold]{args.output.resolve()}[/bold]"
)
if args.skip_upload:
ui.print_step(6, TOTAL_STEPS, "Done")
ui.console.print("[dim]Upload skipped (--skip-upload).[/dim]")
return
ui.print_step(6, TOTAL_STEPS, "Review catalog CSV")
if not ui.confirm_csv_before_upload(args.output, len(products)):
ui.print_step(7, TOTAL_STEPS, "Done")
ui.console.print(
"[dim]Upload cancelled. Your CSV is saved at "
f"{args.output.resolve()} — review it and re-run when ready.[/dim]"
)
return
ui.print_step(7, TOTAL_STEPS, "Upload catalog to CleverTap")
default_catalog_name = _slugify(f"{event_name}_catalog")
catalog_name = args.catalog_name or Prompt.ask(
"Catalog name",
default=default_catalog_name,
).strip()
if not catalog_name:
catalog_name = default_catalog_name
if config.created_by:
created_by = config.created_by
else:
created_by = Prompt.ask(
"Your email (required for catalog API)",
).strip()
if not created_by or "@" not in created_by:
ui.print_error("A valid email address is required for upload.")
sys.exit(1)
catalog_client = CatalogClient(config, console=ui.console)
try:
with ui.console.status("[bold cyan]Creating catalog..."):
catalog_id = catalog_client.create_catalog(
catalog_name=catalog_name,
mapping=mapping,
created_by=created_by,
)
ui.console.print(f" Catalog created with ID [bold]{catalog_id}[/bold]")
catalog_client.upload_csv(
catalog_id=catalog_id,
csv_path=args.output,
created_by=created_by,
)
except Exception as exc:
ui.print_error(f"Upload failed: {exc}")
ui.console.print(
f"[dim]Your CSV was saved at {args.output.resolve()}[/dim]"
)
sys.exit(1)
catalog_url = config.catalog_url(catalog_id)
ui.print_success("Catalog upload complete!")
ui.console.print(f" Queued [green]{len(products):,}[/green] product(s) for processing")
ui.console.print(f"\n View your catalog:\n [link={catalog_url}]{catalog_url}[/link]\n")
if __name__ == "__main__":
main()