Projekt

Obecné

Profil

Stáhnout (15.4 KB) Statistiky
| Větev: | Tag: | Revize:
1 99c92c11 Matej Zeman
from datetime import datetime, date
2 aba60b70 Matej Zeman
3 99c92c11 Matej Zeman
from sqlalchemy.orm import Session
4 b6f0e019 Matej Zeman
from sqlalchemy import and_, desc
5 aba60b70 Matej Zeman
from . import models, schemas
6
7
8
def get_device(db: Session, device_id: int):
9 4911f0ea Matej Zeman
    """
10
    returns one specific devices by given id
11
    """
12 aba60b70 Matej Zeman
    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 4911f0ea Matej Zeman
    """
17
    returns all devices in database
18
    """
19 aba60b70 Matej Zeman
    return db.query(models.Device).offset(skip).limit(limit).all()
20
21
22
def find_device(db: Session, device: schemas.DeviceBase):
23 4911f0ea Matej Zeman
    """
24
    finds one device with product_id, vendor_id and serial_number same as in given DeviceBase object
25
    """
26 99c92c11 Matej Zeman
    return db.query(models.Device).filter(and_(models.Device.product_id == device.product_id,
27 b6f0e019 Matej Zeman
                                               models.Device.vendor_id == device.vendor_id,
28
                                               models.Device.serial_number == device.serial_number)).first()
29 aba60b70 Matej Zeman
30
31
def create_device(db: Session, device: schemas.DeviceBase):
32 4911f0ea Matej Zeman
    """
33
    creates new device with data from given DeviceBase object
34
    """
35 aba60b70 Matej Zeman
    db_device = models.Device(vendor_id=device.vendor_id, product_id=device.product_id,
36 99c92c11 Matej Zeman
                              serial_number=device.serial_number, assigned=False)
37 aba60b70 Matej Zeman
    db.add(db_device)
38
    db.commit()
39
    db.refresh(db_device)
40
    return db_device
41
42
43 6c1e92e3 Matej Zeman
def get_license(db: Session, license_id: int):
44 4911f0ea Matej Zeman
    """
45
    returns one specific license by given id
46
    """
47 6c1e92e3 Matej Zeman
    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 4911f0ea Matej Zeman
    """
52
    returns all licenses in database
53
    """
54 6c1e92e3 Matej Zeman
    return db.query(models.License).offset(skip).limit(limit).all()
55
56
57 b6f0e019 Matej Zeman
def find_license(db: Session, name: str):
58 4911f0ea Matej Zeman
    """
59
    finds one license by given string name
60
    """
61 b6f0e019 Matej Zeman
    return db.query(models.License).filter(models.License.name == name).first()
62
63
64 99c92c11 Matej Zeman
def create_license(db: Session, name: str, expdate: date):
65 4911f0ea Matej Zeman
    """
66
    creates new license with given name and expiration date
67
    """
68 99c92c11 Matej Zeman
    db_license = models.License(name=name, expiration_date=expdate)
69 6c1e92e3 Matej Zeman
    db.add(db_license)
70
    db.commit()
71
    db.refresh(db_license)
72
    return db_license
73
74
75 b6f0e019 Matej Zeman
def get_license_devices(db: Session, license_id: int):
76 4911f0ea Matej Zeman
    """
77 7fe7be79 zemanm98@students.zcu.cz
    returns all entries in devices_licenses table with given license_id
78 4911f0ea Matej Zeman
    """
79 b6f0e019 Matej Zeman
    return db.query(models.DeviceLicense).filter(models.DeviceLicense.license_id == license_id).all()
80
81
82 7fe7be79 zemanm98@students.zcu.cz
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 b6f0e019 Matej Zeman
def create_device_license(db: Session, device: int, license: int, time: datetime):
120 4911f0ea Matej Zeman
    """
121
    creates new entry in devices_licenses table with device id, license id and time.
122
    """
123 b6f0e019 Matej Zeman
    db_device_license = models.DeviceLicense(device_id=device, license_id=license,
124
                                             assigned_datetime=time)
125 6c1e92e3 Matej Zeman
    db.add(db_device_license)
126
    db.commit()
127
    db.refresh(db_device_license)
128
    return db_device_license
129
130
131 7fe7be79 zemanm98@students.zcu.cz
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 b6f0e019 Matej Zeman
def find_pc_by_username(db: Session, name: str):
164 4911f0ea Matej Zeman
    """
165
    Finds one pc by given username
166
    """
167 b6f0e019 Matej Zeman
    return db.query(models.PC).filter(models.PC.username == name).first()
