Addes writerel command.
[crypto.git] / apps / silcmap / silcmap_html.c
index 6e3c8577ddecaf6206f2764c62776fc203189165..5c1aa233aac54f15d8a31dcec618450cb290655c 100644 (file)
@@ -460,3 +460,114 @@ bool silc_map_writemaphtml(SilcMap map)
 
   return TRUE;
 }
+
+/* Writes the server uptime reliablity data file. */
+
+bool silc_map_writerel(SilcMap map, SilcMapConnection mapconn)
+{
+  FILE *fp;
+  int try = 0, success = 0;
+  char f[256], *hostname;
+
+  /* Generate data filename */
+  memset(f, 0, sizeof(f));
+  silc_dlist_start(mapconn->hostnames);
+  hostname = silc_dlist_get(mapconn->hostnames);
+  snprintf(f, sizeof(f) - 1, "%s_%d.rel", hostname, mapconn->port);
+
+  /* Read the current data */
+  fp = fopen(f,  "r");
+  if (fp) {
+    fscanf(fp, "%d:%d", &try, &success);
+    fclose(fp);
+  }
+
+  /* Update the data */
+  try++;
+  success = (mapconn->down == FALSE ? success + 1 : success);
+
+  /* Write the data file */
+  fp = fopen(f, "w+");
+  if (!fp) {
+    fprintf(stderr, "Could not open file '%s'\n", map->writerel.filename);
+    return FALSE;
+  }
+  fprintf(fp, "%d:%d", try, success);
+
+  fclose(fp);
+  return TRUE;
+}
+
+/* Writes the servers' uptime reliability graph as HTML page. */
+
+bool silc_map_writerelhtml(SilcMap map)
+{
+  SilcMapConnection mapconn;
+  char *hostname, *class;
+  FILE *fp, *dp;
+
+  /* Open for writing */
+  fp = fopen(map->writerel.filename, "w+");
+  if (!fp) {
+    fprintf(stderr, "Could not open file '%s'\n", map->writerel.filename);
+    return FALSE;
+  }
+
+  /* Produce the reliability graph as HTML file. */
+  class = map->writerel.text ? map->writerel.text : "silcmap";
+
+  fprintf(fp, "<!-- Automatically generated by silcmap --!>\n");
+  fprintf(fp, "<br />\n");
+  fprintf(fp, "<table cellspacing=\"0\" cellpadding=\"0\" "
+         "class=\"%s\" border=\"0\">\n", class);
+  fprintf(fp,
+         "<tr>\n"
+         "<td align=\"center\" class=\"%s_header\"><b>Server</b></td>\n"
+         "<td colspan=\"2\" align=\"center\" class=\"%s_header\"><b>Reliability</b></td>\n"
+         "</tr>\n", class, class);
+
+  silc_dlist_start(map->conns);
+  while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
+    char f[256];
+    int try = 0, success = 0;
+    double rel = 0;
+
+    silc_dlist_start(mapconn->hostnames);
+    hostname = silc_dlist_get(mapconn->hostnames);
+
+    /* Get the data */
+    memset(f, 0, sizeof(f));
+    snprintf(f, sizeof(f) - 1, "%s_%d.rel", hostname, mapconn->port);
+    dp = fopen(f,  "r");
+    if (dp) {
+      fscanf(dp, "%d:%d", &try, &success);
+      fclose(dp);
+    }
+
+    /* Count the reliability */
+    if (try)
+      rel = ((double)success / (double)try) * (double)200.0;
+
+    fprintf(fp, "<tr>\n");
+    if (mapconn->html_url)
+      fprintf(fp,
+             "<td align = \"center\" class=\"%s\">&nbsp;<a href=\"%s\">%s</a></td>\n", class, mapconn->html_url, hostname);
+    else
+      fprintf(fp,
+             "<td align = \"center\" class=\"%s\">&nbsp;<a href=\"%s_%d.html\">%s</a></td>\n", class, hostname, mapconn->port, hostname);
+    fprintf(fp,
+           "<td class=\"%s\" width=\"200\">"
+           "<table style=\"border: solid 1px black; width: 200px;\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"%s\"><tr>"
+           "<td style=\"width: %fpx; height: 10px;\" bgcolor=\"gray\"></td>"
+           "<td style=\"width: %fpx; height: 10px;\" bgcolor=\"white\"></td>"
+           "</tr></table></td>\n"
+           "<td class=\"%s\">%.2f%%</td>\n"
+           "</tr>\n",
+           class, class, rel, 200 - rel, class,
+           ((double)success / (double)try) * (double)100.0);
+  }
+
+  fprintf(fp, "</table><br />\n");
+
+  return TRUE;
+}