liquid_feedback_core

changeset 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 d420e7a5a75f
children c37c0b4f2027
files lf_update_suggestion_order.c
line diff
     1.1 --- a/lf_update_suggestion_order.c	Sun Mar 17 10:02:49 2013 +0100
     1.2 +++ b/lf_update_suggestion_order.c	Sun Mar 17 10:40:00 2013 +0100
     1.3 @@ -10,6 +10,7 @@
     1.4    char *res;
     1.5    size_t res_len;
     1.6    res = malloc(2*len+3);
     1.7 +  if (!res) return NULL;
     1.8    res[0] = '\'';
     1.9    res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
    1.10    res[res_len+1] = '\'';
    1.11 @@ -142,7 +143,78 @@
    1.12    abort();
    1.13  }
    1.14  
    1.15 -static void process_initiative(PGresult *res) {
    1.16 +static int write_ranks(PGconn *db, char *escaped_initiative_id) {
    1.17 +  PGresult *res;
    1.18 +  char *cmd;
    1.19 +  int i;
    1.20 +  if (asprintf(&cmd, "BEGIN; UPDATE \"suggestion\" SET \"proportional_order\" = NULL WHERE \"initiative_id\" = %s", escaped_initiative_id) < 0) {
    1.21 +    fprintf(stderr, "Could not prepare query string in memory.\n");
    1.22 +    abort();
    1.23 +  }
    1.24 +  res = PQexec(db, cmd);
    1.25 +  free(cmd);
    1.26 +  if (!res) {
    1.27 +    fprintf(stderr, "Error in pqlib while sending SQL command to initiate suggestion update.\n");
    1.28 +    return 1;
    1.29 +  } else if (
    1.30 +    PQresultStatus(res) != PGRES_COMMAND_OK &&
    1.31 +    PQresultStatus(res) != PGRES_TUPLES_OK
    1.32 +  ) {
    1.33 +    fprintf(stderr, "Error while executing SQL command to initiate suggestion update:\n%s", PQresultErrorMessage(res));
    1.34 +    PQclear(res);
    1.35 +    return 1;
    1.36 +  } else {
    1.37 +    PQclear(res);
    1.38 +  }
    1.39 +  for (i=0; i<candidate_count; i++) {
    1.40 +    char *escaped_suggestion_id;
    1.41 +    escaped_suggestion_id = escapeLiteral(db, candidates[i].key, strlen(candidates[i].key));
    1.42 +    if (!escaped_suggestion_id) {
    1.43 +      fprintf(stderr, "Could not escape literal in memory.\n");
    1.44 +      abort();
    1.45 +    }
    1.46 +    if (asprintf(&cmd, "UPDATE \"suggestion\" SET \"proportional_order\" = %i WHERE \"id\" = %s", candidates[i].seat, escaped_suggestion_id) < 0) {
    1.47 +      fprintf(stderr, "Could not prepare query string in memory.\n");
    1.48 +      abort();
    1.49 +    }
    1.50 +    freemem(escaped_suggestion_id);
    1.51 +    res = PQexec(db, cmd);
    1.52 +    free(cmd);
    1.53 +    if (!res) {
    1.54 +      fprintf(stderr, "Error in pqlib while sending SQL command to update suggestion order.\n");
    1.55 +    } else if (
    1.56 +      PQresultStatus(res) != PGRES_COMMAND_OK &&
    1.57 +      PQresultStatus(res) != PGRES_TUPLES_OK
    1.58 +    ) {
    1.59 +      fprintf(stderr, "Error while executing SQL command to update suggestion order:\n%s", PQresultErrorMessage(res));
    1.60 +      PQclear(res);
    1.61 +    } else {
    1.62 +      PQclear(res);
    1.63 +      continue;
    1.64 +    }
    1.65 +    res = PQexec(db, "ROLLBACK");
    1.66 +    if (res) PQclear(res);
    1.67 +    return 1;
    1.68 +  }
    1.69 +  res = PQexec(db, "COMMIT");
    1.70 +  if (!res) {
    1.71 +    fprintf(stderr, "Error in pqlib while sending SQL command to commit transaction.\n");
    1.72 +    return 1;
    1.73 +  } else if (
    1.74 +    PQresultStatus(res) != PGRES_COMMAND_OK &&
    1.75 +    PQresultStatus(res) != PGRES_TUPLES_OK
    1.76 +  ) {
    1.77 +    fprintf(stderr, "Error while executing SQL command to commit transaction:\n%s", PQresultErrorMessage(res));
    1.78 +    PQclear(res);
    1.79 +    return 1;
    1.80 +  } else {
    1.81 +    PQclear(res);
    1.82 +    return 0;
    1.83 +  }
    1.84 +}
    1.85 +
    1.86 +static int process_initiative(PGconn *db, PGresult *res, char *escaped_initiative_id) {
    1.87 +  int err;
    1.88    int ballot_count = 0;
    1.89    struct ballot *ballots;
    1.90    int i;
    1.91 @@ -196,12 +268,16 @@
    1.92        weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
    1.93        if (weight <= 0) {
    1.94          fprintf(stderr, "Unexpected weight value.\n");
    1.95 -        abort();
    1.96 +        free(ballots);
    1.97 +        free(candidates);
    1.98 +        return 1;
    1.99        }
   1.100        preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
   1.101        if (preference < 1 || preference > 4) {
   1.102          fprintf(stderr, "Unexpected preference value.\n");
   1.103 -        abort();
   1.104 +        free(ballots);
   1.105 +        free(candidates);
   1.106 +        return 1;
   1.107        }
   1.108        preference--;
   1.109        ballot->weight = weight;
   1.110 @@ -246,9 +322,9 @@
   1.111    for (i=0; i<candidate_count; i++) {
   1.112      struct candidate *candidate = loser(i, ballots, ballot_count);
   1.113      candidate->seat = candidate_count - i;
   1.114 +    printf("Assigning rank #%i to suggestion #%s.\n", candidate_count - i, candidate->key);
   1.115    }
   1.116  
   1.117 -  free(candidates);
   1.118    for (i=0; i<ballot_count; i++) {
   1.119      int j;
   1.120      for (j=0; j<4; j++) {
   1.121 @@ -258,6 +334,14 @@
   1.122      }
   1.123    }
   1.124    free(ballots);
   1.125 +
   1.126 +  printf("Writing ranks to database.\n");
   1.127 +  err = write_ranks(db, escaped_initiative_id);
   1.128 +  printf("Done.\n");
   1.129 +
   1.130 +  free(candidates);
   1.131 +
   1.132 +  return err;
   1.133  }
   1.134  
   1.135  int main(int argc, char **argv) {
   1.136 @@ -289,8 +373,8 @@
   1.137      for (i=1; i<argc; i++) len += strlen(argv[i]) + 1;
   1.138      conninfo = malloc(len * sizeof(char));
   1.139      if (!conninfo) {
   1.140 -      fprintf(stderr, "Error: Could not allocate memory for conninfo string\n");
   1.141 -      return 1;
   1.142 +      fprintf(stderr, "Error: Could not allocate memory for conninfo string.\n");
   1.143 +      abort();
   1.144      }
   1.145      conninfo[0] = 0;
   1.146      for (i=1; i<argc; i++) {
   1.147 @@ -302,7 +386,7 @@
   1.148    // connect to database:
   1.149    db = PQconnectdb(conninfo);
   1.150    if (!db) {
   1.151 -    fprintf(stderr, "Error: Could not create database handle\n");
   1.152 +    fprintf(stderr, "Error: Could not create database handle.\n");
   1.153      return 1;
   1.154    }
   1.155    if (PQstatus(db) != CONNECTION_OK) {
   1.156 @@ -313,10 +397,10 @@
   1.157    // check initiatives:
   1.158    res = PQexec(db, "SELECT \"initiative_id\", \"final\" FROM \"initiative_suggestion_order_calculation\"");
   1.159    if (!res) {
   1.160 -    fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
   1.161 +    fprintf(stderr, "Error in pqlib while sending SQL command selecting initiatives to process.\n");
   1.162      err = 1;
   1.163    } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
   1.164 -    fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
   1.165 +    fprintf(stderr, "Error while executing SQL command selecting initiatives to process:\n%s", PQresultErrorMessage(res));
   1.166      err = 1;
   1.167      PQclear(res);
   1.168    } else {
   1.169 @@ -329,6 +413,10 @@
   1.170        initiative_id = PQgetvalue(res, i, 0);
   1.171        printf("Processing initiative_id: %s\n", initiative_id);
   1.172        escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
   1.173 +      if (!escaped_initiative_id) {
   1.174 +        fprintf(stderr, "Could not escape literal in memory.\n");
   1.175 +        abort();
   1.176 +      }
   1.177        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) {
   1.178          fprintf(stderr, "Could not prepare query string in memory.\n");
   1.179          abort();
   1.180 @@ -336,21 +424,21 @@
   1.181        res2 = PQexec(db, cmd);
   1.182        free(cmd);
   1.183        if (!res2) {
   1.184 -        fprintf(stderr, "Error in pqlib while sending SQL command selecting open issues\n");
   1.185 +        fprintf(stderr, "Error in pqlib while sending SQL command selecting individual suggestion rankings.\n");
   1.186          err = 1;
   1.187        } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
   1.188 -        fprintf(stderr, "Error while executing SQL command selecting open issues:\n%s", PQresultErrorMessage(res));
   1.189 +        fprintf(stderr, "Error while executing SQL command selecting individual suggestion rankings:\n%s", PQresultErrorMessage(res));
   1.190          err = 1;
   1.191          PQclear(res2);
   1.192        } else if (PQnfields(res2) < 4) {
   1.193 -        fprintf(stderr, "Too few columns returned by SQL command selecting open issues.\n");
   1.194 +        fprintf(stderr, "Too few columns returned by SQL command selecting individual suggestion rankings.\n");
   1.195          err = 1;
   1.196          PQclear(res2);
   1.197        } else {
   1.198          if (PQntuples(res2) == 0) {
   1.199            printf("Nothing to do.\n");
   1.200          } else {
   1.201 -          process_initiative(res2);
   1.202 +          if (process_initiative(db, res2, escaped_initiative_id)) err = 1;
   1.203          }
   1.204          PQclear(res2);
   1.205        }
   1.206 @@ -361,6 +449,8 @@
   1.207  
   1.208    // cleanup and exit
   1.209    PQfinish(db);
   1.210 +  if (!err) printf("Successfully terminated.\n");
   1.211 +  else fprintf(stderr, "Exiting with error code #%i.\n", err);
   1.212    return err;
   1.213  
   1.214  }

Impressum / About Us