168
169
170 6c1e92e3 Matej Zeman
def get_pc(db: Session, pc_id: int):
171 4911f0ea Matej Zeman
    """
172
    returns one specific pc by given id
173
    """
174 6c1e92e3 Matej Zeman
    return db.query(models.PC).filter(models.PC.id == pc_id).first()
175
176
177 b6f0e019 Matej Zeman
def update_pc(db: Session, pc_id: int, team: str):
178 4911f0ea Matej Zeman
    """
179 7fe7be79 zemanm98@students.zcu.cz
    Updates team of one specific pc
180 4911f0ea Matej Zeman
    """
181 b6f0e019 Matej Zeman
    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 6c1e92e3 Matej Zeman
def get_pcs(db: Session, skip: int = 0, limit: int = 100):
193 4911f0ea Matej Zeman
    """
194
    returns all pcs in database
195
    """
196 6c1e92e3 Matej Zeman
    return db.query(models.PC).offset(skip).limit(limit).all()
197
198
199
def find_pc(db: Session, username: str, hostname: str):
200 4911f0ea Matej Zeman
    """
201
    Finds one pc with given username and hostname
202
    """
203 99c92c11 Matej Zeman
    return db.query(models.PC).filter(and_(models.PC.username == username,
204 b6f0e019 Matej Zeman
                                           models.PC.hostname == hostname)).first()
205
206
207
def find_pc_by_name(db: Session, username: str):
208 4911f0ea Matej Zeman
    """
209
    Finds one pc by its username
210
    """
211 b6f0e019 Matej Zeman
    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 4911f0ea Matej Zeman
    """
216
    Finds all pcs with same username
217
    """
218 b6f0e019 Matej Zeman
    return db.query(models.PC).filter(models.PC.username == username).offset(0).limit(100).all()
219 99c92c11 Matej Zeman
220
221
def find_pcs(db: Session, pcs: []):
222 4911f0ea Matej Zeman
    """
223
    Finds all pcs with ids in given id array
224
    """
225 99c92c11 Matej Zeman
    return db.query(models.PC).filter(models.PC.id.in_(pcs)).all()
226 6c1e92e3 Matej Zeman
227
228 b6f0e019 Matej Zeman
def get_pcs_by_team(db: Session, team_id: int):
229 4911f0ea Matej Zeman
    """
230
    returns all pcs in given team by team id
231
    """
232 b6f0e019 Matej Zeman
    return db.query(models.PC).filter(models.PC.team_id == team_id).all()
233
234
235 6c1e92e3 Matej Zeman
def create_pc(db: Session, user: str, host: str):
236 4911f0ea Matej Zeman
    """
237
    creates new pc with given username and hostname
238
    """
239 99c92c11 Matej Zeman
    db_pc = models.PC(username=user, hostname=host, assigned=False)
240 6c1e92e3 Matej Zeman
    db.add(db_pc)
241
    db.commit()
242
    db.refresh(db_pc)
243
    return db_pc
244
245
246 99c92c11 Matej Zeman
def get_team(db: Session, team_id: int):
247 4911f0ea Matej Zeman
    """
248
    returns one specific team wit given id
249
    """
250 99c92c11 Matej Zeman
    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 4911f0ea Matej Zeman
    """
255
    returns all teams currently saved in database
256
    """
257 99c92c11 Matej Zeman
    return db.query(models.Team).offset(skip).limit(limit).all()
258
259
260 b6f0e019 Matej Zeman
def find_team(db: Session, name: str):
261 4911f0ea Matej Zeman
    """
262
    Finds one specific team by its name
263
    """
264 b6f0e019 Matej Zeman
    return db.query(models.Team).filter(models.Team.name == name).first()
265
266
267 99c92c11 Matej Zeman
def create_team(db: Session, name: str):
268 4911f0ea Matej Zeman
    """
269
    Creates new team with given name
270
    """
271 b6f0e019 Matej Zeman
    db_team = models.Team(name=name)
272 99c92c11 Matej Zeman
    db.add(db_team)
273
    db.commit()
274
    db.refresh(db_team)
275
    return db_team
276
277
278 eaf8ace4 Matej Zeman
def get_head_device(db: Session, head_id: int):
279 4911f0ea Matej Zeman
    """
280
    Returns one specific head device by given id
281
    """
282 eaf8ace4 Matej Zeman
    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 4911f0ea Matej Zeman
    """
287
    Returns all head devices saved in database
288
    """
