File size: 5,338 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

-- +migrate Up

-- need to coerce to bigint (instead of default of double precision) to get the correct shift_number.
-- need to compare time intervals in the correct time zone

CREATE OR REPLACE VIEW on_call AS
    WITH rotation_details AS (
        SELECT
            rotations.id,
            rotations.schedule_id,
            rotations.start_time,
            (((rotations.shift_length)::text ||
            CASE
                WHEN (rotations.type = 'hourly') THEN ' hours'
                WHEN (rotations.type = 'daily') THEN ' days'
                ELSE ' weeks'
            END))::interval AS shift,
            ((
            CASE
                WHEN (rotations.type = 'hourly') THEN (date_part('epoch', ((now() at time zone s.time_zone) - (rotations.start_time at time zone s.time_zone)))::bigint / 3600) -- number of hours
                WHEN (rotations.type = 'daily') THEN date_part('days', ((now() at time zone s.time_zone) - (rotations.start_time at time zone s.time_zone)))::bigint -- number of days
                ELSE (date_part('days'::text, ((now() at time zone s.time_zone) - (rotations.start_time at time zone s.time_zone)))::bigint / 7) -- number of weeks
            END / rotations.shift_length)) AS shift_number
        FROM
            rotations,
            schedules s
        WHERE s.id = rotations.schedule_id
    ),
    p_count AS (
        SELECT
            rp.rotation_id,
            count(rp.id) AS count
        FROM
            rotation_participants rp,
            rotation_details d_1
        WHERE (rp.rotation_id = d_1.id)
        GROUP BY rp.rotation_id
    ),
    current_participant AS (
        SELECT
            rp.user_id,
            p.rotation_id
        FROM
            rotation_participants rp,
            rotation_details d_1,
            p_count p
        WHERE ((rp.rotation_id = d_1.id)
            AND (p.rotation_id = rp.rotation_id)
            AND (rp."position" = (d_1.shift_number % p.count)))
    ),
    next_participant AS (
        SELECT
            rp.user_id,
            p.rotation_id
        FROM
            rotation_participants rp,
            rotation_details d_1,
            p_count p
        WHERE ((rp.rotation_id = d_1.id)
            AND (p.rotation_id = rp.rotation_id)
            AND (rp."position" = ((d_1.shift_number + 1) % p.count)))
    )
    SELECT
        d.schedule_id,
        d.id AS rotation_id,
        c.user_id,
        n.user_id AS next_user_id,
        ((d.shift * (d.shift_number)::bigint) + d.start_time) AS start_time,
        ((d.shift * ((d.shift_number + 1))::bigint) + d.start_time) AS end_time,
        d.shift_number
    FROM
        rotation_details d,
        current_participant c,
        next_participant n
    WHERE ((d.id = c.rotation_id)
        AND (c.rotation_id = n.rotation_id));


-- +migrate Down


CREATE OR REPLACE VIEW on_call AS
    WITH rotation_details AS (
        SELECT
            rotations.id,
            rotations.schedule_id,
            rotations.start_time,
            (((rotations.shift_length)::text ||
            CASE
                WHEN (rotations.type = 'hourly'::enum_rotation_type) THEN ' hours'::text
                WHEN (rotations.type = 'daily'::enum_rotation_type) THEN ' days'::text
                ELSE ' weeks'::text
            END))::interval AS shift,
            ((
            CASE
                WHEN (rotations.type = 'hourly'::enum_rotation_type) THEN (date_part('epoch'::text, (now() - rotations.start_time)) / (3600)::double precision)
                WHEN (rotations.type = 'daily'::enum_rotation_type) THEN date_part('days'::text, (now() - rotations.start_time))
                ELSE (date_part('days'::text, (now() - rotations.start_time)) / (7)::double precision)
            END / (rotations.shift_length)::double precision))::bigint AS shift_number
        FROM rotations
    ),
    p_count AS (
        SELECT
            rp.rotation_id,
            count(rp.id) AS count
        FROM
            rotation_participants rp,
            rotation_details d_1
        WHERE (rp.rotation_id = d_1.id)
        GROUP BY rp.rotation_id
    ),
    current_participant AS (
        SELECT
            rp.user_id,
            p.rotation_id
        FROM
            rotation_participants rp,
            rotation_details d_1,
            p_count p
        WHERE ((rp.rotation_id = d_1.id)
            AND (p.rotation_id = rp.rotation_id)
            AND (rp."position" = (d_1.shift_number % p.count)))
    ),
    next_participant AS (
        SELECT
            rp.user_id,
            p.rotation_id
        FROM
            rotation_participants rp,
            rotation_details d_1,
            p_count p
        WHERE ((rp.rotation_id = d_1.id)
            AND (p.rotation_id = rp.rotation_id)
            AND (rp."position" = ((d_1.shift_number + 1) % p.count)))
    )
    SELECT
        d.schedule_id,
        d.id AS rotation_id,
        c.user_id,
        n.user_id AS next_user_id,
        ((d.shift * (d.shift_number)::double precision) + d.start_time) AS start_time,
        ((d.shift * ((d.shift_number + 1))::double precision) + d.start_time) AS end_time,
        d.shift_number
    FROM
        rotation_details d,
        current_participant c,
        next_participant n
    WHERE ((d.id = c.rotation_id)
        AND (c.rotation_id = n.rotation_id));