keygen-sh / keygen-api

Keygen is a fair source software licensing and distribution API built with Ruby on Rails. For developers, by developers.
https://keygen.sh
Other
846 stars 58 forks source link

Add a meters relationship to entitlements #400

Open ezekg opened 3 years ago

ezekg commented 3 years ago

This would allow a license to have many meters/consumables, each metering an action.

ezekg commented 3 years ago

Meter by entitlement? E.g. a license has many meters/consumables through entitlements.

ezekg commented 2 years ago

Should be able to meter by "use count" as well as by aggregate "use time." These would both be the same, backed by a number, but just consumed and displayed differently.

ezekg commented 2 years ago

Should be able to meter by "uniques", via a unique ID. For example, metering by unique client pings per-month.

POST /uses
{
  "data": {
    "type": "use",
    "id": "some-client-id",
    "attributes": {
      "created": "<timestamp>",
      "amount": 1
    },
    "relationships": {
      "meter": {
        "data": { "type": "meter", "id": "..." }
      }
    }
  }
}
ezekg commented 2 years ago

Add consumption strategy to a meter. Options: DECREMENT, INCREMENT.

ezekg commented 2 years ago

Ref: https://www.m3ter.com/

ezekg commented 2 years ago

Maybe we should set this up similarly to entitlements, allowing meters to be attached via the policy or the license.

class Meter
  belongs_to :account
  has_many :policy_meters
  has_many :license_meters

  attribute :consumption_strategy,
    options: %w[INCREMENT DECREMENT]
end

class PolicyMeter
  belongs_to :policy
  belongs_to :meter
end

class LicenseMeter
  belongs_to :license
  belongs_to :meter
end

class LicenseUse
  belongs_to :policy_meter,  optional: true
  belongs_to :license_meter, optional: true
  belongs_to :license

  validates :meter?,
    message: 'meter is missing'

  # Used for tracking when a use occurred
  has_timestamps

  def meter  = policy_meter.presence || license_meter
  def meter? = meter.present?
end
ezekg commented 2 years ago

My big fear here is volume. This may require us to offload to a third-party, or use a different DB such as Clickhouse.

Performance is already hard enough for request/event logs.