1
|
from datetime import datetime, date
|
2
|
|
3
|
from sqlalchemy.orm import Session
|
4
|
from sqlalchemy import and_, desc
|
5
|
from . import models, schemas
|
6
|
|
7
|
|
8
|
def get_device(db: Session, device_id: int):
|
9
|
"""
|
10
|
returns one specific devices by given id
|
11
|
"""
|
12
|
return db.query(models.Device).filter(models.Device.id == device_id).first()
|
13
|
|
14
|
|
15
|
def get_devices(db: Session, skip: int = 0, limit: int = 100):
|
16
|
"""
|
17
|
returns all devices in database
|
18
|
"""
|
19
|
return db.query(models.Device).offset(skip).limit(limit).all()
|
20
|
|
21
|
|
22
|
def find_device(db: Session, device: schemas.DeviceBase):
|
23
|
"""
|
24
|
finds one device with product_id, vendor_id and serial_number same as in given DeviceBase object
|
25
|
"""
|
26
|
return db.query(models.Device).filter(and_(models.Device.product_id == device.product_id,
|
27
|
models.Device.vendor_id == device.vendor_id,
|
28
|
models.Device.serial_number == device.serial_number)).first()
|
29
|
|
30
|
|
31
|
def create_device(db: Session, device: schemas.DeviceBase):
|
32
|
"""
|
33
|
creates new device with data from given DeviceBase object
|
34
|
"""
|
35
|
db_device = models.Device(vendor_id=device.vendor_id, product_id=device.product_id,
|
36
|
serial_number=device.serial_number, assigned=False)
|
37
|
db.add(db_device)
|
38
|
db.commit()
|
39
|
db.refresh(db_device)
|
40
|
return db_device
|
41
|
|
42
|
|
43
|
def get_license(db: Session, license_id: int):
|
44
|
"""
|
45
|
returns one specific license by given id
|
46
|
"""
|
47
|
return db.query(models.License).filter(models.License.id == license_id).first()
|
48
|
|
49
|
|
50
|
def get_licenses(db: Session, skip: int = 0, limit: int = 100):
|
51
|
"""
|
52
|
returns all licenses in database
|
53
|
"""
|
54
|
return db.query(models.License).offset(skip).limit(limit).all()
|
55
|
|
56
|
|
57
|
def find_license(db: Session, name: str):
|
58
|
"""
|
59
|
finds one license by given string name
|
60
|
"""
|
61
|
return db.query(models.License).filter(models.License.name == name).first()
|
62
|
|
63
|
|
64
|
def create_license(db: Session, name: str, expdate: date):
|
65
|
"""
|
66
|
creates new license with given name and expiration date
|
67
|
"""
|
68
|
db_license = models.License(name=name, expiration_date=expdate)
|
69
|
db.add(db_license)
|
70
|
db.commit()
|
71
|
db.refresh(db_license)
|
72
|
return db_license
|
73
|
|
74
|
|
75
|
def get_license_devices(db: Session, license_id: int):
|
76
|
"""
|
77
|
returns all entries in devices_licenses table with given license_id
|
78
|
"""
|
79
|
return db.query(models.DeviceLicense).filter(models.DeviceLicense.license_id == license_id).all()
|
80
|
|
81
|
|
82
|
def get_device_licenses(db: Session, device_id: int):
|
83
|
"""
|
84
|
returns all entries in devices_licenses table with given license_id
|
85
|
"""
|
86
|
return db.query(models.DeviceLicense).filter(models.DeviceLicense.device_id == device_id).all()
|
87
|
|
88
|
|
89
|
def get_devicelicense_by_devicelicense(db: Session, device_id: int, license_id: int):
|
90
|
"""
|
91
|
returns entry in devices_licenses table with given device id and license id
|
92
|
"""
|
93
|
return db.query(models.DeviceLicense).filter(and_(models.DeviceLicense.device_id == device_id,
|
94
|
models.DeviceLicense.license_id == license_id)).first()
|
95
|
|
96
|
|
97
|
def get_bodydevicelicense_by_bodydevicelicense(db: Session, device_id: int, license_id: int):
|
98
|
"""
|
99
|
returns entry in bodydevices_licenses table with given body device id and license id
|
100
|
"""
|
101
|
return db.query(models.BodyDeviceLicense).filter(and_(models.BodyDeviceLicense.bodydevice_id == device_id,
|
102
|
models.BodyDeviceLicense.license_id == license_id)).first()
|
103
|
|
104
|
|
105
|
def get_license_bodydevice(db: Session, license_id: int):
|
106
|
"""
|
107
|
returns all entries in bodydevices_licenses with given license_id
|
108
|
"""
|
109
|
return db.query(models.BodyDeviceLicense).filter(models.BodyDeviceLicense.license_id == license_id).all()
|
110
|
|
111
|
|
112
|
def get_bodydevice_license(db: Session, device_id: int):
|
113
|
"""
|
114
|
returns all entries in bodydevices_licenses with given license_id
|
115
|
"""
|
116
|
return db.query(models.BodyDeviceLicense).filter(models.BodyDeviceLicense.bodydevice_id == device_id).all()
|
117
|
|
118
|
|
119
|
def create_device_license(db: Session, device: int, license: int, time: datetime):
|
120
|
"""
|
121
|
creates new entry in devices_licenses table with device id, license id and time.
|
122
|
"""
|
123
|
db_device_license = models.DeviceLicense(device_id=device, license_id=license,
|
124
|
assigned_datetime=time)
|
125
|
db.add(db_device_license)
|
126
|
db.commit()
|
127
|
db.refresh(db_device_license)
|
128
|
return db_device_license
|
129
|
|
130
|
|
131
|
def delete_device_license(db: Session, device: int, license: int):
|
132
|
"""
|
133
|
deletes entry in devices_licenses table with device id, license id and time.
|
134
|
"""
|
135
|
db_device_license = get_devicelicense_by_devicelicense(db, device, license)
|
136
|
db_lic = db.delete(db_device_license)
|
137
|
db.commit()
|
138
|
return db_lic
|
139
|
|
140
|
|
141
|
def delete_bodydevice_license(db: Session, device: int, license: int):
|
142
|
"""
|
143
|
deletes entry in devices_licenses table with device id, license id and time.
|
144
|
"""
|
145
|
db_device_license = get_bodydevicelicense_by_bodydevicelicense(db, device, license)
|
146
|
db_lic = db.delete(db_device_license)
|
147
|
db.commit()
|
148
|
return db_lic
|
149
|
|
150
|
|
151
|
def create_body_device_license(db: Session, device: int, license: int, time: datetime):
|
152
|
"""
|
153
|
creates new entry in devices_licenses table with device id, license id and time.
|
154
|
"""
|
155
|
db_device_license = models.BodyDeviceLicense(bodydevice_id=device, license_id=license,
|
156
|
assigned_datetime=time)
|
157
|
db.add(db_device_license)
|
158
|
db.commit()
|
159
|
db.refresh(db_device_license)
|
160
|
return db_device_license
|
161
|
|
162
|
|
163
|
def find_pc_by_username(db: Session, name: str):
|
164
|
"""
|
165
|
Finds one pc by given username
|
166
|
"""
|
167
|
return db.query(models.PC).filter(models.PC.username == name).first()
|
168
|
|
169
|
|
170
|
def get_pc(db: Session, pc_id: int):
|
171
|
"""
|
172
|
returns one specific pc by given id
|
173
|
"""
|
174
|
return db.query(models.PC).filter(models.PC.id == pc_id).first()
|
175
|
|
176
|
|
177
|
def update_pc(db: Session, pc_id: int, team: str):
|
178
|
"""
|
179
|
Updates team of one specific pc
|
180
|
"""
|
181
|
old_pc = get_pc(db, pc_id)
|
182
|
team = get_team(db, int(team))
|
183
|
new = {'id': old_pc.id, 'username': old_pc.username, 'hostname': old_pc.hostname, 'assigned': True,
|
184
|
'team_id': team.id}
|
185
|
for key, value in new.items():
|
186
|
setattr(old_pc, key, value)
|
187
|
db.commit()
|
188
|
db.refresh(old_pc)
|
189
|
return old_pc
|
190
|
|
191
|
|
192
|
def get_pcs(db: Session, skip: int = 0, limit: int = 100):
|
193
|
"""
|
194
|
returns all pcs in database
|
195
|
"""
|
196
|
return db.query(models.PC).offset(skip).limit(limit).all()
|
197
|
|
198
|
|
199
|
def find_pc(db: Session, username: str, hostname: str):
|
200
|
"""
|
201
|
Finds one pc with given username and hostname
|
202
|
"""
|
203
|
return db.query(models.PC).filter(and_(models.PC.username == username,
|
204
|
models.PC.hostname == hostname)).first()
|
205
|
|
206
|
|
207
|
def find_pc_by_name(db: Session, username: str):
|
208
|
"""
|
209
|
Finds one pc by its username
|
210
|
"""
|
211
|
return db.query(models.PC).filter(models.PC.username == username).first()
|
212
|
|
213
|
|
214
|
def find_pc_by_name_all(db: Session, username: str):
|
215
|
"""
|
216
|
Finds all pcs with same username
|
217
|
"""
|
218
|
return db.query(models.PC).filter(models.PC.username == username).offset(0).limit(100).all()
|
219
|
|
220
|
|
221
|
def find_pcs(db: Session, pcs: []):
|
222
|
"""
|
223
|
Finds all pcs with ids in given id array
|
224
|
"""
|
225
|
return db.query(models.PC).filter(models.PC.id.in_(pcs)).all()
|
226
|
|
227
|
|
228
|
def get_pcs_by_team(db: Session, team_id: int):
|
229
|
"""
|
230
|
returns all pcs in given team by team id
|
231
|
"""
|
232
|
return db.query(models.PC).filter(models.PC.team_id == team_id).all()
|
233
|
|
234
|
|
235
|
def create_pc(db: Session, user: str, host: str):
|
236
|
"""
|
237
|
creates new pc with given username and hostname
|
238
|
"""
|
239
|
db_pc = models.PC(username=user, hostname=host, assigned=False)
|
240
|
db.add(db_pc)
|
241
|
db.commit()
|
242
|
db.refresh(db_pc)
|
243
|
return db_pc
|
244
|
|
245
|
|
246
|
def get_team(db: Session, team_id: int):
|
247
|
"""
|
248
|
returns one specific team wit given id
|
249
|
"""
|
250
|
return db.query(models.Team).filter(models.Team.id == team_id).first()
|
251
|
|
252
|
|
253
|
def get_teams(db: Session, skip: int = 0, limit: int = 100):
|
254
|
"""
|
255
|
returns all teams currently saved in database
|
256
|
"""
|
257
|
return db.query(models.Team).offset(skip).limit(limit).all()
|
258
|
|
259
|
|
260
|
def find_team(db: Session, name: str):
|
261
|
"""
|
262
|
Finds one specific team by its name
|
263
|
"""
|
264
|
return db.query(models.Team).filter(models.Team.name == name).first()
|
265
|
|
266
|
|
267
|
def create_team(db: Session, name: str):
|
268
|
"""
|
269
|
Creates new team with given name
|
270
|
"""
|
271
|
db_team = models.Team(name=name)
|
272
|
db.add(db_team)
|
273
|
db.commit()
|
274
|
db.refresh(db_team)
|
275
|
return db_team
|
276
|
|
277
|
|
278
|
def get_head_device(db: Session, head_id: int):
|
279
|
"""
|
280
|
Returns one specific head device by given id
|
281
|
"""
|
282
|
return db.query(models.HeadDevice).filter(models.HeadDevice.id == head_id).first()
|
283
|
|
284
|
|
285
|
def get_head_devices(db: Session, skip: int = 0, limit: int = 100):
|
286
|
"""
|
287
|
Returns all head devices saved in database
|
288
|
"""
|
289
|
return db.query(models.HeadDevice).offset(skip).limit(limit).all()
|
290
|
|
291
|
|
292
|
def find_head_device(db: Session, serial: schemas.HeadDeviceBase):
|
293
|
"""
|
294
|
Finds one head device by its serial number
|
295
|
"""
|
296
|
return db.query(models.HeadDevice).filter(models.HeadDevice.serial_number == serial.serial_number).first()
|
297
|
|
298
|
|
299
|
def create_head_device(db: Session, log: schemas.HeadDeviceBase):
|
300
|
"""
|
301
|
Creates new head device
|
302
|
"""
|
303
|
db_head = models.HeadDevice(serial_number=log.serial_number)
|
304
|
db.add(db_head)
|
305
|
db.commit()
|
306
|
db.refresh(db_head)
|
307
|
return db_head
|
308
|
|
309
|
|
310
|
def get_body_device(db: Session, body_id: int):
|
311
|
"""
|
312
|
Returns one specific body device by given id
|
313
|
"""
|
314
|
return db.query(models.BodyDevice).filter(models.BodyDevice.id == body_id).first()
|
315
|
|
316
|
|
317
|
def get_body_devices(db: Session, skip: int = 0, limit: int = 100):
|
318
|
"""
|
319
|
Returns all body devices saved in database
|
320
|
"""
|
321
|
return db.query(models.BodyDevice).offset(skip).limit(limit).all()
|
322
|
|
323
|
|
324
|
def find_body_device(db: Session, serial: schemas.BodyDeviceBase):
|
325
|
"""
|
326
|
Finds one body device by its serial number
|
327
|
"""
|
328
|
return db.query(models.BodyDevice).filter(models.BodyDevice.serial_number == serial.serial_number).first()
|
329
|
|
330
|
|
331
|
def create_body_device(db: Session, log: schemas.BodyDeviceBase):
|
332
|
"""
|
333
|
Creates new Body device
|
334
|
"""
|
335
|
db_body = models.BodyDevice(serial_number=log.serial_number)
|
336
|
db.add(db_body)
|
337
|
db.commit()
|
338
|
db.refresh(db_body)
|
339
|
return db_body
|
340
|
|
341
|
|
342
|
def get_ld_logs(db: Session, skip: int = 0, limit: int = 100):
|
343
|
"""
|
344
|
Returns all ld debugger logs in database
|
345
|
"""
|
346
|
return db.query(models.LDLog).order_by(desc(models.LDLog.timestamp)).offset(skip).limit(limit).all()
|
347
|
|
348
|
|
349
|
def create_ld_logs(db: Session, item: schemas.LDTempBase, head_id: int, body_id: int, pc_id: int, date: datetime):
|
350
|
"""
|
351
|
Creates new ld log for ld_logs database table
|
352
|
"""
|
353
|
db_ld = models.LDLog(pc_id=pc_id, timestamp=date, status=item.status, head_id=head_id, body_id=body_id)
|
354
|
db.add(db_ld)
|
355
|
db.commit()
|
356
|
db.refresh(db_ld)
|
357
|
return db_ld
|
358
|
|
359
|
|
360
|
def get_logs(db: Session, skip: int = 0, limit: int = 100):
|
361
|
"""
|
362
|
Returns all usb logs in database ordered by timestamp
|
363
|
"""
|
364
|
return db.query(models.USBLog).order_by(desc(models.USBLog.timestamp)).offset(skip).limit(limit).all()
|
365
|
|
366
|
|
367
|
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100):
|
368
|
"""
|
369
|
Returns all usb logs in database sorted by id
|
370
|
"""
|
371
|
return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all()
|
372
|
|
373
|
|
374
|
def find_filtered_logs(db: Session, logs: []):
|
375
|
"""
|
376
|
Returns all usb logs with ids in given id array.
|
377
|
"""
|
378
|
return db.query(models.USBLog).filter(models.USBLog.id.in_(logs)).order_by(desc(models.USBLog.timestamp)).all()
|
379
|
|
380
|
|
381
|
def find_filtered_ldlogs(db: Session, logs: []):
|
382
|
"""
|
383
|
Returns all ld logs with ids in given id array.
|
384
|
"""
|
385
|
return db.query(models.LDLog).filter(models.LDLog.id.in_(logs)).order_by(desc(models.LDLog.timestamp)).all()
|
386
|
|
387
|
|
388
|
def get_filtered_ldlogs(db: Session, pc: str, tema: str, lic: str):
|
389
|
"""
|
390
|
Function creates query string used for filtering by pc username, team name and license name.
|
391
|
Depending on selected filters assembles query string for database
|
392
|
"""
|
393
|
execute_string = "SELECT * FROM ld_logs AS logs"
|
394
|
pcs = find_pc_by_username(db, pc)
|
395
|
if pc != "all":
|
396
|
if pcs is not None:
|
397
|
execute_string += " WHERE logs.pc_id = " + str(pcs.id)
|
398
|
if tema != "all":
|
399
|
team = find_team(db, tema)
|
400
|
if team is not None:
|
401
|
pcst = get_pcs_by_team(db, team.id)
|
402
|
pc_ids = "("
|
403
|
for p in pcst:
|
404
|
pc_ids += str(p.id) + ", "
|
405
|
def_pc_ids = pc_ids[:-2] + ")"
|
406
|
if pc != "all" and pcs is not None:
|
407
|
if len(def_pc_ids) > 1:
|
408
|
execute_string += " AND logs.pc_id IN " + def_pc_ids
|
409
|
else:
|
410
|
if len(def_pc_ids) > 1:
|
411
|
execute_string += " WHERE logs.pc_id IN " + def_pc_ids
|
412
|
if lic != "all":
|
413
|
license = find_license(db, lic)
|
414
|
if license is not None:
|
415
|
device_licenses = get_license_bodydevice(db, license.id)
|
416
|
dev_ids = "("
|
417
|
for dev in device_licenses:
|
418
|
dev_ids += str(dev.bodydevice_id) + ", "
|
419
|
defin_ids = dev_ids[:-2] + ")"
|
420
|
if pc != "all" or tema != "all":
|
421
|
if len(defin_ids) > 1:
|
422
|
execute_string += " AND logs.body_id IN " + defin_ids
|
423
|
else:
|
424
|
if len(defin_ids) > 1:
|
425
|
execute_string += " WHERE logs.body_id IN " + defin_ids
|
426
|
|
427
|
# executing assembled query string
|
428
|
result = db.execute(execute_string)
|
429
|
return result
|
430
|
|
431
|
|
432
|
def get_filtered_logs(db: Session, pc: str, tema: str, lic: str):
|
433
|
"""
|
434
|
Function creates query string used for filtering by pc username, team name and license name.
|
435
|
Depending on selected filters assembles query string for database
|
436
|
"""
|
437
|
execute_string = "SELECT * FROM usb_logs AS logs"
|
438
|
pcs = find_pc_by_username(db, pc)
|
439
|
if pc != "all":
|
440
|
if pcs is not None:
|
441
|
execute_string += " WHERE logs.pc_id = " + str(pcs.id)
|
442
|
if tema != "all":
|
443
|
team = find_team(db, tema)
|
444
|
if team is not None:
|
445
|
pcst = get_pcs_by_team(db, team.id)
|
446
|
pc_ids = "("
|
447
|
for p in pcst:
|
448
|
pc_ids += str(p.id) + ", "
|
449
|
def_pc_ids = pc_ids[:-2] + ")"
|
450
|
if pc != "all" and pcs is not None:
|
451
|
if len(def_pc_ids) > 1:
|
452
|
execute_string += " AND logs.pc_id IN " + def_pc_ids
|
453
|
else:
|
454
|
if len(def_pc_ids) > 1:
|
455
|
execute_string += " WHERE logs.pc_id IN " + def_pc_ids
|
456
|
if lic != "all":
|
457
|
license = find_license(db, lic)
|
458
|
if license is not None:
|
459
|
device_licenses = get_license_devices(db, license.id)
|
460
|
dev_ids = "("
|
461
|
for dev in device_licenses:
|
462
|
dev_ids += str(dev.device_id) + ", "
|
463
|
defin_ids = dev_ids[:-2] + ")"
|
464
|
if pc != "all" or tema != "all":
|
465
|
if len(defin_ids) > 1:
|
466
|
execute_string += " AND logs.device_id IN " + defin_ids
|
467
|
else:
|
468
|
if len(defin_ids) > 1:
|
469
|
execute_string += " WHERE logs.device_id IN " + defin_ids
|
470
|
|
471
|
# executing assembled query string
|
472
|
result = db.execute(execute_string)
|
473
|
return result
|
474
|
|
475
|
|
476
|
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int, date: datetime):
|
477
|
"""
|
478
|
Creates new USB log for usb_logs database table
|
479
|
"""
|
480
|
db_log = models.USBLog(pc_id=pc_id, timestamp=date, status=item.status, device_id=dev_id)
|
481
|
db.add(db_log)
|
482
|
db.commit()
|
483
|
db.refresh(db_log)
|
484
|
return db_log
|