在Phoenix Framework中,如何获取当前环境的名称?
我已经尝试过用env变量读取变量了System.get_env("MIX_ENV"),但是并不总是设置值.
我想测试一个正在使用的函数 Task.async
为了使我的测试通过,我需要让它在断言前保持100ms睡眠,否则测试过程在异步任务执行之前被杀死.
有没有更好的办法?
编辑,添加代码示例:
我想测试的代码(大致):
def search(params) do
RateLimiter.rate_limit(fn ->
parsed_params = ExTwitter.Parser.parse_request_params(params)
json = ExTwitter.API.Base.request(:get, "1.1/search/tweets.json", parsed_params)
Task.async(fn -> process_search_output(json) end)
new_max_id(json)
end)
end
Run Code Online (Sandbox Code Playgroud)
我已经写过的测试(仅用于调用睡眠)
test "processes and store tweets" do
with_mock ExTwitter.API.Base, [request: fn(_,_,_) -> json_fixture end] do
with_mock TwitterRateLimiter, [rate_limit: fn(fun) -> fun.() end] do
TSearch.search([q: "my query"])
:timer.sleep(100)
# assertions
assert called TStore.store("some tweet from my fixtures")
assert called TStore.store("another one")
end
end
end
Run Code Online (Sandbox Code Playgroud) 我使用MongoDB的,我想生成博客文章独特的cryptical标识(将在宁静的网址都必须使用),如s52ruf6wst或xR2ru286zjI.
您认为什么是最好的,以及生成这些ID的更具可扩展性的方式?
我在考虑以下架构:
WDYT?
我想计算点集群,并为每个集群获取特定属性的总和(比如,集群中每个点的得分总和)
我已经设法使用ST_ClusterWithin但是我无法计算总和.
这是我尝试过的:
SELECT sum(score), unnest(ST_ClusterWithin(coordinates, 0.1)) AS cluster
FROM locations
GROUP BY cluster;
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误 ERROR: aggregate functions are not allowed in GROUP BY
如果我删除了GROUP BY,我得到所有位置的得分总和,这不是我想要的(我想要群集中位置的总和)
我想检索另一组点的给定范围内的所有点.比方说,找到距离任何地铁站500米范围内的所有商店.
我写了这个查询,这很慢,并且想要优化它:
SELECT DISCTINCT ON(locations.id) locations.id FROM locations, pois
WHERE pois.poi_kind = 'subway'
AND ST_DWithin(locations.coordinates, pois.coordinates, 500, false);
Run Code Online (Sandbox Code Playgroud)
我正在使用最新版本的Postgres和PostGis(Postgres 9.5,PostGis 2.2.1)
这是表元数据:
Table "public.locations"
Column | Type | Modifiers
--------------------+-----------------------------+--------------------------------------------------------
id | integer | not null default nextval('locations_id_seq'::regclass)
coordinates | geometry |
Indexes:
"locations_coordinates_index" gist (coordinates)
Table "public.pois"
Column | Type | Modifiers
-------------+-----------------------------+---------------------------------------------------
id | integer | not null default nextval('pois_id_seq'::regclass)
coordinates | geometry |
poi_kind_id | integer |
Indexes:
"pois_pkey" PRIMARY KEY, btree (id)
"pois_coordinates_index" gist (coordinates)
"pois_poi_kind_id_index" …Run Code Online (Sandbox Code Playgroud) 我想创建一个Rails控制器,从Web下载一系列jpg文件,并直接将它们作为二进制文件写入数据库(我不是要尝试上传表单)
在做这件事的路上有任何线索吗?
谢谢
编辑:这是我使用attachment-fu gem编写的一些代码:
http = Net::HTTP.new('awebsite', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start() { |http|
req = Net::HTTP::Get.new("image.jpg")
req.basic_auth login, password
response = http.request(req)
attachment = Attachment.new(:uploaded_data => response.body)
attachement.save
}
Run Code Online (Sandbox Code Playgroud)
我得到一个"未定义的方法`content_type'为#"错误
我想为我的Phoenix应用程序使用特定的Postgres架构.
我试图通过Ecto.Repo.after_connect/1回调实现这一点,但似乎在超时之前以递归方式创建新的数据库连接大约10次.
这是我的回购文件:
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app
use Scrivener, page_size: 20
def after_connect(_pid) do
IO.puts "after_connect"
Ecto.Adapters.SQL.query(MyApp.Repo, "SET search_path TO 'my_app';", [])
end
end
Run Code Online (Sandbox Code Playgroud) 几个月来我成功使用资产管道.现在我想异步加载一些我的JS文件(使用yepnope库).当它运作良好,config.assets.debug是false.
但是在开发模式(config.assets.debug通常在哪里true),对我来说最好的选择是动态获取我的清单中包含的所有js文件的列表(我有2个清单:application.js和externals.js),以便将它们提供给yepnope异步加载.
有没有想过这样做?
我在我的应用程序中有一些对Twitter照片的引用.
当Twitter上的照片不再可用时(请参阅此示例:http://p.twimg.com/AmL3hpaCEAd2wtN.jpg),我收到404响应代码,图像为正文(灰色404图像)
我在Img上使用onload和onerrorjavascript方法,但由于p.twimg.com正在返回一个正文,因此该onload方法被触发.我发现无法区分404响应和200响应.
任何的想法?
我在heroku上为我的应用程序设置了2个环境:分段和生产,我正在使用Rails 3.1资产管道.
我已经设置了一个自定义的config.action_controller.asset_host属性,以便在http://assets.myapp-staging.com或http://assets.myapp.com上从cloudfront获取我的资产.
问题是,在Heroku上,资产总是与生产环境一起预编译.这意味着即使在分段中,我的css或js文件中存在的所有URL都以http://assets.myapp.com为目标.
任何的想法?
我想使用postgres IN运算符(使用Ecto库)查询jsonb字段
此代码使用simple =运算符:
from a in query, where: fragment("?->>'format' = ?", a.properties, "foo")
Run Code Online (Sandbox Code Playgroud)
但我无法做出任何这些尝试:
from a in query, where: fragment("?->>'format' IN ?", a.properties, ["foo", "bar"])
from a in query, where: fragment("?->>'format' IN (?)", a.properties, ["foo", "bar"])
from a in query, where: fragment("?->>'format' IN ?", a.properties, "('foo', 'bar')"])
Run Code Online (Sandbox Code Playgroud)
任何的想法?