我有一个简单的 Rails 4.1.4 应用程序,我正在尝试将单独主机上的 AngularJS 应用程序连接到它的 API。虽然我访问它没有问题,但 Rails 似乎认为请求是 HTML 并忽略了 Content-Type: 'application/json'
Started GET "/api/v1/locations?access_token=xxx&headers=%7B%22Content-type%22:%22application%2Fjson%22%7D" for 127.0.0.1 at 2014-09-03 17:12:11 +0100
Processing by Api::V1::LocationsController#index as HTML
Parameters: {"access_token"=>"xxx", "headers"=>"{\"Content-type\":\"application/json\"}"}
Run Code Online (Sandbox Code Playgroud)
在我的 NG 应用程序中,我尝试了多种标头组合,包括:
app.factory('Location', ['$resource', "$localStorage",
function($resource, $localStorage){
return $resource('http://my-api.com/api/v1/locations/', {headers:{'Content-type' : 'application/json'}}, {
query: {
method: 'GET',
headers: {'Content-type': 'application/json'},
isArray: true,
dataType: 'json',
params: { access_token: $localStorage.accessToken }
}...
Run Code Online (Sandbox Code Playgroud)
NG 方面的响应看起来不错,尽管我的位置控制器中只有它,但它以 JSON 响应:
class Api::V1::LocationsController < Api::V1::BaseController
doorkeeper_for :all
before_filter :authorize
respond_to :json
def index
@locations = @current_user.locations.order("created_at …Run Code Online (Sandbox Code Playgroud) 我在BigQuery中存储了超过5亿行,它基本上代表了某个时间(不规则)设备的确切位置.
我正在尝试找到一种快速有效的方法来确定设备的第一个和最后一个看到的位置.
到目前为止,我已经使用了一个连接,但它需要花费10多分钟才能完成(除非我只是查询中的限制).我也试过一个dense_rank查询,但我无法对计数进行排序(我不完全理解它).
我有一个client_id,device_id(它是固定的,代表建筑物内的位置)和时间戳.
首先,我通过client_id和device_id做了一个组,以验证我应该期待什么.然后我尝试使用最小和最大时间戳加入表:
SELECT count(FirstSet.device_id), FirstSet.device_id
FROM
(
SELECT client_id, device_id, created_at
FROM [mytable.visitsv3]
WHERE secret = 'xxx'
GROUP each BY client_id, device_id, created_at
ORDER BY client_id, created_at asc
LIMIT 1000
) as FirstSet
inner join
(
SELECT client_id, device_id, min(created_at)
FROM [mytable.visitsv3]
WHERE secret = 'xxx'
GROUP each BY client_id, device_id, created_at
LIMIT 1000
) SecondSet
on FirstSet.device_id = SecondSet.device_id
GROUP BY FirstSet.device_id
ORDER BY 1 DESC
limit 25
Run Code Online (Sandbox Code Playgroud)
我是这个世界的新手,所以会很感激一些建议.正如我所说,它更多是关于性能,因为我们需要实时运行查询.
有大量关于同一概念的教程 - 我只是希望有人可以帮助优化事物.
---编辑---
输出格式应如下所示:
|count|device_id| …Run Code Online (Sandbox Code Playgroud) 我想我在这里有一个大脑冻结,但我无法找到最好的方法来做到这一点...在此先感谢您的帮助.
我的位置表中的一些字段每5分钟由外部脚本更新一次.如果超过1小时未更新,则会向网站管理员发送一封电子邮件.
获得死亡位置列表相当简单:
scope :not_responding, lambda {
where('last_heartbeat < ?', Time.now - 1.hour )
}
Run Code Online (Sandbox Code Playgroud)
我坚持的应该是非常简单....我想要做的就是改变位置名称的样式,如果它没有响应则它是红色的.
%td
- @locations.each do |locations|
= if ('locations.last_heartbeat < ?', Time.now - 1.hour )
red
-else
green
Run Code Online (Sandbox Code Playgroud)
但是这会引发错误(意外',')
任何人都可以建议如何将其移入我的模型?
- 更新 -
我不确定这是一个错误还是什么,但我看到了一些奇怪的结果.
使用下面的答案,我添加了这个:
def not_responding?
last_heartbeat < (Time.now - 1.hour)
end
Run Code Online (Sandbox Code Playgroud)
并致电使用:
- if location.not_responding?
red
- else
green
Run Code Online (Sandbox Code Playgroud)
但是,这给了我一个关于'<'未定义的错误.
最后,我必须这样做才能让它发挥作用.一切似乎都很荒谬......
def not_responding
last_heartbeat <=> (Time.now - 1.hour)
end
Run Code Online (Sandbox Code Playgroud)
在视图中:
- if (hotspot.not_responding_two == -1)
red
- else
green
Run Code Online (Sandbox Code Playgroud)
以下测试没问题没有意义:
def test?
Time.now …Run Code Online (Sandbox Code Playgroud) 我需要将哈希的输出加入到字符串中.
哈希看起来像这样:
nas.location.walledgardens.to_s
=> "[#<Walledgarden id: 1, location_id: 12, url: \"polka.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 2, location_id: 12, url: \"test.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 3, location_id: 12, url: \"help.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 4, location_id: 12, url: \"yell.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 5, location_id: 12, url: \"sausage.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">]"
Run Code Online (Sandbox Code Playgroud)
我需要将url值加入以下格式:
polka.com,test.com,help.com
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?我可以轻松地浏览它,但输出有换行符,我需要删除这些以及逗号.
我一直在关注这个关于多个 jquery 时间跨度滑块的指南。我正在尝试从我的 Rails 应用程序中获取每日起始值。我正在努力将变量正确插入到 jquery 函数中。
我有一个字段代表一周中的每一天,其中包括开始时间(每天)。例如:
@location.active_monday
@location.active_tuesday
@location.active_wednesday
Run Code Online (Sandbox Code Playgroud)
显示滑块的 jquery 函数如下所示:
_sliderhtml: function(day){
var displayday = day.charAt(0).toUpperCase() + day.substr(1);
var html = ""+
'<div class="fieldrow spacer">' +
'<div>' +
'<input type="text" name="location['+day+'_on_wifi]" id="'+day+'-time1" value="xxxxxxx" > - '+
Run Code Online (Sandbox Code Playgroud)
在输入值中,我尝试插入模型中的值,但我无法找出正确的语法。试过:
value="<%= "@location.active_wednesday" %>
Run Code Online (Sandbox Code Playgroud)
这可行,但我需要用“+day+”替换这一天。例如:
value="<%= "@location.active_'+day+'" %>
Run Code Online (Sandbox Code Playgroud)
有办法做到这一点还是我需要重新考虑?
在我的项目中,客户端将请求从具有id的服务器下载文件.我必须执行以下操作:
我使用以下代码检查文件并发送响应.
fs.exists(filename, function(exists) {
if (!exists) {
res.writeHead(404, '', {
"Content-Type" : "text/plain"
})
res.write("404 Not Found\n");
res.end();
return;
}
fs.readFile(filename, "binary", function(err, file) {
if (err) {
res.writeHead(500, '', {
"Content-Type" : "text/plain"
})
res.write(err + "\n");
res.end();
return;
}
res.setHeader("Pragma", "public");
res.setHeader("Cache-Control: private, max-age=3600");
res.setHeader("Transfer-Encoding: chunked");
res.setHeader("Range: chunked");
res.writeHead(200, '', {
"Content-Type" : contentType
});
res.write(file, "binary");
res.end(file, "binary");
});
});
Run Code Online (Sandbox Code Playgroud)
在几毫秒内,客户端将请求数百个文件.支持的文件类型是图像,音频或视频.
当文件夹中有大量文件时,node.js花费了太多时间来下载文件.如何提高性能?
ruby ×2
angularjs ×1
doorkeeper ×1
download ×1
express ×1
jquery ×1
mongoose ×1
node.js ×1
performance ×1
sql ×1