Projekt

Obecné

Profil

Stáhnout (10.5 KB) Statistiky
| Větev: | Tag: | Revize:
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
78
    """
79
    return db.query(models.DeviceLicense).filter(models.DeviceLicense.license_id == license_id).all()
80

    
81

    
82
def create_device_license(db: Session, device: int, license: int, time: datetime):
83
    """
84
    creates new entry in devices_licenses table with device id, license id and time.
85
    """
86
    db_device_license = models.DeviceLicense(device_id=device, license_id=license,
87
                                             assigned_datetime=time)
88
    db.add(db_device_license)
89
    db.commit()
90
    db.refresh(db_device_license)
91
    return db_device_license
92

    
93

    
94
def find_pc_by_username(db: Session, name: str):
95
    """
96
    Finds one pc by given username
97
    """
98
    return db.query(models.PC).filter(models.PC.username == name).first()
99

    
100

    
101
def get_pc(db: Session, pc_id: int):
102
    """
103
    returns one specific pc by given id
104
    """
105
    return db.query(models.PC).filter(models.PC.id == pc_id).first()
106

    
107

    
108
def update_pc(db: Session, pc_id: int, team: str):
109
    """
110
    Function updates team of one specific pc
111
    """
112
    old_pc = get_pc(db, pc_id)
113
    team = get_team(db, int(team))
114
    new = {'id': old_pc.id, 'username': old_pc.username, 'hostname': old_pc.hostname, 'assigned': True,
115
           'team_id': team.id}
116
    for key, value in new.items():
117
        setattr(old_pc, key, value)
118
    db.commit()
119
    db.refresh(old_pc)
120
    return old_pc
121

    
122

    
123
def get_pcs(db: Session, skip: int = 0, limit: int = 100):
124
    """
125
    returns all pcs in database
126
    """
127
    return db.query(models.PC).offset(skip).limit(limit).all()
128

    
129

    
130
def find_pc(db: Session, username: str, hostname: str):
131
    """
132
    Finds one pc with given username and hostname
133
    """
134
    return db.query(models.PC).filter(and_(models.PC.username == username,
135
                                           models.PC.hostname == hostname)).first()
136

    
137

    
138
def find_pc_by_name(db: Session, username: str):
139
    """
140
    Finds one pc by its username
141
    """
142
    return db.query(models.PC).filter(models.PC.username == username).first()
143

    
144

    
145
def find_pc_by_name_all(db: Session, username: str):
146
    """
147
    Finds all pcs with same username
148
    """
149
    return db.query(models.PC).filter(models.PC.username == username).offset(0).limit(100).all()
150

    
151

    
152
def find_pcs(db: Session, pcs: []):
153
    """
154
    Finds all pcs with ids in given id array
155
    """
156
    return db.query(models.PC).filter(models.PC.id.in_(pcs)).all()
157

    
158

    
159
def get_pcs_by_team(db: Session, team_id: int):
160
    """
161
    returns all pcs in given team by team id
162
    """
163
    return db.query(models.PC).filter(models.PC.team_id == team_id).all()
164

    
165

    
166
def create_pc(db: Session, user: str, host: str):
167
    """
168
    creates new pc with given username and hostname
169
    """
170
    db_pc = models.PC(username=user, hostname=host, assigned=False)
171
    db.add(db_pc)
172
    db.commit()
173
    db.refresh(db_pc)
174
    return db_pc
175

    
176

    
177
def get_team(db: Session, team_id: int):
178
    """
179
    returns one specific team wit given id
180
    """
181
    return db.query(models.Team).filter(models.Team.id == team_id).first()
182

    
183

    
184
def get_teams(db: Session, skip: int = 0, limit: int = 100):
185
    """
186
    returns all teams currently saved in database
187
    """
188
    return db.query(models.Team).offset(skip).limit(limit).all()
189

    
190

    
191
def find_team(db: Session, name: str):
192
    """
193
    Finds one specific team by its name
194
    """
195
    return db.query(models.Team).filter(models.Team.name == name).first()
196

    
197

    
198
def create_team(db: Session, name: str):
199
    """
200
    Creates new team with given name
201
    """
202
    db_team = models.Team(name=name)
203
    db.add(db_team)
204
    db.commit()
205
    db.refresh(db_team)
206
    return db_team
207

    
208

    
209
def get_head_device(db: Session, head_id: int):
210
    """
211
    Returns one specific head device by given id
212
    """
213
    return db.query(models.HeadDevice).filter(models.HeadDevice.id == head_id).first()
214

    
215

    
216
def get_head_devices(db: Session, skip: int = 0, limit: int = 100):
217
    """
218
    Returns all head devices saved in database
219
    """
220
    return db.query(models.HeadDevice).offset(skip).limit(limit).all()
221

    
222

    
223
def find_head_device(db: Session, serial: schemas.HeadDeviceBase):
224
    """
225
    Finds one head device by its serial number
