Skip to content

Enable daily metrics models backfill of historical data in platform, but not progress - #452

Merged
johnbaldwin merged 7 commits into
mainfrom
john/daily-metrics-backfill
Apr 26, 2022
Merged

johnbaldwin merged 7 commits into
mainfrom
john/daily-metrics-backfill

Conversation

@johnbaldwin

@johnbaldwin johnbaldwin commented Apr 12, 2022 •

Copy link
Copy Markdown
Contributor

https://appsembler.atlassian.net/browse/BLACK-2375

Porting ad-hoc scripts into Figures to fill approximate metrics for historical values

There should be one more commit, modify the existing Django management command, backfill_figures_daily_metrics and add test coverage for that command

Commit 8aeced4

Add method - figures.course.Course.first_enrollment_timestamp

This exists to help identify when we should include a course in
performing backfill to CourseDailyMetrics for a given date in the
past.

Getting the CourseEnrollment.created value for the first enrollment
provides a fast "close enough best guess" when a course was first
created, as we cannot rely on CourseOverview timestamps and this avoids
diving into MongoDB or the filesystem to dredge up timestamps for the
given course

Commit 7146aa4

Commit message has the important details, but here's a brief summary:

This one fixes a latent bug in an existing test case that appeared because the test case did not explicitly set course enrollment 'created' dates for the CourseEnrollmentFactory instantiations. After we added new CourseEnrollmentFactory created instances, the created date got bumped past the date check.

Commit 8aeced4

New functions to call backfill for a site and date and track the backfill to a log file

Commit 0fa8880

Add new test module, remove test class from old test module

This exists to help identify when we should include a course in
performing backfill to `CourseDailyMetrics` for a given date in the
past.

Getting the `CourseEnrollment.created` value for the first enrollment
provides a fast "close enough best guess" when a course was first
created, as we cannot rely on CourseOverview timestamps and this avoids
diving into MongoDB or the filesystem to dredge up timestamps for the
given course
@johnbaldwin

johnbaldwin commented Apr 12, 2022 •

Copy link
Copy Markdown
Contributor Author

Interesting. Apparently the new test, test_first_enrollment_timestamp_with_enrollments has a side effect causing the following test failure in a different test module, and only when at least both test modules are executed.

The new code in this PR appears to cause the following to fail in multisite environments.

tests/pipeline/test_course_daily_metrics.py ......F............s

 =================================== FAILURES ===================================
___ TestCourseDailyMetricsPipelineFunctions.test_get_num_learners_completed ____

self = <tests.pipeline.test_course_daily_metrics.TestCourseDailyMetricsPipelineFunctions object at 0x7f443a6d7e90>

    def test_get_num_learners_completed(self):
        actual = pipeline_cdm.get_num_learners_completed(
            course_id=self.course_overview.id,
            date_for=self.today)
>       assert actual == len(self.generated_certificates)
E       assert 2 == 3

johnbaldwin and others added 4 commits April 13, 2022 10:00
This one was mildly interesting:

* TestCourseDailyMetricsPipelineFunctions.test_get_num_learners_completed was failing in the multisite environemnts
* The expected number of certificates was not being returned
* The cert dates are based on the enrollment dates and the enrollments, created vis CourseEnrollmentFactory, were assigning whatever the increment was in the factory
* This was fine until the critical extra `CourseEnrollmentFactory` calls were made in the new `test_course.py`
`TestCourse.test_first_enrollment_timestamp_with_enrollments` function
* Then it pushed the date over the limit

So this commit sets explicit `created` values for the the enrollments in the `TestCourseDailyMetricsPipelineFunctions` test class

Also, ran flake8 on this test module and removed unused inports
This commit has new functions in `figures.pipeline.backfill`.
For each function, see the details in each function's docstring

Because we don't want to update courses created after the "date_for", we
need to identify which courses to update. That's what the bulk of this
commit's code does

It uses the existing metrics loader classes to do the actual data
querying and metrics model saves

Add backfill dialy metrics managmenet command

* Still working on getting the mgmt test to pass. Having errors in parsing
the args call to `call_command`, Will pick up again to get this working
tomorrow
@johnbaldwin
johnbaldwin force-pushed the john/daily-metrics-backfill branch from 34115a3 to 97d5346 Compare April 14, 2022 23:39
* Removed old test code from tests/test_commands.py
* Added new test class module to tests/commands/
* Fixed bug in management command
@johnbaldwin
johnbaldwin marked this pull request as ready for review April 15, 2022 21:46
@johnbaldwin

Copy link
Copy Markdown
Contributor Author

@OmarIthawi @melvinsoft @shadinaif @estherjsuh @bryanlandia This is ready for review

@johnbaldwin

Copy link
Copy Markdown
Contributor Author

@OmarIthawi @melvinsoft @shadinaif @estherjsuh @bryanlandia Following up, would someone please review?

@bryanlandia bryanlandia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I left a few comments but approving... .not the most thorough review but it all looks like it makes sense and has good test coverage 🎉

def figures_backfill_log_dir():
"""Specify where to write Figures backfill log files
"""
return settings.ENV_TOKENS['FIGURES'].get('BACKFILL_LOG_DIR',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure what the policy is across Figures, whether to just throw an error is there is no ENV_TOKENS['FIGURES'], but if in other places you do a .get() for those you should do so here too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@bryanlandia settings.ENV_TOKENS['FIGURES'] should always exist when Figures is instantiated as an Open edX plugin. Please see here: https://github.com/appsembler/figures/blob/main/figures/settings/lms_production.py#L127

What I did was create an issue to note that you raised the issue and as a place to discuss how to improve this architecture: #456

course_ids = courses_enrolled_on_or_before(site, date_for)
course_id_count = len(course_ids)

with open(filepath, 'a', encoding='utf-8') as logfile:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why append here and not overwrite if you are starting a new backfill?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a log file that has the site-id and date-for as the unique identifiers. Setting to append is a safe and simple way to make sure that we don't wipe and overwrite an existing log in the event an exception gets raised and we have to manually fix something and restart the backfill for that site and date.

A obvious improvement is to rename an exiting log to say dot some number or dot timestamp. But this is an improvement that is not essential at this time. If we find it a pain point we can improve it

site=self.site,
date_for=date_for)

open_mock.assert_called_with(results['logfile'], 'a', encoding='utf-8')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if you did change from append to overwrite then update here too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point if we did. I'll make inline comments that address your comments

@johnbaldwin

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing @bryanlandia!

@johnbaldwin
johnbaldwin merged commit 5be90ed into main Apr 26, 2022
@johnbaldwin
johnbaldwin deleted the john/daily-metrics-backfill branch April 26, 2022 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants