Skip to content

Commit

Permalink
Remove database/bucket/org configuration from series
Browse files Browse the repository at this point in the history
  • Loading branch information
mneudert committed Dec 3, 2020
1 parent fe1a750 commit 3a0c82f
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 183 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ defmodule MySeries do
use Instream.Series

series do
database "my_database"
measurement "my_measurement"

tag :bar
Expand Down
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if Mix.env() == :test do

config :instream, TestHelpers.Connections.DefaultConnection,
auth: [username: "instream_test", password: "instream_test"],
database: "test_database",
loggers: []

config :instream, TestHelpers.Connections.DefaultConnectionV2,
Expand Down
11 changes: 2 additions & 9 deletions lib/instream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ defmodule Instream do
will be searched (in order from top to bottom) for a configured database:
1. `opts[:database]` parameter
2. `Instream.Series` struct (if used)
3. `Instream.Connection` configuration
4. No database used!
2. `Instream.Connection` configuration
3. No database used!
By default the response of a query will be a map decoded from your
server's JSON response.
Expand Down Expand Up @@ -126,7 +125,6 @@ defmodule Instream do
use Instream.Series
series do
database "my_database"
measurement "my_measurement"
tag :bar
Expand Down Expand Up @@ -154,7 +152,6 @@ defmodule Instream do
MyConnection.write(%{
points: [
%{
database: "my_database",
measurement: "my_measurement",
fields: %{answer: 42, value: 1},
tags: %{foo: "bar"},
Expand All @@ -166,9 +163,5 @@ defmodule Instream do
})
* The field `timestamp` can be omitted, so InfluxDB will use the receive time.
* The field `database` can be used to write to a custom database.
Please be aware that only the database from the first point
will be used when writing multiple points.
"""
end
2 changes: 1 addition & 1 deletion lib/instream/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ defmodule Instream.Connection do
Options:
- `database`: write data to a database differing from the point database
- `database`: write data to a database differing from the connection database
- `precision`: write points with a "precision" other than `:nanosecond`
Additional options depend on the writer module configured.
Expand Down
4 changes: 0 additions & 4 deletions lib/instream/data/write.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ defmodule Instream.Data.Write do
determine_database(point, opts)
end

def determine_database(%{__struct__: series}, opts) do
opts[:database] || series.__meta__(:database)
end

def determine_database(%{database: database}, opts) do
opts[:database] || database
end
Expand Down
63 changes: 1 addition & 62 deletions lib/instream/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,10 @@ defmodule Instream.Series do
If you do not want to define the raw maps for writing data you can
pre-define a series module for later usage:
defmodule MySeries.InfluxDBv1 do
defmodule MySeries.InfluxDB do
use Instream.Series
series do
database "my_database_optional"
measurement "cpu_load"
tag :host, default: "www"
tag :core
field :value, default: 100
field :value_desc
end
end
defmodule MySeries.InfluxDBv2 do
use Instream.Series
series do
bucket "my_bucket_optional"
org "my_org_optional"
measurement "cpu_load"
tag :host, default: "www"
Expand Down Expand Up @@ -126,10 +107,6 @@ defmodule Instream.Series do
Please be aware that the UDP protocol writer does not support custom
timestamp precisions. All UDP timestamps are implicitly expected to already
be at nanosecond precision.
_Note:_ While it is possible to write multiple points a once it is currently
not supported to write them to individual databases/buckets. The first point
written defines the database/bucket, other values are silently ignored!
"""

alias Instream.Series.Hydrator
Expand All @@ -155,10 +132,6 @@ defmodule Instream.Series do
quote do
@behaviour unquote(__MODULE__)

@bucket nil
@database nil
@org nil

@measurement nil

Module.register_attribute(__MODULE__, :fields_raw, accumulate: true)
Expand All @@ -178,10 +151,6 @@ defmodule Instream.Series do
@tags_names @tags_raw |> Keyword.keys() |> Enum.sort()
@tags_struct @tags_raw |> Enum.sort(&unquote(__MODULE__).__sort_tags__/2)

def __meta__(:bucket), do: @bucket
def __meta__(:database), do: @database
def __meta__(:org), do: @org

def __meta__(:fields), do: @fields_names
def __meta__(:measurement), do: @measurement
def __meta__(:tags), do: @tags_names
Expand All @@ -205,11 +174,8 @@ defmodule Instream.Series do
## Available information
- `:bucket` - the bucket where the series is stored (optional)
- `:database` - the database where the series is stored (optional)
- `:fields` - the fields in the series
- `:measurement` - the measurement of the series
- `:org` - the organization the `:bucket` belongs to (optional)
- `:tags` - the available tags defining the series
"""
@callback __meta__(atom) :: any
Expand All @@ -228,24 +194,6 @@ defmodule Instream.Series do
"""
@callback from_result(map) :: [struct]

@doc """
Defines the bucket for the series.
"""
defmacro bucket(name) do
quote do
@bucket unquote(name)
end
end

@doc """
Defines the database for the series.
"""
defmacro database(name) do
quote do
@database unquote(name)
end
end

@doc """
Defines a field in the series.
"""
Expand All @@ -264,15 +212,6 @@ defmodule Instream.Series do
end
end

@doc """
Defines the organization for the series.
"""
defmacro org(name) do
quote do
@org unquote(name)
end
end

@doc """
Defines a tag in the series.
"""
Expand Down
34 changes: 5 additions & 29 deletions test/instream/connection/database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,11 @@ defmodule Instream.Connection.DatabaseTest do
]
end

defmodule DatabaseSeries do
defmodule DefaultSeries do
use Instream.Series

series do
database "database_config_seriesdb_test"
measurement "database_config_seriesdb_test"

tag :foo, default: :bar

field :value, default: 100
end
end

defmodule NoDatabaseSeries do
use Instream.Series

series do
measurement "database_config_nodb_test"
measurement "database_config_series"

tag :foo, default: :bar

Expand Down Expand Up @@ -80,31 +67,20 @@ defmodule Instream.Connection.DatabaseTest do

assert String.contains?(message, "database not found")
assert String.contains?(message, opts[:database])
refute String.contains?(message, InvalidDbConnection.config(:database))
end

test "write || default: database from connection" do
%{error: message} = InvalidDbConnection.write(%NoDatabaseSeries{})

assert String.contains?(message, "database not found")
assert String.contains?(message, InvalidDbConnection.config(:database))
end

test "write || series database has priority over connection database" do
%{error: message} = InvalidDbConnection.write(%DatabaseSeries{})
%{error: message} = InvalidDbConnection.write(%DefaultSeries{})

assert String.contains?(message, "database not found")
assert String.contains?(message, DatabaseSeries.__meta__(:database))
refute String.contains?(message, InvalidDbConnection.config(:database))
end

test "write || opts database has priority over series database" do
test "write || opts database has priority over connection database" do
opts = [database: "database_config_optsdb_test"]

%{error: message} = InvalidDbConnection.write(%DatabaseSeries{}, opts)
%{error: message} = InvalidDbConnection.write(%DefaultSeries{}, opts)

assert String.contains?(message, "database not found")
assert String.contains?(message, opts[:database])
refute String.contains?(message, DatabaseSeries.__meta__(:database))
end
end
1 change: 0 additions & 1 deletion test/instream/connection/error_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Instream.Connection.ErrorTest do
use Instream.Series

series do
database "test_database"
measurement "connection_error_tests"

tag :foo, default: :bar
Expand Down
5 changes: 2 additions & 3 deletions test/instream/connection/writer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule Instream.Connection.WriterTest do
use Instream.Series

series do
database "test_database"
measurement "custom_connection_writer"

field :binary
Expand Down Expand Up @@ -74,12 +73,12 @@ defmodule Instream.Connection.WriterTest do
:ok =
%{binary: "binary"}
|> WriterSeries.from_map()
|> WriterConnection.write()
|> WriterConnection.write(database: "test_database")

%{error: error} =
%{binary: 12_345}
|> WriterSeries.from_map()
|> WriterConnection.write()
|> WriterConnection.write(database: "test_database")

assert "error has changed!" = error
end
Expand Down
3 changes: 1 addition & 2 deletions test/instream/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Instream.ConnectionTest do
use Instream.Series

series do
database "test_database"
measurement "data_write_struct"

tag :bar
Expand Down Expand Up @@ -147,7 +146,7 @@ defmodule Instream.ConnectionTest do
value: 17
}
|> TestSeries.from_map()
|> DefaultConnection.write()
|> DefaultConnection.write(database: @database)

assert %{results: [%{series: [%{tags: values_tags, values: value_rows}]}]} =
DefaultConnection.query(
Expand Down
3 changes: 1 addition & 2 deletions test/instream/log/default_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Instream.Log.DefaultLoggerTest do
use Instream.Series

series do
database "test_database"
measurement "log_write_entry_test"

tag :t
Expand Down Expand Up @@ -130,7 +129,7 @@ defmodule Instream.Log.DefaultLoggerTest do

log =
capture_log(fn ->
:ok = LogConnection.write(points)
:ok = LogConnection.write(points, database: "test_database")

:timer.sleep(10)
end)
Expand Down
Loading

0 comments on commit 3a0c82f

Please sign in to comment.