ingest_stats.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 *
00003 *  COPYRIGHT (C) 2006 Battelle Memorial Institute.  All Rights Reserved.
00004 *
00005 ********************************************************************************
00006 *
00007 *  Author:
00008 *     name:  Brian Ermold
00009 *     phone: (509) 375-2277
00010 *     email: brian.ermold@pnl.gov
00011 *
00012 ********************************************************************************
00013 *
00014 *  RCS INFORMATION:
00015 *    $RCSfile: ingest_stats.c,v $
00016 *    $Revision: 1.5 $
00017 *    $Locker:  $
00018 *    $Date: 2006/08/04 02:23:46 $
00019 *    $State: Exp $
00020 *
00021 ********************************************************************************
00022 *
00023 * DESCRIPTION:
00024 *
00025 *   The <b>set_data_stats</b> function should be called when a Zebra Datachunk
00026 *   is stored. Information about the start time, end time, and number of samples
00027 *   in the datachunk is then recorded in a static structure. When the program is
00028 *   exiting it should then use <b>write_data_stats</b> to write all the gather
00029 *   information to the log file using the <b>append_instrlog</b> function. The
00030 *   following lines will be added to the log file for each platform found:
00031 *     <blockquote><p><b>
00032 *       ${Platform Name} Samples: ${Number Of Samples}<br>
00033 *       ${Platform Name} Begin Time: ${Begin Time}<br>
00034 *       ${Platform Name} End Time: ${End_time}</b></p>
00035 *     </blockquote><p>
00036 *   The <b>set_file_stats</b> function should be called after a file has been
00037 *   processed and moved to the ingested directory. When called it will print the
00038 *   following line to the log file using the <b>append_instrlog</b> function:
00039 *     </p><blockquote><p><b>
00040 *       Processed ${filename}</b></p>
00041 *     </blockquote><p>
00042 *   Information is then gathered about this file and stored in a static
00043 *   structure containing the size of the file and wether it was good or bad
00044 *   (based on wether ".bad." was found in the file name or not). When the
00045 *   program is exiting it should then call <b>write_file_stats</b> wich will
00046 *   write all the gathered information to the log file using the
00047 *   <b>append_instrlog</b> function. The following lines will be added to the
00048 *   log file:</p><blockquote><p><b>
00049 *       Total Bytes: ${gFStats.tot_bytes}<br>
00050 *       Good Bytes: ${gFStats.good_bytes}<br>
00051 *       Bad Bytes: ${gFStats.bad_bytes}<br>
00052 *       Total Files: ${gFStats.tot_files}<br>
00053 *       Good Files: ${gFStats.good_files}<br>
00054 *       Bad Files: ${gFStats.bad_files} </b> </p>
00055 *   </blockquote>
00056 *
00057 * END DESCRIPTION
00058 *******************************************************************************/
00059 
00060 static char *rcsid = "$Id: ingest_stats.c,v 1.5 2006/08/04 02:23:46 ermold Exp $";
00061 
00062 #include <ctype.h>
00063 #include <errno.h>
00064 #include <string.h>
00065 #include <sys/stat.h>
00066 
00067 #include "ingest_lib/ingest_stats.h"
00068 #include "ingest_lib/sds_util.h"
00069 #include "ingest_lib/sds_log.h"
00070 #include "ingest_lib/sds_tdb.h"
00071 
00072 /*******************************************************************************
00073 *  Private Variables:
00074 */
00075 
00076 static FileStats gFStats;  /* The structure to store the file stats      */
00077 static DcStats   gDcStats; /* The structure to store the datachunk stats */
00078 
00079 /* End Private Variables */
00080 
00081 /*******************************************************************************
00082 *
00083 * Description:
00084 *   The reset_file_stats function will reset the values to zero of the file
00085 *   stats structure.
00086 *
00087 * Inputs:
00088 *   None.
00089 *
00090 * Outputs:
00091 *   None.
00092 *
00093 * Returns:
00094 *   None.
00095 *
00096 *******************************************************************************/
00097 void reset_file_stats(void)
00098 {
00099     gFStats.tot_bytes = gFStats.good_bytes = gFStats.bad_bytes = 0;
00100     gFStats.tot_files = gFStats.good_files = gFStats.bad_files = 0;
00101 }
00102 
00103 /*******************************************************************************
00104 *
00105 * Description:
00106 *   The set_file_stats function will increment the current file stats with
00107 *   information obtained from the specified file.
00108 *
00109 * Inputs:
00110 *   filename:  The filename for which stats should be added.
00111 *
00112 * Outputs:
00113 *   None.
00114 *
00115 * Returns:
00116 *   None.
00117 *
00118 *******************************************************************************/
00119 void set_file_stats(char *filename)
00120 {
00121     struct stat stat_buf;
00122     off_t       bytes;
00123 
00124     if ((filename == NULL) || (filename[0] == '\0')) {
00125         print_debug(__FILE__, __LINE__,
00126             "No filename was specified in set_file_stats\n");
00127     }
00128     else {
00129         print_debug(__FILE__, __LINE__,
00130             "Getting file stats for: %s\n", filename);
00131 
00132         /*  Appened this filename to the instr_log
00133          */
00134 
00135         append_instrlog("Processed %s\n", filename);
00136 
00137         if (stat(filename, &stat_buf) == 0) {
00138             bytes = stat_buf.st_size;
00139         }
00140         else {
00141             bytes = 0;
00142             append_log_msg(__FILE__, __LINE__,
00143                 "Error #%i getting stats for file: %s\n -> %s\n",
00144                 errno, filename, strerror(errno));
00145         }
00146 
00147         gFStats.tot_files++;
00148         gFStats.tot_bytes += bytes;
00149 
00150         if (strstr(filename, ".bad.")) {
00151             gFStats.bad_files++;
00152             gFStats.bad_bytes += bytes;
00153         }
00154         else {
00155             gFStats.good_files++;
00156             gFStats.good_bytes += bytes;
00157         }
00158     }
00159 }
00160 
00161 /*******************************************************************************
00162 *
00163 * Description:
00164 *   The write_file_stats function will write the current file stats to the
00165 *   log file.
00166 *
00167 * Inputs:
00168 *   None.
00169 *
00170 * Outputs:
00171 *   None.
00172 *
00173 * Returns:
00174 *   None.
00175 *
00176 *******************************************************************************/
00177 void write_file_stats(void)
00178 {
00179     append_instrlog("\nTotal Bytes: %d\n", gFStats.tot_bytes);
00180     append_instrlog("Good Bytes:  %d\n", gFStats.good_bytes);
00181     append_instrlog("Bad Bytes:   %d\n", gFStats.bad_bytes);
00182     append_instrlog("Total Files: %d\n", gFStats.tot_files);
00183     append_instrlog("Good Files:  %d\n", gFStats.good_files);
00184     append_instrlog("Bad Files:   %d\n", gFStats.bad_files);
00185 }
00186 
00187 /*******************************************************************************
00188 *
00189 * Description:
00190 *   The reset_dc_stats function will reset to zero the value of the
00191 *   data stats struct.
00192 *
00193 * Inputs:
00194 *   None.
00195 *
00196 * Outputs:
00197 *   None.
00198 *
00199 * Returns:
00200 *   None.
00201 *
00202 *******************************************************************************/
00203 void reset_dc_stats(void)
00204 {
00205     int i;
00206 
00207     gDcStats.tot_samples = 0;
00208 
00209     for(i = 0; i < MAX_DCS; i++) {
00210         gDcStats.plat_name[i][0]           = '\0';
00211         gDcStats.plat_samples[i]           = 0;
00212         gDcStats.begin_time[i].zt_Sec      = 0;
00213         gDcStats.begin_time[i].zt_MicroSec = 0;
00214         gDcStats.end_time[i].zt_Sec        = 0;
00215         gDcStats.end_time[i].zt_MicroSec   = 0;
00216     }
00217 }
00218 
00219 /*******************************************************************************
00220 *
00221 * Description:
00222 *   The set_dc_stats function will increment the current data stats with
00223 *   information obtained from the specified DataChunk.
00224 *
00225 * Inputs:
00226 *   dc:   The DataChunk for which stats should be added.
00227 *
00228 * Outputs:
00229 *   None.
00230 *
00231 * Returns:
00232 *   None.
00233 *
00234 *******************************************************************************/
00235 void set_dc_stats(DataChunk *dc)
00236 {
00237     int     nsamples;
00238     int     pindex;
00239     ZebTime when;
00240 
00241     if (dc == NULL) {
00242         print_debug(__FILE__, __LINE__,
00243             "No datachunk was specified in set_dc_stats\n");
00244     }
00245     else {
00246 
00247         /*  Get the platform index
00248          */
00249 
00250         pindex = set_pindex(dc);
00251 
00252         /*  Get the number of samples and add this up
00253          */
00254 
00255         nsamples = dc_GetNSample(dc);
00256         gDcStats.tot_samples          += nsamples;
00257         gDcStats.plat_samples[pindex] += nsamples;
00258 
00259         /*  Store the name of the platform
00260          */
00261 
00262         strcpy(gDcStats.plat_name[pindex], ds_PlatformName(dc->dc_Platform));
00263 
00264         /*  Set the first sample time
00265          */
00266 
00267         dc_GetTime(dc, 0, &when);
00268         if ((when.zt_Sec < gDcStats.begin_time[pindex].zt_Sec)
00269             || (gDcStats.begin_time[pindex].zt_Sec == 0)) {
00270             gDcStats.begin_time[pindex] = when;
00271         }
00272 
00273         /*  Set the last sample time
00274          */
00275 
00276         dc_GetTime(dc, nsamples - 1, &when);
00277         if ((when.zt_Sec > gDcStats.end_time[pindex].zt_Sec)
00278             || (gDcStats.end_time[pindex].zt_Sec == 0)) {
00279             gDcStats.end_time[pindex] = when;
00280         }
00281     }
00282 }
00283 
00284 static void parse_datastream_name(char *ds_name,
00285     char *ds_site, char *ds_base, char *ds_fac, char *ds_level)
00286 {
00287     int i, j;
00288     int site_length;
00289 
00290     /* Example datastream name: sgp5ebbrE12.b1 */
00291 
00292     /* Site */
00293 
00294     site_length = (ds_name[0] == 'D') ? 4 : 3;
00295 
00296     for (i = 0; i < site_length; i++) ds_site[i] = ds_name[i];
00297     ds_site[i] = '\0';
00298 
00299     /* Datastream Base Name */
00300 
00301     for (j = 0; !isupper(ds_name[i]); i++, j++) ds_base[j] = ds_name[i];
00302     ds_base[j] = '\0';
00303 
00304     /* Facility */
00305 
00306     for (j = 0; ds_name[i] != '.'; i++, j++) ds_fac[j] = ds_name[i];
00307     ds_fac[j] = '\0';
00308 
00309     /* Datastream Level */
00310 
00311     i++; /* skip the "dot" */
00312 
00313     for (j = 0; ds_name[i] != '\0'; i++, j++) ds_level[j] = ds_name[i];
00314     ds_level[j] = '\0';
00315 }
00316 
00317 
00318 /*******************************************************************************
00319 *
00320 * Description:
00321 *   The write_dc_stats function will write the current data stats to the log file.
00322 *
00323 * Inputs:
00324 *   None.
00325 *
00326 * Outputs:
00327 *   None.
00328 *
00329 * Returns:
00330 *   None.
00331 *
00332 *******************************************************************************/
00333 void write_dc_stats()
00334 {
00335     time_t zt_Sec;
00336     char   ds_site[8];
00337     char   ds_base[32];
00338     char   ds_fac[8];
00339     char   ds_level[8];
00340     int    dcs;
00341 
00342     append_instrlog("\nTotal Samples: %ld\n", gDcStats.tot_samples);
00343 
00344     for(dcs = 0; dcs < MAX_DCS; dcs++) {
00345 
00346         if (gDcStats.plat_samples[dcs]) {
00347             append_instrlog("%s Samples: %d\n", gDcStats.plat_name[dcs],
00348                 gDcStats.plat_samples[dcs]);
00349 
00350             zt_Sec = gDcStats.begin_time[dcs].zt_Sec;
00351 
00352             append_instrlog("%s Begin Time: %s", gDcStats.plat_name[dcs],
00353                 asctime(gmtime(&zt_Sec)));
00354 
00355             zt_Sec = gDcStats.end_time[dcs].zt_Sec;
00356 
00357             append_instrlog("%s End Time:   %s", gDcStats.plat_name[dcs],
00358                 asctime(gmtime(&zt_Sec)));
00359 
00360             parse_datastream_name(gDcStats.plat_name[dcs],
00361                 ds_site, ds_base, ds_fac, ds_level);
00362 
00363             update_datastream_times(
00364                 ds_base,
00365                 ds_level,
00366                 gDcStats.begin_time[dcs].zt_Sec,
00367                 gDcStats.end_time[dcs].zt_Sec);
00368         }
00369     }
00370 }

Generated on Tue Sep 12 20:12:37 2006 for DSUTIL-INGEST_LIB by doxygen 1.3.5