Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arkindex
Backend
Commits
40b0445d
Verified
Commit
40b0445d
authored
5 years ago
by
Erwan Rouchet
Browse files
Options
Downloads
Patches
Plain Diff
Sliceable and ordered reordering
parent
6b97a5bc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!544
Sliceable and ordered reordering
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
arkindex/images/management/commands/reorder_polygons.py
+36
-7
36 additions, 7 deletions
arkindex/images/management/commands/reorder_polygons.py
with
36 additions
and
7 deletions
arkindex/images/management/commands/reorder_polygons.py
+
36
−
7
View file @
40b0445d
from
datetime
import
timedelta
from
math
import
ceil
from
timeit
import
default_timer
from
django.core.management.base
import
BaseCommand
from
arkindex.project.polygon
import
Polygon
...
...
@@ -17,11 +18,27 @@ class Command(BaseCommand):
def
add_arguments
(
self
,
parser
):
super
().
add_arguments
(
parser
)
parser
.
add_argument
(
'
--offset
'
,
type
=
int
,
default
=
0
,
)
parser
.
add_argument
(
'
--limit
'
,
type
=
int
,
default
=
0
,
)
parser
.
add_argument
(
'
--batch-size
'
,
help
=
'
Batch size used to retrieve zones.
'
,
type
=
int
,
default
=
1000
,
)
parser
.
add_argument
(
'
--update-batch-size
'
,
help
=
'
Batch size used to update zones. Updates are run with each retrieval batch.
'
,
type
=
int
,
)
parser
.
add_argument
(
'
--dry-run
'
,
action
=
'
store_true
'
,
...
...
@@ -34,12 +51,23 @@ class Command(BaseCommand):
default
=
True
,
)
def
handle
(
self
,
*
args
,
batch_size
=
1000
,
remove
=
True
,
**
kwargs
):
completed
,
failed
,
removed
,
total
=
0
,
0
,
0
,
Zone
.
objects
.
count
()
logger
.
info
(
'
{} zones to reorder ({} batches)
'
.
format
(
total
,
int
(
total
/
batch_size
)
+
1
))
def
handle
(
self
,
*
args
,
offset
=
0
,
limit
=
0
,
batch_size
=
1000
,
update_batch_size
=
None
,
remove
=
True
,
**
kwargs
):
assert
batch_size
>
0
,
'
Batch size must be positive.
'
assert
update_batch_size
>
0
,
'
Update batch size must be positive.
'
assert
offset
>=
0
,
'
Offset cannot be negative.
'
queryset
=
Zone
.
objects
.
order_by
(
'
id
'
)
if
limit
:
assert
limit
>
0
,
'
Limit must be positive.
'
queryset
=
queryset
[
offset
:
offset
+
limit
]
else
:
queryset
=
queryset
[
offset
:]
completed
,
reordered
,
failed
,
removed
,
total
=
0
,
0
,
0
,
0
,
queryset
.
count
()
logger
.
info
(
'
{} zones to reorder ({} batches)
'
.
format
(
total
,
ceil
(
total
/
batch_size
)))
start_time
=
default_timer
()
for
i
in
range
(
0
,
total
,
batch_size
):
batch
=
Zone
.
objects
.
all
()[
i
:
i
+
batch_size
]
batch
=
queryset
[
i
:
min
(
i
+
batch_size
,
total
)
]
reordered_zones
=
[]
for
zone
in
batch
:
...
...
@@ -64,11 +92,12 @@ class Command(BaseCommand):
if
zone
.
polygon
!=
old_polygon
:
reordered_zones
.
append
(
zone
)
Zone
.
objects
.
bulk_update
(
reordered_zones
,
[
'
polygon
'
])
Zone
.
objects
.
bulk_update
(
reordered_zones
,
[
'
polygon
'
],
batch_size
=
update_batch_size
)
reordered
+=
len
(
reordered_zones
)
completed
+=
batch
.
count
()
percent
=
(
completed
/
total
)
*
100
elapsed
=
timedelta
(
seconds
=
default_timer
()
-
start_time
)
logger
.
info
(
'
{:6.2f}% completed ({:7d} / {}) in {}, {} failed, {} removed
'
.
format
(
percent
,
completed
,
total
,
elapsed
,
failed
,
removed
logger
.
info
(
'
{:6.2f}% completed ({:7d} / {}) in {}, {}
reordered, {}
failed, {} removed
'
.
format
(
percent
,
completed
,
total
,
elapsed
,
reordered
,
failed
,
removed
))
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