/Users/maurits/Documents/studie/afstuderen/biosphere/modules/stat/stat_functions.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: may 4, 2007
00004  * 
00005  * Function:
00006  * Statistics related functions for the stat module.
00007  * 
00008  * License information:
00009  * 
00010  * Copyright (c) 2006 Maurits Hartman
00011  *
00012  * Permission is hereby granted, free of charge, to any person
00013  * obtaining a copy of this software and associated documentation
00014  * files (the "Software"), to deal in the Software without
00015  * restriction, including without limitation the rights to use,
00016  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following
00019  * conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00026  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00028  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00029  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00031  * OTHER DEALINGS IN THE SOFTWARE.
00032  * 
00033  */
00034 
00035 #include "stat_functions.h"
00036 #include "general.h"
00037 #include "time_functions.h"
00038 #include <list.h>
00039 #include <str.h>
00040 #include <type.h>
00041 
00042 #include <apr_strings.h>
00043 #include <apr_time.h>
00044 
00045 
00046 extern bs_list *timers;
00047 extern apr_pool_t *pool;
00048 
00055 static bs_status bsmod_stat_handle_avgminmax(
00056                 const bs_service_request *request,
00057                 bs_service_response **response)
00058 {
00059         unsigned i;
00060         char *timer_id, *data = NULL;
00061         
00062         /* Retrieve the argument: */
00063         if (request->input->num_parts != 1 ||
00064                         request->input->parts[0] == NULL) return BS_ERROR;
00065         timer_id = request->input->parts[0]->data;
00066         
00067         /* Retrieve the timer data: */
00068         lock_bsmod_stat_mux();
00069         for (i = 0; i < list_size(timers); i++) {
00070                 timer_info *ti = (timer_info *) list_index(timers, i);
00071                 if (streq(ti->timer_id, timer_id)) {
00072                         data = ti->gathered_times;
00073                         break;
00074                 }       
00075         }
00076         
00077         /* Create the response: */
00078         if (i >= list_size(timers)) {
00079                 /* Timer_id unknown: return fault message: */
00080                 *response = create_error_response(request, 1, 
00081                                 "The provided timer_id is unknown.", pool);
00082         }
00083         else if (data == NULL) {
00084                 *response = create_error_response(request, 2, 
00085                                 "No data yet for provided timer_id.", pool);
00086         }
00087         else {
00088                 bs_message_instance *output;
00089                 bs_part_instance *part_avg, *part_min, *part_max;
00090                 bs_data_type *type;
00091                 apr_time_t avg = 0, min = apr_time_now(), max = 0;
00092                 bs_list *times;
00093                 unsigned i;
00094                 char *avg_data, *min_data, *max_data;
00095                 
00096                 /* Calculate average, min and max: */
00097                 times = new_list(pool);
00098                 strtokenize(times, data, NULL);
00099                 for (i = 0; i < list_size(times); i++) {
00100                         char *end;
00101                         apr_time_t val;
00102                         
00103                         val = apr_strtoi64(list_index(times, i), &end, 0);
00104                         if (val <= min) min = val;
00105                         if (val >= max) max = val;
00106                         avg += val;
00107                 }
00108                 if (list_size(times) == 0) avg = min = max = 0;
00109                 else avg /= list_size(times);
00110                 avg_data = apr_psprintf(pool, "%" BS_UINT64_FMT, avg);
00111                 min_data = apr_psprintf(pool, "%" BS_UINT64_FMT, min);
00112                 max_data = apr_psprintf(pool, "%" BS_UINT64_FMT, max);
00113                 
00114                 /* Create response message */
00115                 type = new_bs_data_type(pool, "bsmod_stat_time");
00116                 part_avg = new_bs_part_instance(pool, "average", type,
00117                                 avg_data, strlen(avg_data));
00118                 part_min = new_bs_part_instance(pool, "min", type,
00119                                 min_data, strlen(min_data));
00120                 part_max = new_bs_part_instance(pool, "max", type,
00121                                 max_data, strlen(max_data));
00122                 output = new_bs_message_instance(pool, "BSModStatAvgMinMaxOutput", 3);
00123                 output->parts[0] = part_avg;
00124                 output->parts[1] = part_min;
00125                 output->parts[2] = part_max;
00126                 *response = new_bs_service_response(pool, request->uuid,
00127                                 request->service, request->port, request->operation,
00128                                 output, NULL);
00129         }
00130         unlock_bsmod_stat_mux();
00131         return BS_OK;
00132 }
00133 
00134 
00135 bs_status bsmod_stat_handle_stat_functions(
00136                 const bs_service_request *request,
00137                 bs_service_response **response)
00138 {
00139         if (streq(request->operation, "avgminmax"))
00140                 return bsmod_stat_handle_avgminmax(request, response);
00141         return BS_NO_SERVICE;
00142 }
00143 

Generated on Tue Jul 17 09:50:53 2007 for Bio-SPHERE by  doxygen 1.5.1