liquid_feedback_core

view lf_update_suggestion_order.c @ 358:e22a4d2aea2d

Write ranks to database in "lf_update_suggestion_order"
author jbe
date Sun Mar 17 10:40:00 2013 +0100 (2013-03-17)
parents 4698b68e0942
children c37c0b4f2027
line source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <libpq-fe.h>
5 #include <search.h>
7 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) {
8 // provides compatibility for PostgreSQL versions prior 9.0
9 // in future: return PQescapeLiteral(conn, str, len);
10 char *res;
11 size_t res_len;
12 res = malloc(2*len+3);
13 if (!res) return NULL;
14 res[0] = '\'';
15 res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
16 res[res_len+1] = '\'';
17 res[res_len+2] = 0;
18 return res;
19 }
21 static void freemem(void *ptr) {
22 // to be used for "escapeLiteral" function
23 // provides compatibility for PostgreSQL versions prior 9.0
24 // in future: PQfreemem(ptr);
25 free(ptr);
26 }
28 #define COL_MEMBER_ID 0
29 #define COL_WEIGHT 1
30 #define COL_PREFERENCE 2
31 #define COL_SUGGESTION_ID 3
33 struct candidate {
34 char *key;
35 double score_per_step;
36 int reaches_score;
37 double score;
38 int seat;
39 };
41 static int compare_candidate(struct candidate *c1, struct candidate *c2) {
42 return strcmp(c1->key, c2->key);
43 }
45 static int candidate_count;
46 static struct candidate *candidates;
48 static void register_candidate(char **candidate_key, VISIT visit, int level) {
49 if (visit == postorder || visit == leaf) {
50 struct candidate *candidate;
51 candidate = candidates + (candidate_count++);
52 candidate->key = *candidate_key;
53 candidate->seat = 0;
54 }
55 }
57 static struct candidate *candidate_by_key(char *candidate_key) {
58 struct candidate *candidate;
59 struct candidate compare;
60 compare.key = candidate_key;
61 candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate);
62 if (!candidate) {
63 fprintf(stderr, "Candidate not found (should not happen).\n");
64 abort();
65 }
66 return candidate;
67 }
69 struct ballot_section {
70 int count;
71 struct candidate **candidates;
72 };
74 struct ballot {
75 int weight;
76 struct ballot_section sections[4];
77 };
79 static struct candidate *loser(int round_number, struct ballot *ballots, int ballot_count) {
80 int i, j, k;
81 int remaining;
82 for (i=0; i<candidate_count; i++) {
83 candidates[i].score = 0.0;
84 }
85 remaining = candidate_count - round_number;
86 while (1) {
87 double scale;
88 if (remaining <= 1) break;
89 for (i=0; i<candidate_count; i++) {
90 candidates[i].score_per_step = 0.0;
91 candidates[i].reaches_score = 0;
92 }
93 for (i=0; i<ballot_count; i++) {
94 for (j=0; j<4; j++) {
95 int matches = 0;
96 for (k=0; k<ballots[i].sections[j].count; k++) {
97 struct candidate *candidate;
98 candidate = ballots[i].sections[j].candidates[k];
99 if (candidate->score < 1.0 && !candidate->seat) matches++;
100 }
101 if (matches) {
102 double score_inc;
103 score_inc = 1.0 / (double)matches;
104 for (k=0; k<ballots[i].sections[j].count; k++) {
105 struct candidate *candidate;
106 candidate = ballots[i].sections[j].candidates[k];
107 if (candidate->score < 1.0 && !candidate->seat) {
108 candidate->score_per_step += score_inc;
109 }
110 }
111 break;
112 }
113 }
114 }
115 scale = (double)candidate_count;
116 for (i=0; i<candidate_count; i++) {
117 double max_scale;
118 if (candidates[i].score_per_step > 0.0) {
119 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
120 if (max_scale <= scale) {
121 scale = max_scale;
122 candidates[i].reaches_score = 1;
123 }
124 }
125 }
126 for (i=0; i<candidate_count; i++) {
127 if (candidates[i].score_per_step > 0.0) {
128 if (candidates[i].reaches_score) {
129 candidates[i].score = 1.0;
130 remaining--;
131 } else {
132 candidates[i].score += scale * candidates[i].score_per_step;
133 if (candidates[i].score >= 1.0) remaining--;
134 }
135 if (remaining <= 1) break;
136 }
137 }
138 }
139 for (i=candidate_count-1; i>=0; i--) {
140 if (candidates[i].score < 1.0 && !candidates[i].seat) return candidates+i;
141 }
142 fprintf(stderr, "No remaining candidate (should not happen).");
143 abort();
144 }
146 static int write_ranks(PGconn *db, char *escaped_initiative_id) {
147 PGresult *res;
148 char *cmd;
149 int i;
150 if (asprintf(&cmd, "BEGIN; UPDATE \"suggestion\" SET \"proportional_order\" = NULL WHERE \"initiative_id\" = %s", escaped_initiative_id) < 0) {
151 fprintf(stderr, "Could not prepare query string in memory.\n");
152 abort();
153 }
154 res = PQexec(db, cmd);
155 free(cmd);
156 if (!res) {
157 fprintf(stderr, "Error in pqlib while sending SQL command to initiate suggestion update.\n");
158 return 1;
159 } else if (
160 PQresultStatus(res) != PGRES_COMMAND_OK &&
161 PQresultStatus(res) != PGRES_TUPLES_OK
162 ) {
163 fprintf(stderr, "Error while executing SQL command to initiate suggestion update:\n%s", PQresultErrorMessage(res));
164 PQclear(res);
165 return 1;
166 } else {
167 PQclear(res);
168 }
169 for (i=0; i<candidate_count; i++) {
170 char *escaped_suggestion_id;
171 escaped_suggestion_id = escapeLiteral(db, candidates[i].key, strlen(candidates[i].key));
172 if (!escaped_suggestion_id) {
173 fprintf(stderr, "Could not escape literal in memory.\n");
174 abort();
175 }
176 if (asprintf(&cmd, "UPDATE \"suggestion\" SET \"proportional_order\" = %i WHERE \"id\" = %s", candidates[i].seat, escaped_suggestion_id) < 0) {
177 fprintf(stderr, "Could not prepare query string in memory.\n");
178 abort();
179 }
180 freemem(escaped_suggestion_id);
181 res = PQexec(db, cmd);
182 free(cmd);
183 if (!res) {
184 fprintf(stderr, "Error in pqlib while sending SQL command to update suggestion order.\n");
185 } else if (
186 PQresultStatus(res) != PGRES_COMMAND_OK &&
187 PQresultStatus(res) != PGRES_TUPLES_OK
188 ) {
189 fprintf(stderr, "Error while executing SQL command to update suggestion order:\n%s", PQresultErrorMessage(res));
190 PQclear(res);
191 } else {
192 PQclear(res);
193 continue;
194 }
195 res = PQexec(db, "ROLLBACK");
196 if (res) PQclear(res);
197 return 1;
198 }
199 res = PQexec(db, "COMMIT");
200 if (!res) {
201 fprintf(stderr, "Error in pqlib while sending SQL command to commit transaction.\n");
202 return 1;
203 } else if (
204 PQresultStatus(res) != PGRES_COMMAND_OK &&
205 PQresultStatus(res) != PGRES_TUPLES_OK
206 ) {
207 fprintf(stderr, "Error while executing SQL command to commit transaction:\n%s", PQresultErrorMessage(res));
208 PQclear(res);
209 return 1;
210 } else {
211 PQclear(res);
212 return 0;
213 }
214 }
216 static int process_initiative(PGconn *db, PGresult *res, char *escaped_initiative_id) {
217 int err;
218 int ballot_count = 0;
219 struct ballot *ballots;
220 int i;
221 {
222 void *candidate_tree = NULL;
223 int tuple_count;
224 char *old_member_id = NULL;
225 struct ballot *ballot;
226 int candidates_in_sections[4] = {0, };
227 candidate_count = 0;
228 tuple_count = PQntuples(res);
229 for (i=0; i<=tuple_count; i++) {
230 char *member_id, *suggestion_id;
231 if (i<tuple_count) {
232 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
233 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
234 if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
235 candidate_count++;
236 if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
237 fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n");
238 abort();
239 }
240 }
241 }
242 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
243 ballot_count++;
244 }
245 old_member_id = member_id;
246 }
247 printf("Candidate count: %i\n", candidate_count);
248 candidates = malloc(candidate_count * sizeof(struct candidate));
249 if (!candidates) {
250 fprintf(stderr, "Insufficient memory while creating candidate list.\n");
251 abort();
252 }
253 candidate_count = 0;
254 twalk(candidate_tree, (void *)register_candidate);
255 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)strcmp);
256 printf("Ballot count: %i\n", ballot_count);
257 ballots = calloc(ballot_count, sizeof(struct ballot));
258 if (!ballots) {
259 fprintf(stderr, "Insufficient memory while creating ballot list.\n");
260 abort();
261 }
262 ballot = ballots;
263 for (i=0; i<tuple_count; i++) {
264 char *member_id, *suggestion_id;
265 int weight, preference;
266 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
267 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
268 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
269 if (weight <= 0) {
270 fprintf(stderr, "Unexpected weight value.\n");
271 free(ballots);
272 free(candidates);
273 return 1;
274 }
275 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
276 if (preference < 1 || preference > 4) {
277 fprintf(stderr, "Unexpected preference value.\n");
278 free(ballots);
279 free(candidates);
280 return 1;
281 }
282 preference--;
283 ballot->weight = weight;
284 ballot->sections[preference].count++;
285 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
286 old_member_id = member_id;
287 }
288 for (i=0; i<ballot_count; i++) {
289 int j;
290 for (j=0; j<4; j++) {
291 if (ballots[i].sections[j].count) {
292 ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
293 if (!ballots[i].sections[j].candidates) {
294 fprintf(stderr, "Insufficient memory while creating ballot section.\n");
295 abort();
296 }
297 }
298 }
299 }
300 ballot = ballots;
301 for (i=0; i<=tuple_count; i++) {
302 char *member_id, *suggestion_id;
303 int preference;
304 if (i<tuple_count) {
305 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
306 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
307 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
308 preference--;
309 ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
310 }
311 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
312 ballot++;
313 candidates_in_sections[0] = 0;
314 candidates_in_sections[1] = 0;
315 candidates_in_sections[2] = 0;
316 candidates_in_sections[3] = 0;
317 }
318 old_member_id = member_id;
319 }
320 }
322 for (i=0; i<candidate_count; i++) {
323 struct candidate *candidate = loser(i, ballots, ballot_count);
324 candidate->seat = candidate_count - i;
325 printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key);
326 }
328 for (i=0; i<ballot_count; i++) {
329 int j;
330 for (j=0; j<4; j++) {
331 if (ballots[i].sections[j].count) {
332 free(ballots[i].sections[j].candidates);
333 }
334 }
335 }
336 free(ballots);
338 printf("Writing ranks to database.\n");
339 err = write_ranks(db, escaped_initiative_id);
340 printf("Done.\n");
342 free(candidates);
344 return err;
345 }
347 int main(int argc, char **argv) {
349 // variable declarations:
350 int err = 0;
351 int i, count;
352 char *conninfo;
353 PGconn *db;
354 PGresult *res;
356 // parse command line:
357 if (argc == 0) return 1;
358 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
359 FILE *out;
360 out = argc == 1 ? stderr : stdout;
361 fprintf(stdout, "\n");
362 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
363 fprintf(stdout, "\n");
364 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
365 fprintf(stdout, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
366 fprintf(stdout, "\n");
367 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
368 fprintf(stdout, "\n");
369 return argc == 1 ? 1 : 0;
370 }
371 {
372 size_t len = 0;
373 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
374 conninfo = malloc(len * sizeof(char));
375 if (!conninfo) {
376 fprintf(stderr, "Error: Could not allocate memory for conninfo string.\n");
377 abort();
378 }
379 conninfo[0] = 0;
380 for (i=1; i<argc; i++) {
381 if (i>1) strcat(conninfo, " ");
382 strcat(conninfo, argv[i]);
383 }
384 }
386 // connect to database:
387 db = PQconnectdb(conninfo);
388 if (!db) {
389 fprintf(stderr, "Error: Could not create database handle.\n");
390 return 1;
391 }
392 if (PQstatus(db) != CONNECTION_OK) {
393 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
394 return 1;
395 }
397 // check initiatives:
398 res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
399 if (!res) {
400 fprintf(stderr, "Error in pqlib while sending SQL command selecting initiatives to process.\n");
401 err = 1;
402 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
403 fprintf(stderr, "Error while executing SQL command selecting initiatives to process:\n%s", PQresultErrorMessage(res));
404 err = 1;
405 PQclear(res);
406 } else {
407 count = PQntuples(res);
408 printf("Number of initiatives to process: %i\n", count);
409 for (i=0; i<count; i++) {
410 char *initiative_id, *escaped_initiative_id;
411 char *cmd;
412 PGresult *res2;
413 initiative_id = PQgetvalue(res, i, 0);
414 printf("Processing initiative_id: %s\n", initiative_id);
415 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
416 if (!escaped_initiative_id) {
417 fprintf(stderr, "Could not escape literal in memory.\n");
418 abort();
419 }
420 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) {
421 fprintf(stderr, "Could not prepare query string in memory.\n");
422 abort();
423 }
424 res2 = PQexec(db, cmd);
425 free(cmd);
426 if (!res2) {
427 fprintf(stderr, "Error in pqlib while sending SQL command selecting individual suggestion rankings.\n");
428 err = 1;
429 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
430 fprintf(stderr, "Error while executing SQL command selecting individual suggestion rankings:\n%s", PQresultErrorMessage(res));
431 err = 1;
432 PQclear(res2);
433 } else if (PQnfields(res2) < 4) {
434 fprintf(stderr, "Too few columns returned by SQL command selecting individual suggestion rankings.\n");
435 err = 1;
436 PQclear(res2);
437 } else {
438 if (PQntuples(res2) == 0) {
439 printf("Nothing to do.\n");
440 } else {
441 if (process_initiative(db, res2, escaped_initiative_id)) err = 1;
442 }
443 PQclear(res2);
444 }
445 freemem(escaped_initiative_id);
446 }
447 PQclear(res);
448 }
450 // cleanup and exit
451 PQfinish(db);
452 if (!err) printf("Successfully terminated.\n");
453 else fprintf(stderr, "Exiting with error code #%i.\n", err);
454 return err;
456 }

Impressum / About Us