-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_database.py
More file actions
303 lines (260 loc) · 10.5 KB
/
create_database.py
File metadata and controls
303 lines (260 loc) · 10.5 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""
Create SQLite database from NYC Chromebook CSV data
Generates both DeviceHub and Intune mock data from the CSV
"""
import sqlite3
import csv
import json
import random
from datetime import datetime, timedelta
import os
# Configuration
CSV_FILE = "NYC_Chromebook_ASN_Import_350K.csv"
DB_FILE = "doe_devices.db"
def create_database():
"""Create SQLite database with proper schema"""
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Drop existing tables
cursor.execute("DROP TABLE IF EXISTS devices")
cursor.execute("DROP TABLE IF EXISTS intune_devices")
cursor.execute("DROP TABLE IF EXISTS devicehub_assets")
# Create main devices table from CSV
cursor.execute('''
CREATE TABLE devices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_name TEXT,
po_number TEXT,
purchase_order_number TEXT,
vendor TEXT,
manufacturer TEXT,
model_id TEXT,
serial TEXT UNIQUE NOT NULL,
asset_tag TEXT,
quantity INTEGER,
ship_date TEXT,
carrier TEXT,
tracking_number TEXT,
ship_to_address_street TEXT,
ship_to_address_city TEXT,
ship_to_address_state TEXT,
ship_to_address_zip TEXT,
order_date TEXT,
svc_contract_end_date TEXT
)
''')
# Create Intune devices table (mock Intune API data)
cursor.execute('''
CREATE TABLE intune_devices (
id TEXT PRIMARY KEY,
serial_number TEXT UNIQUE NOT NULL,
device_name TEXT,
manufacturer TEXT,
model TEXT,
operating_system TEXT,
os_version TEXT,
compliance_state TEXT,
management_state TEXT,
enrolled_date_time TEXT,
last_sync_date_time TEXT,
wifi_mac_address TEXT,
physical_memory_bytes INTEGER,
total_storage_bytes INTEGER,
user_principal_name TEXT,
device_category TEXT,
notes TEXT,
FOREIGN KEY (serial_number) REFERENCES devices (serial)
)
''')
# Create DeviceHub assets table (mock DeviceHub API data)
cursor.execute('''
CREATE TABLE devicehub_assets (
asset_id TEXT PRIMARY KEY,
serial_number TEXT UNIQUE NOT NULL,
asset_tag TEXT,
manufacturer TEXT,
model TEXT,
category TEXT,
status TEXT,
location_building TEXT,
assigned_to_name TEXT,
assigned_to_department TEXT,
purchase_price REAL,
purchase_date TEXT,
warranty_expiration TEXT,
FOREIGN KEY (serial_number) REFERENCES devices (serial)
)
''')
# Create indices for fast lookups
cursor.execute("CREATE INDEX idx_devices_serial ON devices(serial)")
cursor.execute("CREATE INDEX idx_intune_serial ON intune_devices(serial_number)")
cursor.execute("CREATE INDEX idx_devicehub_serial ON devicehub_assets(serial_number)")
conn.commit()
return conn
def import_csv_data(conn):
"""Import CSV data into devices table"""
cursor = conn.cursor()
print(f"Reading CSV file: {CSV_FILE}")
with open(CSV_FILE, 'r', newline='', encoding='utf-8') as file:
reader = csv.DictReader(file)
devices_data = []
for row in reader:
devices_data.append((
row['Account name'],
row['PO number'],
row['Purchase order number'],
row['Vendor'],
row['Manufacturer'],
row['Model id'],
row['Serial'],
row['Asset tag'],
int(row['Quantity']) if row['Quantity'] else 1,
row['Ship date'],
row['Carrier'],
row['Tracking number'],
row['Ship to address street'],
row['Ship to address city'],
row['Ship to address state'],
row['Ship to address zip'],
row['Order date'],
row['SVC contract end date']
))
cursor.executemany('''
INSERT OR IGNORE INTO devices (
account_name, po_number, purchase_order_number, vendor,
manufacturer, model_id, serial, asset_tag, quantity,
ship_date, carrier, tracking_number, ship_to_address_street,
ship_to_address_city, ship_to_address_state, ship_to_address_zip,
order_date, svc_contract_end_date
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', devices_data)
print(f"Imported {len(devices_data)} devices from CSV")
conn.commit()
def generate_mock_intune_data(conn):
"""Generate mock Intune device data for each device"""
cursor = conn.cursor()
# Get all devices from CSV
cursor.execute("SELECT serial, asset_tag, manufacturer, model_id FROM devices")
devices = cursor.fetchall()
compliance_states = ['compliant', 'noncompliant', 'conflict', 'error', 'inGracePeriod', 'configManager']
management_states = ['managed', 'retirePending', 'retireFailed', 'wipePending', 'wipeFailed', 'unhealthy']
intune_data = []
for i, (serial, asset_tag, manufacturer, model_id) in enumerate(devices):
# Generate mock Intune device data
device_id = f"intune-{serial.lower().replace('nyc-sn', '')}"
device_name = f"NYC-{asset_tag}" if asset_tag else f"NYC-Device-{i+1}"
# Random dates
enrolled = datetime.now() - timedelta(days=random.randint(30, 365))
last_sync = datetime.now() - timedelta(hours=random.randint(1, 48))
# Generate MAC address
mac = ':'.join([f"{random.randint(0, 255):02X}" for _ in range(6)])
intune_data.append((
device_id,
serial,
device_name,
manufacturer or 'Google',
model_id or 'CC6PF NRD5N',
'ChromeOS',
'11.0.22000.1455',
random.choice(compliance_states),
random.choice(management_states),
enrolled.isoformat() + 'Z',
last_sync.isoformat() + 'Z',
mac,
4 * 1024 * 1024 * 1024, # 4GB RAM
64 * 1000 * 1000 * 1000, # 64GB storage
f"user{i+1}@nycdoe.edu",
'Chromebook',
f"Chromebook from PO: {asset_tag}"
))
cursor.executemany('''
INSERT OR IGNORE INTO intune_devices (
id, serial_number, device_name, manufacturer, model,
operating_system, os_version, compliance_state, management_state,
enrolled_date_time, last_sync_date_time, wifi_mac_address,
physical_memory_bytes, total_storage_bytes, user_principal_name,
device_category, notes
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', intune_data)
print(f"Generated {len(intune_data)} Intune device records")
conn.commit()
def generate_mock_devicehub_data(conn):
"""Generate mock DeviceHub asset data for each device"""
cursor = conn.cursor()
# Get all devices from CSV
cursor.execute("SELECT serial, asset_tag, manufacturer, model_id, svc_contract_end_date FROM devices")
devices = cursor.fetchall()
statuses = ['In Stock', 'In Transit', 'Deployed', 'Pending Transfer', 'Available']
locations = ['HS 789', 'MS 456', 'ES 123', 'NYC Department of Education HQ', 'District Office']
departments = ['Technology', 'Education', 'Administration']
devicehub_data = []
for i, (serial, asset_tag, manufacturer, model_id, warranty_end) in enumerate(devices):
# Generate mock DeviceHub asset data
asset_id = f"DH-{asset_tag}" if asset_tag else f"DH-Asset-{i+1}"
# Generate user assignment
assigned_names = [
'Taylor Rodriguez', 'Jordan Smith', 'Alex Johnson', 'Morgan Davis',
'Casey Wilson', 'Riley Brown', 'Avery Jones', 'Parker Miller'
]
devicehub_data.append((
asset_id,
serial,
asset_tag,
manufacturer or 'Google',
model_id or 'CC6PF NRD5N',
'Chromebook',
random.choice(statuses),
random.choice(locations),
random.choice(assigned_names),
random.choice(departments),
random.uniform(300, 600), # Purchase price
'', # Purchase date (empty like in original)
warranty_end or '2029-09-03'
))
cursor.executemany('''
INSERT OR IGNORE INTO devicehub_assets (
asset_id, serial_number, asset_tag, manufacturer, model,
category, status, location_building, assigned_to_name,
assigned_to_department, purchase_price, purchase_date, warranty_expiration
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', devicehub_data)
print(f"Generated {len(devicehub_data)} DeviceHub asset records")
conn.commit()
def main():
"""Main function to create database and import data"""
print("Creating SQLite database for DOE devices...")
# Remove existing database
if os.path.exists(DB_FILE):
os.remove(DB_FILE)
print(f"Removed existing database: {DB_FILE}")
# Create database and tables
conn = create_database()
print("Created database schema")
# Import data
import_csv_data(conn)
generate_mock_intune_data(conn)
generate_mock_devicehub_data(conn)
# Verify data
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM devices")
device_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM intune_devices")
intune_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM devicehub_assets")
devicehub_count = cursor.fetchone()[0]
print(f"\n✅ Database created successfully!")
print(f"📊 Total devices: {device_count}")
print(f"📱 Intune records: {intune_count}")
print(f"🏢 DeviceHub records: {devicehub_count}")
print(f"💾 Database file: {DB_FILE}")
# Test a specific serial number
cursor.execute("SELECT serial FROM devices WHERE serial = 'NYC-SN65467'")
test_device = cursor.fetchone()
if test_device:
print(f"✅ Test serial NYC-SN65467 found in database")
else:
print(f"❌ Test serial NYC-SN65467 not found - check CSV data")
conn.close()
if __name__ == "__main__":
main()