Ruby post with net::hhtp.post_form - ruby

i have reduced this to as little as possible but wondering what i am doing wrong with this little ruby file:
params ={'title'=>'qwert', 'fulltext'=>'qwert', 'user_twitter_id'=>'qwert'}
res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3000/details/'), params)
puts res.body
This does result in a record being created but none of the params being inserted, yet they seem recognised?
Started POST "/details/" for 127.0.0.1 at 2011-10-31 12:37:02 +0000
Processing by DetailsController#create as
Parameters: {"title"=>"qwert", "fulltext"=>"qwert", "user_twitter_id"=>"qwert"}
AREL (0.3ms) INSERT INTO "details" ("title", "fulltext", "user_twitter_id", "created_at", "updated_at") VALUES (NULL, NULL, NULL, '2011-10-31 12:37:02.401881', '2011- 10-31 12:37:02.401881')
Redirected to http://127.0.0.1:3000/details/23
Completed 302 Found in 185ms

In standard Rails you need a root element (you don't show the controller action, so I must assume):
params = {:detail => {...}}
BTW, a higher-level library as rest-client may come handy ([edit] Marian notes that nested hashes are not managed by Net::HTTP, so try rest-client)

Related

Send logs to datadog with custom formatter shows only the first log of multiple ones

I am trying to send logs to DD (datadog) in such a way that the logs are being received as json and therefore shown properly in the portal through attributes.
My logger is a simple Logger.new(STDOUT, level: Logger::INFO).
If I stick to its standard output, it will in the form
I, [2022-07-30T22:43:35.216846 #1] INFO -- my-app: {"user":"1234"}
which is not really parsable by DD since not a proper JSON. In this case however all the logs appear at least on the DD portal.
Now.. I am trying to format the logs in a JSON manner in this way:
def self.logger
#logger ||= Logger.new(STDOUT, level: Logger::INFO)
#logger.progname = 'my-app'
#logger.formatter = proc do |severity, datetime, progname, msg|
{timestamp: datetime.to_s, progname: progname, severity: severity, correlation: Datadog::Tracing.log_correlation, message: msg}.to_json
end
#logger
end
This is my logger and thanks to this logs are seen properly in DD and parsed correctly because formatted in my app in a proper JSON.
The problem with this approach though seems to be that the logs are sent in 1 full block. Meaning that only the very first log is being visible. Let's say that I want to log this:
my_hash = {"message" => '1', "prop" => '1234'}.to_json
logger.info(my_hash)
my_hash = {"message" => '2', "prop" => '12345'}.to_json
logger.info(my_hash)
only the first log will be shown correctly on the DD portal. Parsed correctly with its message and prop attributes, but nothing about the second log.
Here is the thing, if I see the output of my app locally in the console I see this:
{"timestamp":"2022-07-31 01:15:39 +0200","progname":"my-app","severity":"INFO","correlation":"dd.service=my-app dd.trace_id=2976451780376429536 dd.span_id=0","message":"{"message":"1","prop":"1234"}"}{"timestamp":"2022-07-31 01:15:39 +0200","progname":"my-app","severity":"INFO","correlation":"dd.service=my-app dd.trace_id=2976451780376429536 dd.span_id=0","message":"{"message":"2","prop":"12345"}"}127.0.0.1 - - [31/Jul/2022:01:15:39 +0200] "GET /controller/test_controller HTTP/1.1" 200 - 0.0024
so the 2nd log gets actually outputted! But DD somehow sees only the first log..
(I know there is even a 3rd one shown in this message.. but that's just Sinatra automatic behavior for every http call reaching the api). What do you guys think is the problem?

how to pass nested hash params to searchkick

I am working on a existing rails app, where they are currently using elasticsearch gem. Now I am trying to switch it to searchkick. I have configured the gem on the app.
Currently if I do :
Model.search(keyword) from the console, I am getting the out put.
But currently they are passing query string as a nested hash in the actual application, which looks like follows:
{:query=>{:bool=>{:must=>[{:multi_match=>{:query=>"apple", :fields=>["title^2", "content", "description"]}}], :should=>[], :filter=>[{:term=>{:data_proxy_id=>"4d629f00-c8b1-aacc-97b5-e21e4c0bbe3f"}}, {:term=>{:article_type=>"Article"}}, {:term=>{:status=>"published"}}]}}, :sort=>{"_score"=>{"order"=>"desc"}}, :aggs=>{:group_by_section=>{:terms=>{:field=>"section.name.raw", :size=>0}}, :group_by_category=>{:terms=>{:field=>"category.name.raw", :size=>0}}, :group_by_author=>{:terms=>{:field=>"authors.fullname.raw", :size=>0}}, :year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"1y", :format=>"yyyy", :min_doc_count=>1, :time_zone=>"+00:00"}}, :month_year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"month", :format=>"MM-yyy", :min_doc_count=>1, :time_zone=>"+00:00"}}}}
If I pass this hash as the params to Model.search(query_hash). I am getting the following error.
ArgumentError: unknown keywords: query, sort
Try with
Model.search body: {:query=>{:bool=>{:must=>[{:multi_match=>{:query=>"apple", :fields=>["title^2", "content", "description"]}}], :should=>[], :filter=>[{:term=>{:data_proxy_id=>"4d629f00-c8b1-aacc-97b5-e21e4c0bbe3f"}}, {:term=>{:article_type=>"Article"}}, {:term=>{:status=>"published"}}]}}, :sort=>{"_score"=>{"order"=>"desc"}}, :aggs=>{:group_by_section=>{:terms=>{:field=>"section.name.raw", :size=>0}}, :group_by_category=>{:terms=>{:field=>"category.name.raw", :size=>0}}, :group_by_author=>{:terms=>{:field=>"authors.fullname.raw", :size=>0}}, :year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"1y", :format=>"yyyy", :min_doc_count=>1, :time_zone=>"+00:00"}}, :month_year_based=>{:date_histogram=>{:field=>"publish_date", :interval=>"month", :format=>"MM-yyy", :min_doc_count=>1, :time_zone=>"+00:00"}}}}

