Storing Calendar Data in MySQL - mysql

Just a quick architecture question really on storing calendar data.
Basically, I have a database of services for rental. On the front end, there is a calendar to show either "Available" or "Unavailable" for every future date. In the back-end the user can set any date/date range to available or unavailable (1 or 0) on a jQuery calendar.
The question I have is how would you go about storing this data in mysql and retrieving it on the front end?
Possible have all dates available and store the unavailable dates? Then if they are set to available again, remove the record for that date?
Cheers,
RJ

Possible have all dates available and store the unavailable dates? Then if they are set to available again, remove the record for that date?
Yes, I'd go with that, except I would not remove the record when renting expires - you'll easily know a renting expired because it's in the past, so you automatically keep the history of renting as well.
After all, there is infinite number of available dates1, so you'd have to artificially limit the supported range of dates if you went the other way around (and stored free dates).
1 In the future. And, in some sense, in the past as well.
Also, I'm guessing you want some additional information in case a service is rented (e.g. name of the renter) and there would be nowhere to store that if renting were represented by a non-existent row!
Since the granularity of renting is a whole day, I think you are looking at a database structure similar to this:
Note how RENTING_DAY PK naturally prevents overlaps.
Alternatively, you might ditch the RENTING_DAY and have START_DATE and END_DATE directly in RENTING, but this would require explicit range overlap checks, which may not scale ideally.

Decide whether the default is Available or Unavailable.
Possible have all dates available and store the unavailable dates?
So default is Available?
Then you can put unavailable_start and unavailable_end - store it as a date field. For single days, unavailable_start = _end. Then it's easy to query for a month or any date range and return the unavailability periods in that range. Then have jQuery parse it to display the calendar details for those dates.

Related

Which data model to use to maintain every day historical data for a customer

I need to maintain everyday closing balance of a customer and plot a line graph based on that balance for the last 365 days. Which data model is preferred to maintain this data ?
MySQL, Cassandra or any other databases ?
The obvious solution would be to have a table with a key [client id, data] and the value being closing balance.
The question is how do you fill that data in? You could have a running job at the day end. The big question is how to make the system reliable? If job fails and is restarted next day, will that provide data for the previous day?
Typical way of addressing this type of problem is using "event sourcing". This is a fancy way of saying that it needs to be a storage of records for every operation executed on a balance. Every add/deduct should be there, including client id and date. These records also may have "resulting balance" - which implies that last operation for a customer in a day has the closing balance as well.
At the end, you will have list of transactions and you will be able to "replay" previous event to get balances. It's your choice if you want to have actual table for daily balances per client - as it may be cheaper to look up that data instead of recalculating it every time.
In banking industry, every transaction is stored as a separate record for this specific reason - to be able to get different reports; incl. closing balances per day.

Uploaded CSV file shows Date column as strings, how to convert to Dates for AutoAI use?

I was trying out the AutoAI feature on the IBM cloud with a CSV that only has two columns: a date column and a change column (1 or 0). When I upload it, as a data asset, it sees the Date column as a string. Even after going through data refinery and converting it to a date, when AutoAI reads the file, it still sees it as a column of strings.
How do I make AutoAI recognize the date column as a column?
Blockquote
Unfortunately , date recognition doesn't work on AutoAI at this moment. However, next year there is going to be an update of the autoai service where you will be able to have that feature. If you want to test it, there is a demo version of that: https://autoai.mybluemix.net/home
Blockquote
Just to add a comment to Aleksandar's comment regarding the AutoAI Demo: it is a research demo thus it is not exposed to the general public. It needs further authorization (either an IBM w3id or a special login/pwd through an IBM sales representative).
And to answer the question, we are fully aware the user needs for the TS dataset and the Date/Time detection function, and it will be out very soon.
Unfortunately , date recognition doesn't work on AutoAI at this moment. However, next year there is going to be an update of the autoai service where you will be able to have that feature. If you want to test it, there is a demo version of that: https://autoai.mybluemix.net/home
The time series support is not in place yet. There is on-going effort to have first version available this year.
There is workaround option but not perfect (AutoAI will NOT treat data as timeseries):
do the preprocessing / features creation on your own (create features like year, month, day, week etc.)
pass such preprocessed data to AutoAI
Drawbacks: it will not be treated as ts data. Model validation (cross-validation), folds building will not take into account time order what may lead to unreliable metrics values.

Is there a way to stop a conditional split after a certain date?

