Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-analytics-data-api
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-analytics-data-api
Commits
09c0c280
Commit
09c0c280
authored
Dec 22, 2015
by
Dennis Jen
Committed by
Daniel Friedman
Apr 11, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated sort params
parent
7278f1c5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
34 deletions
+32
-34
analytics_data_api/v0/models.py
+22
-26
analytics_data_api/v0/views/learners.py
+10
-8
No files found.
analytics_data_api/v0/models.py
View file @
09c0c280
...
...
@@ -256,25 +256,28 @@ class RosterEntry(DocType):
cohort
=
None
,
enrollment_mode
=
None
,
text_search
=
None
,
order_by_fields
=
None
,
sort_order_directions
=
None
sort_policies
=
None
,
):
"""
Construct a search query for all users in `course_id` and return
the Search object.
order_by_fields specifies the fields to order by with the first element of
the array as the primary sort. Similarly, sort_order_directions corresponds
to the direction of the sort. These arrays must be of the same size
.
sort_policies is an array, where the first element is the primary sort.
Elements in the array are dicts with fields: order_by (field to sort by)
and sort_order (either 'asc' or 'desc'). Default to 'username' and 'asc'
.
Raises `ValueError` if both `segments` and `ignore_segments` are provided.
"""
if
order_by_fields
is
None
:
order_by_fields
=
[
'username'
]
if
sort_order_directions
is
None
:
sort_order_directions
=
[
'asc'
]
if
sort_policies
is
None
or
len
(
sort_policies
)
==
0
:
sort_policies
=
[{
'order_by'
:
None
,
'sort_order'
:
None
}]
# set default sort policy to 'username' and 'asc'
for
field
,
default
in
[(
'order_by'
,
'username'
),
(
'sort_order'
,
'asc'
)]:
if
sort_policies
[
0
][
field
]
is
None
:
sort_policies
[
0
][
field
]
=
default
# Error handling
if
segments
and
ignore_segments
:
...
...
@@ -289,25 +292,17 @@ class RosterEntry(DocType):
'username'
,
'email'
,
'discussions_contributed'
,
'problems_attempted'
,
'problems_completed'
,
'problem_attempts_per_completed'
,
'attempt_ratio_order'
,
'videos_viewed'
)
for
order_by
in
order_by_fields
:
if
order_by
not
in
order_by_options
:
sort_order_options
=
(
'asc'
,
'desc'
)
for
sort_policy
in
sort_policies
:
if
sort_policy
[
'order_by'
]
not
in
order_by_options
:
raise
ValueError
(
"order_by value '{order_by}' must be one of: ({order_by_options})"
.
format
(
order_by
=
order_by
,
order_by_options
=
', '
.
join
(
order_by_options
)
order_by
=
sort_policy
[
'order_by'
]
,
order_by_options
=
', '
.
join
(
order_by_options
)
))
sort_order_options
=
(
'asc'
,
'desc'
)
for
sort_order
in
sort_order_directions
:
if
sort_order
not
in
sort_order_options
:
if
sort_policy
[
'sort_order'
]
not
in
sort_order_options
:
raise
ValueError
(
"sort_order value '{sort_order}' must be one of: ({sort_order_options})"
.
format
(
sort_order
=
sort_
order
,
sort_order_options
=
', '
.
join
(
sort_order_options
)
sort_order
=
sort_
policy
[
'sort_order'
]
,
sort_order_options
=
', '
.
join
(
sort_order_options
)
))
if
len
(
order_by_fields
)
!=
len
(
sort_order_directions
):
raise
ValueError
(
"The number of items in order_by_fields '{order_by_count}' must equal that of "
"sort_order_directions '{sort_order_count}'"
.
format
(
order_by_count
=
len
(
order_by_fields
),
sort_order_count
=
len
(
sort_order_directions
)
))
search
=
cls
.
search
()
search
.
query
=
Q
(
'bool'
,
must
=
[
Q
(
'term'
,
course_id
=
course_id
)])
...
...
@@ -326,9 +321,10 @@ class RosterEntry(DocType):
# construct the sort hierarchy
sort_terms
=
[]
for
order_by
,
sort_order
in
zip
(
order_by_fields
,
sort_order_directions
):
for
sort_policy
in
sort_policies
:
sort_order
=
sort_policy
[
'sort_order'
]
term
=
{
order_by
:
{
sort_policy
[
'order_by'
]
:
{
'order'
:
sort_order
,
'missing'
:
'_last'
if
sort_order
==
'asc'
else
'_first'
,
# ordering of missing fields
}
...
...
analytics_data_api/v0/views/learners.py
View file @
09c0c280
...
...
@@ -176,17 +176,20 @@ class LearnerListView(CourseViewMixin, generics.ListAPIView):
query_params
=
self
.
request
.
QUERY_PARAMS
order_by
=
query_params
.
get
(
'order_by'
)
order_by_fields
=
[
order_by
]
if
order_by
else
None
sort_order
=
query_params
.
get
(
'sort_order'
)
sort_order_directions
=
[
sort_order
]
if
sort_order
else
None
sort_policies
=
[{
'order_by'
:
order_by
,
'sort_order'
:
sort_order
}]
# Ordering by problem_attempts_per_completed can be ambiguous because
# values could be infinite (e.g. divide by zero) if no problems were completed.
# Instead, secondary sorting by attempt_ratio_order will produce a sensible ordering
# Instead, secondary sorting by attempt_ratio_order will produce a sensible ordering
.
if
order_by
==
'problem_attempts_per_completed'
:
order_by_fields
.
append
(
'attempt_ratio_order'
)
sort_order_directions
.
append
(
'asc'
if
sort_order
==
'desc'
else
'desc'
)
sort_policies
.
append
({
'order_by'
:
'attempt_ratio_order'
,
'sort_order'
:
'asc'
if
sort_order
==
'desc'
else
'desc'
})
params
=
{
'segments'
:
split_query_argument
(
query_params
.
get
(
'segments'
)),
...
...
@@ -194,8 +197,7 @@ class LearnerListView(CourseViewMixin, generics.ListAPIView):
'cohort'
:
query_params
.
get
(
'cohort'
),
'enrollment_mode'
:
query_params
.
get
(
'enrollment_mode'
),
'text_search'
:
query_params
.
get
(
'text_search'
),
'order_by_fields'
:
order_by_fields
,
'sort_order_directions'
:
sort_order_directions
,
'sort_policies'
:
sort_policies
,
}
# Remove None values from `params` so that we don't overwrite default
# parameter values in `get_users_in_course`.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment