Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
brian-dashboard-manager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
geant-swd
brian
brian-dashboard-manager
Commits
af3b0512
Commit
af3b0512
authored
1 month ago
by
Bjarke Madsen
Browse files
Options
Downloads
Patches
Plain Diff
make endpoint synchronous now that the process is fast
parent
d1e3aeef
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
brian_dashboard_manager/routes/update.py
+25
-32
25 additions, 32 deletions
brian_dashboard_manager/routes/update.py
with
25 additions
and
32 deletions
brian_dashboard_manager/routes/update.py
+
25
−
32
View file @
af3b0512
import
datetime
import
datetime
import
logging
from
flask
import
jsonify
,
Response
from
flask
import
jsonify
,
Response
from
concurrent.futures
import
ThreadPoolExecutor
from
flask
import
Blueprint
,
current_app
from
flask
import
Blueprint
,
current_app
from
brian_dashboard_manager.routes
import
common
from
brian_dashboard_manager.routes
import
common
from
brian_dashboard_manager.grafana.provision
import
provision
from
brian_dashboard_manager.grafana.provision
import
provision
from
brian_dashboard_manager
import
CONFIG_KEY
from
brian_dashboard_manager
import
CONFIG_KEY
logger
=
logging
.
getLogger
(
__name__
)
provision_state
=
{
provision_state
=
{
'
time
'
:
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
),
'
time
'
:
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
),
'
provisioning
'
:
False
'
provisioning
'
:
False
...
@@ -29,13 +31,17 @@ def after_request(resp):
...
@@ -29,13 +31,17 @@ def after_request(resp):
return
common
.
after_request
(
resp
)
return
common
.
after_request
(
resp
)
def
provision_maybe
():
@routes.route
(
'
/
'
,
methods
=
[
'
GET
'
])
def
update
():
"""
"""
Check if we should provision in case of multiple requests hitting the endpoint.
This resource is used to trigger the dashboard provisioning to Grafana.
We need to make sure we don
'
t provision if another thread is still running.
The response will be formatted according to the following schema:
.. asjson::
brian_dashboard_manager.routes.update.UPDATE_RESPONSE_SCHEMA
:return: tuple of (bool, datetime) representing if we can provision
:return: json
and the timestamp of the last provisioning, respectively.
"""
"""
global
provision_state
# noqa: F824
global
provision_state
# noqa: F824
...
@@ -43,9 +49,10 @@ def provision_maybe():
...
@@ -43,9 +49,10 @@ def provision_maybe():
now
=
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
)
now
=
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
)
timestamp
=
provision_state
[
'
time
'
]
timestamp
=
provision_state
[
'
time
'
]
provisioning
=
provision_state
[
'
provisioning
'
]
provisioning
=
provision_state
[
'
provisioning
'
]
should
=
True
if
provisioning
and
(
now
-
timestamp
).
total_seconds
()
<
6
00
:
# lockout for
10
minutes at most
if
provisioning
and
(
now
-
timestamp
).
total_seconds
()
<
3
00
:
# lockout for
5
minutes at most
return
False
,
timestamp
should
=
False
def
write_timestamp
(
timestamp
,
provisioning
):
def
write_timestamp
(
timestamp
,
provisioning
):
provision_state
[
'
time
'
]
=
timestamp
provision_state
[
'
time
'
]
=
timestamp
...
@@ -55,32 +62,18 @@ def provision_maybe():
...
@@ -55,32 +62,18 @@ def provision_maybe():
now
=
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
)
now
=
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
)
write_timestamp
(
now
,
False
)
write_timestamp
(
now
,
False
)
write_timestamp
(
now
,
True
)
if
should
:
write_timestamp
(
now
,
True
)
executor
=
ThreadPoolExecutor
(
max_workers
=
1
)
f
=
executor
.
submit
(
provision
,
current_app
.
config
[
CONFIG_KEY
])
f
.
add_done_callback
(
lambda
_
:
_finish
())
return
True
,
now
@routes.route
(
'
/
'
,
methods
=
[
'
GET
'
])
def
update
():
"""
This resource is used to trigger the provisioning to Grafana.
It responds to the request immediately after starting
the provisioning process.
The response will be formatted according to the following schema:
.. asjson::
try
:
brian_dashboard_manager.routes.update.UPDATE_RESPONSE_SCHEMA
provision
(
current_app
.
config
[
CONFIG_KEY
])
except
Exception
:
logger
.
exception
(
"
Error during provisioning:
"
)
return
jsonify
({
'
data
'
:
{
'
message
'
:
'
Provisioning failed, check logs for details.
'
}}),
500
finally
:
_finish
()
:return: json
return
jsonify
({
'
data
'
:
{
'
message
'
:
'
Provisioned dashboards!
'
}})
"""
should
,
timestamp
=
provision_maybe
()
if
should
:
return
jsonify
({
'
data
'
:
{
'
message
'
:
'
Provisioning dashboards!
'
}})
else
:
else
:
seconds_ago
=
(
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
)
-
timestamp
).
total_seconds
()
seconds_ago
=
(
datetime
.
datetime
.
now
(
datetime
.
timezone
.
utc
)
-
timestamp
).
total_seconds
()
message
=
f
'
Provision already in progress since
{
timestamp
}
(
{
seconds_ago
:
.
2
f
}
seconds ago).
'
message
=
f
'
Provision already in progress since
{
timestamp
}
(
{
seconds_ago
:
.
2
f
}
seconds ago).
'
...
...
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