Is datastore good for storing hr shifts?

五迷三道 提交于 2019-12-11 04:49:44

问题


everyone. I'm trying to figure out if Google Datastore is the best option in my case..

I need to store employees with theirs schedules like:

id
name
schedule 

Schedule looks like:

Mon   10am-10pm                 (simple)
Tue   10am-5pm, 5.30pm-8pm      (multiple, not even hours) 
..
Sun  6pm-4am                  (start/end are in different days)

one of the APIs returns if employee is working NOW or not

I tried to play with datastore but GQL queries appeared to be very limited, for example you cant compare in one query two different properties (like

.. WHERE current_time>start_time AND current_time<close_time

You cannot use inequality operators (less than, more than, etc.) on different property names in the same query.

It means that I need to load lots of entities into my backed and parse them, wasting time and resources.. instead of getting results right from database

  1. Is there any way to use datastore for my task? or its better to go sql?
  2. how to design my database to store schedules in datasore/sql?

thanks in advance!


回答1:


Based on your examples, I'm going to assume work shifts are in 30 min increments. If that is not the case you can adjust the below easily, although you may want to consider some optimizations.

The below solution will give you constant time look-ups for employees, regardless of how many shifts they work. It also seamlessly handles shifts that go from one day to the next.

0. Your entity model

I'm going to use the following straw man entity

Entity Kind: Employee
- id: Auto id
- name: string
- schedule: repeated integer, indexed

2. Break day into 30 minute blocks

24 hours gives you 48 periods of 30 minutes, so let's map integers to time periods:

  • 0 = 12:00 AM to 12:30 AM
  • 30 = 12:30 AM to 1:00 AM
  • 60 = 1:00 AM to 1:30 AM
  • ...
  • 1380 = 11:00 PM to 11:30 PM
  • 1440 = 11:30 PM to 12:00 AM

This integer is easy to derive from your current time. Using java.util.Calendar and java.util.Date:

Date date = new Date(); // Initializes to now.
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
int hour = calendar.get(Calendar.HOUR_OF_DAY); // Hour in 24h format
int minute = calendar.get(Calendar.MINUTE); // Minute of the hour

int period = hour*60 + minute/30*30; // Integer division rounds down to nearest 30

I tested this on compilejava.net, test code here: https://gist.github.com/55a98bbdb9b5eb3eeaee5f8984f11687

3. Account for day of week

Now we have 30 minute period blocks in the day, we should merge in the day of the week (Sunday = 0, Saturday = 6). Since the max 30 min period value is 1440, it is convenient to just multiple the day of the week by 10000 so we can add them together without conflict:

int day = calendar.get(Calendar.DAY_OF_WEEK);
int day_period = day*10000 + period;

Expanded test code here: https://gist.github.com/217221e03b3eb143b4be45bf3f641d25

4. Storing the Schedule

Now, instead of storing your schedule as start and stop times per day, use the same idea above to store each 30 minute period that an Employee is scheduled for. In your example you had:

Mon   10am-10pm                 (simple)
Tue   10am-5pm, 5.30pm-8pm      (multiple, not even hours) 
..
Sun  6pm-4am                  (start/end are in different days)

In the schedule field (repeated integer), this would look like:

10600,10630,10660,10690,10720,10750,10780,10810,10840,10870,10900,10930,10960,10990,11020,
11050,11080,11110,11140,11170,11200,11230,11260,11290,20600,20630,20660,20690,20720,20750,
20780,20810,20840,20870,20900,20930,20960,20990,21050,21080,21110,21140,21170,1080,1110,
1140,1170,1200,1230,1260,1290,1320,1350,1380,1410,0,30,60,90,120,150,180,210

5. Query time!

Querying is extremely easy now and merely an equality. This will give you O(1) performance per Employee entity working right now, or more generally O(n) where n is the number of employees that are working now (as opposed to total employees).

... WHERE schedule=day_period



回答2:


You can store long representation of the start time and end time. 1 hour = 3600000 milliseconds. So if 00-00 is the reference point 02-00 will be represented by 2 x 3600000 .
Another option is to store java.util.Date (one for start time and one for end time)



来源:https://stackoverflow.com/questions/39695582/is-datastore-good-for-storing-hr-shifts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!