liquid_feedback_core

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

Impressum / About Us