Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
StorageLeaf
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ProjectLeaf
StorageLeaf
Commits
84a51725
Commit
84a51725
authored
3 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
#9 - moved database routes to own router
parent
8563c586
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/StorageLeaf.py
+2
-1
2 additions, 1 deletion
src/StorageLeaf.py
src/logic/routers/DatabaseRouter.py
+54
-0
54 additions, 0 deletions
src/logic/routers/DatabaseRouter.py
src/logic/routers/GeneralRouter.py
+3
-49
3 additions, 49 deletions
src/logic/routers/GeneralRouter.py
with
59 additions
and
50 deletions
src/StorageLeaf.py
+
2
−
1
View file @
84a51725
...
...
@@ -13,7 +13,7 @@ from logic import Constants
from
logic.DiscoveryService
import
DiscoveryService
from
logic.database
import
Models
from
logic.database.Database
import
engine
from
logic.routers
import
DeviceRouter
,
GeneralRouter
from
logic.routers
import
DeviceRouter
,
GeneralRouter
,
DatabaseRouter
from
logic.routers
import
SensorRouter
,
MeasurementRouter
LOGGER
=
DefaultLogger
().
create_logger_if_not_exists
(
Constants
.
APP_NAME
)
...
...
@@ -63,6 +63,7 @@ def overridden_redoc():
app
.
include_router
(
GeneralRouter
.
router
)
app
.
include_router
(
DatabaseRouter
.
router
)
app
.
include_router
(
DeviceRouter
.
router
)
app
.
include_router
(
SensorRouter
.
router
)
app
.
include_router
(
MeasurementRouter
.
router
)
...
...
This diff is collapsed.
Click to expand it.
src/logic/routers/DatabaseRouter.py
0 → 100644
+
54
−
0
View file @
84a51725
from
datetime
import
datetime
from
fastapi
import
APIRouter
,
Depends
from
sqlalchemy.orm
import
Session
from
Settings
import
SETTINGS
from
logic.Dependencies
import
get_database
,
check_api_key
from
logic.database
import
Schemas
,
DatabaseInfoProvider
from
logic.database.DatabaseCleaner
import
DatabaseCleaner
,
RetentionPolicy
router
=
APIRouter
(
prefix
=
'
/database
'
,
tags
=
[
'
database
'
]
)
@router.get
(
'
/databaseInfo
'
,
summary
=
'
Gets information about the database
'
,
response_model
=
Schemas
.
DatabaseInfo
)
async
def
databaseInfo
(
db
:
Session
=
Depends
(
get_database
)):
return
DatabaseInfoProvider
.
get_database_info
(
db
)
@router.post
(
'
/databaseCleanup
'
,
summary
=
'
Initiates a database cleanup by enforcing the configured retention policies for each sensor
'
,
response_model
=
Schemas
.
Status
,
dependencies
=
[
Depends
(
check_api_key
)])
async
def
databaseCleanup
(
db
:
Session
=
Depends
(
get_database
)):
infoBefore
=
DatabaseInfoProvider
.
get_database_info
(
db
)
cleanupSettings
=
SETTINGS
[
'
database
'
][
'
cleanup
'
]
retentionPolicies
=
cleanupSettings
[
'
retentionPolicies
'
]
policies
=
[]
for
item
in
retentionPolicies
:
policies
.
append
(
RetentionPolicy
(
numberOfMeasurementsPerDay
=
item
[
'
numberOfMeasurementsPerDay
'
],
ageInDays
=
item
[
'
ageInDays
'
]))
DatabaseCleaner
(
policies
,
cleanupSettings
[
'
forceBackupAfterCleanup
'
]).
clean
(
db
,
datetime
.
now
().
date
())
infoAfter
=
DatabaseInfoProvider
.
get_database_info
(
db
)
deletedMeasurements
=
infoBefore
.
number_of_measurements
-
infoAfter
.
number_of_measurements
sizeFreed
=
infoBefore
.
size_on_disk_in_mb
-
infoAfter
.
size_on_disk_in_mb
infoDifference
=
Schemas
.
DatabaseInfo
(
number_of_measurements
=
deletedMeasurements
,
size_on_disk_in_mb
=
sizeFreed
)
return
Schemas
.
DatabaseCleanupInfo
(
before
=
infoBefore
,
after
=
infoAfter
,
difference
=
infoDifference
)
@router.get
(
'
/databaseCleanup
'
,
summary
=
'
Provides the status of the current database cleanup
'
,
response_model
=
Schemas
.
DatabaseCleanupInfo
)
async
def
databaseCleanup
():
return
Schemas
.
DatabaseCleanupInfo
(
status
=
Schemas
.
DatabaseCleanupStatus
.
UNDEFINED
)
This diff is collapsed.
Click to expand it.
src/logic/routers/GeneralRouter.py
+
3
−
49
View file @
84a51725
from
datetime
import
datetime
from
fastapi
import
APIRouter
from
fastapi
import
APIRouter
,
Depends
from
sqlalchemy.orm
import
Session
from
starlette.background
import
BackgroundTasks
from
Settings
import
VERSION
,
SETTINGS
from
logic.Dependencies
import
get_database
,
check_api_key
from
logic.database
import
Schemas
,
DatabaseInfoProvider
from
logic.database.DatabaseCleaner
import
DatabaseCleaner
,
RetentionPolicy
from
Settings
import
VERSION
from
logic.database
import
Schemas
router
=
APIRouter
(
prefix
=
'
/general
'
,
...
...
@@ -20,43 +14,3 @@ router = APIRouter(
response_model
=
Schemas
.
Version
)
async
def
version
():
return
Schemas
.
Version
(
**
VERSION
)
@router.get
(
'
/databaseInfo
'
,
summary
=
'
Gets information about the database
'
,
response_model
=
Schemas
.
DatabaseInfo
)
async
def
databaseInfo
(
db
:
Session
=
Depends
(
get_database
)):
return
DatabaseInfoProvider
.
get_database_info
(
db
)
@router.post
(
'
/databaseCleanup
'
,
summary
=
'
Initiates a database cleanup by enforcing the configured retention policies for each sensor
'
,
response_model
=
Schemas
.
Status
,
dependencies
=
[
Depends
(
check_api_key
)])
async
def
databaseCleanup
(
db
:
Session
=
Depends
(
get_database
)):
infoBefore
=
DatabaseInfoProvider
.
get_database_info
(
db
)
cleanupSettings
=
SETTINGS
[
'
database
'
][
'
cleanup
'
]
retentionPolicies
=
cleanupSettings
[
'
retentionPolicies
'
]
policies
=
[]
for
item
in
retentionPolicies
:
policies
.
append
(
RetentionPolicy
(
numberOfMeasurementsPerDay
=
item
[
'
numberOfMeasurementsPerDay
'
],
ageInDays
=
item
[
'
ageInDays
'
]))
DatabaseCleaner
(
policies
,
cleanupSettings
[
'
forceBackupAfterCleanup
'
]).
clean
(
db
,
datetime
.
now
().
date
())
infoAfter
=
DatabaseInfoProvider
.
get_database_info
(
db
)
deletedMeasurements
=
infoBefore
.
number_of_measurements
-
infoAfter
.
number_of_measurements
sizeFreed
=
infoBefore
.
size_on_disk_in_mb
-
infoAfter
.
size_on_disk_in_mb
infoDifference
=
Schemas
.
DatabaseInfo
(
number_of_measurements
=
deletedMeasurements
,
size_on_disk_in_mb
=
sizeFreed
)
return
Schemas
.
DatabaseCleanupInfo
(
before
=
infoBefore
,
after
=
infoAfter
,
difference
=
infoDifference
)
@router.get
(
'
/databaseCleanup
'
,
summary
=
'
Provides the status of the current database cleanup
'
,
response_model
=
Schemas
.
DatabaseCleanupInfo
)
async
def
databaseCleanup
():
return
Schemas
.
DatabaseCleanupInfo
(
status
=
Schemas
.
DatabaseCleanupStatus
.
UNDEFINED
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment