-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_data.py
More file actions
279 lines (272 loc) · 10.2 KB
/
init_data.py
File metadata and controls
279 lines (272 loc) · 10.2 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
#!/usr/bin/env python3
"""
Initialization script for Automotive Maintenance System
Creates sample data and initializes the database
"""
from datetime import datetime, timedelta
from sqlalchemy.orm import Session
from database import SessionLocal, create_tables, User, Vehicle, ServiceRecord, Feedback
from auth import get_password_hash
import random
def create_sample_users():
"""Create sample users for testing"""
db = SessionLocal()
try:
if db.query(User).first():
print("Sample users already exist, skipping...")
return
users_data = [
{
"username": "demo_user",
"email": "demo@example.com",
"password": "demo123",
"full_name": "Demo User",
"phone": "+91-9876543210"
},
{
"username": "john_doe",
"email": "john@example.com",
"password": "password123",
"full_name": "John Doe",
"phone": "+91-9876543211"
},
{
"username": "jane_smith",
"email": "jane@example.com",
"password": "password123",
"full_name": "Jane Smith",
"phone": "+91-9876543212"
}
]
for user_data in users_data:
user = User(
username=user_data["username"],
email=user_data["email"],
hashed_password=get_password_hash(user_data["password"]),
full_name=user_data["full_name"],
phone=user_data["phone"]
)
db.add(user)
db.commit()
print("✅ Sample users created successfully!")
except Exception as e:
print(f"❌ Error creating users: {e}")
db.rollback()
finally:
db.close()
def create_sample_vehicles():
"""Create sample vehicles for testing"""
db = SessionLocal()
try:
if db.query(Vehicle).first():
print("Sample vehicles already exist, skipping...")
return
users = db.query(User).all()
if not users:
print("No users found, creating vehicles without owners...")
return
vehicles_data = [
{
"vehicle_id": "MH12AB1234",
"model": "XUV700",
"brand": "Mahindra",
"year": 2022,
"odometer": 28000,
"vehicle_age": 2,
"service_count": 3,
"last_service_cost": 8500
},
{
"vehicle_id": "MH14XY9988",
"model": "Splendor",
"brand": "Hero",
"year": 2021,
"odometer": 15000,
"vehicle_age": 3,
"service_count": 2,
"last_service_cost": 3200
},
{
"vehicle_id": "MH01CD5678",
"model": "Scorpio",
"brand": "Mahindra",
"year": 2020,
"odometer": 45000,
"vehicle_age": 4,
"service_count": 5,
"last_service_cost": 12000
},
{
"vehicle_id": "MH02EF9012",
"model": "Passion",
"brand": "Hero",
"year": 2023,
"odometer": 8000,
"vehicle_age": 1,
"service_count": 1,
"last_service_cost": 2800
},
{
"vehicle_id": "MH03GH3456",
"model": "XUV300",
"brand": "Mahindra",
"year": 2021,
"odometer": 22000,
"vehicle_age": 3,
"service_count": 4,
"last_service_cost": 7500
}
]
for i, vehicle_data in enumerate(vehicles_data):
vehicle = Vehicle(
vehicle_id=vehicle_data["vehicle_id"],
model=vehicle_data["model"],
brand=vehicle_data["brand"],
year=vehicle_data["year"],
odometer=vehicle_data["odometer"],
vehicle_age=vehicle_data["vehicle_age"],
service_count=vehicle_data["service_count"],
last_service_cost=vehicle_data["last_service_cost"],
owner_id=users[i % len(users)].id
)
db.add(vehicle)
db.commit()
print("✅ Sample vehicles created successfully!")
except Exception as e:
print(f"❌ Error creating vehicles: {e}")
db.rollback()
finally:
db.close()
def create_sample_service_records():
"""Create sample service records"""
db = SessionLocal()
try:
if db.query(ServiceRecord).first():
print("Sample service records already exist, skipping...")
return
vehicles = db.query(Vehicle).all()
if not vehicles:
print("No vehicles found, skipping service records...")
return
service_centers = [
"Hero Service Center - Mumbai",
"Mahindra Service Center - Delhi",
"Hero Service Center - Bangalore",
"Mahindra Service Center - Chennai"
]
service_types = ["routine", "emergency", "predictive"]
for vehicle in vehicles:
num_records = random.randint(2, 4)
for i in range(num_records):
service_date = datetime.now() - timedelta(days=random.randint(30, 365))
next_service = service_date + timedelta(days=random.randint(90, 180))
service_record = ServiceRecord(
vehicle_id=vehicle.vehicle_id,
service_date=service_date,
service_type=random.choice(service_types),
service_center=random.choice(service_centers),
cost=random.randint(2000, 15000),
odometer_at_service=vehicle.odometer - random.randint(1000, 5000),
issues_found=f"Sample issue {i+1} for {vehicle.model}",
work_performed=f"Routine maintenance and inspection",
parts_replaced=f"Oil filter, air filter",
next_service_due=next_service,
technician_notes=f"Vehicle in good condition, regular maintenance completed"
)
db.add(service_record)
db.commit()
print("✅ Sample service records created successfully!")
except Exception as e:
print(f"❌ Error creating service records: {e}")
db.rollback()
finally:
db.close()
def create_sample_feedback():
"""Create sample feedback records"""
db = SessionLocal()
try:
if db.query(Feedback).first():
print("Sample feedback already exist, skipping...")
return
vehicles = db.query(Vehicle).all()
service_records = db.query(ServiceRecord).all()
if not vehicles or not service_records:
print("No vehicles or service records found, skipping feedback...")
return
issues = [
"Engine noise",
"Brake squeaking",
"Transmission issues",
"Electrical problems",
"Suspension noise",
"Air conditioning not working",
"Battery issues",
"Tire wear"
]
root_causes = [
"Worn out components",
"Lack of regular maintenance",
"Manufacturing defect",
"Normal wear and tear",
"Environmental factors"
]
actions = [
"Component replacement",
"Regular maintenance scheduled",
"Warranty claim processed",
"Adjustment made",
"Cleaning and lubrication"
]
for vehicle in vehicles:
num_feedback = random.randint(1, 3)
for i in range(num_feedback):
service_record = random.choice(service_records)
if not service_record:
continue # Skip if no service record
feedback = Feedback(
vehicle_id=vehicle.vehicle_id,
service_record_id=service_record.id,
issue=random.choice(issues),
root_cause=random.choice(root_causes),
action=random.choice(actions),
rating=random.randint(3, 5),
comments="Service was satisfactory, issue resolved promptly"
)
db.add(feedback)
db.commit()
print("✅ Sample feedback created successfully!")
except Exception as e:
print(f"❌ Error creating feedback: {e}")
db.rollback()
finally:
db.close()
def main():
"""Main initialization function"""
print("🚀 Initializing Automotive Maintenance System...")
print("=" * 50)
print("📊 Creating database tables...")
create_tables()
print("✅ Database tables created!")
print("\n👥 Creating sample users...")
create_sample_users()
print("\n🚗 Creating sample vehicles...")
create_sample_vehicles()
print("\n🔧 Creating sample service records...")
create_sample_service_records()
print("\n💬 Creating sample feedback...")
create_sample_feedback()
print("\n" + "=" * 50)
print("🎉 Initialization completed successfully!")
print("\n📋 Sample Data Created:")
print(" • 3 Users (demo_user, john_doe, jane_smith)")
print(" • 5 Vehicles (various brands and models)")
print(" • Service records for each vehicle")
print(" • Feedback records")
print("\n🔑 Default Login Credentials:")
print(" Username: demo_user")
print(" Password: demo123")
print("\n🌐 Start the application:")
print(" Backend: python main.py")
print(" Frontend: npm run dev")
if __name__ == "__main__":
main()