226
    """
227
    return db.query(models.HeadDevice).filter(models.HeadDevice.serial_number == serial.serial_number).first()
228

    
229

    
230
def create_head_device(db: Session, log: schemas.HeadDeviceBase):
231
    """
232
    Creates new head device
233
    """
234
    db_head = models.HeadDevice(serial_number=log.serial_number)
235
    db.add(db_head)
236
    db.commit()
237
    db.refresh(db_head)
238
    return db_head
239

    
240

    
241
def get_body_device(db: Session, body_id: int):
242
    """
243
    Returns one specific body device by given id
244
    """
245
    return db.query(models.BodyDevice).filter(models.BodyDevice.id == body_id).first()
246

    
247

    
248
def get_body_devices(db: Session, skip: int = 0, limit: int = 100):
249
    """
250
    Returns all body devices saved in database
251
    """
252
    return db.query(models.BodyDevice).offset(skip).limit(limit).all()
253

    
254

    
255
def find_body_device(db: Session, serial: schemas.BodyDeviceBase):
256
    """
257
    Finds one body device by its serial number
258
    """
259
    return db.query(models.BodyDevice).filter(models.BodyDevice.serial_number == serial.serial_number).first()
260

    
261

    
262
def create_body_device(db: Session, log: schemas.BodyDeviceBase):
263
    """
264
    Creates new Body device
265
    """
266
    db_body = models.BodyDevice(serial_number=log.serial_number)
267
    db.add(db_body)
268
    db.commit()
269
    db.refresh(db_body)
270
    return db_body
271

    
272

    
273
def get_ld_logs(db: Session, skip: int = 0, limit: int = 100):
274
    """
275
    Returns all ld debugger logs in database
276
    """
277
    return db.query(models.LDLog).offset(skip).limit(limit).all()
278

    
279

    
280
def create_ld_logs(db: Session, item: schemas.LDTempBase, head_id: int, body_id: int, pc_id: int, date: datetime):
281
    """
282
    Creates new ld log for ld_logs database table
283
    """
284
    db_ld = models.LDLog(pc_id=pc_id, timestamp=date, status=item.status, head_id=head_id, body_id=body_id)
285
    db.add(db_ld)
286
    db.commit()
287
    db.refresh(db_ld)
288
    return db_ld
289

    
290

    
291
def get_logs(db: Session, skip: int = 0, limit: int = 100):
292
    """
293
    Returns all usb logs in database ordered by timestamp
294
    """
295
    return db.query(models.USBLog).order_by(desc(models.USBLog.timestamp)).offset(skip).limit(limit).all()
296

    
297

    
298
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100):
299
    """
300
    Returns all usb logs in database sorted by id
301
    """
302
    return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all()
303

    
304

    
305
def find_filtered_logs(db: Session, logs: []):
306
    """
307
    Returns all usb logs with ids in given id array.
308
    """
309
    return db.query(models.USBLog).filter(models.USBLog.id.in_(logs)).order_by(desc(models.USBLog.timestamp)).all()
310

    
311

    
312
def get_filtered_logs(db: Session, pc: str, tema: str, lic: str):
313
    """
314
    Function creates query string used for filtering by pc username, team name and license name.
315
    Depending on selected filters assembles query string for database
316
    """
317
    execute_string = "SELECT * FROM usb_logs AS logs"
318
    if pc != "all":
319
        pcs = find_pc_by_username(db, pc)
320
        execute_string += "  WHERE logs.pc_id = " + str(pcs.id)
321
    if tema != "all":
322
        team = find_team(db, tema)
323
        pcs = get_pcs_by_team(db, team.id)
324
        pc_ids = "("
325
        for p in pcs:
326
            pc_ids += str(p.id) + ", "
327
        def_pc_ids = pc_ids[:-2] + ")"
328
        if pc != "all":
329
            execute_string += " AND logs.pc_id IN " + def_pc_ids
330
        else:
331
            execute_string += " WHERE logs.pc_id IN " + def_pc_ids
332
    if lic != "all":
333
        license = find_license(db, lic)
334
        device_licenses = get_license_devices(db, license.id)
335
        dev_ids = "("
336
        for dev in device_licenses:
337
            dev_ids += str(dev.device_id) + ", "
338
        defin_ids = dev_ids[:-2] + ")"
339
        if pc != "all" or tema != "all":
340
            execute_string += " AND logs.device_id IN " + defin_ids
341
        else:
342
            execute_string += " WHERE logs.device_id IN " + defin_ids
343

    
344
    # executing assembled query string
345
    result = db.execute(execute_string)
346
    return result
347

    
348

    
349
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int, date: datetime):
350
    """
351
    Creates new USB log for usb_logs database table
352
    """
353
    db_log = models.USBLog(pc_id=pc_id, timestamp=date, status=item.status, device_id=dev_id)
354
    db.add(db_log)
355
    db.commit()
356
    db.refresh(db_log)
357
    return db_log
(2-2/6)