View Source Trifle.Stats.Configuration (Trifle.Stats v2.5.0)

Configuration management for Trifle.Stats with Ruby gem compatibility.

Provides comprehensive configuration options including driver-specific settings, time zone management, granularity filtering, and Ruby gem compatibility.

Summary

Functions

Check if a value is blank (Ruby-compatible behavior).

Clear the global configuration. Useful for testing.

Configure Trifle.Stats with a driver and optional settings.

Configure global application settings. This stores configuration in the Application environment so it can be accessed without passing config to every function call.

Get driver-specific options with defaults.

Get the global configuration from Application environment. Returns nil if no global configuration has been set.

Merge additional driver options into configuration.

Get configuration to use, preferring passed config over global config. If neither is provided, raises an error with helpful instructions.

Returns the storage backend for write operations (buffer or raw driver).

Get timezone object compatible with Ruby TZInfo behavior. Returns a timezone struct that can be used for time calculations.

Functions

Check if a value is blank (Ruby-compatible behavior).

Examples

iex> Trifle.Stats.Configuration.blank?([])
true

iex> Trifle.Stats.Configuration.blank?("")
true

iex> Trifle.Stats.Configuration.blank?(nil)
true

iex> Trifle.Stats.Configuration.blank?(false)
true

iex> Trifle.Stats.Configuration.blank?([1, 2])
false

iex> Trifle.Stats.Configuration.blank?("hello")
false

Clear the global configuration. Useful for testing.

Examples

iex> Trifle.Stats.Configuration.clear_global()
:ok
Link to this function

configure(driver, opts \\ [])

View Source

Configure Trifle.Stats with a driver and optional settings.

Basic Examples

iex> {:ok, driver} = Trifle.Stats.Driver.Process.new()
iex> config = Trifle.Stats.Configuration.configure(driver)
iex> config.time_zone
"GMT"

Advanced Configuration

# Ruby-compatible configuration with driver options
iex> {:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/test")
iex> config = Trifle.Stats.Configuration.configure(
...>   Trifle.Stats.Driver.Mongo.new(conn),
...>   time_zone: "Europe/London",
...>   track_granularities: ["1h", "1d"],
...>   beginning_of_week: :sunday,
...>   driver_options: %{
...>     collection_name: "analytics_stats",
...>     joined_identifier: :full,
...>     expire_after: 86400  # 1 day in seconds
...>   }
...> )

Driver-Specific Options

MongoDB Driver Options

  • collection_name: Collection name (default: "trifle_stats")
  • joined_identifier: nil (separated), "full"/"partial" for joined format
  • expire_after: TTL in seconds for automatic expiration

PostgreSQL Driver Options

  • table_name: Table name (default: "trifle_stats")
  • ping_table_name: Ping table name (default: "{table_name}_ping")
  • joined_identifier: nil (separated), "full"/"partial" for table structure

Redis Driver Options

  • prefix: Key prefix (default: "trfl")

SQLite Driver Options

  • table_name: Table name (default: "trifle_stats")
  • ping_table_name: Ping table name (default: "{table_name}_ping")
  • joined_identifier: nil (separated), "full"/"partial" for table structure
Link to this function

configure(driver, time_zone, time_zone_database, beginning_of_week, track_granularities, separator)

View Source

Configure global application settings. This stores configuration in the Application environment so it can be accessed without passing config to every function call.

Examples

# In your application's config/config.exs or in an initializer
driver = Trifle.Stats.Driver.Process.new()
Trifle.Stats.Configuration.configure_global(
  driver: driver,
  time_zone: "Europe/London",
  track_granularities: ["1h", "1d", "1w"],
  beginning_of_week: :sunday
)

# Then use Trifle.Stats functions without passing config
Trifle.Stats.track("page_views", DateTime.utc_now(), %{count: 1})
Link to this function

driver_option(configuration, key, default \\ nil)

View Source

Get driver-specific options with defaults.

Examples

iex> config = %Trifle.Stats.Configuration{
...>   driver_options: %{collection_name: "custom_stats"}
...> }
iex> Trifle.Stats.Configuration.driver_option(config, :collection_name, "trifle_stats")
"custom_stats"

iex> Trifle.Stats.Configuration.driver_option(config, :expire_after, nil)
nil

Get the global configuration from Application environment. Returns nil if no global configuration has been set.

Examples

iex> Trifle.Stats.Configuration.get_global()
nil

# After configure_global has been called
iex> config = Trifle.Stats.Configuration.get_global()
iex> config.time_zone
"Europe/London"
Link to this function

merge_driver_options(config, additional_options)

View Source

Merge additional driver options into configuration.

Examples

iex> config = %Trifle.Stats.Configuration{driver_options: %{}}
iex> updated = Trifle.Stats.Configuration.merge_driver_options(config, %{
...>   table_name: "analytics",
...>   expire_after: 3600
...> })
iex> updated.driver_options
%{table_name: "analytics", expire_after: 3600}

Get configuration to use, preferring passed config over global config. If neither is provided, raises an error with helpful instructions.

Examples

iex> Trifle.Stats.Configuration.resolve_config(nil)
** (RuntimeError) No configuration provided and no global configuration set

iex> config = Trifle.Stats.Configuration.configure(driver)
iex> Trifle.Stats.Configuration.resolve_config(config)
%Trifle.Stats.Configuration{}
Link to this function

set_beginning_of_week(configuration, beginning_of_week)

View Source
Link to this function

set_granularities(configuration, track_granularities)

View Source
Link to this function

set_separator(configuration, separator)

View Source
Link to this function

set_time_zone(configuration, time_zone)

View Source
Link to this function

set_time_zone_database(configuration, time_zone_database)

View Source

Returns the storage backend for write operations (buffer or raw driver).

Get timezone object compatible with Ruby TZInfo behavior. Returns a timezone struct that can be used for time calculations.

Examples

iex> config = %Trifle.Stats.Configuration{time_zone: "Europe/London"}
iex> tz = Trifle.Stats.Configuration.tz(config)
iex> tz.time_zone
"Europe/London"

# Invalid timezone defaults to GMT with warning
iex> config = %Trifle.Stats.Configuration{time_zone: "Invalid/Zone"}
iex> tz = Trifle.Stats.Configuration.tz(config)
# Warning printed: "Trifle: Invalid timezone Invalid/Zone; Defaulting to GMT"
iex> tz.time_zone
"GMT"