Discussion:
A Basic Activity Feed - Is This Efficient?
BenderisGreat
2013-10-29 11:32:20 UTC
Permalink
Creating a basic activity feed for users. First wrote a simple function that
takes args as user_id, activity_type, and source_page. Here is the
function:

/function activity_tracker($user_id, $activity_type, $source_id ) {
global $wpdb, $current_user;
get_currentuserinfo();

$time = current_time('mysql');

$wpdb->insert( 'wp_jo_activity_feed', array(
'user_id' => $current_user->ID,
'activity_type' => $activity_type,
'source_id' => $source_id,
'time' => $time
));
/

Simple enough, then I drop this function call all over the place:

/ $runme = activity_tracker($_current_member->ID, 'update_info', 'prof');/

So everytime someone submits a form, or deletes an entry, or changes profile
information, friends another user, likes a post, etc... they each have a
specific activity name (that I enter manually) and is logged this way.

Then, I query the DB to show the most recent 10-50 activities, on the
activity feed page, like so:

/ <?php //activity call
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
$i = 0;
$limit = 15;
$count = count($activity_calls);
while ($i < $limit && $i < $count)
{
$row = $activity_calls[$i];
$get_row_user_id = $row->user_id;
$convert_name = get_userdata( $get_row_user_id );
$time = $row->time;

if ($row->activity_type == 'new_record') {

$message = ' created a new log entry at ';
$points = '+10';} elseif

( $row->activity_type == 'deleted_record') {

$message = ' deleted an existing log entry at ';
$points = 0; } else {

$message = ' updated his profile information at ';
$points = 5; }

echo '<li class="list-group-item">'.$points.''.$convert_name->user_login.
$message .$time. '</li>';
++$i; }
?>
</ul>
};/

You get the idea, it's obviously very simple - but it's also VERY DB
intensive (is that the correct terminology?). I am making a ton of DB
calls, which isnt a problem yet, but I would like to know if anyone has a
more efficient method to achieve the same result.

Additionally, for the message being returned in the activity feed, I am
using if, elseif, and else statements, which limits me. I would prefer to
have a more streamlined way to return the message. If I were to write a
function that selected the response based on the activity_type entered- what
would something like that look like?

Before it's recommended, I am aware buddypress is an option for activity
feeds. I want to do this so I can learn more, and create something unique
for myself.



--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/A-Basic-Activity-Feed-Is-This-Efficient-tp42662.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
Abdussamad Abdurrazzaq
2013-10-29 11:53:49 UTC
Permalink
Use $wpdb->prefix in activity_tracker as well

I hope you've sanitized authorid in that select query. You should stick
author id in quotes.

A function to return message based on activity would look like this:

function activity_message( $activity_id ) {
//map activity ids to messages here.
$activity_messages = array( 'update_something' => __( 'Updated
something' ),
'deleted_something' => __( 'Deleted something' )
);
if( array_key_exists( $activity_id, $activity_messages ) ){
return $activity_messages[ $activity_id ];
} else {
return __( 'Unknown activity' );
}
}
Post by BenderisGreat
Creating a basic activity feed for users. First wrote a simple function that
takes args as user_id, activity_type, and source_page. Here is the
/function activity_tracker($user_id, $activity_type, $source_id ) {
global $wpdb, $current_user;
get_currentuserinfo();
$time = current_time('mysql');
$wpdb->insert( 'wp_jo_activity_feed', array(
'user_id' => $current_user->ID,
'activity_type' => $activity_type,
'source_id' => $source_id,
'time' => $time
));
/
/ $runme = activity_tracker($_current_member->ID, 'update_info', 'prof');/
So everytime someone submits a form, or deletes an entry, or changes profile
information, friends another user, likes a post, etc... they each have a
specific activity name (that I enter manually) and is logged this way.
Then, I query the DB to show the most recent 10-50 activities, on the
/ <?php //activity call
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
$i = 0;
$limit = 15;
$count = count($activity_calls);
while ($i < $limit && $i < $count)
{
$row = $activity_calls[$i];
$get_row_user_id = $row->user_id;
$convert_name = get_userdata( $get_row_user_id );
$time = $row->time;
if ($row->activity_type == 'new_record') {
$message = ' created a new log entry at ';
$points = '+10';} elseif
( $row->activity_type == 'deleted_record') {
$message = ' deleted an existing log entry at ';
$points = 0; } else {
$message = ' updated his profile information at ';
$points = 5; }
echo '<li class="list-group-item">'.$points.''.$convert_name->user_login.
$message .$time. '</li>';
++$i; }
?>
</ul>
};/
You get the idea, it's obviously very simple - but it's also VERY DB
intensive (is that the correct terminology?). I am making a ton of DB
calls, which isnt a problem yet, but I would like to know if anyone has a
more efficient method to achieve the same result.
Additionally, for the message being returned in the activity feed, I am
using if, elseif, and else statements, which limits me. I would prefer to
have a more streamlined way to return the message. If I were to write a
function that selected the response based on the activity_type entered- what
would something like that look like?
Before it's recommended, I am aware buddypress is an option for activity
feeds. I want to do this so I can learn more, and create something unique
for myself.
--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/A-Basic-Activity-Feed-Is-This-Efficient-tp42662.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-10-29 18:57:18 UTC
Permalink
Abdussamad - thank you for the array message idea, I didnt think of that.
:) I also added sanitation. So I guess this is how activity feeds are
done? I thought I this was the poorrmans activity feed.


