liquid_feedback_core

view lf_update_suggestion_order.c @ 355:accffcefeff7

Code cleanup in "lf_update_suggestion_order" (own block for preparation of the ballot data)
author jbe
date Sat Mar 16 18:29:55 2013 +0100 (2013-03-16)
parents c23bb5ebcdec
children 4698b68e0942
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 int ballot_count = 0;
79 int i;
80 struct ballot *ballots;
81 {
82 void *candidate_tree = NULL;
83 int tuple_count;
84 char *old_member_id = NULL;
85 struct ballot *ballot;
86 int candidates_in_sections[4] = {0, };
87 candidate_count = 0;
88 tuple_count = PQntuples(res);
89 for (i=0; i<=tuple_count; i++) {
90 char *member_id, *suggestion_id;
91 if (i<tuple_count) {
92 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
93 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
94 if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
95 candidate_count++;
96 if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
97 fprintf(stderr, "Insufficient memory\n");
98 abort();
99 }
100 }
101 }
102 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
103 ballot_count++;
104 }
105 old_member_id = member_id;
106 }
107 printf("Candidate count: %i\n", candidate_count);
108 candidates = malloc(candidate_count * sizeof(struct candidate));
109 if (!candidates) {
110 fprintf(stderr, "Insufficient memory\n");
111 abort();
112 }
113 candidate_count = 0;
114 twalk(candidate_tree, (void *)register_candidate);
115 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)strcmp);
116 printf("Ballot count: %i\n", ballot_count);
117 ballots = calloc(ballot_count, sizeof(struct ballot));
118 if (!ballots) {
119 fprintf(stderr, "Insufficient memory\n");
120 abort();
121 }
122 ballot = ballots;
123 for (i=0; i<tuple_count; i++) {
124 char *member_id, *suggestion_id;
125 int weight, preference;
126 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
127 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
128 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
129 if (weight <= 0) {
130 fprintf(stderr, "Unexpected weight value\n");
131 abort();
132 }
133 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
134 if (preference < 1 || preference > 4) {
135 fprintf(stderr, "Unexpected preference value\n");
136 abort();
137 }
138 preference--;
139 ballot->weight = weight;
140 ballot->sections[preference].count++;
141 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
142 old_member_id = member_id;
143 }
144 for (i=0; i<ballot_count; i++) {
145 int j;
146 for (j=0; j<4; j++) {
147 if (ballots[i].sections[j].count) {
148 ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
149 if (!ballots[i].sections[j].candidates) {
150 fprintf(stderr, "Insufficient memory\n");
151 abort();
152 }
153 }
154 }
155 }
156 ballot = ballots;
157 for (i=0; i<=tuple_count; i++) {
158 char *member_id, *suggestion_id;
159 int preference;
160 if (i<tuple_count) {
161 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
162 suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
163 preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
164 if (preference < 1 || preference > 4) {
165 fprintf(stderr, "Unexpected preference value\n");
166 abort();
167 }
168 preference--;
169 ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
170 }
171 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
172 ballot++;
173 candidates_in_sections[0] = 0;
174 candidates_in_sections[1] = 0;
175 candidates_in_sections[2] = 0;
176 candidates_in_sections[3] = 0;
177 }
178 old_member_id = member_id;
179 }
180 }
182 free(candidates);
183 for (i=0; i<ballot_count; i++) {
184 int j;
185 for (j=0; j<4; j++) {
186 if (ballots[i].sections[j].count) {
187 free(ballots[i].sections[j].candidates);
188 }
189 }
190 }
191 free(ballots);
192 }
194 int main(int argc, char **argv) {
196 // variable declarations:
197 int err = 0;
198 int i, count;
199 char *conninfo;
200 PGconn *db;
201 PGresult *res;
203 // parse command line:
204 if (argc == 0) return 1;
205 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
206 FILE *out;
207 out = argc == 1 ? stderr : stdout;
208 fprintf(stdout, "\n");
209 fprintf(stdout, "Usage: %s <conninfo>\n", argv[0]);
210 fprintf(stdout, "\n");
211 fprintf(stdout, "<conninfo> is specified by PostgreSQL's libpq,\n");
212 fprintf(stdout, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
213 fprintf(stdout, "\n");
214 fprintf(stdout, "Example: %s dbname=liquid_feedback\n", argv[0]);
215 fprintf(stdout, "\n");
216 return argc == 1 ? 1 : 0;
217 }
218 {
219 size_t len = 0;
220 for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
221 conninfo = malloc(len * sizeof(char));
222 if (!conninfo) {
223 fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
224 return 1;
225 }
226 conninfo[0] = 0;
227 for (i=1; i<argc; i++) {
228 if (i>1) strcat(conninfo, " ");
229 strcat(conninfo, argv[i]);
230 }
231 }
233 // connect to database:
234 db = PQconnectdb(conninfo);
235 if (!db) {
236 fprintf(stderr, "Error: Could not create database handle\n");
237 return 1;
238 }
239 if (PQstatus(db) != CONNECTION_OK) {
240 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
241 return 1;
242 }
244 // check initiatives:
245 res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
246 if (!res) {
247 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
248 err = 1;
249 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
250 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
251 err = 1;
252 PQclear(res);
253 } else {
254 count = PQntuples(res);
255 printf("Number of initiatives to process: %i\n", count);
256 for (i=0; i<count; i++) {
257 char *initiative_id, *escaped_initiative_id;
258 char *cmd;
259 PGresult *res2;
260 initiative_id = PQgetvalue(res, i, 0);
261 printf("Processing initiative_id: %s\n", initiative_id);
262 escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
263 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) {
264 fprintf(stderr, "Could not prepare query string in memory.\n");
265 abort();
266 }
267 res2 = PQexec(db, cmd);
268 free(cmd);
269 if (!res2) {
270 fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
271 err = 1;
272 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
273 fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
274 err = 1;
275 PQclear(res2);
276 } else {
277 if (PQntuples(res2) == 0) {
278 printf("Nothing to do.\n");
279 } else {
280 process_initiative(res2);
281 }
282 PQclear(res2);
283 }
284 freemem(escaped_initiative_id);
285 }
286 PQclear(res);
287 }
289 // cleanup and exit
290 PQfinish(db);
291 return err;
293 }

Impressum / About Us