I have a Telegram Bot which updates scores for a set of teams.
Teams are dynamic in size.
def collection_keyboard(identity)
ip_addr = "127.0.0.1:27017"
client = Mongo::Client.new([ip_addr], :database => "camp")
kb = []
client[:inventory].find({"owner": identity}, projection: {"_id": 0, "name": 1}).each do |doc|
kb << Telegram::Bot::Types::InlineKeyboardButton.new(text: doc.to_s , callback_data: ??) //What do i put as the callback data?
end
markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
return markup
end
The above method queries the db and creates an inline keyboard, allowing users to choose which teams score to increment.
Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
case message
when Telegram::Bot::Types::CallbackQuery
if message.data == ?? //What do i check here?
end
end
A second inlinekeyboard will be created once the first CallbackQuery has been returned. This keyboard will allow a user to choose how many points to give e.g. 10, 20, 30.
However, since the group size vary, how do I pass the value of the Inline Keyboard which he user has clicked so that I can add points to that particular team? Since the group is dynamic, I cannot capture all possible callback return value with the regular switch statement?
For example, if a user clicks Yellow, how can I pass this value to the next InlineKeyboard such that the user can add 10 points to them?
I'm attempting to make a linq where contains query quicker.
The data set contains 256,999 clients. The Ids is just a simple list of GUID'S and this would could only contain 3 records.
The below query can take up to a min to return the 3 records. This is because the logic will go through the 256,999 record to see if any of the 256,999 records are within the List of 3 records.
returnItems = context.ExecuteQuery<DataClass.SelectClientsGridView>(sql).Where(x => ids.Contains(x.ClientId)).ToList();
I would like to and get the query to check if the three records are within the pot of 256,999. So in a way this should be much quicker.
I don't want to do a loop as the 3 records could be far more (thousands). The more loops the more hits to the db.
I don't want to grap all the db records (256,999) and then do the query as it would take nearly the same amount of time.
If I grap just the Ids for all the 256,999 from the DB it would take a second. This is where the Ids come from. (A filtered, small and simple list)
Any Ideas?
Thanks
You've said "I don't want to grab all the db records (256,999) and then do the query as it would take nearly the same amount of time," but also "If I grab just the Ids for all the 256,999 from the DB it would take a second." So does this really take "just as long"?
returnItems = context.ExecuteQuery<DataClass.SelectClientsGridView>(sql).Select(x => x.ClientId).ToList().Where(x => ids.Contains(x)).ToList();
Unfortunately, even if this is fast, it's not an answer, as you'll still need effectively the original query to actually extract the full records for the Ids matched :-(
So, adding an index is likely your best option.
The reason the Id query is quicker is due to one field being returned and its only a single table query.
The main query contains sub queries (below). So I get the Ids from a quick and easy query, then use the Ids to get the more details information.
SELECT Clients.Id as ClientId, Clients.ClientRef as ClientRef, Clients.Title + ' ' + Clients.Forename + ' ' + Clients.Surname as FullName,
[Address1] ,[Address2],[Address3],[Town],[County],[Postcode],
Clients.Consent AS Consent,
CONVERT(nvarchar(10), Clients.Dob, 103) as FormatedDOB,
CASE WHEN Clients.IsMale = 1 THEN 'Male' WHEN Clients.IsMale = 0 THEN 'Female' END As Gender,
Convert(nvarchar(10), Max(Assessments.TestDate),103) as LastVisit, ";
CASE WHEN Max(Convert(integer,Assessments.Submitted)) = 1 Then 'true' ELSE 'false' END AS Submitted,
CASE WHEN Max(Convert(integer,Assessments.GPSubmit)) = 1 Then 'true' ELSE 'false' END AS GPSubmit,
CASE WHEN Max(Convert(integer,Assessments.QualForPay)) = 1 Then 'true' ELSE 'false' END AS QualForPay,
Clients.UserIds AS LinkedUsers
FROM Clients
Left JOIN Assessments ON Clients.Id = Assessments.ClientId
Left JOIN Layouts ON Layouts.Id = Assessments.LayoutId
GROUP BY Clients.Id, Clients.ClientRef, Clients.Title, Clients.Forename, Clients.Surname, [Address1] ,[Address2],[Address3],[Town],[County],[Postcode],Clients.Consent, Clients.Dob, Clients.IsMale,Clients.UserIds";//,Layouts.LayoutName, Layouts.SubmissionProcess
ORDER BY ClientRef
I was hoping there was an easier way to do the Contain element. As the pool of Ids would be smaller than the main pool.
A way I've speeded it up for now is. I've done a Stinrg.Join to the list of Ids and added them as a WHERE within the main SQL. This has reduced the time down to a seconds or so now.
I am not quite sure how to title this question and I am having a hard time getting it to work so here goes.
I have a hash of users which is various sizes. This can be anywhere from 2 to 40. I also have a hash of tickets which I want to search through and find any entries that do not contain the user_id of my users hash. I am not quite sure how to accomplish this. My last attempt I used this:
#not_found = []
users.each do |u|
#not_found += #tickets.select{|t| t["user_id"] != u.user_id}
end
I know this is not the right result as it does a comparison for only the one user_id. What I need to do is run through all of the tickets and pull out any results that contain a user_id that is not in the users hash.
I hope I am explaining this properly and appreciate any help!
Try this
known_user_ids = users.map(&:user_id)
tickets_with_unknown_users = #tickets.reject{ |t| known_user_ids.include?(t["user_id"]) }
I'm looking for a way to update all record with certain condition their cur_val + 100:
I have a table Suggestions with id and score fields, and I want all the entries with specific ids to receive a score bump, e.g:
Suggestion.where(id: ids_list).update_all(score: score+100)
How do I do that?
Try plain SQL, read about update_all:
Suggestion.where(id: ids_list).update_all('score = score + 100')
But remember update_all not trigger Active Record callbacks or validations.
Some tips:
You can do it in Ruby but this very bad:
Suggestion.where(id: ids_list).find_each do |x|
x.update(score: x.score + 100)
end
Things like this should happen in database.
I am building a simple app and I want to show some simple statistics to admins. I want to know is it possible to get the array of counts of objects from database that were created on the same date using datamapper or do I have to manually go through records and count them?
Objects have created_at attribute.
So i managed to solve it, I dont know if it is the right way but it works
days = Array.new
count = Array.new
photos_per_day = Photo.aggregate(:all.count, :upload_date)
photos_per_day.each do |ppd|
count.push(ppd[0])
days.push(ppd[1].day.to_s + " " + Date::MONTHNAMES[photo[1].month])
end
{:days => days, :count => count}.to_json
try this out:-
suppose you want to count users created on specific date.
User.group('date(created_at)').count
=> {"2013-05-20"=>66,
"2013-05-07"=>46,
"2013-05-17"=>9,
"2013-05-13"=>28,
"2013-05-22"=>22,
"2013-05-15"=>43,
"2013-05-08"=>32,
"2013-06-12"=>2,
"2013-05-28"=>22,
"2013-05-16"=>35,
"2013-05-09"=>33,
"2013-05-10"=>132,
"2013-05-21"=>5,
"2013-05-14"=>38,
"2013-05-11"=>4}