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

Ensure we don't send unnecessary requests to the Watson API #415

Closed
1 task done
dkotter opened this issue Mar 21, 2023 · 3 comments · Fixed by #455
Closed
1 task done

Ensure we don't send unnecessary requests to the Watson API #415

dkotter opened this issue Mar 21, 2023 · 3 comments · Fixed by #455
Assignees
Labels
type:bug Something isn't working.
Milestone

Comments

@dkotter
Copy link
Collaborator

dkotter commented Mar 21, 2023

Describe the bug

While testing #405 I noticed I was getting HTTP requests to the Watson API even when on the OpenAI settings page. In investigating further, I was seeing requests to this API on every single page load, including the front-end. This can result in potentially thousands of extra API requests on sites with lots of traffic.

In investigating this, the issue is in the can_register method of the NLU class. The can_register method for each Provider is called on init, so this code runs on all requests. The problem in the NLU class is we make an authentication request within can_register, so we hit the API on every single request.

This may have just been an oversight when this was built or it may have been done purposely to ensure we always have a valid connection but I would classify this as a bug that we need to fix. I would suggest we change how this works to match what we do for ComputerVision and ChatGPT, where we check these credentials when settings are saved and store the result of that, which we then check in the can_register methods.

The only downside I can think of is there is a scenario where credentials are valid when they are saved but later an API key may be revoked. Checking credentials more often allows us to alert the user sooner, though I don't think we are actually outputting any error message at the moment if that happens. So I don't think there's any issue with making this change.

Steps to Reproduce

Install a plugin like Query Monitor and inspect the HTTP requests that are made on any page load

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@dkotter dkotter added the type:bug Something isn't working. label Mar 21, 2023
@jeffpaul jeffpaul added this to the 2.0.0 milestone Mar 21, 2023
@jeffpaul jeffpaul moved this from Incoming to To Do in Open Source Practice Mar 21, 2023
@Spoygg Spoygg moved this from To Do to In Progress in Open Source Practice Mar 28, 2023
@dkotter dkotter moved this from In Progress to To Do in Open Source Practice Apr 24, 2023
@benlk benlk self-assigned this Apr 28, 2023
@benlk benlk moved this from To Do to In Progress in Open Source Practice Apr 28, 2023
@benlk
Copy link
Contributor

benlk commented Apr 28, 2023

Other Providers have an is_configured() method unmodified from parent::is_configured(), but NLU replaces the parent method with a check to see whether the plugin itself is configured?

/**
* Returns whether the provider is configured or not.
*
* @return bool
*/
public function is_configured() {
return get_option( 'classifai_configured', false );
}

It seems like we could get rid of this is_configured and let NLU fall back to the parent is_configured():

/**
* Returns whether the provider is configured or not.
*
* @return bool
*/
public function is_configured() {
$settings = $this->get_settings();
$is_configured = false;
if ( ! empty( $settings ) && ! empty( $settings['authenticated'] ) ) {
$is_configured = true;
}
return $is_configured;
}

Then NLU's can_register() becomes a wrapper around NLU's is_configured().

If this sounds like a good approach, I'll update my PR to match.

@dkotter
Copy link
Collaborator Author

dkotter commented May 5, 2023

@benlk The is_configured method was just barely added in the last release (see https://github.com/10up/classifai/pull/411/files#diff-39e68d5df2219939bc675114fe73e5922f0ef0ccd6c13dee7fc500ba34c8afb8R389) so I don't believe any providers are currently taking advantage of that (I believe only the new user onboarding is using that).

That said, one of my main goals over the last month or two has been trying to clean up the codebase and bring alignment in approaches. The way NLU is set up differs from our other providers and the goal is to make those as similar as possible. So this is a great opportunity to help with that.

I'd suggest looking to use the is_configured method in all providers can_register method. This should be fairly plug-and-play other than for NLU, as we'll need to modify how we save settings to set the authenticated property (right now looks like we store that in an option).

@benlk
Copy link
Contributor

benlk commented May 9, 2023

Next steps:

  • one of these:
    • change how NLU stores its settings to save the authenticated property, or
    • change NLU's is_configured method to check the custom place where authenticated is saved
    • save NLU's authenticated in both the current place and the old place, then write the is_configured method to check both places, with an eye to deprecating the old method
  • modify can_register() to wrap the customized is_configured()

@benlk benlk assigned dkotter and unassigned benlk May 9, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Merged in Open Source Practice May 18, 2023
@vikrampm1 vikrampm1 moved this from Merged to Done/Released in Open Source Practice May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working.
Projects
Archived in project
4 participants