How to check whether a person is logged-in atleast once in a day and how to calculate the length of session of login in mysql-flask application? - database-schema

I am using mysql and flask to develop a time-cards management system (which is basically an attendance taking software), in which I need to check the time a user is logged in and logged out and the interval of the session to calculate his working time and I should be able to update the database as a leave if he didn't login the whole day. And I need to store the data monthly-wise so that we can calculate the salary of the employee depending on his working hours and leaves in that month.
Can someone please help me how to:
Design my database such that we can store the monthly information of all
employees efficiently (like should i create a table for each month or some
other way?)
How to check the time of login and logout and the length of session of a
user?
How to check whether a user is logged in at least once in a day or not?
Thanks in advance.

Record the start and end time of each work period in a table.
create table time_record (
time_record_id int not null auto_increment primary key,
start_time datetime not null,
end_time datetime not null,
person_id int not null references person(person_id)
);
Then you can report on daily or monthly activity for each user with some simple queries. For instance, to roll up to a daily summary, you can join a set of calendar days against the time records.

Related

Handle date and time for different timezones

I'm working on a feature of an app where users can create groups, and each group can have multiple posts. The groups have a start and end date. I'm using Node.js for the backend and PostgreSQL to model the data. The tables look like this (simplified):
CREATE TABLE IF NOT EXISTS public."group"
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
start_date timestamp with time zone NOT NULL,
end_date timestamp with time zone NOT NULL,
owner_id uuid
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
)
CREATE TABLE IF NOT EXISTS public."post"
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
group_id uuid,
created_at timestamp with time zone NOT NULL
)
A user can only post to a specific group once a day and the days in which they post should be recorded (users will be able to see in a "progress calendar" the days in which other people in the group and themselves created a post). Users from different countries may enter the same group.
How should I approach the following problems:
1. Make sure the user doesn't post more than once a day.
Here I could just create a query which checks if the user already
posted on day X. The problem is not knowing the user's
timezone, as the server's timezone is generally used. Should I just
store users' location in the database when they create an account
and use the timezone from there? What if they move? I also thought
about sending a timezone along with the request, but that doesn't
seem like a good idea, as a malicious user could send anything
there.
2. Sync the progress calendars for users with different time zones (e.g. user X from Romania, at 1 AM 22 August, should not see an x-mark in user's Y calendar for the date of 21 August, denoting that user Y hasn't posted on that date, as user Y still has time to post (user Y's date/time is 21 August 6 PM).
3. Not as important as the other 2 - What's a good practice for choosing the start / end date timezone for the group, so that users are not surprised when the group starts / ends. I would go with UTC, but I'm not sure people having UTC-9 and UTC+9 would have a pleasant experience with this.
Just to give an example, timestamp with timezone is represented as 2021-08-17 20:01:00.427+03 in postgres.

How to make table triggier based on date?

Is there a way to make a table trigger that sends an email once a date attribute get 30 days from the date listed? e.g. 07/31/2020 is listed in the table and when it gets to 07/01/2020 in real life, an email is sent out to notify someone. it would be a 30 day in advance notification.
Thanks
You can make an SQL Job, which will executed on daily basis and send db email once it get date difference (datediff(D, getdate(),'2020-07-31) equals to 30 OR change in the table to trigger

Cohort on Tableau

Is it possible to do cohort on tableau? how would I see cohort of first time purchase if I only have name of customer, date of sign up, date of first time purchase. I want to see the relation between sign up and purchase on cohort.
company_name signup_date approval_status approval_date first_paid_plan first_paid_plan_date
I think you'll like what you find here: https://www.tableau.com/about/blog/LOD-expressions

Crystal Reports - create calendar

I need to create an attendence list showing days in rows and employee names in colums. The list will always cover one full month chosen in parameters.
How can I create a recordset of days of chosen month? I've done it in command section but, due to ERP system limitations, it must done otherways.
Thank you,
Przemek
A good approach is to create a Calendar table (aka Date Dimension in data warehousing lingo). It makes it easy to show days without any attendance. If you don't need that aspect, you can simply create a formula that returns the attendance date month's day, and Group on that formula. The Day() function gets you the day of month. For example,
Day ({Orders.Order Date})
If you search 'creating a data dimension or calendar table' you'll find many helpful sources such as this one: https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/
For your case, I agree with the comments in that post about using date instead of integer as the primary key. Integer PK makes more sense for true data warehousing scenarios as opposed to legacy databases.

SYS Date in Oracle

I have a microservice and I use REST methods such as GET PUT POST, for this issue lets take an example POST !
I want to add a record to one table, but I need to only add two values and the remaining two are id and date. ID is auto increment and have used Sequence Generator, hence working even if I do not give an ID as input.
I would like to do the same for Date but would like to not input date plus make sure the date set for that record is the SYS Date in the Oracle DB, I have made Date variable default as SYSDate in DB but its still not taking in a null value as its not null. How do I implement this ?
Help is truly appreciated !