liquid_feedback_core

annotate lf_update_suggestion_order.c @ 353:31ce1877320b

Extended "lf_update_suggestion_order" to create a candidate structure
author jbe
date Sat Mar 16 18:19:15 2013 +0100 (2013-03-16)
parents 98c14d8d07f1
children c23bb5ebcdec
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@352 7 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) {
jbe@352 8 // provides compatibility for PostgreSQL versions prior 9.0
jbe@352 9 // in future: return PQescapeLiteral(conn, str, len);
jbe@352 10 char *res;
jbe@352 11 size_t res_len;
jbe@352 12 res = malloc(2*len+3);
jbe@352 13 res[0] = '\'';
jbe@352 14 res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
jbe@352 15 res[res_len+1] = '\'';
jbe@352 16 res[res_len+2] = 0;
jbe@352 17 return res;
jbe@352 18 }
jbe@352 19
jbe@352 20 static void freemem(void *ptr) {
jbe@352 21 // to be used for "escapeLiteral" function
jbe@352 22 // provides compatibility for PostgreSQL versions prior 9.0
jbe@352 23 // in future: PQfreemem(ptr);
jbe@352 24 free(ptr);
jbe@352 25 }
jbe@352 26
jbe@352 27 #define COL_MEMBER_ID 0
jbe@352 28 #define COL_WEIGHT 1
jbe@352 29 #define COL_PREFERENCE 2
jbe@352 30 #define COL_SUGGESTION_ID 3
jbe@352 31
jbe@353 32 struct candidate {
jbe@353 33 char *key;
jbe@353 34 double score;
jbe@353 35 int seat;
jbe@353 36 };
jbe@353 37
jbe@353 38 static int compare_candidate(struct candidate *c1, struct candidate *c2) {
jbe@353 39 return strcmp(c1->key, c2->key);
jbe@353 40 }
jbe@352 41
jbe@353 42 static int candidate_count;
jbe@353 43 static struct candidate *candidates;
jbe@353 44
jbe@353 45 static void register_candidate(char **candidate_key, VISIT visit, int level) {
jbe@352 46 if (visit == postorder || visit == leaf) {
jbe@353 47 struct candidate *candidate;
jbe@353 48 candidate = candidates + (candidate_count++);
jbe@353 49 candidate->key = *candidate_key;
jbe@353 50 candidate->score = 0.0;
jbe@353 51 candidate->seat = 0;
jbe@352 52 }
jbe@352 53 }
jbe@352 54
jbe@353 55 static struct candidate *candidate_by_key(char *candidate_key) {
jbe@353 56 struct candidate *candidate;
jbe@353 57 struct candidate compare;
jbe@353 58 compare.key = candidate_key;
jbe@353 59 candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate);
jbe@353 60 if (!candidate) {
jbe@352 61 fprintf(stderr, "Candidate not found (should not happen)\n");
jbe@352 62 abort();
jbe@352 63 }
jbe@353 64 return candidate;
jbe@352 65 }
jbe@352 66
jbe@352 67 struct ballot_section {
jbe@352 68 int count;
jbe@353 69 struct candidate **candidates;
jbe@352 70 };
jbe@352 71
jbe@352 72 struct ballot {
jbe@352 73 int weight;
jbe@352 74 struct ballot_section sections[4];
jbe@352 75 };
jbe@352 76
jbe@352 77 static void process_initiative(PGresult *res) {
jbe@352 78 void *candidate_tree = NULL;
jbe@352 79 int ballot_count = 0;
jbe@352 80 int tuple_count, i;
jbe@352 81 char *old_member_id = NULL;
jbe@352 82 struct ballot *ballots, *ballot;
jbe@352 83 int candidates_in_sections[4] = {0, };
jbe@352 84 candidate_count = 0;
jbe@352 85 tuple_count = PQntuples(res);
jbe@352 86 for (i=0; i<=tuple_count; i++) {
jbe@352 87 char *member_id, *suggestion_id;
jbe@352 88 if (i<tuple_count) {
jbe@352 89 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@352 90 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@352 91 if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
jbe@352 92 candidate_count++;
jbe@352 93 if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
jbe@352 94 fprintf(stderr, "Insufficient memory\n");
jbe@352 95 abort();
jbe@352 96 }
jbe@352 97 }
jbe@352 98 }
jbe@352 99 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
jbe@352 100 ballot_count++;
jbe@352 101 }
jbe@352 102 old_member_id = member_id;
jbe@352 103 }
jbe@352 104 printf("Candidate count: %i\n", candidate_count);
jbe@353 105 candidates = malloc(candidate_count * sizeof(struct candidate));
jbe@352 106 if (!candidates) {
jbe@352 107 fprintf(stderr, "Insufficient memory\n");
jbe@352 108 abort();
jbe@352 109 }
jbe@352 110 candidate_count = 0;
jbe@352 111 twalk(candidate_tree, (void *)register_candidate);
jbe@352 112 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)strcmp);
jbe@352 113 printf("Ballot count: %i\n", ballot_count);
jbe@352 114 ballots = calloc(ballot_count, sizeof(struct ballot));
jbe@352 115 if (!ballots) {
jbe@352 116 fprintf(stderr, "Insufficient memory\n");
jbe@352 117 abort();
jbe@352 118 }
jbe@352 119 ballot = ballots;
jbe@353 120 for (i=0; i<tuple_count; i++) {
jbe@352 121 char *member_id, *suggestion_id;
jbe@352 122 int weight, preference;
jbe@353 123 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@353 124 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@353 125 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
jbe@353 126 if (weight <= 0) {
jbe@353 127 fprintf(stderr, "Unexpected weight value\n");
jbe@353 128 abort();
jbe@352 129 }
jbe@353 130 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
jbe@353 131 if (preference < 1 || preference > 4) {
jbe@353 132 fprintf(stderr, "Unexpected preference value\n");
jbe@353 133 abort();
jbe@352 134 }
jbe@353 135 preference--;
jbe@353 136 ballot->weight = weight;
jbe@353 137 ballot->sections[preference].count++;
jbe@353 138 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
jbe@352 139 old_member_id = member_id;
jbe@352 140 }
jbe@352 141 for (i=0; i<ballot_count; i++) {
jbe@352 142 int j;
jbe@352 143 for (j=0; j<4; j++) {
jbe@352 144 if (ballots[i].sections[j].count) {
jbe@353 145 ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
jbe@352 146 if (!ballots[i].sections[j].candidates) {
jbe@352 147 fprintf(stderr, "Insufficient memory\n");
jbe@352 148 abort();
jbe@352 149 }
jbe@352 150 }
jbe@352 151 }
jbe@352 152 }
jbe@352 153 ballot = ballots;
jbe@352 154 for (i=0; i<=tuple_count; i++) {
jbe@352 155 char *member_id, *suggestion_id;
jbe@352 156 int preference;
jbe@352 157 if (i<tuple_count) {
jbe@352 158 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
jbe@352 159 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
jbe@352 160 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
jbe@352 161 if (preference < 1 || preference > 4) {
jbe@352 162 fprintf(stderr, "Unexpected preference value\n");
jbe@352 163 abort();
jbe@352 164 }
jbe@352 165 preference--;
jbe@353 166 ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
jbe@352 167 }
jbe@352 168 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
jbe@352 169 ballot++;
jbe@352 170 candidates_in_sections[0] = 0;
jbe@352 171 candidates_in_sections[1] = 0;
jbe@352 172 candidates_in_sections[2] = 0;
jbe@352 173 candidates_in_sections[3] = 0;
jbe@352 174 }
jbe@352 175 old_member_id = member_id;
jbe@352 176 }
jbe@352 177 }
jbe@352 178
jbe@352 179 int main(int argc, char **argv) {
jbe@352 180
jbe@352 181 // variable declarations:
jbe@352 182 int err = 0;
jbe@352 183 int i, count;
jbe@352 184 char *conninfo;
jbe@352 185 PGconn *db;
jbe@352 186 PGresult *res;
jbe@352 187
jbe@352 188 // parse command line:
jbe@352 189 if (argc == 0) return 1;
jbe@352 190 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
jbe@352 191 FILE *out;
jbe@352 192 out = argc == 1 ? stderr : stdout;
jbe@352 193 fprintf(stdout, "\n");
jbe@352 194 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
jbe@352 195 fprintf(stdout, "\n");
jbe@352 196 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
jbe@352 197 fprintf(stdout, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
jbe@352 198 fprintf(stdout, "\n");
jbe@352 199 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
jbe@352 200 fprintf(stdout, "\n");
jbe@352 201 return argc == 1 ? 1 : 0;
jbe@352 202 }
jbe@352 203 {
jbe@352 204 size_t len = 0;
jbe@352 205 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
jbe@352 206 conninfo = malloc(len * sizeof(char));
jbe@352 207 if (!conninfo) {
jbe@352 208 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
jbe@352 209 return 1;
jbe@352 210 }
jbe@352 211 conninfo[0] = 0;
jbe@352 212 for (i=1; i<argc; i++) {
jbe@352 213 if (i>1) strcat(conninfo, " ");
jbe@352 214 strcat(conninfo, argv[i]);
jbe@352 215 }
jbe@352 216 }
jbe@352 217
jbe@352 218 // connect to database:
jbe@352 219 db = PQconnectdb(conninfo);
jbe@352 220 if (!db) {
jbe@352 221 fprintf(stderr, "Error: Could not create database handle\n");
jbe@352 222 return 1;
jbe@352 223 }
jbe@352 224 if (PQstatus(db) != CONNECTION_OK) {
jbe@352 225 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
jbe@352 226 return 1;
jbe@352 227 }
jbe@352 228
jbe@352 229 // check initiatives:
jbe@352 230 res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
jbe@352 231 if (!res) {
jbe@352 232 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
jbe@352 233 err = 1;
jbe@352 234 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
jbe@352 235 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
jbe@352 236 err = 1;
jbe@352 237 PQclear(res);
jbe@352 238 } else {
jbe@352 239 count = PQntuples(res);
jbe@352 240 printf("Number of initiatives to process: %i\n", count);
jbe@352 241 for (i=0; i<count; i++) {
jbe@352 242 char *initiative_id, *escaped_initiative_id;
jbe@352 243 char *cmd;
jbe@352 244 PGresult *res2;
jbe@352 245 initiative_id = PQgetvalue(res, i, 0);
jbe@352 246 printf("Processing initiative_id: %s\n", initiative_id);
jbe@352 247 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
jbe@352 248 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 249 fprintf(stderr, "Could not prepare query string in memory.\n");
jbe@352 250 err = 1;
jbe@352 251 freemem(escaped_initiative_id);
jbe@352 252 break;
jbe@352 253 }
jbe@352 254 res2 = PQexec(db, cmd);
jbe@352 255 free(cmd);
jbe@352 256 if (!res2) {
jbe@352 257 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
jbe@352 258 err = 1;
jbe@352 259 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
jbe@352 260 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
jbe@352 261 err = 1;
jbe@352 262 PQclear(res2);
jbe@352 263 } else {
jbe@352 264 if (PQntuples(res2) == 0) {
jbe@352 265 printf("Nothing to do.\n");
jbe@352 266 } else {
jbe@352 267 process_initiative(res2);
jbe@352 268 }
jbe@352 269 PQclear(res2);
jbe@352 270 }
jbe@352 271 freemem(escaped_initiative_id);
jbe@352 272 }
jbe@352 273 PQclear(res);
jbe@352 274 }
jbe@352 275
jbe@352 276 // cleanup and exit
jbe@352 277 PQfinish(db);
jbe@352 278 return err;
jbe@352 279
jbe@352 280 }

Impressum / About Us