liquid_feedback_core

annotate lf_update_suggestion_order.c @ 376:4a18576a359f

More verbose logging in lf_update_suggestion_order.c
author jbe
date Mon Mar 18 09:09:29 2013 +0100 (2013-03-18)
parents 3f7a89ad996d
children f65814f4d3fc
rev   line source
jbe@352 1 #include <stdlib.h>
jbe@352 2 #include <stdio.h>
jbe@352 3 #include <string.h>
jbe@352 4 #include <libpq-fe.h>
jbe@352 5 #include <search.h>
jbe@352 6
jbe@374 7 static int logging = 0;
jbe@374 8
jbe@352 9 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) {
jbe@352 10 // provides compatibility for PostgreSQL versions prior 9.0
jbe@352 11 // in future: return PQescapeLiteral(conn, str, len);
jbe@352 12 char *res;
jbe@352 13 size_t res_len;
jbe@352 14 res = malloc(2*len+3);
jbe@358 15 if (!res) return NULL;
jbe@352 16 res[0] = '\'';
jbe@352 17 res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
jbe@352 18 res[res_len+1] = '\'';
jbe@352 19 res[res_len+2] = 0;
jbe@352 20 return res;
jbe@352 21 }
jbe@352 22
jbe@352 23 static void freemem(void *ptr) {
jbe@352 24 // to be used for "escapeLiteral" function
jbe@352 25 // provides compatibility for PostgreSQL versions prior 9.0
jbe@352 26 // in future: PQfreemem(ptr);
jbe@352 27 free(ptr);
jbe@352 28 }
jbe@352 29
jbe@372 30 // column numbers when querying "individual_suggestion_ranking" view in function main():
jbe@352 31 #define COL_MEMBER_ID 0
jbe@352 32 #define COL_WEIGHT 1
jbe@352 33 #define COL_PREFERENCE 2
jbe@352 34 #define COL_SUGGESTION_ID 3
jbe@352 35
jbe@372 36 // data structure for a candidate (in this case a suggestion) to the proportional runoff system:
jbe@353 37 struct candidate {
jbe@372 38 char *key; // identifier of the candidate, which is the "suggestion_id" string
jbe@372 39 double score_per_step; // added score per step
jbe@372 40 double score; // current score of candidate; a score of 1.0 is needed to survive a round
jbe@372 41 int seat; // equals 0 for unseated candidates, or contains rank number
jbe@353 42 };
jbe@353 43
jbe@372 44 // compare two integers stored as strings (invocation like strcmp):
jbe@369 45 static int compare_id(char *id1, char *id2) {
jbe@369 46 int ldiff;
jbe@369 47 ldiff = strlen(id1) - strlen(id2);
jbe@369 48 if (ldiff) return ldiff;
jbe@369 49 else return strcmp(id1, id2);
jbe@369 50 }
jbe@369 51
jbe@372 52 // compare two candidates by their key (invocation like strcmp):
jbe@353 53 static int compare_candidate(struct candidate *c1, struct candidate *c2) {
jbe@369 54 return compare_id(c1->key, c2->key);
jbe@353 55 }
jbe@352 56
jbe@372 57 // candidates are stored as global variables due to the constrained twalk() interface:
jbe@353 58 static int candidate_count;
jbe@353 59 static struct candidate *candidates;
jbe@353 60
jbe@372 61 // function to be passed to twalk() to store candidates ordered in candidates[] array:
jbe@353 62 static void register_candidate(char **candidate_key, VISIT visit, int level) {
jbe@352 63 if (visit == postorder || visit == leaf) {
jbe@353 64 struct candidate *candidate;
jbe@353 65 candidate = candidates + (candidate_count++);
jbe@353 66 candidate->key = *candidate_key;
jbe@353 67 candidate->seat = 0;
jbe@376 68 if (logging) printf("Candidate #%i is suggestion #%s.\n", candidate_count, candidate->key);
jbe@352 69 }
jbe@352 70 }
jbe@352 71
jbe@372 72 // performs a binary search in candidates[] array to lookup a candidate by its key (which is the suggestion_id):
jbe@353 73 static struct candidate *candidate_by_key(char *candidate_key) {
jbe@353 74 struct candidate *candidate;
jbe@353 75 struct candidate compare;
jbe@353 76 compare.key = candidate_key;
jbe@353 77 candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate);
jbe@353 78 if (!candidate) {
jbe@356 79 fprintf(stderr, "Candidate not found (should not happen).\n");
jbe@352 80 abort();
jbe@352 81 }
jbe@353 82 return candidate;
jbe@352 83 }
jbe@352 84
jbe@372 85 // section of a ballot with equally ranked candidates:
jbe@352 86 struct ballot_section {
jbe@352 87 int count;
jbe@353 88 struct candidate **candidates;
jbe@352 89 };
jbe@352 90
jbe@372 91 // ballot of the proportional runoff system:
jbe@352 92 struct ballot {
jbe@372 93 int weight; // if weight is greater than 1, then the ballot is counted multiple times
jbe@372 94 struct ballot_section sections[4]; // 4 sections, most preferred candidates first
jbe@352 95 };
jbe@352 96
jbe@372 97 // determine candidate, which is assigned the next seat (starting with the worst rank):
jbe@356 98 static struct candidate *loser(int round_number, struct ballot *ballots, int ballot_count) {
jbe@372 99 int i, j, k; // index variables for loops
jbe@372 100 int remaining; // remaining candidates to be seated
jbe@372 101 // reset scores of all candidates:
jbe@356 102 for (i=0; i<candidate_count; i++) {
jbe@356 103 candidates[i].score = 0.0;
jbe@356 104 }
jbe@372 105 // calculate remaining candidates to be seated:
jbe@356 106 remaining = candidate_count - round_number;
jbe@373 107 // repeat following loop, as long as there is more than one remaining candidate:
jbe@373 108 while (remaining > 1) {
jbe@376 109 if (logging) printf("There are %i remaining candidates.\n", remaining);
jbe@372 110 double scale; // factor to be later multiplied with score_per_step:
jbe@372 111 // reset score_per_step for all candidates:
jbe@356 112 for (i=0; i<candidate_count; i++) {
jbe@356 113 candidates[i].score_per_step = 0.0;
jbe@356 114 }
jbe@372 115 // calculate score_per_step for all candidates:
jbe@356 116 for (i=0; i<ballot_count; i++) {
jbe@356 117 for (j=0; j<4; j++) {
jbe@356 118 int matches = 0;
jbe@356 119 for (k=0; k<ballots[i].sections[j].count; k++) {
jbe@356 120 struct candidate *candidate;
jbe@356 121 candidate = ballots[i].sections[j].candidates[k];
jbe@356 122 if (candidate->score < 1.0 && !candidate->seat) matches++;
jbe@356 123 }
jbe@356 124 if (matches) {
jbe@356 125 double score_inc;
jbe@368 126 score_inc = (double)ballots[i].weight / (double)matches;
jbe@356 127 for (k=0; k<ballots[i].sections[j].count; k++) {
jbe@356 128 struct candidate *candidate;
jbe@356 129 candidate = ballots[i].sections[j].candidates[k];
jbe@356 130 if (candidate->score < 1.0 && !candidate->seat) {
jbe@356 131 candidate->score_per_step += score_inc;
jbe@356 132 }
jbe@356 133 }
jbe@356 134 break;
jbe@356 135 }
jbe@356 136 }
jbe@356 137 }
jbe@372 138 // calculate scale factor:
jbe@372 139 scale = (double)0.0; // 0.0 is used to indicate that there is no value yet
jbe@356 140 for (i=0; i<candidate_count; i++) {
jbe@356 141 double max_scale;
jbe@356 142 if (candidates[i].score_per_step > 0.0) {
jbe@356 143 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
jbe@368 144 if (scale == 0.0 || max_scale <= scale) {
jbe@356 145 scale = max_scale;
jbe@356 146 }
jbe@356 147 }
jbe@356 148 }
jbe@372 149 // add scale*score_per_step to each candidates score:
jbe@356 150 for (i=0; i<candidate_count; i++) {
jbe@376 151 int log_candidate = 0;
jbe@376 152 if (logging && candidates[i].score < 1.0 && !candidates[i].seat) log_candidate = 1;
jbe@376 153 if (log_candidate) printf("Score for suggestion #%s = %.4f+%.4f*%.4f=", candidates[i].key, candidates[i].score, scale, candidates[i].score_per_step);
jbe@356 154 if (candidates[i].score_per_step > 0.0) {
jbe@370 155 double max_scale;
jbe@370 156 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
jbe@370 157 if (max_scale == scale) {
jbe@372 158 // score of 1.0 should be reached, so we set score directly to avoid floating point errors:
jbe@356 159 candidates[i].score = 1.0;
jbe@356 160 remaining--;
jbe@356 161 } else {
jbe@356 162 candidates[i].score += scale * candidates[i].score_per_step;
jbe@356 163 if (candidates[i].score >= 1.0) remaining--;
jbe@356 164 }
jbe@376 165 }
jbe@376 166 if (log_candidate) printf("%.4f.\n", candidates[i].score);
jbe@376 167 // when there is only one candidate remaining, then break inner (and thus outer) loop:
jbe@376 168 if (remaining <= 1) {
jbe@376 169 break;
jbe@356 170 }
jbe@356 171 }
jbe@356 172 }
jbe@372 173 // return remaining candidate:
jbe@372 174 for (i=0; i<candidate_count; i++) {
jbe@356 175 if (candidates[i].score < 1.0 && !candidates[i].seat) return candidates+i;
jbe@356 176 }
jbe@372 177 // if there is no remaining candidate, then something went wrong:
jbe@356 178 fprintf(stderr, "No remaining candidate (should not happen).");
jbe@356 179 abort();
jbe@356 180 }
jbe@356 181
jbe@372 182 // write results to database:
jbe@359 183 static int write_ranks(PGconn *db, char *escaped_initiative_id, int final) {
jbe@358 184 PGresult *res;
jbe@358 185 char *cmd;
jbe@358 186 int i;
jbe@359 187 if (final) {
jbe@371 188 if (asprintf(&cmd, "BEGIN; UPDATE \"initiative\" SET \"final_suggestion_order_calculated\" = TRUE WHERE \"id\" = %s; UPDATE \"suggestion\" SET \"proportional_order\" = 0 WHERE \"initiative_id\" = %s", escaped_initiative_id, escaped_initiative_id) < 0) {
jbe@359 189 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@359 190 abort();
jbe@359 191 }
jbe@359 192 } else {
jbe@371 193 if (asprintf(&cmd, "BEGIN; UPDATE \"suggestion\" SET \"proportional_order\" = 0 WHERE \"initiative_id\" = %s", escaped_initiative_id) < 0) {
jbe@359 194 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@359 195 abort();
jbe@359 196 }
jbe@358 197 }
jbe@358 198 res = PQexec(db, cmd);
jbe@358 199 free(cmd);
jbe@358 200 if (!res) {
jbe@358 201 fprintf(stderr, "Error in pqlib while sending SQL command to initiate suggestion update.\n");
jbe@358 202 return 1;
jbe@358 203 } else if (
jbe@358 204 PQresultStatus(res) != PGRES_COMMAND_OK &&
jbe@358 205 PQresultStatus(res) != PGRES_TUPLES_OK
jbe@358 206 ) {
jbe@358 207 fprintf(stderr, "Error while executing SQL command to initiate suggestion update:\n%s", PQresultErrorMessage(res));
jbe@358 208 PQclear(res);
jbe@358 209 return 1;
jbe@358 210 } else {
jbe@358 211 PQclear(res);
jbe@358 212 }
jbe@358 213 for (i=0; i<candidate_count; i++) {
jbe@358 214 char *escaped_suggestion_id;
jbe@358 215 escaped_suggestion_id = escapeLiteral(db, candidates[i].key, strlen(candidates[i].key));
jbe@358 216 if (!escaped_suggestion_id) {
jbe@358 217 fprintf(stderr, "Could not escape literal in memory.\n");
jbe@358 218 abort();
jbe@358 219 }
jbe@358 220 if (asprintf(&cmd, "UPDATE \"suggestion\" SET \"proportional_order\" = %i WHERE \"id\" = %s", candidates[i].seat, escaped_suggestion_id) < 0) {
jbe@358 221 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@358 222 abort();
jbe@358 223 }
jbe@358 224 freemem(escaped_suggestion_id);
jbe@358 225 res = PQexec(db, cmd);
jbe@358 226 free(cmd);
jbe@358 227 if (!res) {
jbe@358 228 fprintf(stderr, "Error in pqlib while sending SQL command to update suggestion order.\n");
jbe@358 229 } else if (
jbe@358 230 PQresultStatus(res) != PGRES_COMMAND_OK &&
jbe@358 231 PQresultStatus(res) != PGRES_TUPLES_OK
jbe@358 232 ) {
jbe@358 233 fprintf(stderr, "Error while executing SQL command to update suggestion order:\n%s", PQresultErrorMessage(res));
jbe@358 234 PQclear(res);
jbe@358 235 } else {
jbe@358 236 PQclear(res);
jbe@358 237 continue;
jbe@358 238 }
jbe@358 239 res = PQexec(db, "ROLLBACK");
jbe@358 240 if (res) PQclear(res);
jbe@358 241 return 1;
jbe@358 242 }
jbe@358 243 res = PQexec(db, "COMMIT");
jbe@358 244 if (!res) {
jbe@358 245 fprintf(stderr, "Error in pqlib while sending SQL command to commit transaction.\n");
jbe@358 246 return 1;
jbe@358 247 } else if (
jbe@358 248 PQresultStatus(res) != PGRES_COMMAND_OK &&
jbe@358 249 PQresultStatus(res) != PGRES_TUPLES_OK
jbe@358 250 ) {
jbe@358 251 fprintf(stderr, "Error while executing SQL command to commit transaction:\n%s", PQresultErrorMessage(res));
jbe@358 252 PQclear(res);
jbe@358 253 return 1;
jbe@358 254 } else {
jbe@358 255 PQclear(res);
jbe@358 256 return 0;
jbe@358 257 }
jbe@358 258 }
jbe@358 259
jbe@372 260 // calculate ordering of suggestions for an initiative and call write_ranks() to write it to database:
jbe@359 261 static int process_initiative(PGconn *db, PGresult *res, char *escaped_initiative_id, int final) {
jbe@372 262 int err; // variable to store an error condition (0 = success)
jbe@372 263 int ballot_count = 1; // number of ballots, must be initiatized to 1, due to loop below
jbe@372 264 struct ballot *ballots; // data structure containing the ballots
jbe@372 265 int i; // index variable for loops
jbe@372 266 // create candidates[] and ballots[] arrays:
jbe@355 267 {
jbe@372 268 void *candidate_tree = NULL; // temporary structure to create a sorted unique list of all candidate keys
jbe@372 269 int tuple_count; // number of tuples returned from the database
jbe@372 270 char *old_member_id = NULL; // old member_id to be able to detect a new ballot in loops
jbe@372 271 struct ballot *ballot; // pointer to current ballot
jbe@372 272 int candidates_in_sections[4] = {0, }; // number of candidates that have been placed in each section
jbe@372 273 // reset candidate count:
jbe@367 274 candidate_count = 0;
jbe@372 275 // determine number of tuples:
jbe@359 276 tuple_count = PQntuples(res);
jbe@372 277 // trivial case, when there are no tuples:
jbe@359 278 if (!tuple_count) {
jbe@359 279 if (final) {
jbe@374 280 if (logging) printf("No suggestions found, but marking initiative as finally calculated.\n");
jbe@359 281 err = write_ranks(db, escaped_initiative_id, final);
jbe@374 282 if (logging) printf("Done.\n");
jbe@359 283 return err;
jbe@359 284 } else {
jbe@374 285 if (logging) printf("Nothing to do.\n");
jbe@359 286 return 0;
jbe@359 287 }
jbe@359 288 }
jbe@372 289 // calculate ballot_count and generate set of candidate keys (suggestion_id is used as key):
jbe@366 290 for (i=0; i<tuple_count; i++) {
jbe@355 291 char *member_id, *suggestion_id;
jbe@366 292 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@366 293 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@369 294 if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)compare_id)) {
jbe@366 295 candidate_count++;
jbe@369 296 if (!tsearch(suggestion_id, &candidate_tree, (void *)compare_id)) {
jbe@366 297 fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n");
jbe@366 298 abort();
jbe@352 299 }
jbe@352 300 }
jbe@366 301 if (old_member_id && strcmp(old_member_id, member_id)) ballot_count++;
jbe@355 302 old_member_id = member_id;
jbe@352 303 }
jbe@372 304 // allocate memory for candidates[] array:
jbe@355 305 candidates = malloc(candidate_count * sizeof(struct candidate));
jbe@355 306 if (!candidates) {
jbe@356 307 fprintf(stderr, "Insufficient memory while creating candidate list.\n");
jbe@353 308 abort();
jbe@352 309 }
jbe@372 310 // transform tree of candidate keys into sorted array:
jbe@372 311 candidate_count = 0; // needed by register_candidate()
jbe@355 312 twalk(candidate_tree, (void *)register_candidate);
jbe@372 313 // free memory of tree structure (tdestroy() is not available on all platforms):
jbe@369 314 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)compare_id);
jbe@372 315 // allocate memory for ballots[] array:
jbe@355 316 ballots = calloc(ballot_count, sizeof(struct ballot));
jbe@355 317 if (!ballots) {
jbe@356 318 fprintf(stderr, "Insufficient memory while creating ballot list.\n");
jbe@353 319 abort();
jbe@352 320 }
jbe@372 321 // set ballot weights, determine ballot section sizes, and verify preference values:
jbe@355 322 ballot = ballots;
jbe@365 323 old_member_id = NULL;
jbe@355 324 for (i=0; i<tuple_count; i++) {
jbe@363 325 char *member_id;
jbe@355 326 int weight, preference;
jbe@352 327 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@355 328 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
jbe@355 329 if (weight <= 0) {
jbe@356 330 fprintf(stderr, "Unexpected weight value.\n");
jbe@358 331 free(ballots);
jbe@358 332 free(candidates);
jbe@358 333 return 1;
jbe@355 334 }
jbe@352 335 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
jbe@352 336 if (preference < 1 || preference > 4) {
jbe@356 337 fprintf(stderr, "Unexpected preference value.\n");
jbe@358 338 free(ballots);
jbe@358 339 free(candidates);
jbe@358 340 return 1;
jbe@352 341 }
jbe@352 342 preference--;
jbe@366 343 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
jbe@355 344 ballot->weight = weight;
jbe@355 345 ballot->sections[preference].count++;
jbe@355 346 old_member_id = member_id;
jbe@355 347 }
jbe@372 348 // allocate memory for ballot sections:
jbe@355 349 for (i=0; i<ballot_count; i++) {
jbe@355 350 int j;
jbe@355 351 for (j=0; j<4; j++) {
jbe@355 352 if (ballots[i].sections[j].count) {
jbe@355 353 ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
jbe@355 354 if (!ballots[i].sections[j].candidates) {
jbe@356 355 fprintf(stderr, "Insufficient memory while creating ballot section.\n");
jbe@355 356 abort();
jbe@355 357 }
jbe@355 358 }
jbe@355 359 }
jbe@352 360 }
jbe@372 361 // fill ballot sections with candidate references:
jbe@365 362 old_member_id = NULL;
jbe@355 363 ballot = ballots;
jbe@365 364 for (i=0; i<tuple_count; i++) {
jbe@355 365 char *member_id, *suggestion_id;
jbe@355 366 int preference;
jbe@366 367 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@366 368 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@366 369 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
jbe@366 370 preference--;
jbe@366 371 if (old_member_id && strcmp(old_member_id, member_id)) {
jbe@366 372 ballot++;
jbe@366 373 candidates_in_sections[0] = 0;
jbe@366 374 candidates_in_sections[1] = 0;
jbe@366 375 candidates_in_sections[2] = 0;
jbe@366 376 candidates_in_sections[3] = 0;
jbe@355 377 }
jbe@366 378 ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
jbe@355 379 old_member_id = member_id;
jbe@352 380 }
jbe@376 381 // print ballots, if logging is enabled:
jbe@376 382 if (logging) {
jbe@376 383 for (i=0; i<ballot_count; i++) {
jbe@376 384 int j;
jbe@376 385 printf("Ballot #%i: ", i+1);
jbe@376 386 for (j=0; j<4; j++) {
jbe@376 387 int k;
jbe@376 388 if (j) printf(", ");
jbe@376 389 printf("preference %i (", j+1);
jbe@376 390 for (k=0; k<ballots[i].sections[j].count; k++) {
jbe@376 391 if (k) printf(",");
jbe@376 392 printf("s#%s", ballots[i].sections[j].candidates[k]->key);
jbe@376 393 }
jbe@376 394 printf(")");
jbe@376 395 }
jbe@376 396 printf(".\n");
jbe@376 397 }
jbe@376 398 }
jbe@352 399 }
jbe@355 400
jbe@372 401 // calculate ranks based on constructed data structures:
jbe@356 402 for (i=0; i<candidate_count; i++) {
jbe@356 403 struct candidate *candidate = loser(i, ballots, ballot_count);
jbe@356 404 candidate->seat = candidate_count - i;
jbe@376 405 if (logging) printf("Assigning rank #%i to suggestion #%s.\n", candidate_count-i, candidate->key);
jbe@356 406 }
jbe@356 407
jbe@372 408 // free ballots[] array:
jbe@354 409 for (i=0; i<ballot_count; i++) {
jbe@354 410 int j;
jbe@354 411 for (j=0; j<4; j++) {
jbe@354 412 if (ballots[i].sections[j].count) {
jbe@354 413 free(ballots[i].sections[j].candidates);
jbe@354 414 }
jbe@354 415 }
jbe@354 416 }
jbe@354 417 free(ballots);
jbe@358 418
jbe@372 419 // write results to database:
jbe@361 420 if (final) {
jbe@374 421 if (logging) printf("Writing final ranks to database.\n");
jbe@361 422 } else {
jbe@374 423 if (logging) printf("Writing ranks to database.\n");
jbe@361 424 }
jbe@359 425 err = write_ranks(db, escaped_initiative_id, final);
jbe@374 426 if (logging) printf("Done.\n");
jbe@358 427
jbe@372 428 // free candidates[] array:
jbe@358 429 free(candidates);
jbe@358 430
jbe@372 431 // return error code of write_ranks() call
jbe@358 432 return err;
jbe@352 433 }
jbe@352 434
jbe@352 435 int main(int argc, char **argv) {
jbe@352 436
jbe@352 437 // variable declarations:
jbe@352 438 int err = 0;
jbe@352 439 int i, count;
jbe@352 440 char *conninfo;
jbe@352 441 PGconn *db;
jbe@352 442 PGresult *res;
jbe@352 443
jbe@352 444 // parse command line:
jbe@352 445 if (argc == 0) return 1;
jbe@352 446 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
jbe@352 447 FILE *out;
jbe@352 448 out = argc == 1 ? stderr : stdout;
jbe@362 449 fprintf(out, "\n");
jbe@374 450 fprintf(out, "Usage: %s [-v|--verbose] <conninfo>\n", argv[0]);
jbe@362 451 fprintf(out, "\n");
jbe@362 452 fprintf(out, "<conninfo> is specified by PostgreSQL's libpq,\n");
jbe@362 453 fprintf(out, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
jbe@362 454 fprintf(out, "\n");
jbe@362 455 fprintf(out, "Example: %s dbname=liquid_feedback\n", argv[0]);
jbe@362 456 fprintf(out, "\n");
jbe@352 457 return argc == 1 ? 1 : 0;
jbe@352 458 }
jbe@352 459 {
jbe@352 460 size_t len = 0;
jbe@374 461 int argb = 1;
jbe@374 462 if (
jbe@374 463 argc >= 2 &&
jbe@374 464 (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--verbose"))
jbe@374 465 ) {
jbe@374 466 argb = 2;
jbe@374 467 logging = 1;
jbe@374 468 }
jbe@374 469 for (i=argb; i<argc; i++) len += strlen(argv[i]) + 1;
jbe@352 470 conninfo = malloc(len * sizeof(char));
jbe@352 471 if (!conninfo) {
jbe@358 472 fprintf(stderr, "Error: Could not allocate memory for conninfo string.\n");
jbe@358 473 abort();
jbe@352 474 }
jbe@352 475 conninfo[0] = 0;
jbe@374 476 for (i=argb; i<argc; i++) {
jbe@374 477 if (i>argb) strcat(conninfo, " ");
jbe@352 478 strcat(conninfo, argv[i]);
jbe@352 479 }
jbe@352 480 }
jbe@352 481
jbe@352 482 // connect to database:
jbe@352 483 db = PQconnectdb(conninfo);
jbe@352 484 if (!db) {
jbe@358 485 fprintf(stderr, "Error: Could not create database handle.\n");
jbe@352 486 return 1;
jbe@352 487 }
jbe@352 488 if (PQstatus(db) != CONNECTION_OK) {
jbe@352 489 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
jbe@352 490 return 1;
jbe@352 491 }
jbe@352 492
jbe@352 493 // check initiatives:
jbe@352 494 res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
jbe@352 495 if (!res) {
jbe@358 496 fprintf(stderr, "Error in pqlib while sending SQL command selecting initiatives to process.\n");
jbe@352 497 err = 1;
jbe@352 498 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
jbe@358 499 fprintf(stderr, "Error while executing SQL command selecting initiatives to process:\n%s", PQresultErrorMessage(res));
jbe@352 500 err = 1;
jbe@352 501 PQclear(res);
jbe@359 502 } else if (PQnfields(res) < 2) {
jbe@359 503 fprintf(stderr, "Too few columns returned by SQL command selecting initiatives to process.\n");
jbe@359 504 err = 1;
jbe@359 505 PQclear(res);
jbe@352 506 } else {
jbe@352 507 count = PQntuples(res);
jbe@374 508 if (logging) printf("Number of initiatives to process: %i\n", count);
jbe@352 509 for (i=0; i<count; i++) {
jbe@352 510 char *initiative_id, *escaped_initiative_id;
jbe@359 511 int final;
jbe@352 512 char *cmd;
jbe@352 513 PGresult *res2;
jbe@352 514 initiative_id = PQgetvalue(res, i, 0);
jbe@359 515 final = (PQgetvalue(res, i, 1)[0] == 't') ? 1 : 0;
jbe@376 516 if (logging) printf("Processing initiative #%s:\n", initiative_id);
jbe@352 517 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
jbe@358 518 if (!escaped_initiative_id) {
jbe@358 519 fprintf(stderr, "Could not escape literal in memory.\n");
jbe@358 520 abort();
jbe@358 521 }
jbe@352 522 if (asprintf(&cmd, "SELECT \"member_id\", \"weight\", \"preference\", \"suggestion_id\" FROM \"individual_suggestion_ranking\" WHERE \"initiative_id\" = %s ORDER BY \"member_id\", \"preference\"", escaped_initiative_id) < 0) {
jbe@352 523 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@355 524 abort();
jbe@352 525 }
jbe@352 526 res2 = PQexec(db, cmd);
jbe@352 527 free(cmd);
jbe@352 528 if (!res2) {
jbe@358 529 fprintf(stderr, "Error in pqlib while sending SQL command selecting individual suggestion rankings.\n");
jbe@352 530 err = 1;
jbe@352 531 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
jbe@358 532 fprintf(stderr, "Error while executing SQL command selecting individual suggestion rankings:\n%s", PQresultErrorMessage(res));
jbe@352 533 err = 1;
jbe@352 534 PQclear(res2);
jbe@356 535 } else if (PQnfields(res2) < 4) {
jbe@358 536 fprintf(stderr, "Too few columns returned by SQL command selecting individual suggestion rankings.\n");
jbe@356 537 err = 1;
jbe@356 538 PQclear(res2);
jbe@352 539 } else {
jbe@359 540 if (process_initiative(db, res2, escaped_initiative_id, final)) err = 1;
jbe@352 541 PQclear(res2);
jbe@352 542 }
jbe@352 543 freemem(escaped_initiative_id);
jbe@352 544 }
jbe@352 545 PQclear(res);
jbe@352 546 }
jbe@352 547
jbe@375 548 // cleanup and exit:
jbe@352 549 PQfinish(db);
jbe@374 550 if (!err) {
jbe@374 551 if (logging) printf("Successfully terminated.\n");
jbe@374 552 } else {
jbe@374 553 fprintf(stderr, "Exiting with error code %i.\n", err);
jbe@374 554 }
jbe@352 555 return err;
jbe@352 556
jbe@352 557 }

Impressum / About Us