Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Asset Manager v2 test
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
arjun.b
Asset Manager v2 test
Commits
51db0542
Commit
51db0542
authored
May 17, 2023
by
arjun.b
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
17 may commit
parent
25493b85
Changes
15
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
516 additions
and
35 deletions
+516
-35
.idea/sonarlint/issuestore/index.pb
.idea/sonarlint/issuestore/index.pb
+4
-6
__version__.py
__version__.py
+1
-0
app.py
app.py
+45
-2
log/assets_manager_V2_log.log
log/assets_manager_V2_log.log
+298
-0
scripts/constants/api.py
scripts/constants/api.py
+1
-0
scripts/constants/db_constants.py
scripts/constants/db_constants.py
+1
-0
scripts/core/handlers/device_handler.py
scripts/core/handlers/device_handler.py
+32
-2
scripts/database/mongodb/asset_manager/collections/device.py
scripts/database/mongodb/asset_manager/collections/device.py
+1
-1
scripts/database/mongodb/asset_manager/collections/download_file.py
...tabase/mongodb/asset_manager/collections/download_file.py
+39
-0
scripts/errors/__init__.py
scripts/errors/__init__.py
+27
-0
scripts/schemas/deafult_responses.py
scripts/schemas/deafult_responses.py
+14
-0
scripts/schemas/tag_schema.py
scripts/schemas/tag_schema.py
+8
-1
scripts/services/v2/asset_management_services.py
scripts/services/v2/asset_management_services.py
+20
-22
scripts/utils/mongo_tools/mongo_sync.py
scripts/utils/mongo_tools/mongo_sync.py
+1
-1
test.py
test.py
+24
-0
No files found.
.idea/sonarlint/issuestore/index.pb
View file @
51db0542
...
@@ -7,8 +7,6 @@ L
...
@@ -7,8 +7,6 @@ L
scripts/utils/mongo_utils.py,0\2\028245be183e97120b9832f8912cad7b0f292d72
scripts/utils/mongo_utils.py,0\2\028245be183e97120b9832f8912cad7b0f292d72
7
7
main.py,f\1\f1bdda93d9a278e358509d498e17d97764c1fb29
main.py,f\1\f1bdda93d9a278e358509d498e17d97764c1fb29
6
app.py,1\3\13cce7fd076299c81b4986166f3d822791c9490e
:
:
Dockerfile,6\6\6651ddff6eb82c840ced7c1dddee15c6e1913dd4
Dockerfile,6\6\6651ddff6eb82c840ced7c1dddee15c6e1913dd4
...
@@ -16,7 +14,7 @@ Dockerfile,6\6\6651ddff6eb82c840ced7c1dddee15c6e1913dd4
...
@@ -16,7 +14,7 @@ Dockerfile,6\6\6651ddff6eb82c840ced7c1dddee15c6e1913dd4
update_db.py,b\e\bed2b5a444c481de1fb8954f4017582ec23f989e
update_db.py,b\e\bed2b5a444c481de1fb8954f4017582ec23f989e
?
?
requirments.txt,6\3\63ca6d3cb5645a462d48767fff3c8b7de2fad47f
requirments.txt,6\3\63ca6d3cb5645a462d48767fff3c8b7de2fad47f
n
b
>scripts/database/mongodb/asset_manager/collections/__init__.py,8\5\85662d542fe3a35fddc27f559d6e72183c0c7c60
2scripts/database/mongodb/asset_manager/__init__.py,d\2\d2443db26301eb4305c1a214cc05c5dd30a84918
I
T
scripts/logging/logger.py,2\a\2a7cfbe29f4cfc67ff2d5340ee99290abe2b82f7
$scripts/database/mongodb/__init__.py,9\c\9c64991521effa9b1b4f0ff07644b9b658515308
\ No newline at end of file
\ No newline at end of file
__version__.py
0 → 100644
View file @
51db0542
__version__
=
"2.0"
\ No newline at end of file
app.py
View file @
51db0542
import
__version__
__author__
=
'INT- 714: Arjun B'
__version__
=
__version__
.
__version__
__date__
=
'15 May 2023'
import
uvicorn
import
uvicorn
from
scripts.config
import
Services
from
scripts.config
import
Services
import
argparse
from
scripts.logging.logger
import
logger
from
scripts.logging.logger
import
logger
# starting the application
ap
=
argparse
.
ArgumentParser
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
try
:
uvicorn
.
run
(
"main:app"
,
host
=
Services
.
HOST
,
port
=
Services
.
PORT
)
logger
.
info
(
f
"****Starting {Services.PROJECT_NAME} v.{__version__}***"
)
print
(
f
"****************************Starting {Services.PROJECT_NAME} {__version__}****************************"
)
# creating the argument for the port
ap
.
add_argument
(
"--port"
,
"-p"
,
required
=
False
,
default
=
Services
.
PORT
,
help
=
"Port to start the application."
,
)
# creating the argument for the host
ap
.
add_argument
(
"--bind"
,
"-b"
,
required
=
False
,
default
=
Services
.
HOST
,
help
=
"IF to start the application."
,
)
ap
.
add_argument
(
"--workers"
,
"-w"
,
type
=
int
,
default
=
Services
.
WORKERS
,
help
=
"Number of worker processes to use."
,
)
arguments
=
vars
(
ap
.
parse_args
())
# logging the info
logger
.
info
(
f
"App Starting at {arguments['bind']}:{arguments['port']} with {arguments['workers']} workers."
)
uvicorn
.
run
(
"main:app"
,
host
=
arguments
[
"bind"
],
port
=
int
(
arguments
[
"port"
]),
workers
=
arguments
[
"workers"
]
)
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
f
"error from app {e}"
)
logger
.
exception
(
f
'Services Failed with error from app {e}'
)
log/assets_manager_V2_log.log
View file @
51db0542
This diff is collapsed.
Click to expand it.
scripts/constants/api.py
View file @
51db0542
...
@@ -3,6 +3,7 @@ class _APIEndPoints:
...
@@ -3,6 +3,7 @@ class _APIEndPoints:
api_create
:
str
=
"/create"
api_create
:
str
=
"/create"
db_test
:
str
=
"/update"
db_test
:
str
=
"/update"
create_tag
:
str
=
"/create_tag"
create_tag
:
str
=
"/create_tag"
download_file
:
str
=
"/download_file"
APIEndPoints
=
_APIEndPoints
()
APIEndPoints
=
_APIEndPoints
()
scripts/constants/db_constants.py
View file @
51db0542
...
@@ -8,3 +8,4 @@ class DatabaseConstants:
...
@@ -8,3 +8,4 @@ class DatabaseConstants:
# Collections
# Collections
collection
=
"test"
collection
=
"test"
download_file_details
=
"download_file_details"
scripts/core/handlers/device_handler.py
View file @
51db0542
from
scripts.database.mongodb.asset_manager.collections.device
import
Devices
from
scripts.database.mongodb.asset_manager.collections.device
import
Devices
from
scripts.database.mongodb.asset_manager.collections.download_file
import
FileDetails
from
scripts.logging.logger
import
logger
from
scripts.logging.logger
import
logger
obj
=
Devices
()
obj
=
Devices
()
file_obj
=
FileDetails
()
class
AssetHandler
:
class
AssetHandler
:
...
@@ -28,12 +30,40 @@ class AssetHandler:
...
@@ -28,12 +30,40 @@ class AssetHandler:
return
None
return
None
@
staticmethod
@
staticmethod
def
fetch_
tag_details
(
details
):
def
tag_details
(
details
):
try
:
try
:
logger
.
info
(
"
fetching the tag details
....."
)
logger
.
info
(
"
new tag created
....."
)
# fetching the tag details
# fetching the tag details
# Construct the filter to match the document with the given _id
fetch
=
{
"mac_address"
:
details
.
mac
}
data
=
{
"country"
:
details
.
country
,
"state"
:
details
.
state
,
"vendor"
:
details
.
vendor
,
"device"
:
details
.
device
,
"customer"
:
details
.
customer
}
db_update
=
obj
.
create_tag
(
fetch
,
data
)
if
db_update
:
logger
.
info
(
"new tag created"
)
else
:
return
None
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
f
"error from handler {e}"
)
logger
.
error
(
f
"error from handler {e}"
)
@
staticmethod
def
store_file_details
(
file_data
):
try
:
logger
.
info
(
"new file added into db"
)
data
=
{
"file_name"
:
file_data
.
file_name
,
"value"
:
file_data
.
link
}
file_store
=
file_obj
.
store_file_details
(
data
)
if
file_store
:
logger
.
info
(
"file details inserted"
)
else
:
return
None
except
Exception
as
e
:
logger
.
error
(
f
"error in storing the file detalis {e}"
)
scripts/database/mongodb/asset_manager/collections/device.py
View file @
51db0542
...
@@ -4,7 +4,7 @@ from scripts.constants.db_constants import DatabaseConstants
...
@@ -4,7 +4,7 @@ from scripts.constants.db_constants import DatabaseConstants
from
scripts.database.mongodb
import
CollectionBaseClass
,
mongo_client
from
scripts.database.mongodb
import
CollectionBaseClass
,
mongo_client
from
scripts.database.mongodb.asset_manager
import
database
from
scripts.database.mongodb.asset_manager
import
database
from
scripts.logging.logger
import
logger
from
scripts.logging.logger
import
logger
from
scripts.schemas.tag_schema
import
TestUpdate
from
scripts.utils.mongo_utils
import
MongoStageCreator
from
scripts.utils.mongo_utils
import
MongoStageCreator
collection_name
=
DatabaseConstants
.
collection
collection_name
=
DatabaseConstants
.
collection
...
...
scripts/database/mongodb/asset_manager/collections/download_file.py
0 → 100644
View file @
51db0542
from
scripts.constants.db_constants
import
DatabaseConstants
from
scripts.database.mongodb
import
CollectionBaseClass
,
mongo_client
from
scripts.database.mongodb.asset_manager
import
database
from
scripts.logging.logger
import
logger
from
scripts.utils.mongo_utils
import
MongoStageCreator
collection_name
=
DatabaseConstants
.
download_file_details
mongo_stage_creator
=
MongoStageCreator
()
class
FileDetails
(
CollectionBaseClass
):
def
__init__
(
self
):
super
()
.
__init__
(
mongo_client
,
database
=
database
,
collection
=
collection_name
)
def
fetch_file_name
(
self
,
file_data
):
try
:
fetch_file_name
=
self
.
find_one
(
file_data
.
file_name
)
if
fetch_file_name
is
None
:
logger
.
info
(
""
)
def
store_file_details
(
self
,
file_data
):
try
:
if
fetch_file_name
is
None
:
logger
.
info
(
"new file added"
)
result
=
self
.
insert_one
(
file_data
)
if
result
:
return
{
"file details added"
}
else
:
return
None
else
:
return
{
f
"{file_data.file_name} already added"
}
except
Exception
as
e
:
logger
.
error
(
f
"error while storing file details {e}"
)
return
None
scripts/errors/__init__.py
0 → 100644
View file @
51db0542
class
ILensErrors
(
Exception
):
"""Generic iLens Error"""
class
AuthenticationError
(
ILensErrors
):
pass
class
ErrorMessages
:
UNKNOWN_ERROR
=
"Unknown Error Occurred"
OP_FAILED
=
"Operation failed"
RES_FAILED
=
"Failed To get Details from remote devices"
# Authorization Errors
DEVICE_CREATION_FAILED
=
"Device creation failed. Check your inputs or contact support."
INVALID_DEVICE_ID
=
"Device ID is invalid, please check input."
class
DeviceCreationError
(
ILensErrors
):
"""
Raises when device creation or registration fails
"""
class
InvalidDeviceIDError
(
ILensErrors
):
"""
Raise when device ID is invalid or not found
"""
scripts/schemas/deafult_responses.py
0 → 100644
View file @
51db0542
from
typing
import
Any
,
Optional
from
pydantic
import
BaseModel
class
DefaultResponse
(
BaseModel
):
status
:
str
=
"success"
message
:
Optional
[
str
]
data
:
Optional
[
Any
]
class
DefaultFailureResponse
(
DefaultResponse
):
status
:
str
=
"failed"
error
:
Any
scripts/schemas/tag_schema.py
View file @
51db0542
from
pydantic
import
BaseModel
from
pydantic
import
BaseModel
class
T
estUpdate
(
BaseModel
):
class
T
ag
(
BaseModel
):
mac
:
str
mac
:
str
country
:
str
country
:
str
state
:
str
state
:
str
vendor
:
str
vendor
:
str
device
:
str
customer
:
str
customer
:
str
class
File
(
BaseModel
):
file_name
:
str
link
:
str
scripts/services/v2/asset_management_services.py
View file @
51db0542
...
@@ -4,43 +4,41 @@ from fastapi import APIRouter, Request
...
@@ -4,43 +4,41 @@ from fastapi import APIRouter, Request
from
scripts.constants.api
import
APIEndPoints
from
scripts.constants.api
import
APIEndPoints
from
scripts.core.handlers.device_handler
import
AssetHandler
from
scripts.core.handlers.device_handler
import
AssetHandler
from
scripts.database.mongodb.asset_manager.collections.device
import
Devices
from
scripts.database.mongodb.asset_manager.collections.device
import
Devices
from
scripts.errors
import
ErrorMessages
from
scripts.logging.logger
import
logger
from
scripts.logging.logger
import
logger
from
scripts.schemas.tag_schema
import
TestUpdate
from
scripts.schemas.deafult_responses
import
DefaultResponse
from
scripts.schemas.tag_schema
import
Tag
,
File
router
=
APIRouter
()
router
=
APIRouter
()
Asset_obj
=
AssetHandler
()
Asset_obj
=
AssetHandler
()
@
router
.
post
(
APIEndPoints
.
api_create
,
tags
=
[
"tag api"
])
@
router
.
get
(
APIEndPoints
.
root
,
tags
=
[
"Asset Manager"
])
async
def
store_tag
():
async
def
root
():
logger
.
info
(
"tag of a device is stored"
)
try
:
try
:
# payload=await response.body()
return
{
"API running"
}
# status=
return
{
"data"
:
"api running successfully"
}
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
f
"error from asset_management"
)
logger
.
error
(
f
"root api failed due to {e}"
)
return
DefaultResponse
(
error
=
ErrorMessages
.
OP_FAILED
)
@
router
.
post
(
APIEndPoints
.
db_test
,
tags
=
[
"update test"
])
async
def
update_db
(
data
:
TestUpdate
):
try
:
logger
.
info
(
"update method called"
)
print
(
data
)
Asset_obj
.
update_db
(
data
)
return
{
"data"
:
"updated"
}
except
Exception
as
e
:
logger
.
error
(
f
"error from asset management.py {e}"
)
return
None
@
router
.
post
(
APIEndPoints
.
create_tag
,
tags
=
[
"creating tags"
])
@
router
.
post
(
APIEndPoints
.
create_tag
,
tags
=
[
"creating tags"
])
async
def
tag_creation
():
async
def
tag_creation
(
payload
:
Tag
):
try
:
try
:
logger
.
info
(
"creating new tag api called"
)
logger
.
info
(
"creating new tag api called"
)
Asset_obj
.
fetch_tag_details
(
)
Asset_obj
.
tag_details
(
payload
)
return
{
"data"
:
"tag created"
}
return
{
"data"
:
"tag created"
}
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
f
"error from tag creation api {e}"
)
logger
.
error
(
f
"error from tag creation api {e}"
)
return
None
return
None
@
router
.
post
(
APIEndPoints
.
download_file
,
tags
=
[
"download file"
])
async
def
download_file_details
(
file_details
:
File
):
try
:
logger
.
info
(
"download file details api called"
)
Asset_obj
.
store_file_details
(
file_details
)
return
{
"data"
:
"file details stored"
}
except
Exception
as
e
:
logger
.
error
(
f
"error from download file details api"
)
scripts/utils/mongo_tools/mongo_sync.py
View file @
51db0542
...
@@ -127,7 +127,7 @@ class MongoCollectionBaseClass:
...
@@ -127,7 +127,7 @@ class MongoCollectionBaseClass:
query
:
dict
,
query
:
dict
,
data
:
dict
,
data
:
dict
,
upsert
:
bool
=
False
,
upsert
:
bool
=
False
,
strategy
:
str
=
"$
set
"
,
strategy
:
str
=
"$
push
"
,
)
->
UpdateResult
:
)
->
UpdateResult
:
"""
"""
...
...
test.py
0 → 100644
View file @
51db0542
from
pymongo
import
MongoClient
# Connect to MongoDB
client
=
MongoClient
(
'mongodb://localhost:27017'
)
db
=
client
[
'asset_manager'
]
collection
=
db
[
'test'
]
fetch
=
{
"mac_address"
:
"b8:27:eb:67:bd:3d"
}
# New field values
new_tag
=
{
"country"
:
"india"
,
"state"
:
"kerala"
,
"vendor"
:
"seimens"
,
"device"
:
"MP201A"
,
"customer"
:
"Dalmia"
}
# Add the new field to the "tags" array in all documents
collection
.
update_many
(
fetch
,
{
"$push"
:
{
"tags"
:
new_tag
}}
)
# Close the MongoDB connection
client
.
close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment