Redis: find items active on a given date - redis

I have a bunch of objects with start and end dates like so:
{id: "1", start_date: "2020-01-01", end_date: "2020-01-15"}
{id: "2", start_date: "2020-01-15", end_date: "2020-01-30"}
{id: "3", start_date: "2020-01-07", end_date: "2020-02-01"}
{id: "4", start_date: "2020-02-01", end_date: "2020-02-15"}
What I need to do is find the objects that are active on a given date. For example, given the date "2020-01-10", it should return objects 1 and 3 since their start_date is <= to 2020-01-10 and their end_date is >= 2020-01-10.
What is the proper Redis data structure to be able to perform this type of lookup? Sorted sets seem like the right choice, but I can't wrap my head around how to perform this sort of lookup with sorted sets.

Related

react-native-calendars dynamically color dates based on information from API

I'm working on a project, which should include calendar and hourly list of reservations in each day. I'm stuck on a problem, where i need to set data from rest api in the markedDates object.
This is the object i get from api
{
"dateTimeFrom":"2020-10-01 00:00:00",
"dateTimeTo":"2020-10-31 00:00:00",
"dom":"[\"2020-10-13\"]",
"dow":"[1,4]",
"id":"1",
"transport_id":"1",
"typeID":"1"
},
The response means, that the reservation is from 2020-10-01 till 2020-10-31, except all Mondays and Thursdays, and also 2020-10-13
dateTime from and To stands for reservation start date and end date,
dom and dow stands for blocked days in a month, dom : '2020-10-13' means that 13th date in 10th month is blocked, and dow : 1,4 stands for all mondays and thursdays are blocked.
Is it possible to achieve this with react-native-calendars, or should i use something else / create custom component?

How to configure Autosys Extended calendar to run for first 14 days in every quarter

I have tried with Jan MNTHD#1.... |Jan MNTHD#14|Apr MNTHD#1... so on, but autosys has date condition limitation for 255 chars. Can you please suggest me.
Autosys CYCLE and EXTENDED CALENDAR can he helpful here.
Step 1: Create a CYCLE, to define a range.
cycle: YEARLY_QUARTER
start_date: 01/01/1972
end_date: 01/14/1972
start_date: 04/01/1972
end_date: 04/14/1972
start_date: 07/01/1972
end_date: 07/14/1972
start_date: 10/01/1972
end_date: 10/14/1972
Note: To set the cycle to repeat every year, omit the year or select the year "1972"
Step 2: Create an extended calendar based on this cycle.
extended_calendar: FIRST_14_QUARTER
workday:
non_workday:
holiday: S
holcal:
cyccal: YEARLY_QUARTER
adjust:
condition: CYCLE
This will include all the days of the cycle and run each day irrespective of holiday or non working day.
Also, you can increase the range of the cycle and then change the condition of the extended calendar accordingly.
Good Luck.

SQL Server - getting the correct price for the date period of an activity

