|
1 | 1 | import itertools |
2 | | -import json |
| 2 | +import json, os |
3 | 3 | import logging |
4 | 4 |
|
5 | 5 | from collections import defaultdict |
|
17 | 17 | from django.utils.decorators import method_decorator |
18 | 18 | from django.views.decorators.csrf import csrf_exempt |
19 | 19 |
|
| 20 | +from aviso.framework.tenant_mongo_resolver import TenantMongoResolver |
| 21 | +from gbm_apis.deal_result.result_Utils import deals_results_by_period |
| 22 | + |
20 | 23 | logger = logging.getLogger("aviso-core.%s" % __name__) |
21 | 24 |
|
22 | 25 |
|
@@ -177,9 +180,28 @@ def build_global_territory_owners(groups: Iterable[Iterable[str]], period: Optio |
177 | 180 | logger.warning("sec_context.etl not available; returning empty drilldown set") |
178 | 181 | return {} |
179 | 182 |
|
180 | | - records = sec_context.etl.uip( |
181 | | - "UIPIterator", dataset="OppDS", record_filter=record_filter, fields_requested=list(required_fields) |
182 | | - ) |
| 183 | + # records = sec_context.etl.uip( |
| 184 | + # "UIPIterator", dataset="OppDS", record_filter=record_filter, fields_requested=list(required_fields), |
| 185 | + # skip_prepare=True |
| 186 | + # ) |
| 187 | + |
| 188 | + cname = os.environ.get("CNAME", "preprod") |
| 189 | + etl_db = TenantMongoResolver().ms_connection_mongo_client_db(tenant=sec_context.name, db_type='etl', cname=cname) |
| 190 | + |
| 191 | + coll = etl_db[sec_context.name + '.OppDS._uip._data'] |
| 192 | + |
| 193 | + projection = { |
| 194 | + "object.terminal_date": 1 |
| 195 | + } |
| 196 | + |
| 197 | + for field in required_fields: |
| 198 | + projection[f"object.history.{field}"] = 1 |
| 199 | + |
| 200 | + records = coll.find( |
| 201 | + record_filter, |
| 202 | + projection=projection, |
| 203 | + no_cursor_timeout=True |
| 204 | + ).batch_size(50000) |
183 | 205 |
|
184 | 206 | hierarchy = _fetch_raw_hierarchy() |
185 | 207 | parents = {} |
@@ -229,36 +251,79 @@ def build_global_territory_owners(groups: Iterable[Iterable[str]], period: Optio |
229 | 251 | processed_count = 0 |
230 | 252 | skipped_count = 0 |
231 | 253 |
|
| 254 | + # for record in records: |
| 255 | + # #print("Processing record:", record) |
| 256 | + # # raise |
| 257 | + # feat_map = record['object']['history'] |
| 258 | + # #print("Feature map keys:", feat_map.keys()) |
| 259 | + # # raise |
| 260 | + # if territory_key not in feat_map: |
| 261 | + # skipped_count += 1 |
| 262 | + # continue |
| 263 | + |
| 264 | + # territory_data = feat_map[territory_key] |
| 265 | + # if not territory_data: |
| 266 | + # skipped_count += 1 |
| 267 | + # continue |
| 268 | + |
| 269 | + # #print("Territory data:", territory_data) |
| 270 | + # leaf_node = territory_data[-1][1] |
| 271 | + # if leaf_node not in global_children_set: |
| 272 | + # skipped_count += 1 |
| 273 | + # continue |
| 274 | + |
| 275 | + # processed_count += 1 |
| 276 | + # for group_name, field_mappings in group_configs: |
| 277 | + # current_row = [] |
| 278 | + # for fld_label, map_key in field_mappings: |
| 279 | + # if map_key in feat_map: |
| 280 | + # field_data = feat_map[map_key] |
| 281 | + # if field_data: |
| 282 | + # current_row.append([fld_label, field_data[-1][1]]) |
| 283 | + # else: |
| 284 | + # current_row.append([fld_label, "N/A"]) |
| 285 | + # else: |
| 286 | + # current_row.append([fld_label, "N/A"]) |
| 287 | + # staging[group_name][leaf_node].add(tuple(tuple(pair) for pair in current_row)) |
| 288 | + |
232 | 289 | for record in records: |
233 | | - feat_map = record.featMap |
234 | | - if territory_key not in feat_map: |
| 290 | + obj = record.get("object") |
| 291 | + if not obj: |
| 292 | + skipped_count += 1 |
| 293 | + continue |
| 294 | + |
| 295 | + history = obj.get("history") |
| 296 | + if not history: |
235 | 297 | skipped_count += 1 |
236 | 298 | continue |
237 | 299 |
|
238 | | - territory_data = feat_map[territory_key] |
| 300 | + # Get territory data (single lookup only) |
| 301 | + territory_data = history.get(territory_key) |
239 | 302 | if not territory_data: |
240 | 303 | skipped_count += 1 |
241 | 304 | continue |
242 | 305 |
|
| 306 | + # Get leaf node |
243 | 307 | leaf_node = territory_data[-1][1] |
| 308 | + |
244 | 309 | if leaf_node not in global_children_set: |
245 | 310 | skipped_count += 1 |
246 | 311 | continue |
247 | 312 |
|
248 | 313 | processed_count += 1 |
| 314 | + |
| 315 | + # Process groups |
249 | 316 | for group_name, field_mappings in group_configs: |
250 | | - current_row = [] |
| 317 | + row = [] |
| 318 | + |
251 | 319 | for fld_label, map_key in field_mappings: |
252 | | - if map_key in feat_map: |
253 | | - field_data = feat_map[map_key] |
254 | | - if field_data: |
255 | | - current_row.append([fld_label, field_data[-1][1]]) |
256 | | - else: |
257 | | - current_row.append([fld_label, "N/A"]) |
258 | | - else: |
259 | | - current_row.append([fld_label, "N/A"]) |
260 | | - staging[group_name][leaf_node].add(tuple(tuple(pair) for pair in current_row)) |
| 320 | + field_data = history.get(map_key) |
| 321 | + value = field_data[-1][1] if field_data else "N/A" |
| 322 | + row.append((fld_label, value)) # use tuple directly |
261 | 323 |
|
| 324 | + staging[group_name][leaf_node].add(tuple(row)) |
| 325 | + |
| 326 | + |
262 | 327 | logger.info( |
263 | 328 | "build_global_territory_owners: processed %s records, skipped %s records", |
264 | 329 | processed_count, |
@@ -302,32 +367,32 @@ def drilldown_values_by_owner_v2(drilldown: str, periods: Iterable[str]): |
302 | 367 | return output |
303 | 368 |
|
304 | 369 |
|
305 | | -def deals_results_by_period(periods: Iterable[str]) -> Dict[str, Dict[str, Any]]: |
306 | | - """ |
307 | | - Lightweight helper to fetch deal results for the requested periods. |
308 | | - """ |
309 | | - results: Dict[str, Dict[str, Any]] = {} |
310 | | - if sec_context and getattr(sec_context, "gbm", None): |
311 | | - gbm_client = sec_context.gbm |
312 | | - for period in periods: |
313 | | - try: |
314 | | - url = f"/gbm/deals_results?period={period}" |
315 | | - results[period] = gbm_client.api(url, None) or {"results": {}} |
316 | | - except Exception as exc: |
317 | | - logger.exception("Failed to fetch deals results for %s", period, exc_info=exc) |
318 | | - results[period] = {"results": {}} |
319 | | - else: |
320 | | - logger.warning("GBM client not configured; returning empty results for periods %s", list(periods)) |
321 | | - for period in periods: |
322 | | - results[period] = {"results": {}} |
323 | | - return results |
324 | | - |
325 | | - |
326 | | -def drilldown_values_by_period(periods: Iterable[str], groups: Iterable[Iterable[str]]): |
| 370 | +# def deals_results_by_period(periods: Iterable[str]) -> Dict[str, Dict[str, Any]]: |
| 371 | +# """ |
| 372 | +# Lightweight helper to fetch deal results for the requested periods. |
| 373 | +# """ |
| 374 | +# results: Dict[str, Dict[str, Any]] = {} |
| 375 | +# if sec_context and getattr(sec_context, "gbm", None): |
| 376 | +# gbm_client = sec_context.gbm |
| 377 | +# for period in periods: |
| 378 | +# try: |
| 379 | +# url = f"/gbm/deals_results?period={period}" |
| 380 | +# results[period] = gbm_client.api(url, None) or {"results": {}} |
| 381 | +# except Exception as exc: |
| 382 | +# logger.exception("Failed to fetch deals results for %s", period, exc_info=exc) |
| 383 | +# results[period] = {"results": {}} |
| 384 | +# else: |
| 385 | +# logger.warning("GBM client not configured; returning empty results for periods %s", list(periods)) |
| 386 | +# for period in periods: |
| 387 | +# results[period] = {"results": {}} |
| 388 | +# return results |
| 389 | + |
| 390 | + |
| 391 | +def drilldown_values_by_period(periods, groups,): |
327 | 392 | """ |
328 | 393 | Given a set of drilldown groups, return all possible combos of those field values. |
329 | 394 | """ |
330 | | - output: Dict[str, Dict[str, List[List[tuple]]]] = {} |
| 395 | + output = {} |
331 | 396 | curr_period = current_period().mnemonic |
332 | 397 |
|
333 | 398 | for period in periods: |
|
0 commit comments