66
77"""Database abstraction"""
88
9- from bson import ObjectId
109from beanie import init_beanie
10+ from bson import ObjectId
1111from fastapi_pagination .ext .motor import paginate
12- from motor import motor_asyncio
13- from redis import asyncio as aioredis
1412from kernelci .api .models import (
15- EventHistory , Hierarchy , Node , TelemetryEvent , parse_node_obj
13+ EventHistory ,
14+ Hierarchy ,
15+ Node ,
16+ TelemetryEvent ,
17+ parse_node_obj ,
1618)
19+ from motor import motor_asyncio
20+ from redis import asyncio as aioredis
21+
1722from .models import User , UserGroup
1823
1924
@@ -26,33 +31,30 @@ class Database:
2631 """
2732
2833 COLLECTIONS = {
29- User : ' user' ,
30- Node : ' node' ,
31- UserGroup : ' usergroup' ,
32- EventHistory : ' eventhistory' ,
33- TelemetryEvent : ' telemetry' ,
34+ User : " user" ,
35+ Node : " node" ,
36+ UserGroup : " usergroup" ,
37+ EventHistory : " eventhistory" ,
38+ TelemetryEvent : " telemetry" ,
3439 }
3540
3641 OPERATOR_MAP = {
37- 'lt' : ' $lt' ,
38- ' lte' : ' $lte' ,
39- 'gt' : ' $gt' ,
40- ' gte' : ' $gte' ,
41- 'ne' : ' $ne' ,
42- 're' : ' $regex' ,
43- 'in' : ' $in' ,
44- ' nin' : ' $nin' ,
42+ "lt" : " $lt" ,
43+ " lte" : " $lte" ,
44+ "gt" : " $gt" ,
45+ " gte" : " $gte" ,
46+ "ne" : " $ne" ,
47+ "re" : " $regex" ,
48+ "in" : " $in" ,
49+ " nin" : " $nin" ,
4550 }
4651
47- BOOL_VALUE_MAP = {
48- 'true' : True ,
49- 'false' : False
50- }
52+ BOOL_VALUE_MAP = {"true" : True , "false" : False }
5153
52- def __init__ (self , service = ' mongodb://db:27017' , db_name = ' kernelci' ):
54+ def __init__ (self , service = " mongodb://db:27017" , db_name = " kernelci" ):
5355 self ._motor = motor_asyncio .AsyncIOMotorClient (service )
5456 # TBD: Make redis host configurable
55- self ._redis = aioredis .from_url (' redis://redis:6379' )
57+ self ._redis = aioredis .from_url (" redis://redis:6379" )
5658 self ._db = self ._motor [db_name ]
5759
5860 async def initialize_beanie (self ):
@@ -143,14 +145,13 @@ def _translate_operators(self, attributes):
143145 for op_name , op_value in value .items ():
144146 op_key = self .OPERATOR_MAP .get (op_name )
145147 if op_key :
146- if op_key in (' $in' , ' $nin' ):
148+ if op_key in (" $in" , " $nin" ):
147149 # Create a list of values from ',' separated string
148150 op_value = op_value .split ("," )
149151 if isinstance (op_value , str ) and op_value .isdecimal ():
150152 op_value = int (op_value )
151153 if translated_attributes .get (key ):
152- translated_attributes [key ].update ({
153- op_key : op_value })
154+ translated_attributes [key ].update ({op_key : op_value })
154155 else :
155156 translated_attributes [key ] = {op_key : op_value }
156157 return translated_attributes
@@ -160,7 +161,7 @@ def _convert_int_values(cls, attributes):
160161 for key , val in attributes .items ():
161162 if isinstance (val , dict ):
162163 for sub_key , sub_val in val .items ():
163- if sub_key == ' int' :
164+ if sub_key == " int" :
164165 attributes [key ] = int (sub_val )
165166 return attributes
166167
@@ -205,14 +206,13 @@ async def find_by_attributes_nonpaginated(self, model, attributes):
205206 query = self ._prepare_query (attributes )
206207 # find "limit" and "offset" keys in the query, retrieve them and
207208 # remove them from the query
208- limit = query .pop (' limit' , None )
209- offset = query .pop (' offset' , None )
209+ limit = query .pop (" limit" , None )
210+ offset = query .pop (" offset" , None )
210211 # convert to int if limit and offset are strings
211212 limit = int (limit ) if limit is not None else None
212213 offset = int (offset ) if offset is not None else None
213214 if limit is not None and offset is not None :
214- return await (col .find (query )
215- .skip (offset ).limit (limit ).to_list (None ))
215+ return await col .find (query ).skip (offset ).limit (limit ).to_list (None )
216216 if limit is not None :
217217 return await col .find (query ).limit (limit ).to_list (None )
218218 if offset is not None :
@@ -239,7 +239,7 @@ async def create(self, obj):
239239 """
240240 if obj .id is not None :
241241 raise ValueError (f"Object cannot be created with id: { obj .id } " )
242- delattr (obj , 'id' )
242+ delattr (obj , "id" )
243243 col = self ._get_collection (obj .__class__ )
244244 res = await col .insert_one (obj .model_dump (by_alias = True ))
245245 obj .id = res .inserted_id
@@ -251,22 +251,19 @@ async def insert_many(self, model, documents):
251251 result = await col .insert_many (documents )
252252 return result .inserted_ids
253253
254- async def _create_recursively (self , hierarchy : Hierarchy , parent : Node ,
255- cls , col ):
254+ async def _create_recursively (self , hierarchy : Hierarchy , parent : Node , cls , col ):
256255 obj = parse_node_obj (hierarchy .node )
257256 if parent :
258257 obj .parent = parent .id
259258 if obj .id :
260259 obj .update ()
261260 if obj .parent == obj .id :
262261 raise ValueError ("Parent cannot be the same as the object" )
263- res = await col .replace_one (
264- {'_id' : ObjectId (obj .id )}, obj .dict (by_alias = True )
265- )
262+ res = await col .replace_one ({"_id" : ObjectId (obj .id )}, obj .dict (by_alias = True ))
266263 if res .matched_count == 0 :
267264 raise ValueError (f"No object found with id: { obj .id } " )
268265 else :
269- delattr (obj , 'id' )
266+ delattr (obj , "id" )
270267 res = await col .insert_one (obj .dict (by_alias = True ))
271268 obj .id = res .inserted_id
272269 obj = cls (** await col .find_one (ObjectId (obj .id )))
@@ -296,9 +293,7 @@ async def update(self, obj):
296293 obj .update ()
297294 if obj .parent == obj .id :
298295 raise ValueError ("Parent cannot be the same as the object" )
299- res = await col .replace_one (
300- {'_id' : ObjectId (obj .id )}, obj .dict (by_alias = True )
301- )
296+ res = await col .replace_one ({"_id" : ObjectId (obj .id )}, obj .dict (by_alias = True ))
302297 if res .matched_count == 0 :
303298 raise ValueError (f"No object found with id: { obj .id } " )
304299 return obj .__class__ (** await col .find_one (ObjectId (obj .id )))
0 commit comments