I'm wanting to setup an app that will retrieve all follower's IDs, count how many followers each has, and sum the total.
Example: MyAccount > MyFollowersAccount > followers_count
I want to do this for each follower and then add the total amount of followers they all have. So if I have 100 followers and my followers have 100 followers, I should get a return value of 10,000 followers or 10,100 (including my own).
I am having difficulty figuring out how to do this with multiple users. I've seen ones that will retrieve a single user, but I'm worried about looping through potentially 1,000 of followers.
Any ideas or suggestions?
MyAccount > MyFollowersAccount > followers_count
What I would do is this,
run the Twitter API method GET followers/ids
https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=twitterapi
to get the ID's of the followers of screenname X
and then switch over to Twitter Counter which will give you a more detailed list and information about the followers, followers specifically. Also, this could be a way to split up the two calls so you won't be blocked by a rate limit problem on one end.
http://twittercounter.com/pages/api?ref=footer
Related
Use case:
I need to create a report on office groups without owner. This happens when people leave the company and their account is deleted. Their groups live further, but eventually group expiration kicks in and somebody need to take action.
Question: What is the easiest was to create query in graph to filter groups that do not have an owner?
What I currently do is:
List all groups
Enumerate this list and look for groups where the owner array is empty:
https://graph.microsoft.com/v1.0/groups/{id}/owners?$select=mail
This returns an empty array when there are no owners.
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects(mail)",
"value": []
}
This, in theory, works, but takes ages as we have many 10 thousands of groups, so I am looking for a solution that makes this possible with one query. I also tried to filter the exprationDatTime property to limit results but query this doesn't seem to be supported. I mainly need groups that are due to expire.
There is no way how to get groups without owners with one query.
What you can do is to query all groups, expand owners and select only id of the group and id of the owner. It will minimize the response size.
Then iterate through the all groups and check for empty owners collection.
GET https://graph.microsoft.com/v1.0/groups?$expand=owners($select=id)&$select=id
I can't find an efficient way to query Posts(PFObject) or Users(PFUser) classes and also have the isPostLiked(boolean) and isUserFollowed(boolean) included in the results array respectively.
Lets say, I have queried and received 25 Posts from the server. I want to fill in the like heart button with red if I have previously liked this Post. It would be very inefficient to query all the likes of these Posts and see if current user is contained in the results.
Is it possible to write a cloud code function to insert an 'isLiked' field to the query results and return it to the User for instance?
I am open to new strategies since I am stuck here. It is obvious that most of the social apps are having this need as a standard so there must be an effective solution. Thanks
Your best action is to rid yourself of the relational database thinking. It seems to me you have a separate Likes class that tracks which user likes which post.
In the NoSQL space you should focus on your queries when you plan your datamodel. Ask yourself this question:
How do I want to query my data?
In this use case, I'm thinking you might want to
Show how many likes a Post has
Maybe show which users did like the Post
Track whether the current user has liked a certain post
Maybe find all the Posts the current user has liked?
To solve this, I would do the following:
On the Post class, add a column likedby.
On the User class, add a column likedposts.
Both these columns are Array columns
Every time a user likes a post, you add a Pointer to the current user to the likedby array column for the Post AND a pointer to the post to the likedposts array column for the User.
This makes it very easy to
find how many likes a post has (number of elements in likedby)
list all the users that liked the post (using query.includeKey("likedby") on the Post)
check if the current user has already liked the post (if likedby array contains currentuser)
list all the posts a user has liked (using query.includeKey("likedposts") on the User).
Use the same logic for followings.
In a scenario where there are users that have posts, and each user has a view representing a news feed (much like with a logged in Tumblr account), and each post overview has a link to the comments with a comment counter per post, what is the best caching strategy here (On a Rails 4 stack)?
Assuming 5 users, A B C D E, with each being subscribed to the 2 users on their right (A is subscribed to B and C, B is subscribed to C and D etc.) and only having the users they've subscribed to showing up on their news feed view.
Edit:
Assume a fan-out-on-write approach is taken, where each user has a unique set (of post ids) in Redis, and on every post create, the id of the new post is appended to every of the post creator's friends' sets. The redis sets act as an index and a user's feed is fetched via a single SQL query.
Bearing this in mind, caching each feed should be a matter of this approach:
Check set in redis (first hit)
write #feed_array to memcached
fetch posts with single SQL command and save to #feed
write #feed to memcached
Check set in redis (second hit)
If set values match #feed_array then return #feed from memcached. Otherwise new SQL query and override #feed in memcached
This approach would mean easy cache use for the views when iterating through the #post divs, but how would one handle the comment counts?
unrelated of the application stack that you are using, i don't think that a caching approach scales in your situation. twitter-like functionality is often handled by de-normalization.
in your situation, this could mean implementing a feed model for each user, appending new posts of the followers, so that it is fast to load the 'timeline' of a user from his own feed, instead of joining all his (possible thousands) of friends.
I have tried different combinations but it seems that I cannot work this out.
I want to retrieve, from an Event model, those events which have the biggest number of users.
For example, I retrieve users of an event like this
#users = Event.find(x).users
They can be counted using this
Event.find(x).users.count
So, How should be done to order the list by the number of users each event has. And then retrieve the 8 first?
The same issue was resolved in: How to get highest count of associated model (Rails)?
Event.order("events.users_count DESC")
I am trying to make an admin dashboard that shows an administrator relevant statistics about the site. For example, given a company has many users, finding the average number of users per company, or the maximum number of users a company has.
I have found activerecord::calculations, which seems to do most of what I want, but as far as I can tell, it doesn't let you do anything with relations. How would I go about finding counts or averages that are grouped by relations?
You have to think of it in terms of the User.
The simplest way would be
# get a hash of company_ids and user counts
User.group(:company_id).count
But then you'll have to load the Companies and match them up.
Then you could try and do
user_counts = User.group(:company_id).count
company_users = Company.all.map{|company| user_counts[company.id]}
# the maximum
company_users.max
# the average
company_users.sum.to_f / company_users.length