PhoenixAnalytics.Services.Utility (PhoenixAnalytics v0.4.1)
View SourceA helper module containing static utility functions for Phoenix Analytics.
This module provides various utility functions that are used across the Phoenix Analytics application. It includes functions for timestamp generation and device type detection based on user agent strings.
Summary
Functions
Determines the current database type based on configuration.
Gets a configuration value with an optional default.
Determines the device type based on the user agent string.
Checks if a specific configuration key exists and has a value.
Returns the current UTC timestamp in database-compatible format.
Generates a new UUID (Universally Unique Identifier).
Functions
Determines the current database type based on configuration.
This function checks the application environment for database configurations to determine the current database type being used.
Returns
An atom indicating the database type:
:postgresif PostgreSQL is configured:sqliteif SQLite is configured:mysqlif MySQL is configured:postgresas a fallback if no valid configuration is found
Examples
iex> PhoenixAnalytics.Services.Utility.database_type()
:postgres
Gets a configuration value with an optional default.
Parameters
- key - The configuration key to retrieve
- default - The default value if the key is not found
Returns
The configuration value or the default value.
Examples
iex> PhoenixAnalytics.Services.Utility.get_config(:pool_size, 10)
10
Determines the device type based on the user agent string.
This function provides a simple and fast way to categorize devices as mobile, tablet, or desktop based on the user agent string. It was implemented as a lightweight alternative to ua_parser and ua_inspector, which were found to be excessively slow for this use case.
While this method is not as comprehensive as full-fledged user agent parsing libraries, it offers a quick classification that is sufficient for many analytics purposes.
Parameters
- agent_string - The user agent string to analyze
Returns
A string indicating the device type:
- "mobile" for mobile devices
- "tablet" for tablet devices
- "desktop" for desktop devices or any unrecognized device type
Examples
iex> PhoenixAnalytics.Services.Utility.get_device_type("Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1")
"mobile"
iex> PhoenixAnalytics.Services.Utility.get_device_type("Mozilla/5.0 (iPad; CPU OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1")
"tablet"
iex> PhoenixAnalytics.Services.Utility.get_device_type("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
"desktop"
Checks if a specific configuration key exists and has a value.
Parameters
- key - The configuration key to check
Returns
A boolean indicating whether the configuration exists and has a value.
Examples
iex> PhoenixAnalytics.Services.Utility.has_config?(:postgres_conn)
true
Returns the current UTC timestamp in database-compatible format.
This function generates a timestamp string that is compatible with standard database TIMESTAMP data types. The timestamp is in UTC and includes millisecond precision.
Examples
iex> PhoenixAnalytics.Services.Utility.inserted_at()
"2023-05-15 14:30:45.123"Returns a string in the format "YYYY-MM-DD HH:MM:SS.mmm".
Generates a new UUID (Universally Unique Identifier).
This function creates a version 4 UUID, which is randomly generated.
Examples
iex> PhoenixAnalytics.Services.Utility.uuid()
"550e8400-e29b-41d4-a716-446655440000"Returns a string representation of the UUID.