|
7 | 7 | router = APIRouter(prefix='/box') |
8 | 8 |
|
9 | 9 | @router.get('/{id}') |
10 | | -def api_get_box(id: int, get_onu_level: bool = False, get_tasks: bool = False): |
| 10 | +def api_get_box( |
| 11 | + id: int, |
| 12 | + get_onu_level: bool = False, |
| 13 | + get_tasks: bool = False, |
| 14 | + limit: int | None = None, |
| 15 | + skip: int | None = None |
| 16 | +): |
11 | 17 | def _get_onu_level(name) -> float | None: |
12 | | - if extract_sn(name) is None: return |
13 | | - res = api_call('device', 'get_ont_data', f'id={extract_sn(name)}') |
14 | | - if not isinstance(res.get('data'), dict): return |
15 | | - return res['data'].get('level_onu_rx') |
16 | | - |
17 | | - house = api_call('address', 'get_house', f'building_id={id}').get('data') |
18 | | - if house: |
19 | | - house = list(house.values())[0] |
20 | | - customers_id = api_call('customer', 'get_customers_id', f'house_id={id}')['data'] |
21 | | - customers = [{ |
| 18 | + if extract_sn(name) is None: |
| 19 | + return |
| 20 | + data = api_call('device', 'get_ont_data', f'id={extract_sn(name)}').get('data') |
| 21 | + if not isinstance(data, dict): |
| 22 | + return |
| 23 | + return data.get('level_onu_rx') |
| 24 | + |
| 25 | + def _get_tasks(entity: str, entity_id: int) -> list[int]: |
| 26 | + res = api_call('task', 'get_list', f'{entity}_id={entity_id}&state_id=18,3,17,11,1,16,19') |
| 27 | + return list(map(int, str_to_list(res.get('list', '')))) |
| 28 | + |
| 29 | + def _build_customer(customer: dict) -> dict | None: |
| 30 | + name = customer.get('full_name') |
| 31 | + if name is None: |
| 32 | + return None |
| 33 | + return { |
22 | 34 | 'id': customer['id'], |
23 | | - 'name': remove_sn(customer['full_name']), |
| 35 | + 'name': remove_sn(name), |
24 | 36 | 'last_activity': customer.get('date_activity'), |
25 | 37 | 'status': status_to_str(customer['state_id']), |
26 | | - 'sn': extract_sn(customer['full_name']), |
27 | | - 'onu_level': _get_onu_level(customer['full_name']) if get_onu_level else None, |
28 | | - 'tasks': list(map(int, str_to_list( |
29 | | - api_call('task', 'get_list', f'customer_id={customer["id"]}&state_id=18,3,17,11,1,16,19')['list'] |
30 | | - ))) if get_tasks else None |
31 | | - } for customer in normalize_items(api_call('customer', 'get_data', |
32 | | - f'id={list_to_str(customers_id)}')) if customer['full_name'] is not None] |
33 | | - |
34 | | - onu_levels = [ |
35 | | - customer['onu_level'] |
36 | | - for customer in customers |
37 | | - if customer['onu_level'] |
38 | | - ] |
39 | | - |
40 | | - return { |
41 | | - 'status': 'success', |
42 | | - 'id': id, |
43 | | - 'address_id': house['id'], |
44 | | - 'name': house['full_name'], |
45 | | - 'average_onu_level': sum(onu_levels) / len(onu_levels) if onu_levels else None, |
46 | | - 'tasks': list(map(int, str_to_list( |
47 | | - api_call('task', 'get_list', f'house_id={id}&state_id=18,3,17,11,1,16,19')['list'] |
48 | | - ))) if get_tasks else None, |
49 | | - 'manager_id': house.get('manage_employee_id'), |
50 | | - 'coords': get_coordinates(house['coordinates']), |
51 | | - 'active': not house.get('is_not_use', True), |
52 | | - 'map_link': get_box_map_link(get_coordinates(house['coordinates']), id), |
53 | | - 'customers': customers |
| 38 | + 'sn': extract_sn(name), |
| 39 | + 'onu_level': _get_onu_level(name) if get_onu_level else None, |
| 40 | + 'tasks': _get_tasks('customer', customer['id']) if get_tasks else None |
54 | 41 | } |
55 | | - return JSONResponse({ |
56 | | - 'status': 'fail', |
57 | | - 'detail': 'box not found' |
58 | | - }, 404) |
| 42 | + |
| 43 | + house_data = api_call('address', 'get_house', f'building_id={id}').get('data') |
| 44 | + if not house_data: |
| 45 | + return JSONResponse({'status': 'fail', 'detail': 'box not found'}, 404) |
| 46 | + |
| 47 | + house = list(house_data.values())[0] |
| 48 | + customer_ids = api_call('customer', 'get_customers_id', f'house_id={id}').get('data', []) |
| 49 | + customers_count = len(customer_ids) |
| 50 | + |
| 51 | + customers = [] |
| 52 | + if customer_ids: |
| 53 | + if skip: |
| 54 | + customer_ids = customer_ids[skip:] |
| 55 | + if limit: |
| 56 | + customer_ids = customer_ids[:limit] |
| 57 | + raw_customers = normalize_items( |
| 58 | + api_call('customer', 'get_data', f'id={list_to_str(customer_ids)}') |
| 59 | + ) |
| 60 | + customers = [c for c in map(_build_customer, raw_customers) if c is not None] |
| 61 | + |
| 62 | + onu_levels = [c['onu_level'] for c in customers if c['onu_level']] |
| 63 | + avg_onu_level = sum(onu_levels) / len(onu_levels) if onu_levels else None |
| 64 | + |
| 65 | + coords = get_coordinates(house['coordinates']) if house.get('coordinates') else None |
| 66 | + |
| 67 | + return { |
| 68 | + 'status': 'success', |
| 69 | + 'id': id, |
| 70 | + 'address_id': house['id'], |
| 71 | + 'name': house['full_name'], |
| 72 | + 'average_onu_level': avg_onu_level, |
| 73 | + 'tasks': _get_tasks('house', id) if get_tasks else None, |
| 74 | + 'manager_id': house.get('manage_employee_id'), |
| 75 | + 'coords': coords, |
| 76 | + 'active': not house.get('is_not_use', True), |
| 77 | + 'map_link': get_box_map_link(coords, id) if coords else None, |
| 78 | + 'customers': customers, |
| 79 | + 'customers_count': customers_count, |
| 80 | + 'customers_limit': customers_count > limit + (skip or 0) if limit else False |
| 81 | + } |
0 commit comments