I have some activity occurrences with the date range they occur across:
ActivityOccurrence:
ID: 1, ActivityID: 1, StartDate: 2018-05-01, EndDate: 2018-06-30
ID: 2, ActivityID: 2, StartDate: 2018-06-01, EndDate: 2018-07-31
ID: 3, ActivityID: 3, StartDate: 2018-07-01, EndDate: 2018-08-31
Each activity has a price which apply within an effective period:
EffectivePeriod
ID: 1, ActivityID: 1, ValidFrom: 2018-01-01, ValidTo: 2018-06-30, Price: 50
ID: 2, ActivityID: 2, ValidFrom: 2018-01-01, ValidTo: 2018-06-30, Price: 100
ID: 3, ActivityID: 3, ValidFrom: 2018-01-01, ValidTo: 2018-06-30, Price: 70
ID: 4, ActivityID: 1, ValidFrom: 2018-07-01, ValidTo: 2018-12-31, Price: 55
ID: 5, ActivityID: 2, ValidFrom: 2018-07-01, ValidTo: 2018-12-31, Price: 120
ID: 6, ActivityID: 3, ValidFrom: 2018-07-01, ValidTo: 2018-12-31, Price: 80
I'd like to link the Activity Occurrences with their correct rates. So:
ActivityOccurrence ID of 1 would link with EffectivePeriod ID of 1, spanning only the first effective period.
ActivityOccurrence ID of 2 would link with both EffectivePeriod ID of 2 and 5 as it spans across 2 effective periods.
ActivityOccurrence ID of 3 would link with EffectivePeriod ID of 6, spanning only the second effective period.
Doing a standard JOIN gets both effective periods for all 3 activity occurrences which I don't want. Using StartDate >= ValidFrom is correct for the first activity occurrence, but not the second and third. Using StartDate <= ValidTo means the first one is wrong, but the second and third are correct. Switching StartDate to EndDate also has some issues.
SQLFiddle: http://sqlfiddle.com/#!18/576c6/6
I feel like I'm missing something and the answer is very simple but I can't figure out what it is.
Are you trying to make sure that each ActivityOccurrence is joined with EACH EffectivePeriod that is temporally included in its date range?
What I use in such cases is make sure either start or end date of one table is between the start-end of the other:
SELECT ao.ActivityID, ao.StartDate, ao.EndDate, ep.Price
FROM ActivityOccurrence ao
JOIN EffectivePeriod ep ON ao.ActivityID = ep.ActivityID
AND
(
(ao.StartDate between ep.ValidFrom and ep.ValidTo)
OR
(ao.EndDate between ep.ValidFrom and ep.ValidTo)
)

Postgres concatenation of a column for a given group

I currently have a Postgres DB with a SHOWS table that looks like this:
id date venue price is_sold ages time multi_day pit
13 2016-01-02 924 Gilman Street, Berkeley $8 FALSE a/a 7:30pm FALSE FALSE
I have a query that orders everything in this table by date (which is of timestamptz type):
SELECT shows.* FROM shows ORDER BY date
Right now, running this query with the pg-promise library produces an array of objects that look like this:
[
{ id: 1,
date: Thu Oct 20 2016 17:00:00 GMT-0700 (PDT),
venue: 'Regency Ballroom, S.F.',
price: null,
is_sold: false,
ages: 'a/a',
time: null,
multi_day: false,
pit: false },
{ id: 2,
date: Thu Oct 20 2016 17:00:00 GMT-0700 (PDT),
venue: 'Great American Music Hall.',
price: null,
is_sold: false,
ages: 'a/a',
time: null,
multi_day: false,
pit: false } ... ]
I want to return an array that groups this data by date column. Example output:
[
{date: Thu Oct 20 2016 17:00:00 GMT-0700 (PDT),
shows: [1, 2]}, // or however this should be structured; I just want a way to get multiple show ids from a single date.
...
]
which I assume I could then INNER JOIN to get the rest of the show details that I need.
How do I adjust my query to return this sort of grouping?
Just to answer the question, use
SELECT date, string_agg(id, ', ') AS show_ids
FROM shows
GROUP BY date

How to consolidate row with gaps using Sql Server?

I have a table with data having a start date (and time), a end date (and time) a value and an identifier.
for instance I could have such rows (I left out the date value for breivety) :
id: 5, start: 00:00, end: 12:00, value: 50
id: 6, start: 00:00, end: 08:00, value: 60
id: 7, start: 08:00, end: 20:00, value: 70
id: 8, start: 22:00, end: 23:00, value: 80
id: 9, start: 00:00, end: 24:00, value: 90
id: 10, start: 05:00, end: 07:00, value: 100
Then I'd like to query the best (smaller id is better) matches which have value between 00:00 and 24:00 and not overlapping each other.
So it would result in:
start: 00:00, end: 12:00, value: 50 (from id 5)
start: 12:00, end: 20:00, value: 70 (from id 7, but cropped not to overlap previous match)
start: 22:00, end: 23:00, value: 80 (from id 8)
start: 20:00, end: 22:00, value: 90 (from id 9, but cropped not to overlap previous matches)
start: 23:00, end: 24:00, value: 90 (from id 9 again, but cropped not to overlap previous matches)
Nothing more, there are already values for the full datetime range.
There is no smallest unit of time (like one hour) so I can't generate a "tally table" with values from 00:00 to 24:00 and retrieves the best match for each unit of time (like 00:00 - 01:00).
There is no guarantee there will be enough values to fill the datetime range.
How could I achieve this?