Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Sep 20, 2024
1 parent c405c42 commit 2246978
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 172 deletions.
134 changes: 0 additions & 134 deletions lib/sanbase/clickhouse/query/environment.ex

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Sanbase.SanLang.Environment do
defmodule Sanbase.Environment do
defstruct env_bindings: %{}, local_bindings: %{}

@type t :: %__MODULE__{
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/queries/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ defmodule Sanbase.Queries do
def run_query(%Query{} = query, user, query_metadata, opts \\ []) do
query_metadata = Map.put_new(query_metadata, :sanbase_user_id, user.id)

with {:ok, environment} <- Environment.new(query, user),
{:ok, query} <- resolve_code_parameters(query, user),
with {:ok, query} <- resolve_code_parameters(query, user),
{:ok, environment} <- Environment.new(query, user),
{:ok, result} <- Queries.Executor.run(query, query_metadata, environment) do
maybe_store_execution_data_async(result, user.id, opts)

Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/san_lang/interpreter.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Sanbase.SanLang.Interpreter do
alias Sanbase.SanLang
alias Sanbase.SanLang.Environment
alias Sanbase.Environment
alias Sanbase.SanLang.UnboundError
alias Sanbase.SanLang.UndefinedFunctionError
alias Sanbase.SanLang.OperatorArgumentError
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/san_lang/kernel.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Sanbase.SanLang.Kernel do
alias Sanbase.SanLang.Environment
alias Sanbase.Environment
alias Sanbase.SanLang.Interpreter
alias Sanbase.SanLang.FunctionArgumentError

Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/san_lang/san_lang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Sanbase.SanLang do
- Comparisons and boolean expressions: ==, !=, >, <, >=, <=, and, or
- 1 + 2 * 3 + 10 > 10 => true
"""
alias Sanbase.SanLang.Environment
alias Sanbase.Environment
alias Sanbase.SanLang.Interpreter

import Sanbase.Utils.Transform, only: [to_bang: 1]
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/template_engine/code_evaluation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Sanbase.TemplateEngine.CodeEvaluation do
The inner_content of the capture map is evaluated, using the lang and lang_version
specified.
"""
@spec eval(CaptureMap.t(), SanLang.Environment.t()) :: {:ok, any()} | {:error, String.t()}
@spec eval(CaptureMap.t(), Environment.t()) :: {:ok, any()} | {:error, String.t()}
def eval(capture, env) do
with true <- lang_supported?(capture),
{:ok, result} <- do_eval(capture.inner_content, capture.lang, env) do
Expand Down
8 changes: 4 additions & 4 deletions lib/sanbase/template_engine/template_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ defmodule Sanbase.TemplateEngine do
alias Sanbase.TemplateEngine
alias Sanbase.TemplateEngine.CodeEvaluation

@type option :: {:params, map()} | {:env, Sanbase.SanLang.Environment.t()}
@type option :: {:params, map()} | {:env, Sanbase.Environment.t()}

@typedoc ~s"""
The TemplateEngine function can accept the following options:
- params: A map of key-value pairs that will be used to replace the simple keys in the template.
The simple keys are the onces that are in the format {{<param_name}}.
- env: A SanLang environment that will be used to evaluate the code templates in the template.
- env: A Environment that will be used to evaluate the code templates in the template.
"""
@type opts :: [option()]

Expand Down Expand Up @@ -68,7 +68,7 @@ defmodule Sanbase.TemplateEngine do
@spec run(String.t(), opts) :: {:ok, String.t()} | {:error, String.t()}
def run(template, opts \\ []) do
params = Keyword.get(opts, :params, %{}) |> Map.new(fn {k, v} -> {to_string(k), v} end)
env = Keyword.get(opts, :env, Sanbase.SanLang.Environment.new())
env = Keyword.get(opts, :env, Sanbase.Environment.new())

with {:ok, captures} <- TemplateEngine.Captures.extract_captures(template) do
template =
Expand Down Expand Up @@ -113,7 +113,7 @@ defmodule Sanbase.TemplateEngine do
@spec run_generate_positional_params(String.t(), opts) :: {String.t(), list(any())}
def run_generate_positional_params(template, opts) do
params = Keyword.get(opts, :params, %{}) |> Map.new(fn {k, v} -> {to_string(k), v} end)
env = Keyword.get(opts, :env, Sanbase.SanLang.Environment.new())
env = Keyword.get(opts, :env, Sanbase.Environment.new())

with {:ok, captures} <- TemplateEngine.Captures.extract_captures(template),
{:ok, result} <- do_run_generate_positional_params(template, captures, params, env) do
Expand Down
13 changes: 13 additions & 0 deletions test/sanbase/queries/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dt,from,to,value,valueExactBase36,blockNumber,transactionHash,transactionPosition,type,primaryKey
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
2015-07-30 15:26:13,GENESIS,0x001d14804b399c6ef80e64576f657660804fec0b,4.2e21,omdmprieouisqo,0,GENESIS_001d14804b399c6ef80e64576f657660804fec0b,0,genesis,2
38 changes: 13 additions & 25 deletions test/sanbase/san_lang/san_lang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ defmodule Sanbase.SanLangTest do
end

test "env vars are pulled from the environment" do
env = SanLang.Environment.new()
env = SanLang.Environment.put_env_bindings(env, %{"a" => 1, "b" => "some string value"})
env = Environment.new()
env = Environment.put_env_bindings(env, %{"a" => 1, "b" => "some string value"})

assert SanLang.eval!("@a", env) == 1
assert SanLang.eval!("@b", env) == "some string value"
end

test "access operator" do
env = SanLang.Environment.new()
env = Environment.new()

env =
SanLang.Environment.put_env_bindings(env, %{
Environment.put_env_bindings(env, %{
"slugs" => %{"bitcoin" => %{"github_orgs" => ["bitcoin-core-dev", "bitcoin"]}}
})

Expand Down Expand Up @@ -90,10 +90,10 @@ defmodule Sanbase.SanLangTest do
end

test "map/2" do
env = SanLang.Environment.new()
env = Environment.new()

env =
SanLang.Environment.put_env_bindings(env, %{
Environment.put_env_bindings(env, %{
"data" => [1, 2, 3]
})

Expand All @@ -102,10 +102,10 @@ defmodule Sanbase.SanLangTest do
end

test "filter/2" do
env = SanLang.Environment.new()
env = Environment.new()

env =
SanLang.Environment.put_env_bindings(env, %{
Environment.put_env_bindings(env, %{
"data" => [%{"key" => 1}, %{"key" => 2}, %{"key" => 3}],
"data2" => [1, 2, 3.14, 4, 5, 6.15]
})
Expand All @@ -116,10 +116,10 @@ defmodule Sanbase.SanLangTest do
end

test "map/2 + flat_map/2 + map_keys/1" do
env = SanLang.Environment.new()
env = Environment.new()

env =
SanLang.Environment.put_env_bindings(env, %{
Environment.put_env_bindings(env, %{
"projects" => %{
"bitcoin" => %{"github_organizations" => ["bitcoin", "bitcoin-core-dev"]},
"santiment" => %{"github_organizations" => ["santiment"]}
Expand All @@ -138,23 +138,11 @@ defmodule Sanbase.SanLangTest do
end

test "arithmetic, env vars and access operator" do
env = SanLang.Environment.new()

env = SanLang.Environment.put_env_bindings(env, %{"pi" => 3.14, "vals" => %{"pi" => 3.14}})
env =
Environment.new()
|> Environment.put_env_bindings(%{"pi" => 3.14, "vals" => %{"pi" => 3.14}})

assert SanLang.eval!("@pi * 1000", env) == 3140.0
assert SanLang.eval!(~s|@vals["pi"] * 1000|, env) == 3140.0
end

test "include external file in the environment and use it" do
env = SanLang.Environment.new()

"""
{% addresses = load_external_csv("user/32/my_labels.csv") %}
SELECT *
FROM balances
WHERE address in {% addresses %}
"""
end
end
4 changes: 2 additions & 2 deletions test/sanbase/template_engine/template_engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ defmodule Sanbase.TemplateEngineTest do
end

test "Run code accessing the env" do
env = Sanbase.SanLang.Environment.new()
env = Sanbase.Environment.new()

env =
Sanbase.SanLang.Environment.put_env_bindings(env, %{"a" => 1, "b" => "some string value"})
Sanbase.Environment.put_env_bindings(env, %{"a" => 1, "b" => "some string value"})

template = """
1 + @a = {% 1 + @a %}
Expand Down

0 comments on commit 2246978

Please sign in to comment.