I have an SSIS package with a conditional split that essentially checks if one number is bigger than another.
The number is actually a financial period but this is stored as an int in a "yyyymm" format e.g. "201911"
The conditional split sends anything less than or equal to the current financial period to be loaded into our reconciliation software and anything greater than to a holding table.
After 3rd December we will no longer need to check this as all data will be sent to the same place.
I am trying to avoid the need to update the package on the day and I would like to build in some sort of date check to change how the conditional split works
I've considered checking if the period number is greater than or equal to "201912" in conjunction with our current condition but, we regularly receive transactions that would fall into future or past periods and would then get split incorrectly.
Our current conditions are;
intPeriodNo <= #[User_var::intBankPeriodNo] This will be loaded into the rec software
intPeriodNo > #[User_var::intBankPeriodNo] This will be loaded into the holding table
intPeriodNo is derived from the source data and intBankPeriodNo comes from our period control table.
I want to be able to stop the conditional split from splitting the data on 3rd December regardless of the period number and force all data to be loaded into the rec software.
I have some experience with SSIS but not a huge amount so any help or ideas are greatly appreciated.
You could edit both of your current conditionals to check today's date as part of the condition.
(GETDATE()>(DT_DBDATE)("2019-12-03")) || (intPeriodNo <= #[User_var::intBankPeriodNo])
Current date greater than 2019-12-03 OR current condition -> Choose this path.
And then,
(GETDATE()<=(DT_DBDATE)("2019-12-03")) && intPeriodNo > #[User_var::intBankPeriodNo]
Current date less than or equal to 2019-12-03 AND current condition -> Choose this path.
After 2019-12-03, all rows will go down path one, and you can go back and edit the package at your leisure whenever you get around to it.
in a sql task save something like this in a variable
SELECT CONVERT(datetime,SUBSTRING('201911',1,4)+'-'+SUBSTRING('201911',5,2)+'-01',121)
in a condition split you could use this to manage your flow
getdate() < #[User_var::variable]

Database structure for storing schedules/cron job?

I am stuck with a problem. In an app's db, I am having a schedule table which will store user provided schedules. E.g
Daily
Every Week
Twice a Week
Every 3rd (or any user chosen) day of week
Every Month
Twice a month
Every x day of month
Every x month of year
And so on. These schedules will then provide reference point to schedule different tasks or identify their repeat-ance.
I am not able to think of a proper database structure for it. The best I can get is to have a table with following columns:
Day
Week
Month
Year
type
Then store the specified schedule in the related column and provide the type.
e.g Every week can go like 1 in week column and 1 (designated value for repeating whole) or something like that.
The problem with this approach is that this table is gonna be used very frequently and the data retrieved will not be straightforward. It will need calculation to know the schedule type and hence will require complex db queries to get each type of schedule.
I am implementing it in Laravel app if that can provide any other methodology. It's a SAAS app with huge amount of data related to the schedule table.
Any help will be very much appreciated. Thanks
I suggest you are approaching the problem backwards.
Devise several rules. Code the rules in your app, not in SQL. When inserting an event, pre-fill a calendar through the next 12 months with all occurrences of the event. Every month, go through all events and extend the "pre-fill" through another month (13 months hence).
Now the SELECTs are simple and fast.
SELECT ... WHERE date = '...'
has all the events for that day (assuming it is within 12 months).
The complexity is on inserting. But presumably you insert less often than you select.
The table with the event definitions would be only as complex as needed for your app to figure out what to do. Perhaps
start_date DATE,
frequency ENUM('day', 'week', 'month', ...)
multiplier TINYINT, -- this lets you say "every second week"
offset TINYINT, -- to get "15th of every month"
Twice a week would be two entries.
Better yet, there are several packages (in Perl, shell, etc) that provide a very rich language for expressing event-date-patterns. Furthermore, you may be able to simply 'call' it to do all the work for you!

MySQL Database Structure For Employee Timeclock

I'm working on an app that is partly an employee time clock. It's not too complex but I want to make sure I head in the right direction the first time. I currently have this table structure:
id - int
employee_id - int (fk)
timestamp - mysql timestamp
event_code - int (1 for clock in, 0 for clock out)
I've got everything working where if their last event was a "clock in" they only see the "clock out" button and visa-versa.
My problem is that we will need to run a report that shows how many hours an employee has worked in a month and also total hours during the current fiscal year (Since June 1 of the current year).
Seems like I could store clock in and outs in the same record and maybe even calculate minutes worked between the two events and store that in a column called "worked". Then I would just need to get the sum of all that column for that employee to know how much time total.
Should I keep the structure I have, move to all on one row per pair of clock in and out events, or is there a better way that I'm totally missing?
I know human error is also a big issue for time clocks since people often forget to clock in or out and I'm not sure which structure can handle that easier.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Thanks for any advise/direction.
Rich
I would go with two tables:
One table should be simple log of what events occurred, like your existing design.
The second table contains the calculated working hours. There are columns for the logged in and logged out times and perhaps also a third column with the time difference between them precalculated.
The point is that the calculation of how many hours an employee has worked is complicated, as you mention. Employees may complain that they worked longer hours than your program reports. In this case you want to have access to the original log of all events with no information loss so that you can see and debug exactly what happened. But this raw format is slow and difficult to work with in SQL so for reporting purposes you also want the second table so that you can quickly generate reports with weekly, monthly or yearly sums.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Timestamp is good because there are lots of MySQL functions that work well with timestamp. You might also want to consider using datetime which is very similar to timestamp.
Related
Should I use field 'datetime' or 'timestamp'?