I got this query:
User.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_time"},
"month" => {"$month" => "$created_time"},
"year" => {"$year" => "$created_time"},
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1}
}},
{"$sort" => {
"created_at" => -1
}}
])
My user table has username and fullname which I need,
I did something like
User.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_at"},
"month" => {"$month" => "$created_at"},
"year" => {"$year" => "$created_at"},
"username" => "$username"
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1}
}},
{"$sort" => {
"created_at" => -1
}}
])
but it doesn't work and gives same result like above, doesn't print username, any ideas ?
My goal is get username, after grouping by created at date.
User.collection.aggregate([
{"$project" => {
"dayOfMonth" => {"$dayOfMonth" => "$created_at"},
"month" => {"$month" => "$created_at"},
"year" => {"$year" => "$created_at"},
"username" => "$username"
}},
{"$group" => {
"_id" => { "dayOfMonth" => "$dayOfMonth", "month" => "$month", "year" => "$year"},
"Total" => {"$sum" => 1},
"username" => {"$first" => "$username"}
}},
{"$sort" => {
"created_at" => -1
}}
])
Related
I try make feature when in chat there are some unacceptable message that any other users can reply on this message with command '/report' and admins received meesage from bot with inline keyboad where he can:
ban sender of report
ban reporter of message
So, for this I have two methods:
Action "/report" - it sends to admin inline keyboard(there is all
needed data)
"callback_query" - it processes message(don't have data what need
for ban of user) - it has only data of chat, user who clicked inline
button
Ok, i can save data from point 1 to redis and pass it into point 2, but how can I found(indenify) that variable to use it...
Any tips?
def report!(*)
Rails.cache.write(:update, update)
# create_report
# binding.pry
user_name_reporter = "#{update.dig('message', 'from', 'first_name')} #{update.dig('message', 'from', 'last_name')}"
user_name_sender = "#{update.dig('message', 'reply_to_message', 'from', 'first_name')} #{update.dig('message', 'reply_to_message', 'from', 'last_name')}"
respond_with :message, text: "We got report in the group!\n#{user_name_reporter} sent report on #{user_name_sender}", reply_markup: {
inline_keyboard: [
[
{text: t('.restrict_messages_for_sender'), callback_data: 'restrict_messages_for_sender'},
{text: t('.restrict_messages_for_reporter'), callback_data: 'restrict_messages_for_reporter'},
],
[
{text: t('.ban_for_sender'), callback_data: 'ban_for_sender'},
{text: t('.ban_for_reporter'), callback_data: 'ban_for_reporter'},
],
# [{text: t('.repo'), url: 'https://github.com/telegram-bot-rb/telegram-bot'}],
],
}
end
def callback_query(data)
report_update = Rails.cache.read(:update)
sender_user_id = report_update.dig('message', 'from', 'id')
reporter_user_id = report_update.dig('message', 'reply_to_message', 'from', 'id')
chat_id = report_update.dig('message', 'chat', 'id')
# binding.pry
case data
when 'restrict_messages_for_sender'
answer_callback_query t('.alert'), show_alert: true
bot.restrict_chat_member(chat_id: chat_id, user_id: sender_user_id, permissions: { can_send_messages: false }.to_json, until_date: (Time.now + 1.hours).to_i)
when 'restrict_messages_for_reporter'
answer_callback_query t('.no_alert')
bot.restrict_chat_member(chat_id: chat_id, user_id: reporter_user_id, permissions: { can_send_messages: false }.to_json, until_date: (Time.now + 1.hours).to_i)
when 'ban_for_sender'
answer_callback_query t('.no_alert')
bot.ban_chat_member(chat_id: chat_id, user_id: sender_user_id)
when 'ban_for_reporter'
answer_callback_query t('.no_alert')
bot.ban_chat_member(chat_id: chat_id, user_id: reporter_user_id)
end
end
data in action '/report' :
{
"update_id" => 156273636,
"message" => {
"message_id" => 523,
"from" => {
"id" => 308967324,
"is_bot" => false,
"first_name" => "PRD",
"last_name" => "RSD",
"username" => "anko20094",
"language_code" => "en"
},
"chat" => {
"id" => -686671946,
"title" => "test gropt for bots",
"type" => "group",
"all_members_are_administrators" => true
},
"date" => 1659272502,
"reply_to_message" => {
"message_id" => 522,
"from" => {
"id" => 1949490661,
"is_bot" => false,
"first_name" => "Den",
"username" => "ItshenotI",
"language_code" => "uk"
},
"chat" => {
"id" => -686671946,
"title" => "test gropt for bots",
"type" => "group",
"all_members_are_administrators" => true
},
"date" => 1659272484,
"text" => "unacceptable text, that must be reported"
},
"text" => "/report",
"entities" => [
[0] {
"offset" => 0,
"length" => 7,
"type" => "bot_command"
}
]
}
}
and here data in action 'callback_query':
{
"update_id" => 156273638,
"callback_query" => {
"id" => "1327004554721482603",
"from" => {
"id" => 308967324,
"is_bot" => false,
"first_name" => "PRD",
"last_name" => "RSD",
"username" => "anko20094",
"language_code" => "en"
},
"message" => {
"message_id" => 524,
"from" => {
"id" => 5362380113,
"is_bot" => true,
"first_name" => "anko_test",
"username" => "anko_test_bot"
},
"chat" => {
"id" => -686671946,
"title" => "test gropt for bots",
"type" => "group",
"all_members_are_administrators" => true
},
"date" => 1659272601,
"text" => "We got report in the group!\nPRD RSD sent report on Den",
"reply_markup" => {
"inline_keyboard" => [
[0] [
[0] {
"text" => "Restrict messages for sender user",
"callback_data" => "restrict_messages_for_sender"
},
[1] {
"text" => "Restrict messages for reported user",
"callback_data" => "restrict_messages_for_reporter"
}
],
[1] [
[0] {
"text" => "Ban for sender",
"callback_data" => "ban_for_sender"
},
[1] {
"text" => "Ban for reporter",
"callback_data" => "ban_for_reporter"
}
]
]
}
},
"chat_instance" => "-5057992267045364113",
"data" => "restrict_messages_for_sender"
}
}
and screenshot how it looks in telegram:in telegram
I'd like to create a Payment model along the official Paypal Example on Github.
But I'm stuck in the creating of the model with the desired fields.
Payment.new({
:intent => "sale",
:payer => {
:payment_method => "paypal" },
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/" },
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "5",
:currency => "USD",
:quantity => 1 }]},
:amount => {
:total => "5",
:currency => "USD" },
:description => "This is the payment transaction description." }]})
Starting with rails g model Payment intent:string ... I don't know how to create the nested fields like
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/" }
and more deeper
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "5",
:currency => "USD",
:quantity => 1 }]},
Thanks for any help!
You can use OpenStruct to do this for you. It will be something like this :
paypal_hash = {
:intent => "sale",
:payer => {
:payment_method => "paypal" },
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/" },
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "5",
:currency => "USD",
:quantity => 1 }]},
:amount => {
:total => "5",
:currency => "USD" },
:description => "This is the payment transaction description." }]}
paypal_obj = OpenStruct.new(paypal_hash)
paypal_obj.intent
# => "sales"
When I try to add a variable value into session, I am getting singleton cant be dumped err.
Here is the value in the varibale
[
[0] {
:id => "574ecb43a7a5bb44c000443b",
:_id => "574ecb43a7a5bb44c000443b",
:active => true,
:capabilities => {
:network_connections => [
[0] {
:type => "ethernet-wan",
:name => "wan"
},
[1] {
:type => "ethernet-lan",
:name => "lan"
},
[2] {
:type => "wifi",
:name => "wlan"
},
[3] {
:type => "cellular",
:name => "wwan"
}
]
},
:commander_ids => [],
:created_at => "2016-05-11T15:46:12+00:00",
:deleted_at => nil,
:firmware_upgradable => true,
:ingestor_ids => [],
:last_known_translator_port => nil,
:long_description => "this is a liong desc",
:manufacturer => "test_sushant",
:model => "sushant_test",
:name => "zxc",
:parent_gateway_data_source_type_id => nil,
:rule_ids => [],
:software => "qwe",
:translator => "edge",
:type => "asd",
:updated_at => "2016-05-11T15:46:12+00:00",
:user_id => "572adee5a7a5bb320b000852"
},
The variable is an array of objects. I do not know why this is causing an err.
def self.group_by(field, format = 'day')
key_op = [['year', '$year'], ['month', '$month'], ['day', '$dayOfMonth']]
key_op = key_op.take(1 + key_op.find_index { |key, op| format == key })
project_date_fields = Hash[*key_op.collect { |key, op| [key, {op => "$#{field}"}] }.flatten]
group_id_fields = Hash[*key_op.collect { |key, op| [key, "$#{key}"] }.flatten]
pipeline = [
{"$project" => {"name" => 1, field => 1}.merge(project_date_fields)},
{"$group" => {"_id" => group_id_fields, "count" => {"$sum" => "$QtyUsed"}}},
{"$sort" => {"count" => -1}}
]
collection.aggregate(pipeline)<br>
end
when I execute that script, count result is 0.
How can I sum attributes QtyUsed?
you are not projecting the "QtyUsed" field
pipeline = [
{"$project" => {"name" => 1, field => 1, "QtyUsed' => 1}.merge(project_date_fields)},
{"$group" => {"_id" => group_id_fields, "count" => {"$sum" => "$QtyUsed"}}},
{"$sort" => {"count" => -1}}
]
I'm trying to pass the following ruby hash into an active resource(3.0.9) find(:from) call.
my_hash = {
:p => {:s => 100, :e => 2},
:k => "blah",
:f => [
{
:fl => :bt,
:tp => :trm,
:vl => "A::B"
},
{
:fl => :jcni,
:tp => :trm,
:vl => [133, 134]
},
{
:mnfl => :bmns,
:mxfl => :bmxs,
:tp => :rfstv,
:vl => 1e5
},
{
:fl => :bpo,
:tp => :rftv,
:op => :eta,
:vl => 1.months.ago.strftime("%Y-%m-%d")
}
]
}
Resource.find_by_x_and_y(:all, :from => :blah, params: my_hash)
On the server side action, when I print the params hash, its all messed up. ( last 3 hashes in the array mapped to :f )
{
"f" => [
{
"fl" => "bt",
"tp" => "trm",
"vl" => "A::B"
},
{
"fl" => "jcni",
"tp" => "trm",
"vl" => [
"133",
"134"
],
"mnfl" => "bmns",
"mxfl" => "bmxs"
},
{
"tp" => "rfstv",
"vl" => "100000.0",
"fl" => "bpo",
"op" => "eta"
},
{
"tp" => "rftv",
"vl" => "2013-01-25"
}
],
"k" => "blah",
"p" => {
"e" => "2",
"s" => "100"
},
"action" => "blah",
"controller" => "x/Y",
"format" => "json"
}
my_hash.to_query gives me
f[][fl]=bt&f[][tp]=trm&f[][vl]=A%3A%3AB&f[][fl]=jcni&f[][tp]=trm&f[][vl][]=133&f[][vl][]=134&f[][mnfl]=bmns&f[][mxfl]=bmxs&f[][tp]=rfstv&f[][vl]=100000.0&f[][fl]=bpo&f[][op]=eta&f[][tp]=rftv&f[][vl]=2013-01-25&k=blah&p[e]=2&p[s]=100
which doesn't have indices, hence the mixup.
Is there a name for this type of serialization using "[]" ? Is this guaranteed to serialize/deserialize arbitrarily nested hashes/arrays/primitives faithfully ? How do I make active resource behave sanely ?