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

✨ Added email.open_rate order option to posts api #12439

Merged

Conversation

kevinansfield
Copy link
Contributor

refs #12420

  • updated order bookshelf plugin's parseOrderOption() method to return multiple order-related properties
    • order same as before, a key-value object of property-direction
    • orderRaw new property that is a raw SQL order string generated from orderRawAttributes() method in models
    • eagerLoad new property that is an array of properties the eagerLoad plugin should use to join across
  • updated pagination.fetchAll() to apply normal order + raw order if both are available and to handle eager loading / joins when options.eagerLoad is populated
  • updated post model to include details for email relationship and to add orderRawAttributes() that allows email.open_rate to be used as an order option

@@ -39,6 +42,15 @@ const order = function order(Bookshelf) {
field = match[1].toLowerCase();
direction = match[2].toUpperCase();

if (orderRawAttributes && orderRawAttributes[field]) {
const {orderByRaw, eagerLoad} = orderRawAttributes[field];
orderRaw.push(`${orderByRaw} ${direction}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thinking it would probably be better to pass direction to this.orderRawAttributes() because appending it like this is unlikely to work with all possible raw order queries.

Copy link
Member

Choose a reason for hiding this comment

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

How would the call to this.orderRawAttributes() look like? Would we be storing full orderRaw value including order inside the orderRawAttributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const orderRawAttributes = this.orderRawAttributes(withRelated, direction);
...
    orderRawAttributes: function orderRawAttributes(withRelated, direction) {
        if (withRelated && withRelated.indexOf('email') > -1) {
            return {
                'email.open_rate': {
                    orderByRaw: `emails.opened_count / emails.email_count * 100 ${direction}`,
                    eagerLoad: 'email.open_rate'
                }
            };
        }
    },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's something we can change in the future if/when needed though. I was thinking ahead to having multi-clause order by queries but it's an edge case, not needed for our current use-cases.

if (withRelated && withRelated.indexOf('email') > -1) {
return {
'email.open_rate': {
orderByRaw: 'emails.opened_count / emails.email_count * 100',
Copy link
Member

Choose a reason for hiding this comment

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

this orderByRaw expression will become just open_rate once the column is introduced, correct? In that case we might not even need a special "raw" clause and order by relation just like we do with post_meta fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the column was a backup in case the on-the-fly ordering wasn't feasible.

@naz
Copy link
Member

naz commented Nov 30, 2020

Added questions about minor things (and a small broken test I used to check the functionality), otherwise LGTM!

refs TryGhost#12420

- updated `order` bookshelf plugin's `parseOrderOption()` method to return multiple order-related properties
  - `order` same as before, a key-value object of property-direction
  - `orderRaw` new property that is a raw SQL order string generated from `orderRawQuery()` method in models
  - `eagerLoad` new property that is an array of properties the `eagerLoad` plugin should use to join across
- updated `pagination.fetchAll()` to apply normal order + raw order if both are available and to handle eager loading / joins when `options.eagerLoad` is populated
- updated post model to include details for email relationship and to add `orderRawQuery()` that allows `email.open_rate` to be used as an order option
@kevinansfield kevinansfield merged commit 9ff7423 into TryGhost:master Dec 3, 2020
@kevinansfield kevinansfield deleted the sort-posts-by-open-rate branch December 3, 2020 20:13
allouis added a commit to allouis/Ghost that referenced this pull request Dec 7, 2020
…porter

* upstream/master:
  Update dependency @sentry/node to v5.29.0
  v3.39.2
  Updated Ghost-Admin to v3.39.2
  Added guard for page.items existing in Mailgun response
  Added guard for page.items existing in Mailgun response
  v3.39.1
  Updated Ghost-Admin to v3.39.1
  Update dependency eslint to v7.15.0
  Fixed posts without open rate being ordered in reverse
  🐛  Fixed page preview
  Changed URL protocol in oembed acceptance tests
  ✨ Added `email.open_rate` order option to posts api (TryGhost#12439)
  Updated logging message for webhooks
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.

None yet

2 participants