Skip to content

Instantly share code, notes, and snippets.

@mort
Created August 19, 2011 12:51
Show Gist options
  • Select an option

  • Save mort/1156727 to your computer and use it in GitHub Desktop.

Select an option

Save mort/1156727 to your computer and use it in GitHub Desktop.
Beancounter API
class Activity < ActiveRecord::Base
def beancount
Rails.logger.info("Beancounting model")
# Counter for records of this class
# This will update a counter with a key of the form ["activity"]
beancount_model
Rails.logger.info("Beancounting attributes")
# Counter for appearances of specific values for attributes, including support for single (:foo) or multiple [:foo,:bar,:baz] attribute keys.
# For a given record, this will update counters of the form
# ["activity", "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "verb", "post"]
# ["activity", "object_type", "item"]
# ["activity", "verb", "post", "object_type", "item"]
beancount_attributes :attrs => [:actor_token, :verb, :object_type, :object_url, :target_type, :target_token, :target_url, [:actor_token, :target_token], [:verb, :object_type]]
Rails.logger.info("Beancounting timeseries")
# Counter for appearances of specific values for attributes, distributed for several (almost every) possible combinations of invoking the "timeseries_fields" params on each of the "timestamps" params
# For a given record, this will update counters of the form
# ["activity", "created_at", "mday", 3, "month", 8, "wday", 3, "year", 2011, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "mday", 3, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "month", 8, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "wday", 3, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "year", 2011, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "mday", 3, "month", 8, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "mday", 3, "wday", 3, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
# ["activity", "created_at", "mday", 3, "year", 2011, "actor_token", "0b0998bac7f5d07be419fb21a6fae35c"]
beancount_timeseries :attrs => [:actor_token, :verb, :object_type, :object_url, :target_type, :target_token, :target_url], :timestamps => [:created_at, :published], :timeseries_fields => [:year, :month, :wday, :mday]
# The "scope" option will add to the key the result of invoking the "foo_id" method on the record
Rails.logger.info("Beancounting model with scope")
beancount_model :scopes => [:app]
Rails.logger.info("Beancounting attributes with scope")
beancount_attributes :attrs => [:actor_token, :verb], :scopes => [:app]
Rails.logger.info("Beancounting timeseries with scope")
beancount_timeseries :attrs => [:actor_token, :verb], :timestamps => [:created_at, :published], :timeseries_fields => [:year, :month, :wday, :mday], :scopes => [:app]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment