Question

MODEL

In the current model of my application, events can occur at several non-combinable frequencies, whose values are enumerated as follows: ONCE, DAILY, WEEKLY, MONTHLY, YEARLY, and that (i.e. the event occurrences) are starting at a certain date and optionally ending at a later one (when they're not ending, their value is NULL).

For instance, an event:

  • happening YEARLY and WEEKLY
  • that started exactly n weeks ago in both cases
  • and whose end date is null

has:

  • a weekly occurrence right now
  • a yearly occurrence in (52 - n%52) weeks

USAGE

This data can be updated via a back-office very frequently. It is needed on our public pages in order to display a 3-day timetable of all event occurrences, gathered by 1-hour slots.

The default start of the timetable is the date of today at midnight. The timetable start can go down to 1 month before the current date and 2 months after.

WHAT I NEED

All events data is inserted without any problem via the back-office. The tricky part comes when I need to fetch them, organized in a timetable way. I tried a brute approach (select all event, and duplicating the occurrences myself) but I kinda failed (and this raises the issue of updated the big cache of replicated events whenever the data changes and everyday!).

What I really want from this beautiful model of mine (stupid me: I didn't think of both the write AND read query at the same time) is to have all occurrences of events between beginning_date and end_date, organized by 1-hour slot.

What do you suggest ?

  1. Denormalize my model and find a tool that automagically syncs my "master" model to the re-organized model? Then, how do I first denormalize it? How do I sync it?
  2. Find a JPQL (or HQL, or raw SQL) query that can fetch all the replicated occurrences of events during the provided interval (+ maybe some post-processing to index them by 1-hour slots)? I'd be extremely happy if you could provide it to me :-)
  3. something else?

By the way, the model is open, so it can be changed in order to better address the issue. Everything is open to discussion here.

Best regards,

Rolf

P.S.: the current (interesting part of the) model looks like:

mysql> describe occurrences;
+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| id             | bigint(20)  | NO   | PRI | NULL    | auto_increment |
| start_date     | datetime    | NO   |     | NULL    |                |
| end_date       | datetime    | YES  |     | NULL    |                |
| frequency_type | varchar(50) | NO   | MUL | NULL    |                |
| event_id       | bigint(20)  | NO   | MUL | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+

mysql> describe frequency_types;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| value | varchar(50) | NO   | PRI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
Was it helpful?

Solution

You can query all events occurring whose start is before the requested end and end is null OR after the requested beginning, and then replicate/calculate all events from the result set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top