00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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
00078 if (i >= list_size(timers)) {
00079
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
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
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