289 eaf8ace4 Matej Zeman
    return db.query(models.HeadDevice).offset(skip).limit(limit).all()
290
291
292
def find_head_device(db: Session, serial: schemas.HeadDeviceBase):
293 4911f0ea Matej Zeman
    """
294
    Finds one head device by its serial number
295
    """
296 eaf8ace4 Matej Zeman
    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 4911f0ea Matej Zeman
    """
301
    Creates new head device
302
    """
303 eaf8ace4 Matej Zeman
    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 4911f0ea Matej Zeman
    """
312
    Returns one specific body device by given id
313
    """
314 eaf8ace4 Matej Zeman
    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 4911f0ea Matej Zeman
    """
319
    Returns all body devices saved in database
320
    """
321 eaf8ace4 Matej Zeman
    return db.query(models.BodyDevice).offset(skip).limit(limit).all()
322
323
324
def find_body_device(db: Session, serial: schemas.BodyDeviceBase):
325 4911f0ea Matej Zeman
    """
326
    Finds one body device by its serial number
327
    """
328 eaf8ace4 Matej Zeman
    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 4911f0ea Matej Zeman
    """
333
    Creates new Body device
334
    """
335 eaf8ace4 Matej Zeman
    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 4911f0ea Matej Zeman
    """
344
    Returns all ld debugger logs in database
345
    """
346 7fe7be79 zemanm98@students.zcu.cz
    return db.query(models.LDLog).order_by(desc(models.LDLog.timestamp)).offset(skip).limit(limit).all()
347 eaf8ace4 Matej Zeman
348
349
def create_ld_logs(db: Session, item: schemas.LDTempBase, head_id: int, body_id: int, pc_id: int, date: datetime):
350 4911f0ea Matej Zeman
    """
351
    Creates new ld log for ld_logs database table
352
    """
353 eaf8ace4 Matej Zeman
    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 aba60b70 Matej Zeman
def get_logs(db: Session, skip: int = 0, limit: int = 100):
361 4911f0ea Matej Zeman
    """
362
    Returns all usb logs in database ordered by timestamp
363
    """
364 b6f0e019 Matej Zeman
    return db.query(models.USBLog).order_by(desc(models.USBLog.timestamp)).offset(skip).limit(limit).all()
365 aba60b70 Matej Zeman
366
367
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100):
368 4911f0ea Matej Zeman
    """
369
    Returns all usb logs in database sorted by id
370
    """
371 aba60b70 Matej Zeman
    return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all()
372
373
374 b6f0e019 Matej Zeman
def find_filtered_logs(db: Session, logs: []):
375 4911f0ea Matej Zeman
    """
376
    Returns all usb logs with ids in given id array.
377
    """
378 b6f0e019 Matej Zeman
    return db.query(models.USBLog).filter(models.USBLog.id.in_(logs)).order_by(desc(models.USBLog.timestamp)).all()
379
380
381 7fe7be79 zemanm98@students.zcu.cz
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 b6f0e019 Matej Zeman
def get_filtered_logs(db: Session, pc: str, tema: str, lic: str):
433 4911f0ea Matej Zeman
    """
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 b6f0e019 Matej Zeman
    execute_string = "SELECT * FROM usb_logs AS logs"
438 7fe7be79 zemanm98@students.zcu.cz
    pcs = find_pc_by_username(db, pc)
439 b6f0e019 Matej Zeman
    if pc != "all":
440 7fe7be79 zemanm98@students.zcu.cz
        if pcs is not None:
441
            execute_string += "  WHERE logs.pc_id = " + str(pcs.id)
442 b6f0e019 Matej Zeman
    if tema != "all":
443
        team = find_team(db, tema)
444 7fe7be79 zemanm98@students.zcu.cz
        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 b6f0e019 Matej Zeman
    if lic != "all":
457
        license = find_license(db, lic)
458 7fe7be79 zemanm98@students.zcu.cz
        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 b6f0e019 Matej Zeman
471 4911f0ea Matej Zeman
    # executing assembled query string
472 b6f0e019 Matej Zeman
    result = db.execute(execute_string)
473
    return result
474
475
476 99c92c11 Matej Zeman
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int, date: datetime):
477 4911f0ea Matej Zeman
    """
478
    Creates new USB log for usb_logs database table
479
    """
480 99c92c11 Matej Zeman
    db_log = models.USBLog(pc_id=pc_id, timestamp=date, status=item.status, device_id=dev_id)
481 aba60b70 Matej Zeman
    db.add(db_log)
482
    db.commit()
483
    db.refresh(db_log)
484
    return db_log