ubiquity-os / plugin-template

Template repository for plugins that will run within Ubiquibot.
0 stars 9 forks source link

Build analytics into the template #30

Open Keyrxng opened 3 days ago

Keyrxng commented 3 days ago

Investigating this issue I built a minimal Octokit Proxy to capture access and call counts.

We should built something like this with a toggle maybe into the template especially if we are going to restrict plugin devs to a % of our the hourly call count quota.

We should consider adding support for detecting a secondary rate limit being hit as this is usually the one we hit.

context.octokit is the sole octokit object throughout plugins so it makes it very easily to setup decent analytics through a Proxy.

function octoProxy(octokit: Octokit) {
  const callCount = new Map<string, number>();

  function createProxy(target: any, path: string): any {
    return new Proxy(target, {
      get(target, prop, receiver) {
        const value = Reflect.get(target, prop, receiver);
        const propPath = path ? `${path}.${String(prop)}` : String(prop);

        if (typeof value === 'function') {
          return function (...args: any[]) {
            const count = callCount.get(propPath) || 0;
            callCount.set(propPath, count + 1);
            return value.apply(this, args);
          };
        } else if (typeof value === 'object' && value !== null) {
          return createProxy(value, propPath);
        } else {
          return value;
        }
      },
    });
  }

  const proxy = createProxy(octokit, '');

  return { proxy, callCount };
}
Keyrxng commented 2 days ago

@0x4007 I think this should be given a higher priority and priced for at least a day.

Pros:

Limits have tiers so for V1 we can cover requests-per-minute etc but per-hour would involve calculating how often the plugin will likely run which would pretty difficult to implement.

Make too many requests per minute. No more than 90 seconds of CPU time per 60 seconds of real time is allowed. No more than 60 seconds of this CPU time may be for the GraphQL API. You can roughly estimate the CPU time by measuring the total response time for your API requests. Make too many requests that consume excessive compute resources in a short period of time. Create too much content on GitHub in a short amount of time. In general, no more than 80 content-generating requests per minute and no more than 500 content-generating requests per hour are allowed. Some endpoints have lower content creation limits. Content creation limits include actions taken on the GitHub web interface as well as via the REST API and GraphQL API.