liquid_feedback_core

annotate lf_update.c @ 142:54ac8c473263

Use an improved definition for "disqualified" initiatives

"initiative"."disqualified" is TRUE, if the initiative may not win, because it either (a) has no better rank than the status quo, or (b) because there exists a better ranked initiative X, which directly beats this initiative, and either more voters prefer X to this initiative than voters preferring X to the status quo or less voters prefer this initiative to X than voters preferring the status quo to X
author jbe
date Wed Jun 01 16:58:00 2011 +0200 (2011-06-01)
parents 0d03c57ebae5
children ed2f94a397cd
rev   line source
jbe@0 1 #include <stdlib.h>
jbe@0 2 #include <stdio.h>
jbe@0 3 #include <string.h>
jbe@0 4 #include <libpq-fe.h>
jbe@0 5
jbe@0 6 int main(int argc, char **argv) {
jbe@1 7
jbe@1 8 // variable declarations:
jbe@65 9 int err = 0;
jbe@0 10 int i, count;
jbe@0 11 char *conninfo;
jbe@0 12 PGconn *db;
jbe@0 13 PGresult *list;
jbe@0 14 PGresult *status;
jbe@1 15
jbe@1 16 // parse command line:
jbe@0 17 if (argc == 0) return 1;
jbe@0 18 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
jbe@0 19 FILE *out;
jbe@0 20 out = argc == 1 ? stderr : stdout;
jbe@0 21 fprintf(stdout, "\n");
jbe@0 22 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
jbe@0 23 fprintf(stdout, "\n");
jbe@0 24 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
jbe@0 25 fprintf(stdout, "see http://www.postgresql.org/docs/8.4/static/libpq-connect.html\n");
jbe@0 26 fprintf(stdout, "\n");
jbe@0 27 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
jbe@0 28 fprintf(stdout, "\n");
jbe@0 29 return argc == 1 ? 1 : 0;
jbe@0 30 }
jbe@0 31 {
jbe@0 32 size_t len = 0;
jbe@0 33 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
jbe@0 34 conninfo = malloc(len * sizeof(char));
jbe@0 35 if (!conninfo) {
jbe@0 36 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
jbe@0 37 return 1;
jbe@0 38 }
jbe@0 39 conninfo[0] = 0;
jbe@0 40 for (i=1; i<argc; i++) {
jbe@0 41 if (i>1) strcat(conninfo, " ");
jbe@0 42 strcat(conninfo, argv[i]);
jbe@0 43 }
jbe@0 44 }
jbe@1 45
jbe@1 46 // connect to database:
jbe@0 47 db = PQconnectdb(conninfo);
jbe@0 48 if (!db) {
jbe@0 49 fprintf(stderr, "Error: Could not create database handle\n");
jbe@0 50 return 1;
jbe@0 51 }
jbe@0 52 if (PQstatus(db) != CONNECTION_OK) {
jbe@0 53 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
jbe@0 54 return 1;
jbe@0 55 }
jbe@1 56
jbe@1 57 // delete expired sessions:
jbe@1 58 status = PQexec(db, "DELETE FROM \"expired_session\"");
jbe@1 59 if (!status) {
jbe@1 60 fprintf(stderr, "Error in pqlib while sending SQL command deleting expired sessions\n");
jbe@65 61 err = 1;
jbe@65 62 } else if (
jbe@1 63 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@1 64 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@1 65 ) {
jbe@1 66 fprintf(stderr, "Error while executing SQL command deleting expired sessions:\n%s", PQresultErrorMessage(status));
jbe@65 67 err = 1;
jbe@65 68 PQclear(status);
jbe@65 69 } else {
jbe@65 70 PQclear(status);
jbe@1 71 }
jbe@1 72
jbe@104 73 // check last login:
jbe@104 74 status = PQexec(db, "SELECT \"check_last_login\"()");
jbe@103 75 if (!status) {
jbe@104 76 fprintf(stderr, "Error in pqlib while sending SQL command checking last logins\n");
jbe@103 77 err = 1;
jbe@103 78 } else if (
jbe@103 79 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@103 80 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@103 81 ) {
jbe@104 82 fprintf(stderr, "Error while executing SQL command checking last logins:\n%s", PQresultErrorMessage(status));
jbe@103 83 err = 1;
jbe@103 84 PQclear(status);
jbe@103 85 } else {
jbe@103 86 PQclear(status);
jbe@103 87 }
jbe@103 88
jbe@4 89 // calculate member counts:
jbe@4 90 status = PQexec(db, "SELECT \"calculate_member_counts\"()");
jbe@4 91 if (!status) {
jbe@4 92 fprintf(stderr, "Error in pqlib while sending SQL command calculating member counts\n");
jbe@65 93 err = 1;
jbe@65 94 } else if (
jbe@4 95 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@4 96 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@4 97 ) {
jbe@4 98 fprintf(stderr, "Error while executing SQL command calculating member counts:\n%s", PQresultErrorMessage(status));
jbe@65 99 err = 1;
jbe@65 100 PQclear(status);
jbe@65 101 } else {
jbe@65 102 PQclear(status);
jbe@4 103 }
jbe@4 104
jbe@1 105 // update open issues:
jbe@0 106 list = PQexec(db, "SELECT \"id\" FROM \"open_issue\"");
jbe@0 107 if (!list) {
jbe@0 108 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
jbe@65 109 err = 1;
jbe@65 110 } else if (PQresultStatus(list) != PGRES_TUPLES_OK) {
jbe@0 111 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(list));
jbe@65 112 err = 1;
jbe@65 113 PQclear(list);
jbe@65 114 } else {
jbe@65 115 count = PQntuples(list);
jbe@65 116 for (i=0; i<count; i++) {
jbe@65 117 const char *params[1];
jbe@65 118 params[0] = PQgetvalue(list, i, 0);
jbe@65 119 status = PQexecParams(
jbe@65 120 db, "SELECT \"check_issue\"($1)", 1, NULL, params, NULL, NULL, 0
jbe@65 121 );
jbe@65 122 if (!status) {
jbe@65 123 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"check_issue\"(...):\n");
jbe@65 124 err = 1;
jbe@65 125 } else if (
jbe@65 126 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@65 127 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@65 128 ) {
jbe@65 129 fprintf(stderr, "Error while calling SQL function \"check_issue\"(...):\n%s", PQresultErrorMessage(status));
jbe@65 130 err = 1;
jbe@65 131 PQclear(status);
jbe@65 132 } else {
jbe@65 133 PQclear(status);
jbe@65 134 }
jbe@65 135 }
jbe@65 136 PQclear(list);
jbe@0 137 }
jbe@1 138
jbe@1 139 // calculate ranks after voting is finished:
jbe@1 140 // (NOTE: This is a seperate process to avoid long transactions with locking)
jbe@0 141 list = PQexec(db, "SELECT \"id\" FROM \"issue_with_ranks_missing\"");
jbe@0 142 if (!list) {
jbe@0 143 fprintf(stderr, "Error in pqlib while sending SQL command selecting issues where ranks are missing\n");
jbe@65 144 err = 1;
jbe@65 145 } else if (PQresultStatus(list) != PGRES_TUPLES_OK) {
jbe@0 146 fprintf(stderr, "Error while executing SQL command selecting issues where ranks are missing:\n%s", PQresultErrorMessage(list));
jbe@65 147 err = 1;
jbe@65 148 PQclear(list);
jbe@65 149 } else {
jbe@65 150 count = PQntuples(list);
jbe@65 151 for (i=0; i<count; i++) {
jbe@65 152 const char *params[1];
jbe@65 153 params[0] = PQgetvalue(list, i, 0);
jbe@65 154 status = PQexecParams(
jbe@65 155 db, "SELECT \"calculate_ranks\"($1)", 1, NULL, params, NULL, NULL, 0
jbe@65 156 );
jbe@65 157 if (!status) {
jbe@65 158 fprintf(stderr, "Error in pqlib while sending SQL command to call function \"calculate_ranks\"(...):\n");
jbe@65 159 err = 1;
jbe@65 160 } else if (
jbe@65 161 PQresultStatus(status) != PGRES_COMMAND_OK &&
jbe@65 162 PQresultStatus(status) != PGRES_TUPLES_OK
jbe@65 163 ) {
jbe@65 164 fprintf(stderr, "Error while calling SQL function \"calculate_ranks\"(...):\n%s", PQresultErrorMessage(status));
jbe@65 165 err = 1;
jbe@65 166 PQclear(status);
jbe@65 167 } else {
jbe@65 168 PQclear(status);
jbe@65 169 }
jbe@65 170 }
jbe@65 171 PQclear(list);
jbe@0 172 }
jbe@1 173
jbe@1 174 // cleanup and exit
jbe@0 175 PQfinish(db);
jbe@65 176 return err;
jbe@1 177
jbe@0 178 }

Impressum / About Us