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 |
|
|
returns all entries in devices_licenses table
|
78 |
|
|
"""
|
79 |
b6f0e019
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
84 |
|
|
creates new entry in devices_licenses table with device id, license id and time.
|
85 |
|
|
"""
|
86 |
b6f0e019
|
Matej Zeman
|
db_device_license = models.DeviceLicense(device_id=device, license_id=license,
|
87 |
|
|
assigned_datetime=time)
|
88 |
6c1e92e3
|
Matej Zeman
|
db.add(db_device_license)
|
89 |
|
|
db.commit()
|
90 |
|
|
db.refresh(db_device_license)
|
91 |
|
|
return db_device_license
|
92 |
|
|
|
93 |
|
|
|
94 |
b6f0e019
|
Matej Zeman
|
def find_pc_by_username(db: Session, name: str):
|
95 |
4911f0ea
|
Matej Zeman
|
"""
|
96 |
|
|
Finds one pc by given username
|
97 |
|
|
"""
|
98 |
b6f0e019
|
Matej Zeman
|
return db.query(models.PC).filter(models.PC.username == name).first()
|
99 |
|
|
|
100 |
|
|
|
101 |
6c1e92e3
|
Matej Zeman
|
def get_pc(db: Session, pc_id: int):
|
102 |
4911f0ea
|
Matej Zeman
|
"""
|
103 |
|
|
returns one specific pc by given id
|
104 |
|
|
"""
|
105 |
6c1e92e3
|
Matej Zeman
|
return db.query(models.PC).filter(models.PC.id == pc_id).first()
|
106 |
|
|
|
107 |
|
|
|
108 |
b6f0e019
|
Matej Zeman
|
def update_pc(db: Session, pc_id: int, team: str):
|
109 |
4911f0ea
|
Matej Zeman
|
"""
|
110 |
|
|
Function updates team of one specific pc
|
111 |
|
|
"""
|
112 |
b6f0e019
|
Matej Zeman
|
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 |
6c1e92e3
|
Matej Zeman
|
def get_pcs(db: Session, skip: int = 0, limit: int = 100):
|
124 |
4911f0ea
|
Matej Zeman
|
"""
|
125 |
|
|
returns all pcs in database
|
126 |
|
|
"""
|
127 |
6c1e92e3
|
Matej Zeman
|
return db.query(models.PC).offset(skip).limit(limit).all()
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
def find_pc(db: Session, username: str, hostname: str):
|
131 |
4911f0ea
|
Matej Zeman
|
"""
|
132 |
|
|
Finds one pc with given username and hostname
|
133 |
|
|
"""
|
134 |
99c92c11
|
Matej Zeman
|
return db.query(models.PC).filter(and_(models.PC.username == username,
|
135 |
b6f0e019
|
Matej Zeman
|
models.PC.hostname == hostname)).first()
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
def find_pc_by_name(db: Session, username: str):
|
139 |
4911f0ea
|
Matej Zeman
|
"""
|
140 |
|
|
Finds one pc by its username
|
141 |
|
|
"""
|
142 |
b6f0e019
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
147 |
|
|
Finds all pcs with same username
|
148 |
|
|
"""
|
149 |
b6f0e019
|
Matej Zeman
|
return db.query(models.PC).filter(models.PC.username == username).offset(0).limit(100).all()
|
150 |
99c92c11
|
Matej Zeman
|
|
151 |
|
|
|
152 |
|
|
def find_pcs(db: Session, pcs: []):
|
153 |
4911f0ea
|
Matej Zeman
|
"""
|
154 |
|
|
Finds all pcs with ids in given id array
|
155 |
|
|
"""
|
156 |
99c92c11
|
Matej Zeman
|
return db.query(models.PC).filter(models.PC.id.in_(pcs)).all()
|
157 |
6c1e92e3
|
Matej Zeman
|
|
158 |
|
|
|
159 |
b6f0e019
|
Matej Zeman
|
def get_pcs_by_team(db: Session, team_id: int):
|
160 |
4911f0ea
|
Matej Zeman
|
"""
|
161 |
|
|
returns all pcs in given team by team id
|
162 |
|
|
"""
|
163 |
b6f0e019
|
Matej Zeman
|
return db.query(models.PC).filter(models.PC.team_id == team_id).all()
|
164 |
|
|
|
165 |
|
|
|
166 |
6c1e92e3
|
Matej Zeman
|
def create_pc(db: Session, user: str, host: str):
|
167 |
4911f0ea
|
Matej Zeman
|
"""
|
168 |
|
|
creates new pc with given username and hostname
|
169 |
|
|
"""
|
170 |
99c92c11
|
Matej Zeman
|
db_pc = models.PC(username=user, hostname=host, assigned=False)
|
171 |
6c1e92e3
|
Matej Zeman
|
db.add(db_pc)
|
172 |
|
|
db.commit()
|
173 |
|
|
db.refresh(db_pc)
|
174 |
|
|
return db_pc
|
175 |
|
|
|
176 |
|
|
|
177 |
99c92c11
|
Matej Zeman
|
def get_team(db: Session, team_id: int):
|
178 |
4911f0ea
|
Matej Zeman
|
"""
|
179 |
|
|
returns one specific team wit given id
|
180 |
|
|
"""
|
181 |
99c92c11
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
186 |
|
|
returns all teams currently saved in database
|
187 |
|
|
"""
|
188 |
99c92c11
|
Matej Zeman
|
return db.query(models.Team).offset(skip).limit(limit).all()
|
189 |
|
|
|
190 |
|
|
|
191 |
b6f0e019
|
Matej Zeman
|
def find_team(db: Session, name: str):
|
192 |
4911f0ea
|
Matej Zeman
|
"""
|
193 |
|
|
Finds one specific team by its name
|
194 |
|
|
"""
|
195 |
b6f0e019
|
Matej Zeman
|
return db.query(models.Team).filter(models.Team.name == name).first()
|
196 |
|
|
|
197 |
|
|
|
198 |
99c92c11
|
Matej Zeman
|
def create_team(db: Session, name: str):
|
199 |
4911f0ea
|
Matej Zeman
|
"""
|
200 |
|
|
Creates new team with given name
|
201 |
|
|
"""
|
202 |
b6f0e019
|
Matej Zeman
|
db_team = models.Team(name=name)
|
203 |
99c92c11
|
Matej Zeman
|
db.add(db_team)
|
204 |
|
|
db.commit()
|
205 |
|
|
db.refresh(db_team)
|
206 |
|
|
return db_team
|
207 |
|
|
|
208 |
|
|
|
209 |
eaf8ace4
|
Matej Zeman
|
def get_head_device(db: Session, head_id: int):
|
210 |
4911f0ea
|
Matej Zeman
|
"""
|
211 |
|
|
Returns one specific head device by given id
|
212 |
|
|
"""
|
213 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
218 |
|
|
Returns all head devices saved in database
|
219 |
|
|
"""
|
220 |
eaf8ace4
|
Matej Zeman
|
return db.query(models.HeadDevice).offset(skip).limit(limit).all()
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
def find_head_device(db: Session, serial: schemas.HeadDeviceBase):
|
224 |
4911f0ea
|
Matej Zeman
|
"""
|
225 |
|
|
Finds one head device by its serial number
|
226 |
|
|
"""
|
227 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
232 |
|
|
Creates new head device
|
233 |
|
|
"""
|
234 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
243 |
|
|
Returns one specific body device by given id
|
244 |
|
|
"""
|
245 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
250 |
|
|
Returns all body devices saved in database
|
251 |
|
|
"""
|
252 |
eaf8ace4
|
Matej Zeman
|
return db.query(models.BodyDevice).offset(skip).limit(limit).all()
|
253 |
|
|
|
254 |
|
|
|
255 |
|
|
def find_body_device(db: Session, serial: schemas.BodyDeviceBase):
|
256 |
4911f0ea
|
Matej Zeman
|
"""
|
257 |
|
|
Finds one body device by its serial number
|
258 |
|
|
"""
|
259 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
264 |
|
|
Creates new Body device
|
265 |
|
|
"""
|
266 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
275 |
|
|
Returns all ld debugger logs in database
|
276 |
|
|
"""
|
277 |
eaf8ace4
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
282 |
|
|
Creates new ld log for ld_logs database table
|
283 |
|
|
"""
|
284 |
eaf8ace4
|
Matej Zeman
|
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 |
aba60b70
|
Matej Zeman
|
def get_logs(db: Session, skip: int = 0, limit: int = 100):
|
292 |
4911f0ea
|
Matej Zeman
|
"""
|
293 |
|
|
Returns all usb logs in database ordered by timestamp
|
294 |
|
|
"""
|
295 |
b6f0e019
|
Matej Zeman
|
return db.query(models.USBLog).order_by(desc(models.USBLog.timestamp)).offset(skip).limit(limit).all()
|
296 |
aba60b70
|
Matej Zeman
|
|
297 |
|
|
|
298 |
|
|
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100):
|
299 |
4911f0ea
|
Matej Zeman
|
"""
|
300 |
|
|
Returns all usb logs in database sorted by id
|
301 |
|
|
"""
|
302 |
aba60b70
|
Matej Zeman
|
return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all()
|
303 |
|
|
|
304 |
|
|
|
305 |
b6f0e019
|
Matej Zeman
|
def find_filtered_logs(db: Session, logs: []):
|
306 |
4911f0ea
|
Matej Zeman
|
"""
|
307 |
|
|
Returns all usb logs with ids in given id array.
|
308 |
|
|
"""
|
309 |
b6f0e019
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
"""
|
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 |
b6f0e019
|
Matej Zeman
|
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 |
4911f0ea
|
Matej Zeman
|
# executing assembled query string
|
345 |
b6f0e019
|
Matej Zeman
|
result = db.execute(execute_string)
|
346 |
|
|
return result
|
347 |
|
|
|
348 |
|
|
|
349 |
99c92c11
|
Matej Zeman
|
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int, pc_id: int, date: datetime):
|
350 |
4911f0ea
|
Matej Zeman
|
"""
|
351 |
|
|
Creates new USB log for usb_logs database table
|
352 |
|
|
"""
|
353 |
99c92c11
|
Matej Zeman
|
db_log = models.USBLog(pc_id=pc_id, timestamp=date, status=item.status, device_id=dev_id)
|
354 |
aba60b70
|
Matej Zeman
|
db.add(db_log)
|
355 |
|
|
db.commit()
|
356 |
|
|
db.refresh(db_log)
|
357 |
|
|
return db_log
|