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

Write DomainMetrics and add FF to switch reads from calculated properties #35522

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

nospame
Copy link
Contributor

@nospame nospame commented Dec 13, 2024

Technical Summary

SAAS-16283

Writes "calculated properties" currently stored on the Domain Elasticsearch doc to the new DomainMetrics SQL model and adds a feature flag so that reads can be switched from the ES doc to the SQL model once populated. This is part 2 of 3 for moving calculated properties into SQL, following #35415. This should be reviewable by commit.

Force-pushed to pull out migration commits #35574

Feature Flag

Adds the slightly wordy CALCULATED_PROPERTIES_FROM_DOMAIN_METRICS feature flag, which can be used to switch calculated properties reads over to the SQL model for a single domain, to toggle on for all domains.

Safety Assurance

Safety story

This doesn't affect existing data or the write process for calculated properties on the ES doc. Reading from the new model is controlled by a feature flag, so if there are any issues after switching over, it can easily be switched back.

Automated test coverage

Adds automated tests for identifying the correct project spaces to have their DomainMetrics updated. These are based on existing tests that look to see the right domains will get their calculated properties updated.

QA Plan

I can see an argument for QA, but not planning it at this time. I think dev testing on staging will suffice.

Migrations

No migrations here, but must follow migration applied in #35574

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@dimagimon dimagimon added reindex/migration Reindex or migration will be required during or before deploy Risk: High Change affects files that have been flagged as high risk. labels Dec 13, 2024
@nospame nospame requested a review from gherceg December 13, 2024 00:46
@nospame nospame force-pushed the em/write-domain-metrics branch from 075ab84 to 42eafa5 Compare December 19, 2024 18:16
@nospame nospame marked this pull request as ready for review December 19, 2024 19:37
Copy link
Contributor

@gherceg gherceg left a comment

Choose a reason for hiding this comment

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

I know this is a pretty minor migration, but I think the general rule of thumb to apply migrations in a separate PR to be able to easily rollback the code that depends on it if necessary is still valid here. Granted, it looks like you made the migration reversible so I'm not sure how much it really matters, but I'd lean towards moving out the migration since it is pretty straightforward anyway.

Overall this looks great!

metrics_dict['domain'] = domain_obj.name
metrics_dict['last_modified'] = datetime.now(tz=timezone.utc)

# these are properties on the Django model, so don't try to write them
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could be a bit more explicit to say these are "calculated fields on the Django model" or something like that. I recognize that it is a tough situation since we have referred to the DomainMetrics as calculated properties, but hopefully going forward we can limit where we use "calculated properties" to avoid conflicting with the conventional definition of that term.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you think of "these are model properties on DomainMetrics"? "Calculated fields" gets the point across just as well but it doesn't feel as technically correct to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh interesting. In my mind, calculated (or computed) is technically correct when referring to these properties/fields of a model class, since they aren't stored in the table but instead calculated/computed after fetching the object from the database.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair actually, I guess it maybe depends whether you're looking at it from the database's perspective or the code's -- whether it's a "calculated" field versus a regular field, or if it's a "property" versus a regular model method.

Comment on lines +131 to +134
if metrics:
active_users_by_domain[domain] = metrics['active_mobile_workers']

datadog_report_user_stats('commcare.active_mobile_workers.count', active_users_by_domain)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering out loud, but maybe it would be nice to track more of these metrics in datadog.

corehq/apps/reports/tasks.py Outdated Show resolved Hide resolved
@nospame nospame added the product/invisible Change has no end-user visible impact label Jan 2, 2025
@nospame nospame marked this pull request as draft January 2, 2025 18:12
@nospame nospame changed the base branch from master to em/domain-metrics-model-changes January 2, 2025 18:12
@dimagimon dimagimon removed the reindex/migration Reindex or migration will be required during or before deploy label Jan 2, 2025
@nospame nospame closed this Jan 2, 2025
@nospame nospame force-pushed the em/write-domain-metrics branch from 6c5d5b4 to 1f9cff1 Compare January 2, 2025 18:42
@nospame nospame reopened this Jan 2, 2025
@nospame nospame changed the base branch from em/domain-metrics-model-changes to master January 2, 2025 18:45
@dimagimon dimagimon added the reindex/migration Reindex or migration will be required during or before deploy label Jan 2, 2025
@nospame nospame marked this pull request as ready for review January 2, 2025 21:11
@dimagimon dimagimon removed the reindex/migration Reindex or migration will be required during or before deploy label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product/invisible Change has no end-user visible impact Risk: High Change affects files that have been flagged as high risk.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants