How to return all the rows in tracker_sessions table using antonioribeiro/tracker - laravel

I am using antonioribeiro/tracker for my website analytics.
Everything is working as intended but I want to count all the rows in tracker_sessions table, when I do
$sessions = Tracker::sessions();
return count($sessions);
It returns only the number of rows created today.
How would I return all the rows in tracker_sessions table?

Looking at the documentation, you can do this to get more sessions:
$sessions = Tracker::sessions(60 * 24 * 365 ); // get sessions (visits) from the past 365 days
.., or any number of minutes you want.
You can also use Query Builder to count the table directly:
use Illuminate\Support\Facades\DB;
$sessions = DB::table('tracker_sessions')->count();

Related

Laravel 8 - count clicks but user can't spam the function

In my website I track clicks on a specific link and save the count on database with this function:
public function redirect($url)
{
$data = Class::where('url', $url)->first('clicks');
$cnt = $data->clicks;
$cnt++;
$sql = Class::where('url', $url)->update([
'clicks' => $cnt,
]);
if (!$sql) {
abort(500);
}
return redirect('https://website.com/'.$url);
}
the problem is if someone spam the link , the count goes up. I want stop count if user clicked on link 5mins ago.
he will redirected to the link but count doesn't go up.
I'm new so it's so good if you explain it with details. Thanks
I would create a new table, lets call it link_clicks. You will need 3 columns, one to identify the person, one to get the time and one to identify the link (I dont exactly know how you store the links you want to observe).
So more or less you will have the following table:
link_clicks
user_id
link_id
created_at
If the users are always logged in, I would store the user_id, if they are not logged in, I would store the IP-address. So instead of user_id make a column called ip or ip-address.
Afterwards you can easily get the last click and its time.
--Example
Your new table contains following entry:
user_id = 1, link_id = 1 and created_at = 2021-04-21 19:00:00
Now in your controller you get the current date_time date("Y-m-d H:i:s") and the user id like that: auth()->id(). You can also define your time treshold in minutes e.g max_dif = 5.
Afterwards you can query the table for all clicks for the user. You can either make the time comparision in your where() statement or you can make some comaprison in your php code to check if the time treshhold is reached.

Retrieve data according to timestamp value from Room Database

I'm trying to retrieve entries that were written in the last 2 minutes into the database.
I use the following query:
#Query("SELECT * FROM Contacts where ('now()' - gatt_server_connection_timestamp) <= 120000")
List<Contact> getContactsByGattServerConnectionTimestamp();
However the result I get is the whole database.
What is wrong with this query?
The SQLite date-time functions are described here.
If gatt_server_connection_timestamp is in milliseconds since epoch, this query should work:
#Query("SELECT * FROM Contacts where gatt_server_connection_timestamp >= (1000 * strftime('%s', datetime('now', '-2 minutes'))))
List<Contact> getContactsByGattServerConnectionTimestamp();

Laravel Eloquent - Get a record every hour

I have a table that stores statistics every 3 minutes with a cron job.
I want to display a chart with this data but I want the chart to have an interval of 1 hour otherwise it looks ugly as hell and is too much resource demanding.
The table has the created_at and updated_at columns.
How can I do this with eloquent?
EDIT: I want to query the records from the last 24 hours but that gives me around 480 records which is too much for a chart. I'd like to have only 24 records instead (one for every hour).
Thanks for your help!
Thanks Tim!
For anyone reading through this later, here is the solution: https://laracasts.com/discuss/channels/laravel/count-rows-grouped-by-hours-of-the-day
Model::where('created_at', '>=', Carbon::now()->subDay())->get()->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('h');
});
This will allow you to get data 1 hours ago base on current time.
//Get all data for the day
$all_data = Model::where('created_at','>=',Carbon::today()->get());
//Recursive to groupBy hours
$i=1;
while ($all_data->last() != null)
{
$hourly_data = Model::where('created_at','>=',Carbon::today()->addHours($i))->get();
$all_data= $all_data->merge($hourly_data);
$i++
}
return $all_data;

Laravel 5 search records 30min and older for today

I am trying to get all records that are 30min old and are today with a field called smsed value = to 0.
What i am trying to do is get all the records in my database with todays date and are older than 30min.
$data = DB::table('applicant')->whereRaw('AppDate < (NOW() - INTERVAL 30 MINUTE)')->where('smsed','=',0)->limit(5000)->get();
what the above does is get all records in the DB and not only for today.
This is because you only asking it for records that are over 30minutes old and not including anything to limit it to today.
You could use something like whereBetween:
$data = DB::table('applicant')
->whereBetween('AppDate', [Carbon\Carbon::now()->startOfDay(), Carbon\Carbon::now()->subMinute(30)])
->where('smsed', '=', 0)
->limit(5000)
->get();
Alternatively, if you just want to keep your sql functions you could do something like:
$data = DB::table('applicant')
->whereRaw('AppDate < (NOW() - INTERVAL 30 MINUTE)')
->whereRaw('DATE(AppDate) = CURDATE()')
->where('smsed','=',0)
->limit(5000)
->get();
Hope this helps!

MongoDB get count of total records after applying query filters

I just need to find the count of total records which i get from the mentioned query.
$orderData = $this->mongo_db->get_where('order', array('order_date'=> array('$gte'=>floatval(1337797800000), '$lte'=>floatval(1337970600000))));
I am using alexbilbie's library for codeigniter.
echo count($orderData); will return the number of records from your result

Resources