Created
March 22, 2018 12:43
-
-
Save maxdemarzi/1dd106c823f96f08606a6781fc911a13 to your computer and use it in GitHub Desktop.
Find meeting times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE (person1:Person {name: "Max"}) | |
CREATE (person2:Person {name: "Alex"}) | |
CREATE (person3:Person {name: "Andrew"}) | |
CREATE (floor1:Floor {name: "Floor 1"}) | |
CREATE (floor2:Floor {name: "Floor 2"}) | |
CREATE (person1)-[:SITS_IN]->(floor1) | |
CREATE (person2)-[:SITS_IN]->(floor1) | |
CREATE (person3)-[:SITS_IN]->(floor2) | |
CREATE (room1:Room {name:"Room 1"}) | |
CREATE (room2:Room {name:"Room 2"}) | |
CREATE (room3:Room {name:"Room 3"}) | |
CREATE (room4:Room {name:"Room 4"}) | |
CREATE (room1)-[:LOCATED_IN]->(floor1) | |
CREATE (room2)-[:LOCATED_IN]->(floor1) | |
CREATE (room3)-[:LOCATED_IN]->(floor2) | |
CREATE (room4)-[:LOCATED_IN]->(floor2) | |
CREATE (m1:Meeting {start_time: 1521534600000, end_time:1521538200000}) // 8:30-9:30am | |
CREATE (m2:Meeting {start_time: 1521543600000, end_time:1521550800000}) // 11-1pm | |
CREATE (m3:Meeting {start_time: 1521550800000, end_time:1521558000000}) // 1-3pm | |
CREATE (m4:Meeting {start_time: 1521534600000, end_time:1521543600000}) // 8:30-11am | |
CREATE (m5:Meeting {start_time: 1521550800000, end_time:1521554400000}) // 1-2pm | |
CREATE (m6:Meeting {start_time: 1521561600000, end_time:1521565200000}) // 4-5pm | |
CREATE (m7:Meeting {start_time: 1521558000000, end_time:1521561600000}) // 3-4pm | |
CREATE (room1)-[:IS_BOOKED_ON_2018_03_20]->(m1) | |
CREATE (room1)-[:IS_BOOKED_ON_2018_03_20]->(m2) | |
CREATE (room1)-[:IS_BOOKED_ON_2018_03_20]->(m3) | |
CREATE (room2)-[:IS_BOOKED_ON_2018_03_20]->(m4) | |
CREATE (room2)-[:IS_BOOKED_ON_2018_03_20]->(m5) | |
CREATE (room2)-[:IS_BOOKED_ON_2018_03_20]->(m6) | |
CREATE (room4)-[:IS_BOOKED_ON_2018_03_20]->(m7) | |
CREATE (person2)-[:HAS_MEETING_ON_2018_03_20]->(m7) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MATCH (p:Person) | |
WHERE p.name IN ["Max", "Alex", "Andrew"] | |
OPTIONAL MATCH (p)-[:HAS_MEETING_ON_2018_03_20]->(m:Meeting) | |
WITH COLLECT(m) AS occupied | |
MATCH (p:Person)-[:SITS_IN]->(f:Floor)<-[:LOCATED_IN]-(r:Room) | |
WHERE p.name IN ["Max", "Alex", "Andrew"] | |
WITH r, occupied | |
OPTIONAL MATCH (r)-[:IS_BOOKED_ON_2018_03_20]->(m:Meeting) | |
WITH r, [{start_time:1521565200000, end_time:1521534600000}] + COLLECT(m) + occupied AS meetings | |
UNWIND meetings AS m | |
WITH r, [min(m.start_time), max(m.end_time)] AS rslot, COLLECT(m) AS mm | |
WITH r, rslot, mm | |
UNWIND mm AS m1 | |
UNWIND mm AS m2 | |
WITH r, rslot, m1, m2 WHERE (m2.start_time >= m1.end_time) | |
WITH r, rslot, [m1.end_time, min(m2.start_time)] AS slot | |
WITH r, [[1521534600000, rslot[0]]] + collect(slot) + [[rslot[1], 1521565200000]] AS open | |
WITH r, filter(x IN open WHERE x[0]<>x[1]) AS available | |
UNWIND available AS dups | |
WITH r, COLLECT(DISTINCT dups) AS tslots | |
RETURN r.name, EXTRACT (x IN tslots | apoc.date.format(x[0]) + ' to ' + apoc.date.format(x[1])) | |
ORDER BY r.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MATCH (p:Person) | |
WHERE p.name IN ["Max", "Alex", "Andrew"] | |
OPTIONAL MATCH (p)-[:HAS_MEETING_ON_2018_03_20]->(m:Meeting) | |
WITH COLLECT(m) AS occupied | |
MATCH (p:Person)-[:SITS_IN]->(f:Floor)<-[:LOCATED_IN]-(r:Room) | |
WHERE p.name IN ["Max", "Alex", "Andrew"] | |
WITH r, occupied | |
OPTIONAL MATCH (r)-[:IS_BOOKED_ON_2018_03_20]->(m:Meeting) | |
WITH r, [{start_time:1521565200000, end_time:1521534600000}] + COLLECT(m) + occupied AS meetings | |
UNWIND meetings AS m | |
WITH r, [min(m.start_time), max(m.end_time)] AS rslot, COLLECT(m) AS mm | |
WITH r, rslot, mm | |
UNWIND mm AS m1 | |
UNWIND mm AS m2 | |
WITH r, rslot, m1, m2 WHERE (m2.start_time >= m1.end_time) | |
WITH r, rslot, [m1.end_time, min(m2.start_time)] AS slot | |
WITH r, [[1521534600000, rslot[0]]] + collect(slot) + [[rslot[1], 1521565200000]] AS open | |
WITH r, filter(x IN open WHERE x[0]<>x[1]) AS available | |
UNWIND available AS dups | |
WITH r, COLLECT(DISTINCT dups) AS tslots | |
RETURN r.name, EXTRACT (x IN tslots | toString(datetime.fromepochmillis(x[0])) + ' to ' + toString(datetime.fromepochmillis(x[1]))) | |
ORDER BY r.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer:
"Room 1" | ["2018-03-20 09:30:00 to 2018-03-20 11:00:00", "2018-03-20 16:00:00 to 2018-03-20 17:00:00"]
"Room 2" | ["2018-03-20 11:00:00 to 2018-03-20 13:00:00", "2018-03-20 14:00:00 to 2018-03-20 15:00:00"]
"Room 3" | ["2018-03-20 08:30:00 to 2018-03-20 15:00:00", "2018-03-20 16:00:00 to 2018-03-20 17:00:00"]
"Room 4" | ["2018-03-20 08:30:00 to 2018-03-20 15:00:00", "2018-03-20 16:00:00 to 2018-03-20 17:00:00"]