-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_sample_data.py
More file actions
225 lines (188 loc) Β· 8.19 KB
/
import_sample_data.py
File metadata and controls
225 lines (188 loc) Β· 8.19 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
#!/usr/bin/env python3
"""
ResQTrack Data Import Script
Imports sample datasets into the ResQTrack database
"""
import os
import sys
import argparse
from pathlib import Path
# Add the backend directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
from backend.app import create_app
from backend.app.extensions import db
from backend.app.data_integration import DatasetImporter
from backend.app.models import NGO, Volunteer, Hospital, PoliceStation, BloodBank, FireStation, EmergencyContact
def import_sample_data():
"""Import all sample datasets"""
app = create_app()
with app.app_context():
importer = DatasetImporter()
# Get the sample data directory
sample_dir = Path(__file__).parent / 'sample_data'
print("π Starting ResQTrack Data Import...")
print("=" * 50)
# Import NGOs
ngo_file = sample_dir / 'ngos.csv'
if ngo_file.exists():
print(f"π Importing NGOs from {ngo_file.name}...")
result = importer.import_ngos_csv(str(ngo_file))
print(f" β
Successful: {result['successful']}")
print(f" β Failed: {result['failed']}")
print(f" β οΈ Skipped: {result['skipped']}")
if result['errors']:
print(f" π Errors: {len(result['errors'])}")
else:
print(f"β οΈ NGO file not found: {ngo_file}")
print()
# Import Volunteers
volunteer_file = sample_dir / 'volunteers.csv'
if volunteer_file.exists():
print(f"π₯ Importing Volunteers from {volunteer_file.name}...")
result = importer.import_volunteers_csv(str(volunteer_file))
print(f" β
Successful: {result['successful']}")
print(f" β Failed: {result['failed']}")
print(f" β οΈ Skipped: {result['skipped']}")
if result['errors']:
print(f" π Errors: {len(result['errors'])}")
else:
print(f"β οΈ Volunteer file not found: {volunteer_file}")
print()
# Import Hospitals
hospital_file = sample_dir / 'hospitals.csv'
if hospital_file.exists():
print(f"π₯ Importing Hospitals from {hospital_file.name}...")
result = importer.import_hospitals_csv(str(hospital_file))
print(f" β
Successful: {result['successful']}")
print(f" β Failed: {result['failed']}")
print(f" β οΈ Skipped: {result['skipped']}")
if result['errors']:
print(f" π Errors: {len(result['errors'])}")
else:
print(f"β οΈ Hospital file not found: {hospital_file}")
print()
# Import Police Stations
police_file = sample_dir / 'police_stations.csv'
if police_file.exists():
print(f"π Importing Police Stations from {police_file.name}...")
result = importer.import_police_stations_csv(str(police_file))
print(f" β
Successful: {result['successful']}")
print(f" β Failed: {result['failed']}")
print(f" β οΈ Skipped: {result['skipped']}")
if result['errors']:
print(f" π Errors: {len(result['errors'])}")
else:
print(f"β οΈ Police Station file not found: {police_file}")
print()
# Import Blood Banks
blood_bank_file = sample_dir / 'blood_banks.csv'
if blood_bank_file.exists():
print(f"π©Έ Importing Blood Banks from {blood_bank_file.name}...")
result = importer.import_blood_banks_csv(str(blood_bank_file))
print(f" β
Successful: {result['successful']}")
print(f" β Failed: {result['failed']}")
print(f" β οΈ Skipped: {result['skipped']}")
if result['errors']:
print(f" π Errors: {len(result['errors'])}")
else:
print(f"β οΈ Blood Bank file not found: {blood_bank_file}")
print()
# Import Fire Stations
fire_station_file = sample_dir / 'fire_stations.csv'
if fire_station_file.exists():
print(f"π Importing Fire Stations from {fire_station_file.name}...")
result = importer.import_fire_stations_csv(str(fire_station_file))
print(f" β
Successful: {result['successful']}")
print(f" β Failed: {result['failed']}")
print(f" β οΈ Skipped: {result['skipped']}")
if result['errors']:
print(f" π Errors: {len(result['errors'])}")
else:
print(f"β οΈ Fire Station file not found: {fire_station_file}")
print()
# Add some emergency contacts
print("π Adding Emergency Contacts...")
emergency_contacts = [
{
'name': 'National Emergency Number',
'phone': '100',
'service_type': 'Police',
'location': 'All India',
'is_24x7': True,
'description': 'National police emergency number',
'priority_level': 1
},
{
'name': 'Fire Emergency Number',
'phone': '101',
'service_type': 'Fire',
'location': 'All India',
'is_24x7': True,
'description': 'National fire emergency number',
'priority_level': 1
},
{
'name': 'Medical Emergency Number',
'phone': '108',
'service_type': 'Medical',
'location': 'All India',
'is_24x7': True,
'description': 'National medical emergency number',
'priority_level': 1
},
{
'name': 'Disaster Management Helpline',
'phone': '108',
'service_type': 'Disaster',
'location': 'All India',
'is_24x7': True,
'description': 'National disaster management helpline',
'priority_level': 1
}
]
for contact_data in emergency_contacts:
contact = EmergencyContact(**contact_data)
db.session.add(contact)
db.session.commit()
print(f" β
Added {len(emergency_contacts)} emergency contacts")
print()
print("=" * 50)
print("π Data import completed successfully!")
print()
# Show final statistics
print("π Final Statistics:")
print(f" NGOs: {NGO.query.count()}")
print(f" Volunteers: {Volunteer.query.count()}")
print(f" Hospitals: {Hospital.query.count()}")
print(f" Police Stations: {PoliceStation.query.count()}")
print(f" Blood Banks: {BloodBank.query.count()}")
print(f" Fire Stations: {FireStation.query.count()}")
print(f" Emergency Contacts: {EmergencyContact.query.count()}")
def clear_all_data():
"""Clear all data from the database"""
app = create_app()
with app.app_context():
print("ποΈ Clearing all data from ResQTrack database...")
# Delete all records
EmergencyContact.query.delete()
FireStation.query.delete()
BloodBank.query.delete()
PoliceStation.query.delete()
Hospital.query.delete()
Volunteer.query.delete()
NGO.query.delete()
db.session.commit()
print("β
All data cleared successfully!")
def main():
"""Main function"""
parser = argparse.ArgumentParser(description='ResQTrack Data Import Tool')
parser.add_argument('--clear', action='store_true', help='Clear all existing data before import')
parser.add_argument('--import-only', action='store_true', help='Only import data, do not clear existing data')
args = parser.parse_args()
if args.clear:
clear_all_data()
print()
if not args.clear or args.import_only:
import_sample_data()
if __name__ == '__main__':
main()