📄Post Sorting
This page describes how posts are sorted on Matar.
Sort Parent Posts
Post sorting in Matar utilizes Flask-SQLAlchemy which provides various attributes on our models to get back a new query object over records. Here is how a post-sorting function looks like:
Describing Code:
A
posts_sorted
counter has been initalized to 0 for keeping a check on how many posts are sorted.Inside the for loop, with a **query SQLAlchemy **attribute select records where following conditions are met: ** - Post.parent_post_id **is **None **(select posts that do not have a parent post)(posts with questions) - **Post.sorted_at **is less than the current timestamp minus 30 minutes **cur_ms() **function is used to get the current timestamp in milliseconds .More explanation on the **cur_ms() **function here.
Limits result to the first 50 records where the above conditions are met.
Also inside the for loop, **post.sort_child_posts() **method is called on each post which sorts child posts related to the current post.
Increment the posts_sorted variable by 1 for each post processed in the loop.
Finally returns a dictionary containing a message and key value pair that the posts were sorted and the number of posts that were sorted, stored in posts_sorted variable.
Sort Child(Answer) Posts
Here is
sort_child_posts
definition which has the following functionality:
Describing sort_child_posts
Code:
An empty map
child_posts_map
is defined to store information about child posts.Query the Posts table and perform the following actions:
Query the Posts table and perform the following actions:
For each child post, extracts its _id and adds an entry to child_posts_map with initial values for "block," "upvote," "downvote," and "like" set to 0.
If the post already exists in child_posts_map donot create a new entry but query the database to fetch post activities related to child posts. such as "upvote," "downvote," "block," or "like."
For each post activity, update the entry in child_posts_map based on the activity type and post ID.
Also iterate through the child posts again and update their sort order and data based on the computed values in child_posts_map
Update the sorted_at attribute of the parent post if child posts are sorted.
Compute a sort_order value for the parent post based on a combination of the timestamp of its creation and the number of child posts. The specific calculation appears to be a weighted sum of the normalized timestamp and normalized subpost count.
Finally commit the changes to the parent post and save them to db
Sort Post Order
Describing compute_sort_order
code:
Input Parameters: The method takes an optional argument post_activity_map, which is expected to be a dictionary. This dictionary is assumed to contain information about the post's activity, such as the number of likes, dislikes, and blocks. If post_activity_map is not provided or is not a dictionary, the method returns None.
Weight Constants: Several weight constants (W_LIKEABILITY, W_BLOCK, W_UPVOTE, W_DOWNVOTE, and W_AGE) are defined at the beginning of the method. For example, W_LIKEABILITY is the weight assigned to the likeability factor.
Age Calculation: The code calculates the age of the post in days by subtracting the post's creation time (self.created_at) from the current UTC time (datetime.utcnow()). This age in days is stored in the age_days_count variable.
Activity Counts: The code then retrieves the counts of various activities (blocks, upvotes, downvotes, and likes) from the post_activity_map dictionary. If any of these counts are missing in the dictionary, it defaults to 0.
Likeability Score: The likeability_count is calculated as the difference between the number of upvotes and downvotes. This can be considered a measure of the post's overall likeability.
Sort Order Calculation: The sort_order is calculated by multiplying each activity count by its respective weight constant and summing them up. Additionally, the age of the post is factored in by subtracting its age in days multiplied by W_AGE.
Return Value: The computed sort_order value is returned as the result of the method.
Overall, this method computes a single numeric value (sort_order) that represents the "sorting order" of a post based on a combination of factors such as the post's age, likeability, blocks, upvotes, and downvotes.
Convert Current Time to Milliseconds
Code Summary :
In summary, when you call the cur_ms function, it returns the current timestamp in milliseconds since the Unix epoch. This can be useful for various time-related operations and for recording the time when an event occurs.
Last updated