Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding last updated to summary endpoint [ENG-1879] #9418

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions api/institutions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Meta:
public_project_count = ser.IntegerField(read_only=True)
private_project_count = ser.IntegerField(read_only=True)
user_count = ser.IntegerField(read_only=True)
last_updated = ser.DateTimeField(read_only=True)

links = LinksField({
'self': 'get_absolute_url',
Expand Down
4 changes: 3 additions & 1 deletion api/institutions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ class InstitutionSummaryMetrics(JSONAPIBaseView, generics.RetrieveAPIView, Insti
# overrides RetrieveAPIView
def get_object(self):
institution = self.get_institution()
return InstitutionProjectCounts.get_latest_institution_project_document(institution)
results = InstitutionProjectCounts.get_latest_institution_project_document(institution)
results['last_updated'] = UserInstitutionProjectCounts.get_recent_datetime(institution)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corbinSanders thinking on this again, it looks like all metrics should have a timestamp field -- is it possible to get the timestamp off the "latest" InstitutionProjectCounts result that's already being fetched? rather than make additional requests to elastic for UserInstitutionProjectCounts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

return results


class InstitutionImpactList(JSONAPIBaseView, ListFilterMixin, generics.ListAPIView, InstitutionMixin):
Expand Down
14 changes: 12 additions & 2 deletions api_tests/institutions/views/test_institution_summary_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
AuthUserFactory,
InstitutionFactory
)
from osf.metrics import InstitutionProjectCounts
from osf.metrics import InstitutionProjectCounts, UserInstitutionProjectCounts


@pytest.mark.es
Expand Down Expand Up @@ -48,6 +48,15 @@ def test_get(self, app, url, institution, user, admin):
institution_user_count_latest = 9
timestamp_latest = datetime.datetime.now()

# Record for timestamp
UserInstitutionProjectCounts.record(
user_id=user._id,
institution_id=institution._id,
public_project_count=1,
private_project_count=1,
timestamp=timestamp_latest
).save()

# Uses record to specify user_count
InstitutionProjectCounts.record(
institution_id=institution._id,
Expand Down Expand Up @@ -86,7 +95,8 @@ def test_get(self, app, url, institution, user, admin):
'attributes': {
'public_project_count': public_project_count_latest,
'private_project_count': private_project_count_latest,
'user_count': institution_user_count_latest
'user_count': institution_user_count_latest,
'last_updated': timestamp_latest.replace(microsecond=0, second=0).isoformat() + 'Z'
},
'links': {
'self': f'http://localhost:8000/v2/institutions/{institution._id}/metrics/summary/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_filter(self, app, url, admin, populate_counts):
resp = app.get(f'{url}?filter[department]=Psychology dept', auth=admin.auth)
assert resp.json['data'][0]['attributes']['department'] == 'Psychology dept'

def test_sort_and_pagination(self, app, url, admin, populate_more_counts, institution):
def test_sort_and_pagination(self, app, url, user, user2, user3, admin, populate_counts, populate_more_counts, institution):
resp = app.get(f'{url}?sort=user_name&page[size]=1&page=2', auth=admin.auth)
assert resp.status_code == 200
assert resp.json['links']['meta']['total'] == 11
Expand All @@ -227,15 +227,15 @@ def test_sort_and_pagination(self, app, url, admin, populate_more_counts, instit
assert resp.json['links']['meta']['total'] == 11
assert resp.json['data'][-1]['attributes']['user_name'] == 'Zedd'

def test_filter_and_pagination(self, app, url, admin, populate_more_counts, institution):
def test_filter_and_pagination(self, app, user, user2, user3, url, admin, populate_counts, populate_more_counts, institution):
resp = app.get(f'{url}?page=2', auth=admin.auth)
assert resp.json['links']['meta']['total'] == 11
assert resp.json['data'][0]['attributes']['user_name'] == 'Zedd'
resp = app.get(f'{url}?filter[user_name]=Zedd', auth=admin.auth)
assert resp.json['links']['meta']['total'] == 1
assert resp.json['data'][0]['attributes']['user_name'] == 'Zedd'

def test_filter_and_sort(self, app, url, admin, user4, populate_counts, populate_na_department, institution):
def test_filter_and_sort(self, app, url, user, user2, user3, admin, user4, populate_counts, populate_na_department, institution):
"""
Testing for bug where sorting and filtering would throw 502.
:param app:
Expand Down