Total time spend by day on one page - sql

I have a table which summarize the time spent by users on a specific page of the app.
I want to get the total time spent on this page per day.
The table:
user_id date label_of_the_page
1 2019-03-03T00:21:56.384Z . page we want
1 2019-03-03T00:21:57.314Z page we want
1 2019-03-03T00:21:58.024Z . page we want
1 2019-03-03T00:21:59.384Z new page
The idea is to make the difference between the first time the label_of_the_page is the page I want, and the time for which the label change. But, we have to do it for each person who reach the page. It can be several time the same user_id, so grouping by user_id is not a good idea, I think...

So I guess the simplest way is like this:
Make a new column with three labels:
Case 1: User in the page we are interested in, and in the previous row he was not
Case 2: User in an other page and was in the page we are interested in, in the row before.
Case 3: User in the page and was also on the page the row before, or in other page and other page in the row before
Then we only keep the rows of case 1 and 2, and we just have to make the difference between [time(case 2) - time(case 1)] for each session.
But Im not sure how I can make all of it with SQL.

Related

Best practice for pagination based on item updated time

Let's consider I have 30 items in my db. And clientA will make an api call to get the first 10 records based on item updated time. And think of a use case where clientB updated the 11th record (item) by making some changes in it. But now when clientA makes an api call for next set of items based on the pagination page 2 (items from 11 to 20) It's because the clientB has updated the 11th item the pagination is going to break here (Bases on updated time 11th item will become 1 and 1 become 2, 2 become 3 ...10 becomes 11).There is a chance that clientA is will receive the duplicate data.
Is there any better approach for this kind of problem ??
Any help would be thankfull
I think you could retrieve all elements each time using no pagination at all, to prevent this kind of "false information" at your table.
If visualizing the actual values of each record is mandatory, you could always add a new function to your api working as a trigger. Each time a user modifies any record, this api's function will trigger a message for all active sessions to notify the user some data has been changed. As an example, think about something like the "twitter's live feed". In which when a new bunch of tweets are created, Twitter will notify all users to reload the page if they want to see realtime information.

Clockify API: Time Report for Project and aggregate by User

I would like to create a report for one Clockify project (I do know its ID) agregated by users.
The result would be like this:
Project XY
User 1: XX (grouped duration)
User 2: YY (grouped duration)
User 3: ZZ (grouped duration)
etc.
Is there any better way than iterating through all users calling this API?
/workspaces/{$workspace_id}/user/{$user_id}/time-entries?project={$project_id}&start={$start}&end={$end}
This is too slow and the request return only 50 time entries. Our projects have hundreds (some projects even thousends) of entries.
From the documentation if you go down to Find time entries for user on workspace you can see that the page-size parameter has a default value of 50. So if you change it to 10000 let's say, you can get away with just 1 query per user.

Pagination issue of Bootstrap data-tables

I am using bootstrap for better look and feel of UI, my issue is pagination in data-table. I am showing 10 (showing Active records only in the list ) records per page, for example 11 records are there in listing, data-tables shows pages count as Previous, 1, 2 and Next so its fine for now.
Suppose I changed the status of anyone record in that list to In Active, now the records in the listing are 10 only. So my issue is it shows the pagination as same as Previous one without any record (11th record) i.e. Previous, 1, 2 and Next.
If I click on number 2 it will navigate to 2nd page without any records and it shows "No records found", That was my big issue with Bootstrap data-tables.

Variable number of records per page in Yii

I'm trying to get variable number of records per page in Yii. The page size is dependent on the number of records that match a criteria, so It has to be changed on every AJAX request sent by Yii's pagination.
Here's a sketch of the exact problem:
Table Structure [pk, user_id, entry_date, entry_message]
The view has a CGridView with pagination enabled and pageSize set to 10. What I want to do is to show the entries from one date on one page. For example, page one should show the entries from today. Page two should show the entries from yesterday. So I presume that the limit and the offset should be variable, but I can't quiet understand how to change them.

SQL - mantain sort order for paginating data when change in real time

I'm implementing a php page that display data in pagination format. My problem is that these data change in real time, so when user request next page, I submit last id and query is executed for retrieve more 10 rows from last id order by a column that value change in real time. For example I have 20 rows:
Id 1 col_real_time 5
Id 2 col_real_time 3
Id 3 col_real_time 11
Etc
I get data sorted by col_real_time in ascending order, so result is
id 2, id 1, id 3
Now in realtime id 2 change in col_real_time 29 before user send request for next page, user now send request for next results and because id 2 now is 29 he already see it.
How can I do?
Now in realtime id 2 change
You basically have to take a snapshot of the data if you don't want the data to appear to change to the user. This isn't something that you can do very efficiently in SQL, so I'd recommend downloading the entire result set into a PHP session variable that can be persisted across pages. That way, you can just get rows on demand. There are Javascript widgets that will effectively do the same thing, but will send the entire result set to the client which is a bad idea if you have a lot of data.
This is not as easy to do as pure SQL pagination, as you will have to take responsibility for cleaning the stored var out when it's no longer needed. Otherwise, you'll rather quickly run out of memory.
If you have just a few pages, you could:
Save it to session and page over it, instead of going back to the
database server.
Save it to a JSON object list and use Jquery to read it and page
over it.
Save it to a temp table indicating generation timestamp, user_id and
session_id, and page over it.