Get total_rows from couchbase ruby library

When having a look to my couchbase view using the web api I got this result:
{
"total_rows": 18279385,
"rows": []
}
But I'm using the ruby couchbase gem like follows
require 'couchbase'
c = Couchbase.connect(...)
sources = c.design_docs['Data']
pp sources.All
#<Couchbase::View:47373151271840 #endpoint="_design/Data/_view/All" #params={:connection_timeout=>75000}>
But how do I get the total_rows from the view? I've found few documentation which relates to a Method "total_rows" but that doesn't seem to be present at this point.
The comment of Anthony solves the problem:
sources.All(limit: 0).fetch.total_rows
Use limit: 0 for speeding up the request.

With Zabbix API, how do I get the values of items/resources rather than just the ID's?

I have some data in a Custom Screen in Zabbix, and would like to pull the data from the screen via the API. I'm using this Ruby gem: https://github.com/express42/zabbixapi
I'm able to successfully connect and query, but the results I'm getting are not very useful:
p zbx.query(
:method => "item.get",
:params => {
:itemids => "66666",
:output => "extend"
}
)
# [{"itemid"=>"66666", "type"=>"0", "snmp_community"=>"", "snmp_oid"=>"", "hostid"=>"77777", "name"=>"Fro Packages", "key_"=>"system.sw.packages[davekey1|davekey2|davekey3|davekey4]", "delay"=>"300", "history"=>"90", "trends"=>"365", "status"=>"0", "value_type"=>"1", "trapper_hosts"=>"", "units"=>"", "multiplier"=>"0", "delta"=>"0", "snmpv3_securityname"=>"", "snmpv3_securitylevel"=>"0", "snmpv3_authpassphrase"=>"", "snmpv3_privpassphrase"=>"", "formula"=>"1", "error"=>"", "lastlogsize"=>"0", "logtimefmt"=>"", "templateid"=>"88888", "valuemapid"=>"0", "delay_flex"=>"", "params"=>"", "ipmi_sensor"=>"", "data_type"=>"0", "authtype"=>"0", "username"=>"", "password"=>"", "publickey"=>"", "privatekey"=>"", "mtime"=>"0", "flags"=>"0", "filter"=>"", "interfaceid"=>"25", "port"=>"", "description"=>"", "inventory_link"=>"0", "lifetime"=>"30", "snmpv3_authprotocol"=>"0", "snmpv3_privprotocol"=>"0", "state"=>"0", "snmpv3_contextname"=>""}]
You can see that it's returning a bunch of ID's for the items, including the correct keys, but I can't seem to get the actual plain text values, which is the data I'm interested in.
I started with the screen_id, then got the screenitem_id, now the item_id, but I don't seem to be getting any closer to what I want!
Thanks for any help
Getting items or getting hosts means getting their description, not the data. is You are after history. Reading the actual Zabbix user manual and API docs is highly recommended.

Using Ruby & Github API to filter commits by date

I am using the ruby gem 'octokit' which implements the Github API v3. Mostly works great but I cannot seem to filter by date. I believe I have the syntax and time format correct, but it appears my option is ignored and the API returns the past 35 entries regardless of the since or until dates.
Here's a minimal reproducible example (after installing the octokit gem).
require 'octokit'
require 'time'
#day = "2012-09-27"
#until = DateTime.parse(#date).iso8601
#since = (DateTime.parse(#day) - 60*60*48).iso8601
a = Octokit.commits({:username => "cboettig", :repo => "labnotebook", :since => #since, :until => #until})
see the date of the output of last entry
a.last.commit.author.date
explicit day doesn't work either
b = Octokit.commits({:username => "cboettig", :repo => "labnotebook", :since => "2012-09-27T00:00:00+00:00"})
b.last.commit.author.date
The date I get in both examples is from August, outside the specified range given. What did I miss?
Background: I'm trying to write a little Jekyll plugin that uses the API to return commits made to a specified repo on the day of the post.
joeyw gives a great answer to this question here.
The second argument should be the sha or branch, and options should be the third argument, e.g.
Octokit.commits("cboettig/labnotebook", "master", :since => "2012-09-28T00:00:00+00:00").length
or
Octokit.commits("cboettig/labnotebook", nil, :since => "2012-09-28T00:00:00+00:00").length
works just fine. Here's my corresponding jekyll plugin

Resources