-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_all_data.py
More file actions
155 lines (124 loc) · 5.14 KB
/
get_all_data.py
File metadata and controls
155 lines (124 loc) · 5.14 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
import logging
from obp_client import token, obp_host
from get_and_delete_dynamic_entities import get_all_objects_for_system_dynamic_entity
from dynamic_entities import (
ENTITY_PROJECT,
ENTITY_PARCEL,
ENTITY_PARCEL_OWNERSHIP_VERIFICATION,
ENTITY_PROJECT_PARCEL_VERIFICATION,
ENTITY_PROJECT_VERIFICATION,
ENTITY_PARCEL_MONITORING_PERIOD_VERIFICATION,
ENTITY_PROJECT_MONITORING_PERIOD_VERIFICATION,
get_list_key
)
import json
# Configure logging with better formatting
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s | %(levelname)-8s | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
# Configuration
BASE_URL = obp_host
DIRECTLOGIN_TOKEN = token
# All entities to retrieve
ENTITIES = [
ENTITY_PROJECT,
ENTITY_PARCEL,
ENTITY_PARCEL_OWNERSHIP_VERIFICATION,
ENTITY_PROJECT_PARCEL_VERIFICATION,
ENTITY_PROJECT_VERIFICATION,
ENTITY_PARCEL_MONITORING_PERIOD_VERIFICATION,
ENTITY_PROJECT_MONITORING_PERIOD_VERIFICATION
]
def print_separator(char="=", length=80):
"""Print a separator line for better visual separation"""
logger.info(char * length)
def format_json(data):
"""Format data as indented JSON string"""
return json.dumps(data, indent=2)
def display_record(record, entity_name, indent=" "):
"""Display a single record in a readable format"""
for key, value in record.items():
if isinstance(value, dict):
logger.info(f"{indent}{key}:")
for sub_key, sub_value in value.items():
logger.info(f"{indent} {sub_key}: {sub_value}")
elif isinstance(value, str) and len(value) > 60:
# Truncate long strings (like geo_data)
logger.info(f"{indent}{key}: {value[:60]}...")
else:
logger.info(f"{indent}{key}: {value}")
def main():
logger.info("Starting Get All Data Script")
print_separator()
all_data = {}
total_records = 0
# =========================================================================
# Retrieve data from all entities
# =========================================================================
logger.info("Retrieving data from all dynamic entities")
print_separator("-")
for idx, entity_name in enumerate(ENTITIES, 1):
logger.info(f"[{idx}/{len(ENTITIES)}] Fetching: {entity_name}")
try:
response = get_all_objects_for_system_dynamic_entity(
entity_name,
token=DIRECTLOGIN_TOKEN
)
# If response is None, entity doesn't exist (404) - skip it
if response is None:
logger.info(f" → Entity {entity_name} does not exist yet - skipping")
all_data[entity_name] = []
continue
# Handle case where the list key might not exist (no objects)
# The key format is: prefix_entityname_list (with underscores between each part)
# e.g., "ogcr3_project_list" for ogcr3_project
list_key = get_list_key(entity_name)
objects = response.get(list_key, [])
if not objects:
logger.info(f" → No records found for {entity_name}")
all_data[entity_name] = []
continue
logger.info(f" ✓ Found {len(objects)} record(s)")
all_data[entity_name] = objects
total_records += len(objects)
# Display summary of records
for record_idx, record in enumerate(objects, 1):
logger.info(f" Record #{record_idx}:")
display_record(record, entity_name)
logger.info("") # Empty line between records
except Exception as e:
logger.error(f" ✗ Error fetching {entity_name}: {e}")
all_data[entity_name] = []
continue
logger.info("") # Empty line between entities
print_separator("-")
logger.info(f"Total records retrieved: {total_records}")
print_separator()
# =========================================================================
# Summary by entity
# =========================================================================
logger.info("SUMMARY BY ENTITY")
print_separator("-")
for entity_name in ENTITIES:
count = len(all_data.get(entity_name, []))
logger.info(f" {entity_name:<30} {count:>3} record(s)")
print_separator()
# =========================================================================
# Optional: Save to JSON file
# =========================================================================
try:
output_file = "all_dynamic_entities_data.json"
with open(output_file, 'w') as f:
json.dump(all_data, f, indent=2)
logger.info(f"✓ Data saved to: {output_file}")
except Exception as e:
logger.error(f"✗ Failed to save data to file: {e}")
print_separator()
logger.info("Get All Data Script Completed")
print_separator("=")
return all_data
if __name__ == "__main__":
main()