simplestatex v0.3.0 SimpleStatEx

SimpleStatEx is a lightweight library that supports logging simple statistics for any elixir project, including the Phoenix Framework. Stats are stored via ecto to your data store or in memory. They are rolled up by category and time window and can be queried conveniently. SimpleStatEx provides the recommended interface to your stats.

Link to this section Summary

Functions

Retrieve a stat using simple stat query builder helpers. This is usually called via pipe from SimpleStatEx.query

See get/1 above but only return one result with no list structure

Add a limit to a stat query, overriding the default 1

Attempt to transform any simple stat operation into using memory instead of repository. Meant for use in piping from other parts of this interface such as stat and query

Add an offset to a stat query, overriding the default 0

Build a stat query that can be used to obtain results from the database or stat set. You are free to query using Ecto in any way you like, Simple Stats helpers simple give you an easy interface to query in the suggested way, and are compatible with the Stat Sets held in memory

Save a stat or stat container to the datastore or to state. If within the time and period of a stat of the same category, updates the counter, incrementing by your new stat’s count

Generate a stat model based on passed arguments

Link to this section Functions

Link to this function get!(stat_query_tuple)
Link to this function get!(stat_query_tuple, atom)

Retrieve a stat using simple stat query builder helpers. This is usually called via pipe from SimpleStatEx.query.

Example

iex> SimpleStatEx.get(%SimpleStat{category: “mongol visit”, period: :daily}, %SimpleStatQuery{limit: 7, offset: 7}) {:ok,

[%{category: "mongol visit", period: "daily", time: ~N[2018-01-10 00:00:00.000000],
 updated_at: ~N[2018-01-10 05:26:03.562011]}]}

iex> SimpleStatEx.query(“mongol visit”) |> SimpleStatEx.limit(7) |> SimpleStatEx.offset(7) |> SimpleStatEx.get() {:ok, [%{category: “test”, period: “daily”, time: ~N[2018-01-10 00:00:00.000000], updated_at: ~N[2018-01-10 05:26:03.562011]}]}

Link to this function get(stat_query_tuple, atom)

See get/1 above but only return one result with no list structure

Example

iex> SimpleStatEx.get(%SimpleStatQuery{category: “mongol visit”, period: :daily}, :single) {:ok,

%{category: "test", period: "daily", time: ~N[2018-01-10 00:00:00.000000],
 updated_at: ~N[2018-01-10 05:26:03.562011]}}
Link to this function limit(error_reason, limit)

Add a limit to a stat query, overriding the default 1

Example

iex> SimpleStatEx.query(“index visit”) |> SimpleStatEx.limit(50) |> SimpleStatEx.get()

Attempt to transform any simple stat operation into using memory instead of repository. Meant for use in piping from other parts of this interface such as stat and query.

Example

iex> SimpleStatEx.stat(“mongol visit”) |> SimpleStatEx.memory() |> SimpleStatEx.save()

iex> SimpleStatEx.query(“mongol visit”) |> SimpleStatEx.memory() |> SimpleStatEx.get()

Link to this function offset(error_reason, offset)

Add an offset to a stat query, overriding the default 0

Example

# Get 1 day stats from 50 days ago iex> SimpleStatEx.query(“index visit”) |> SimpleStatEx.offset(50) |> Simple StatEx.get()

Link to this function query(category)
Link to this function query(category, period)

Build a stat query that can be used to obtain results from the database or stat set. You are free to query using Ecto in any way you like, Simple Stats helpers simple give you an easy interface to query in the suggested way, and are compatible with the Stat Sets held in memory.

Example

iex> SimpleStatEx.query(“index visit”, :daily) |> SimpleStatEx.limit(10) |> SimpleStatEx.get()

Link to this function save(error_reason)

Save a stat or stat container to the datastore or to state. If within the time and period of a stat of the same category, updates the counter, incrementing by your new stat’s count.

Example

iex> SimpleStatEx.stat(“index visit”) |> SimpleStatEx.save() {:ok,

%SimpleStatEx.SimpleStat{__meta__: #Ecto.Schema.Metadata<:loaded, "simplestats">,
  category: "index visit", count: 1, id: 1,
  inserted_at: ~N[2018-01-10 05:50:35.225979], period: "daily",
  time: #DateTime<2018-01-10 00:00:00Z>,
  updated_at: ~N[2018-01-10 05:50:35.225986]}}

Generate a stat model based on passed arguments

Examples

iex> SimpleStatEx.stat("index visit", :daily)
%SimpleStat{category: "index visit", period: "daily", count: 1, ...}
Link to this function stat(category, period, count \\ 1)