如何在RoR 网站中使用"POST url" JSON 文件?
How to use "POST url" JSON file in RoR website?
我在 Rails 和 Kibana 上使用 Ruby 在我的网站上显示可视化(Ruby2.6.0,Rails5.2.3,Kibana6.6.1) .通过使用 Xpack->Share->Generate PDF/PNG,我找到了 'copy POST url'。我可以使用 'net/http' 从 POST url 中获取以下 JSON 文件,但是如何在我的 RoR 中使用 POST url/JSON 文件网站下载 'assets' 文件夹中的 PNG 文件?
JSON 文件我从 POST url:
{'path': '/api/reporting/jobs/download/jvds4zs10qv79d0062b9cel6',
'job': {'id': 'jvds4zs10qv79d0062b9cel6', 'index':
'.reporting-2019.05.05', 'type': 'esqueue', 'jobtype': 'PNG',
'created_by': False, 'payload': {'type': 'visualization', 'title':
'[eCommerce] Sales by Gender', 'relativeUrl':
"/app/kibana#/visualize/edit/ed8436b0-b88b-11e8-a6d9-e546fe2bba5f?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-60d,mode:quick,to:now))&_a=(filters:!(),linked:!f,query:(language:lucene,query:''),uiState:(),vis:(aggs:!((enabled:!t,id:'1',params:(),schema:metric,type:count),(enabled:!t,id:'2',params:(field:customer_gender,json:'',missingBucket:!f,missingBucketLabel:Missing,order:desc,orderBy:'1',otherBucket:!f,otherBucketLabel:Other,size:5),schema:segment,type:terms)),params:(addLegend:!t,addTooltip:!t,isDonut:!t,labels:(last_level:!t,show:!t,truncate:100,values:!t),legendPosition:right,type:pie),title:'%5BeCommerce%5D+Sales+by+Gender',type:pie))",
'headers':
'uxY4w6gCRwwLZyFkDX0ujVlANYq7ae5UuNJAN0GpbTOX7vw5aPuCuyr37nNcpz3vtA8kNVaL6Gacs24mPjZfwl4mB9xRKGA62CkCLo8Xz1amuIEthI+BtKPGo5QAk2k2+7zLvLgX3KouVvYdd61U5rLAvfjv4TydcpH9qJ4qrL5OELkzAJGrNdujtDLWaoy2Qj9YXbLnh7gSdI+lrNml6usZIh4pMzz8qdRo597iO/4AeSJRa2JfAruyGB3zYInZRtMuCbA4f3ANvT2WeAn6lpiwThB993uEjwu4LBVUZuo2XL0TcB29YNNa9oMCUEhWjdJ1ase5VQdz8V2IOgecQ1W52V5JuVTvl9SkVvFWSBhXJg==',
'browserTimezone': 'Europe/Zurich', 'layout': {'dimensions':
{'height': 589.234375, 'width': 866}}, 'basePath': '', 'forceNow':
'2019-05-07T12:38:12.768Z'}, 'timeout': 120000, 'max_attempts': 3,
'priority': 10, 'browser_type': 'chromium'}}
我正在尝试使用 'net/http'
,我在控制器中添加的内容:
class MaisonController < ApplicationController
require 'net/http'
def require_post
uri = URI.parse("http://localhost:5601/api/reporting/generate/png?jobParams=(browserTimezone:Europe%2FZurich,layout:(dimensions:(height:589.234375,width:866)),objectType:visualization,relativeUrl:%27%2Fapp%2Fkibana%23%2Fvisualize%2Fedit%2Fed8436b0-b88b-11e8-a6d9-e546fe2bba5f%3F_g%3D(refreshInterval:(pause:!!t,value:0),time:(from:now-60d,mode:quick,to:now))%26_a%3D(filters:!!(),linked:!!f,query:(language:lucene,query:!%27!%27),uiState:(),vis:(aggs:!!((enabled:!!t,id:!%271!%27,params:(),schema:metric,type:count),(enabled:!!t,id:!%272!%27,params:(field:customer_gender,json:!%27!%27,missingBucket:!!f,missingBucketLabel:Missing,order:desc,orderBy:!%271!%27,otherBucket:!!f,otherBucketLabel:Other,size:5),schema:segment,type:terms)),params:(addLegend:!!t,addTooltip:!!t,isDonut:!!t,labels:(last_level:!!t,show:!!t,truncate:100,values:!!t),legendPosition:right,type:pie),title:!%27%255BeCommerce%255D%2BSales%2Bby%2BGender!%27,type:pie))%27,title:%27%5BeCommerce%5D%20Sales%20by%20Gender%27)")
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
render :json => response.body
end
end
编辑:
我尝试使用 'attachment_fu'
gem 来下载带有上述 JSON 文件的 png 文件。将Controller中的代码修改为如下代码:
class MaisonController < ApplicationController
require 'net/http'
require 'open-uri'
def require_post
uri = URI.parse("http://localhost:5601/api/reporting/generate/png?jobParams=(browserTimezone:Europe%2FZurich,layout:(dimensions:(height:589.234375,width:866)),objectType:visualization,relativeUrl:%27%2Fapp%2Fkibana%23%2Fvisualize%2Fedit%2Fed8436b0-b88b-11e8-a6d9-e546fe2bba5f%3F_g%3D(refreshInterval:(pause:!!t,value:0),time:(from:now-60d,mode:quick,to:now))%26_a%3D(filters:!!(),linked:!!f,query:(language:lucene,query:!%27!%27),uiState:(),vis:(aggs:!!((enabled:!!t,id:!%271!%27,params:(),schema:metric,type:count),(enabled:!!t,id:!%272!%27,params:(field:customer_gender,json:!%27!%27,missingBucket:!!f,missingBucketLabel:Missing,order:desc,orderBy:!%271!%27,otherBucket:!!f,otherBucketLabel:Other,size:5),schema:segment,type:terms)),params:(addLegend:!!t,addTooltip:!!t,isDonut:!!t,labels:(last_level:!!t,show:!!t,truncate:100,values:!!t),legendPosition:right,type:pie),title:!%27%255BeCommerce%255D%2BSales%2Bby%2BGender!%27,type:pie))%27,title:%27%5BeCommerce%5D%20Sales%20by%20Gender%27)")
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
render :json => response.body
http.start() { |http|
tempfile = Tempfile.new('test.png')
File.open(tempfile.path, 'w') do |f|
f.write response.body
end
attachment = Attachment.new(:upload_data => LocalFile.new(tempfile.path))
attachment.save
}
end
我得到的错误是 'uninitialized constant MaisonController::Attachment'
。
预期结果:使用上述JSON文件在assets中下载png文件。
我将回答我自己关于我在我的项目中尝试过的问题并关闭这个问题。
我在 'Kibana->Visualisation/Dashboard->PDF/PNG' 中获得了 'copy POST url'。就是一个posturl。我用 url 向 kibana 服务器发送了 post 请求。该操作将触发 kibana 服务器 'pending'、'processing' 并生成报告。我将从服务器返回一个 JSON 文件。 JSON 文件中有一个重要的作业 ID,例如 'jvi53ajd21w09d006233kcvo'。我可以在 'Management->Reporting' 中找到所有历史报告。最后,我使用生成的 url 'http://localhost:5601/api/reporting/jobs/download/jvi53ajd21w09d006233kcvo' 和 'wget' 命令将报告下载到我的本地文件夹中。
您可以在这里找到类似的答案:https://discuss.elastic.co/t/how-to-use-post-url-to-share-visualisation-in-my-website/179671/4
希望我的回答能对想在Kibana中使用POSTURL的朋友有所帮助
我在 Rails 和 Kibana 上使用 Ruby 在我的网站上显示可视化(Ruby2.6.0,Rails5.2.3,Kibana6.6.1) .通过使用 Xpack->Share->Generate PDF/PNG,我找到了 'copy POST url'。我可以使用 'net/http' 从 POST url 中获取以下 JSON 文件,但是如何在我的 RoR 中使用 POST url/JSON 文件网站下载 'assets' 文件夹中的 PNG 文件?
JSON 文件我从 POST url:
{'path': '/api/reporting/jobs/download/jvds4zs10qv79d0062b9cel6', 'job': {'id': 'jvds4zs10qv79d0062b9cel6', 'index': '.reporting-2019.05.05', 'type': 'esqueue', 'jobtype': 'PNG', 'created_by': False, 'payload': {'type': 'visualization', 'title': '[eCommerce] Sales by Gender', 'relativeUrl': "/app/kibana#/visualize/edit/ed8436b0-b88b-11e8-a6d9-e546fe2bba5f?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-60d,mode:quick,to:now))&_a=(filters:!(),linked:!f,query:(language:lucene,query:''),uiState:(),vis:(aggs:!((enabled:!t,id:'1',params:(),schema:metric,type:count),(enabled:!t,id:'2',params:(field:customer_gender,json:'',missingBucket:!f,missingBucketLabel:Missing,order:desc,orderBy:'1',otherBucket:!f,otherBucketLabel:Other,size:5),schema:segment,type:terms)),params:(addLegend:!t,addTooltip:!t,isDonut:!t,labels:(last_level:!t,show:!t,truncate:100,values:!t),legendPosition:right,type:pie),title:'%5BeCommerce%5D+Sales+by+Gender',type:pie))", 'headers': 'uxY4w6gCRwwLZyFkDX0ujVlANYq7ae5UuNJAN0GpbTOX7vw5aPuCuyr37nNcpz3vtA8kNVaL6Gacs24mPjZfwl4mB9xRKGA62CkCLo8Xz1amuIEthI+BtKPGo5QAk2k2+7zLvLgX3KouVvYdd61U5rLAvfjv4TydcpH9qJ4qrL5OELkzAJGrNdujtDLWaoy2Qj9YXbLnh7gSdI+lrNml6usZIh4pMzz8qdRo597iO/4AeSJRa2JfAruyGB3zYInZRtMuCbA4f3ANvT2WeAn6lpiwThB993uEjwu4LBVUZuo2XL0TcB29YNNa9oMCUEhWjdJ1ase5VQdz8V2IOgecQ1W52V5JuVTvl9SkVvFWSBhXJg==', 'browserTimezone': 'Europe/Zurich', 'layout': {'dimensions': {'height': 589.234375, 'width': 866}}, 'basePath': '', 'forceNow': '2019-05-07T12:38:12.768Z'}, 'timeout': 120000, 'max_attempts': 3, 'priority': 10, 'browser_type': 'chromium'}}
我正在尝试使用 'net/http'
,我在控制器中添加的内容:
class MaisonController < ApplicationController
require 'net/http'
def require_post
uri = URI.parse("http://localhost:5601/api/reporting/generate/png?jobParams=(browserTimezone:Europe%2FZurich,layout:(dimensions:(height:589.234375,width:866)),objectType:visualization,relativeUrl:%27%2Fapp%2Fkibana%23%2Fvisualize%2Fedit%2Fed8436b0-b88b-11e8-a6d9-e546fe2bba5f%3F_g%3D(refreshInterval:(pause:!!t,value:0),time:(from:now-60d,mode:quick,to:now))%26_a%3D(filters:!!(),linked:!!f,query:(language:lucene,query:!%27!%27),uiState:(),vis:(aggs:!!((enabled:!!t,id:!%271!%27,params:(),schema:metric,type:count),(enabled:!!t,id:!%272!%27,params:(field:customer_gender,json:!%27!%27,missingBucket:!!f,missingBucketLabel:Missing,order:desc,orderBy:!%271!%27,otherBucket:!!f,otherBucketLabel:Other,size:5),schema:segment,type:terms)),params:(addLegend:!!t,addTooltip:!!t,isDonut:!!t,labels:(last_level:!!t,show:!!t,truncate:100,values:!!t),legendPosition:right,type:pie),title:!%27%255BeCommerce%255D%2BSales%2Bby%2BGender!%27,type:pie))%27,title:%27%5BeCommerce%5D%20Sales%20by%20Gender%27)")
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
render :json => response.body
end
end
编辑:
我尝试使用 'attachment_fu'
gem 来下载带有上述 JSON 文件的 png 文件。将Controller中的代码修改为如下代码:
class MaisonController < ApplicationController
require 'net/http'
require 'open-uri'
def require_post
uri = URI.parse("http://localhost:5601/api/reporting/generate/png?jobParams=(browserTimezone:Europe%2FZurich,layout:(dimensions:(height:589.234375,width:866)),objectType:visualization,relativeUrl:%27%2Fapp%2Fkibana%23%2Fvisualize%2Fedit%2Fed8436b0-b88b-11e8-a6d9-e546fe2bba5f%3F_g%3D(refreshInterval:(pause:!!t,value:0),time:(from:now-60d,mode:quick,to:now))%26_a%3D(filters:!!(),linked:!!f,query:(language:lucene,query:!%27!%27),uiState:(),vis:(aggs:!!((enabled:!!t,id:!%271!%27,params:(),schema:metric,type:count),(enabled:!!t,id:!%272!%27,params:(field:customer_gender,json:!%27!%27,missingBucket:!!f,missingBucketLabel:Missing,order:desc,orderBy:!%271!%27,otherBucket:!!f,otherBucketLabel:Other,size:5),schema:segment,type:terms)),params:(addLegend:!!t,addTooltip:!!t,isDonut:!!t,labels:(last_level:!!t,show:!!t,truncate:100,values:!!t),legendPosition:right,type:pie),title:!%27%255BeCommerce%255D%2BSales%2Bby%2BGender!%27,type:pie))%27,title:%27%5BeCommerce%5D%20Sales%20by%20Gender%27)")
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
render :json => response.body
http.start() { |http|
tempfile = Tempfile.new('test.png')
File.open(tempfile.path, 'w') do |f|
f.write response.body
end
attachment = Attachment.new(:upload_data => LocalFile.new(tempfile.path))
attachment.save
}
end
我得到的错误是 'uninitialized constant MaisonController::Attachment'
。
预期结果:使用上述JSON文件在assets中下载png文件。
我将回答我自己关于我在我的项目中尝试过的问题并关闭这个问题。
我在 'Kibana->Visualisation/Dashboard->PDF/PNG' 中获得了 'copy POST url'。就是一个posturl。我用 url 向 kibana 服务器发送了 post 请求。该操作将触发 kibana 服务器 'pending'、'processing' 并生成报告。我将从服务器返回一个 JSON 文件。 JSON 文件中有一个重要的作业 ID,例如 'jvi53ajd21w09d006233kcvo'。我可以在 'Management->Reporting' 中找到所有历史报告。最后,我使用生成的 url 'http://localhost:5601/api/reporting/jobs/download/jvi53ajd21w09d006233kcvo' 和 'wget' 命令将报告下载到我的本地文件夹中。
您可以在这里找到类似的答案:https://discuss.elastic.co/t/how-to-use-post-url-to-share-visualisation-in-my-website/179671/4
希望我的回答能对想在Kibana中使用POSTURL的朋友有所帮助