On Tue, Oct 29, 2013 at 4:53 AM, Abdussamad Abdurrazzaq <
Post by Abdussamad Abdurrazzaq
Use $wpdb->prefix in activity_tracker as well
I hope you've sanitized authorid in that select query. You should stick
author id in quotes.
function activity_message( $activity_id ) {
//map activity ids to messages here.
$activity_messages = array( 'update_something' => __( 'Updated
something' ),
'deleted_something' => __( 'Deleted something' )
);
if( array_key_exists( $activity_id, $activity_messages ) ){
return $activity_messages[ $activity_id ];
} else {
return __( 'Unknown activity' );
}
}
Post by BenderisGreat
Creating a basic activity feed for users. First wrote a simple function that
takes args as user_id, activity_type, and source_page. Here is the
/function activity_tracker($user_id, $activity_type, $source_id ) {
global $wpdb, $current_user;
get_currentuserinfo();
$time = current_time('mysql');
$wpdb->insert( 'wp_jo_activity_feed', array(
'user_id' => $current_user->ID,
'activity_type' => $activity_type,
'source_id' => $source_id,
'time' => $time
));
/
/ $runme = activity_tracker($_current_**member->ID, 'update_info',
'prof');/
So everytime someone submits a form, or deletes an entry, or changes profile
information, friends another user, likes a post, etc... they each have a
specific activity name (that I enter manually) and is logged this way.
Then, I query the DB to show the most recent 10-50 activities, on the
/ <?php //activity call
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
$i = 0;
$limit = 15;
$count = count($activity_calls);
while ($i < $limit && $i < $count)
{
$row = $activity_calls[$i];
$get_row_user_id = $row->user_id;
$convert_name = get_userdata( $get_row_user_id );
$time = $row->time;
if ($row->activity_type == 'new_record') {
$message = ' created a new log entry at ';
$points = '+10';} elseif
( $row->activity_type == 'deleted_record') {
$message = ' deleted an existing log entry at ';
$points = 0; } else {
$message = ' updated his profile information at ';
$points = 5; }
echo '<li class="list-group-item">'.$**
points.''.$convert_name->user_**login.
$message .$time. '</li>';
++$i; }
?>
</ul>
};/
You get the idea, it's obviously very simple - but it's also VERY DB
intensive (is that the correct terminology?). I am making a ton of DB
calls, which isnt a problem yet, but I would like to know if anyone has a
more efficient method to achieve the same result.
Additionally, for the message being returned in the activity feed, I am
using if, elseif, and else statements, which limits me. I would prefer to
have a more streamlined way to return the message. If I were to write a
function that selected the response based on the activity_type entered- what
would something like that look like?
Before it's recommended, I am aware buddypress is an option for activity
feeds. I want to do this so I can learn more, and create something unique
for myself.
--
View this message in context: http://wordpress-hackers.**
1065353.n5.nabble.com/A-Basic-**Activity-Feed-Is-This-**
Efficient-tp42662.html<http://wordpress-hackers.1065353.n5.nabble.com/A-Basic-Activity-Feed-Is-This-Efficient-tp42662.html>
Sent from the Wordpress Hackers mailing list archive at Nabble.com.
______________________________**_________________
wp-hackers mailing list
http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
______________________________**_________________
wp-hackers mailing list
http://lists.automattic.com/**mailman/listinfo/wp-hackers<http://lists.automattic.com/mailman/listinfo/wp-hackers>
J.D. Grimes
2013-10-29 12:34:23 UTC
Permalink
Post by BenderisGreat
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
You should be using $wpdb->prepare():

$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC', $author_id ) );

-J.D.
J.D. Grimes
2013-10-29 12:54:11 UTC
Permalink
Any activity feed will require a lot of database queries, and yours probably requires less than BuddyPress’s. I would suggest that you put the limit directly in the SQL query. Right now you are pulling more entries from the DB than you need. So:

$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC LIMIT %d', $author_id, $limit ) );

Abdussamad’s solution for the activity message is good. Just FYI, you could also have used a switch statement:

switch ( $row->activity_type ) {

case ‘new_record’:
$message = //…
$points = //…
break;

case ‘deleted_record’:
//...
}

But his solution is better.

-J.D.
Post by J.D. Grimes
Post by BenderisGreat
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id ASC');
$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC', $author_id ) );
-J.D.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-10-29 19:11:18 UTC
Permalink
oh JD- If I limit the pull to say 10 rows, how would I allow the user to
pull 10 more at s time? So show 10 and on click load the next 10 previous,
etc...
Post by J.D. Grimes
Any activity feed will require a lot of database queries, and yours
probably requires less than BuddyPress’s. I would suggest that you put the
limit directly in the SQL query. Right now you are pulling more entries
$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC LIMIT %d', $author_id, $limit ) );
Abdussamad’s solution for the activity message is good. Just FYI, you
switch ( $row->activity_type ) {
$message = //…
$points = //…
break;
//...
}
But his solution is better.
-J.D.
Post by J.D. Grimes
Post by BenderisGreat
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id
ASC');
Post by J.D. Grimes
$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' .
$wpdb->prefix .
Post by J.D. Grimes
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC', $author_id ) );
-J.D.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-10-29 19:32:29 UTC
Permalink
Oh, if you want to paginate it, then you will want to look at some tutorials on pagination. You would reload the page or update the page via AJAX, and that request would load the next/previous 10 rows for the user. You can output all rows an paginate them with JS, but depending on the number of rows of activity, that will probably be a waste.
Post by Gregory Lancaster
oh JD- If I limit the pull to say 10 rows, how would I allow the user to
pull 10 more at s time? So show 10 and on click load the next 10 previous,
etc...
Post by J.D. Grimes
Any activity feed will require a lot of database queries, and yours
probably requires less than BuddyPress’s. I would suggest that you put the
limit directly in the SQL query. Right now you are pulling more entries
$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC LIMIT %d', $author_id, $limit ) );
Abdussamad’s solution for the activity message is good. Just FYI, you
switch ( $row->activity_type ) {
$message = //…
$points = //…
break;
//...
}
But his solution is better.
-J.D.
Post by J.D. Grimes
Post by BenderisGreat
$activity_calls = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix .
'jo_activity_feed' .' WHERE user_id = '. $author_id .' ORDER BY id
ASC');
Post by J.D. Grimes
$activity_calls = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' .
$wpdb->prefix .
Post by J.D. Grimes
'jo_activity_feed WHERE user_id = %d ORDER BY id ASC', $author_id ) );
-J.D.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Continue reading on narkive:
Loading...