Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
f2e29d79
Verified
Commit
f2e29d79
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Rework progress API to be more coherent with upstream and DRY
parent
0d35f974
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!494
Refactor ProgressRecorder for non-optional Celery usage and add doc strings
,
!491
Resolve "Make Celery non-optional"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/core/util/celery_progress.py
+37
-20
37 additions, 20 deletions
aleksis/core/util/celery_progress.py
with
37 additions
and
20 deletions
aleksis/core/util/celery_progress.py
+
37
−
20
View file @
f2e29d79
from
decimal
import
Decimal
from
functools
import
wraps
from
typing
import
Callable
,
Union
from
numbers
import
Number
from
typing
import
Callable
,
Optional
from
django.contrib
import
messages
from
celery_progress.backend
import
PROGRESS_STATE
,
AbstractProgressRecorder
...
...
@@ -26,11 +28,11 @@ class ProgressRecorder(AbstractProgressRecorder):
@recorded_task
def do_something(foo, bar, recorder, baz=None):
# ...
recorder.total
=
len(list_with_data)
recorder.
set_progress(
total
=
len(list_with_data)
)
for i, item in enumerate(list_with_data):
# ...
recorder.set_progress(i
+
1)
recorder.set_progress(i
+
1)
# ...
recorder.add_message(messages.SUCCESS,
"
All data were imported successfully.
"
)
...
...
@@ -61,42 +63,57 @@ class ProgressRecorder(AbstractProgressRecorder):
def
__init__
(
self
,
task
):
self
.
task
=
task
self
.
messages
=
[]
self
.
total
=
100
self
.
current
=
0
def
set_progress
(
self
,
current
:
Union
[
int
,
float
],
**
kwargs
):
self
.
_messages
=
[]
self
.
_current
=
0
self
.
_total
=
100
def
set_progress
(
self
,
current
:
Optional
[
Number
]
=
None
,
total
:
Optional
[
Number
]
=
None
,
description
:
Optional
[
str
]
=
None
,
level
:
int
=
messages
.
INFO
,
):
"""
Set the current progress in the frontend.
The progress percentage is automatically calculated in relation to self.total.
:param current: The absolute number of processed items
:param current: The number of processed items; relative to total, default unchanged
:param total: The total number of items (or 100 if using a percentage), default unchanged
:param description: A textual description, routed to the frontend as an INFO message
"""
self
.
current
=
current
if
current
is
not
None
:
self
.
_current
=
current
if
total
is
not
None
:
self
.
_total
=
total
percent
=
0
if
self
.
total
>
0
:
percent
=
(
Decimal
(
current
)
/
Decimal
(
self
.
total
))
*
Decimal
(
100
)
percent
=
float
(
round
(
percent
,
2
))
if
self
.
_total
>
0
:
percent
=
self
.
_current
/
self
.
_total
if
description
is
not
None
:
self
.
_messages
.
append
((
level
,
description
))
self
.
task
.
update_state
(
state
=
PROGRESS_STATE
,
meta
=
{
"
current
"
:
current
,
"
total
"
:
self
.
total
,
"
current
"
:
self
.
_
current
,
"
total
"
:
self
.
_
total
,
"
percent
"
:
percent
,
"
messages
"
:
self
.
messages
,
"
messages
"
:
self
.
_
messages
,
},
)
def
add_message
(
self
,
level
:
int
,
message
:
str
):
def
add_message
(
self
,
level
:
int
,
message
:
str
)
->
None
:
"""
Show a message in the progress frontend.
This method is a shortcut for set_progress with no new progress arguments,
passing only the message and level as description.
:param level: The message level (default levels from django.contrib.messages)
:param message: The actual message (should be translated)
"""
self
.
messages
.
append
((
level
,
message
))
self
.
set_progress
(
self
.
current
)
self
.
set_progress
(
description
=
message
,
level
=
level
)
def
recorded_task
(
orig
:
Callable
,
*
args
,
**
kwargs
)
->
app
.
Task
:
...
...
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