Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AKPlanning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KIF
AKPlanning
Commits
1ffe196f
Commit
1ffe196f
authored
3 months ago
by
Benjamin Hättasch
Browse files
Options
Downloads
Patches
Plain Diff
AKPlan: Speed-up (queries)
Eliminate duplicate queries through select_related and restructuring looping code
parent
bdf57308
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
AKPlan/views.py
+30
-17
30 additions, 17 deletions
AKPlan/views.py
with
30 additions
and
17 deletions
AKPlan/views.py
+
30
−
17
View file @
1ffe196f
...
...
@@ -22,7 +22,10 @@ class PlanIndexView(FilterByEventSlugMixin, ListView):
def
get_queryset
(
self
):
# Ignore slots not scheduled yet
return
super
().
get_queryset
().
filter
(
start__isnull
=
False
).
select_related
(
'
ak
'
,
'
room
'
,
'
ak__category
'
)
return
(
super
().
get_queryset
().
filter
(
start__isnull
=
False
).
select_related
(
'
event
'
,
'
ak
'
,
'
room
'
,
'
ak__category
'
,
'
ak__event
'
))
# Need to prefetch both event and ak__event
# since django is not aware that the two are always the same
def
get_context_data
(
self
,
*
,
object_list
=
None
,
**
kwargs
):
context
=
super
().
get_context_data
(
object_list
=
object_list
,
**
kwargs
)
...
...
@@ -38,6 +41,7 @@ class PlanIndexView(FilterByEventSlugMixin, ListView):
# Get list of current and next slots
for
akslot
in
context
[
"
akslots
"
]:
self
.
_process_slot
(
akslot
)
# Construct a list of all rooms used by these slots on the fly
if
akslot
.
room
is
not
None
:
rooms
.
add
(
akslot
.
room
)
...
...
@@ -62,6 +66,16 @@ class PlanIndexView(FilterByEventSlugMixin, ListView):
return
context
def
_process_slot
(
self
,
akslot
):
"""
Function to be called for each slot when looping over the slots
(meant to be overridden in inherited views)
:param akslot: current slot
:type akslot: AKSlot
"""
pass
class
PlanScreenView
(
PlanIndexView
):
"""
...
...
@@ -95,12 +109,20 @@ class PlanScreenView(PlanIndexView):
# Restrict AK slots to relevant ones
# This will automatically filter all rooms not needed for the selected range in the orginal get_context method
akslots
=
super
().
get_queryset
().
filter
(
start__gt
=
self
.
start
)
return
super
().
get_queryset
().
filter
(
start__gt
=
self
.
start
)
def
get_context_data
(
self
,
*
,
object_list
=
None
,
**
kwargs
):
# Find the earliest hour AKs start and end (handle 00:00 as 24:00)
self
.
earliest_start_hour
=
23
self
.
latest_end_hour
=
1
for
akslot
in
akslots
.
all
():
context
=
super
().
get_context_data
(
object_list
=
object_list
,
**
kwargs
)
context
[
"
start
"
]
=
self
.
start
context
[
"
end
"
]
=
self
.
event
.
end
context
[
"
earliest_start_hour
"
]
=
self
.
earliest_start_hour
context
[
"
latest_end_hour
"
]
=
self
.
latest_end_hour
return
context
def
_process_slot
(
self
,
akslot
):
start_hour
=
akslot
.
start
.
astimezone
(
self
.
event
.
timezone
).
hour
if
start_hour
<
self
.
earliest_start_hour
:
# Use hour - 1 to improve visibility of date change
...
...
@@ -112,15 +134,6 @@ class PlanScreenView(PlanIndexView):
elif
end_hour
>
self
.
latest_end_hour
:
# Always use hour + 1, since AK may end at :xy and not always at :00
self
.
latest_end_hour
=
min
(
end_hour
+
1
,
24
)
return
akslots
def
get_context_data
(
self
,
*
,
object_list
=
None
,
**
kwargs
):
context
=
super
().
get_context_data
(
object_list
=
object_list
,
**
kwargs
)
context
[
"
start
"
]
=
self
.
start
context
[
"
end
"
]
=
self
.
event
.
end
context
[
"
earliest_start_hour
"
]
=
self
.
earliest_start_hour
context
[
"
latest_end_hour
"
]
=
self
.
latest_end_hour
return
context
class
PlanRoomView
(
FilterByEventSlugMixin
,
DetailView
):
...
...
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