0002_create_views.py 8.22 KB
Newer Older
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('memopol_scores', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL(
            """
            CREATE OR REPLACE VIEW "memopol_scores_v_vote_score"
            AS SELECT
                "representatives_votes_vote"."id" AS "vote_id",
                decay_score(
                    CAST(CASE
                        WHEN "representatives_votes_vote"."position"::text = "representatives_recommendations_recommendation"."recommendation"::text
                        THEN "representatives_recommendations_recommendation"."weight"
                        ELSE 0 - "representatives_recommendations_recommendation"."weight"
                    END AS NUMERIC),
                    "representatives_votes_proposal"."datetime",
                    "decay_num"."value",
                    "decay_denom"."value",
                    "exponent"."value",
                    "decimals"."value"
                ) AS "score"
            FROM "representatives_votes_vote"
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_NUM') "decay_num" ON 1=1
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_DENOM') "decay_denom" ON 1=1
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_EXPONENT') "exponent" ON 1=1
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS INTEGER) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECIMALS') "decimals" ON 1=1
                JOIN "representatives_votes_proposal" ON "representatives_votes_vote"."proposal_id" = "representatives_votes_proposal"."id"
                LEFT JOIN "representatives_recommendations_recommendation" ON "representatives_votes_proposal"."id" = "representatives_recommendations_recommendation"."proposal_id"
            WHERE "representatives_recommendations_recommendation"."id" IS NOT NULL;
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE VIEW "memopol_scores_v_dossier_score"
            AS SELECT
                "representatives_votes_vote"."representative_id" AS "representative_id",
                "representatives_votes_proposal"."dossier_id" AS "dossier_id",
                SUM("memopol_scores_votescore"."score") AS "score"
            FROM
                "memopol_scores_votescore"
                INNER JOIN "representatives_votes_vote"
                    ON "memopol_scores_votescore"."vote_id" = "representatives_votes_vote"."id"
                INNER JOIN "representatives_votes_proposal"
                    ON "representatives_votes_vote"."proposal_id" = "representatives_votes_proposal"."id"
            GROUP BY
                "representatives_votes_vote"."representative_id",
                "representatives_votes_proposal"."dossier_id"
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE VIEW "memopol_scores_v_position_score"
            AS SELECT
                "representatives_positions_position"."id" AS "position_id",
                decay_score(
                    "representatives_positions_position"."score",
                    "representatives_positions_position"."datetime",
                    "decay_num"."value",
                    "decay_denom"."value",
                    "exponent"."value",
                    "decimals"."value"
                ) AS "score"
            FROM
                "representatives_positions_position"
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_NUM') "decay_num" ON 1=1
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECAY_DENOM') "decay_denom" ON 1=1
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS NUMERIC) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_EXPONENT') "exponent" ON 1=1
                JOIN (SELECT CAST(TO_NUMBER("value", '99999') AS INTEGER) AS "value" FROM "memopol_settings_setting" WHERE "key" = 'SCORE_DECIMALS') "decimals" ON 1=1;
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE VIEW "memopol_scores_v_representative_score"
            AS SELECT
                "source"."representative_id" AS "representative_id" ,
                SUM("source"."score") AS "score"
            FROM
                (
                    SELECT
                        "memopol_scores_dossierscore"."representative_id" AS "representative_id",
                        "memopol_scores_dossierscore"."score" AS "score"
                    FROM "memopol_scores_dossierscore"
                    UNION ALL
                    SELECT
                        "representatives_positions_position"."representative_id" AS "representative_id",
                        "memopol_scores_positionscore"."score" AS "score"
                    FROM
                        "memopol_scores_positionscore"
                        INNER JOIN "representatives_positions_position"
                            ON "memopol_scores_positionscore"."position_id" = "representatives_positions_position"."id"
                ) "source"
            GROUP BY
                "source"."representative_id"
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE FUNCTION refresh_vote_scores()
            RETURNS VOID AS $$
            BEGIN
                TRUNCATE TABLE "memopol_scores_votescore";

                INSERT INTO "memopol_scores_votescore" ("vote_id", "score")
                SELECT "vote_id", "score" FROM "memopol_scores_v_vote_score";
            END;
            $$ LANGUAGE PLPGSQL;
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE FUNCTION refresh_dossier_scores()
            RETURNS VOID AS $$
            BEGIN
                TRUNCATE TABLE "memopol_scores_dossierscore";

                PERFORM refresh_vote_scores();

                INSERT INTO "memopol_scores_dossierscore" ("representative_id", "dossier_id", "score")
                SELECT "representative_id", "dossier_id", "score" FROM "memopol_scores_v_dossier_score";
            END;
            $$ LANGUAGE PLPGSQL;
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE FUNCTION refresh_position_scores()
            RETURNS VOID AS $$
            BEGIN
                TRUNCATE TABLE "memopol_scores_positionscore";

                INSERT INTO "memopol_scores_positionscore" ("position_id", "score")
                SELECT "position_id", "score" FROM "memopol_scores_v_position_score";
            END;
            $$ LANGUAGE PLPGSQL;
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE FUNCTION refresh_representative_scores()
            RETURNS VOID AS $$
            BEGIN
                TRUNCATE TABLE "memopol_scores_representativescore";

                PERFORM refresh_dossier_scores();
                PERFORM refresh_position_scores();

                INSERT INTO "memopol_scores_representativescore" ("representative_id", "score")
                SELECT
                    "representatives_representative"."id",
                    COALESCE("memopol_scores_v_representative_score"."score", 0)
                FROM
                    "representatives_representative"
                    LEFT OUTER JOIN "memopol_scores_v_representative_score"
                        ON "memopol_scores_v_representative_score"."representative_id" = "representatives_representative"."id";
            END;
            $$ LANGUAGE PLPGSQL;
            """
        ),

        migrations.RunSQL(
            """
            CREATE OR REPLACE FUNCTION refresh_scores()
            RETURNS VOID AS $$
            BEGIN
                PERFORM refresh_representative_scores();
            END;
            $$ LANGUAGE PLPGSQL;
            """
        ),
    ]