X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fdata.c;h=19828bc0492b4c5e93719d06f1e9f3f348ec04f4;hb=6bc90047ed36b392d90a1bf778baf9687b835f2d;hp=c4d7b584302ba9f786ad4dd21c3550d136d54931;hpb=1a311e29d6b72830d62e2cba21c1b9e33433cfbf;p=matches%2FMCTX3420.git diff --git a/server/data.c b/server/data.c index c4d7b58..19828bc 100644 --- a/server/data.c +++ b/server/data.c @@ -1,6 +1,6 @@ /** * @file data.c - * @purpose Implementation of data handling functions; saving, loading, displaying, selecting. + * @brief Implementation of data handling functions; saving, loading, displaying, selecting. */ #include "data.h" @@ -13,8 +13,8 @@ void Data_Init(DataFile * df) { // Everything is NULL - df->filename = NULL; - df->file = NULL; + memset(df, 0, sizeof(DataFile)); + pthread_mutex_init(&(df->mutex), NULL); } /** @@ -161,13 +161,13 @@ void Data_PrintByIndexes(DataFile * df, int start_index, int end_index, DataForm switch (format) { case JSON: - fmt_string = "[%f,%f]"; + fmt_string = "[%.9f,%f]"; separator = ','; // For JSON we need an opening bracket FCGI_PrintRaw("["); break; case TSV: - fmt_string = "%f\t%f"; + fmt_string = "%.9f\t%f"; separator = '\n'; break; } @@ -285,3 +285,122 @@ int Data_FindByTime(DataFile * df, double time_stamp, DataPoint * closest) return index; } + +/** + * Helper; handle FCGI response that requires data + * Should be called first. + * @param df - DataFile to access + * @param start - Info about start_time param + * @param end - Info about end_time param + * @param fmt - Info about format param + * @param current_time - Current time + */ +void Data_Handler(DataFile * df, FCGIValue * start, FCGIValue * end, DataFormat format, double current_time) +{ + double start_time = *(double*)(start->value); + double end_time = *(double*)(end->value); + + if (format == JSON) + { + FCGI_JSONKey("data"); + } + + // If a time was specified + if (FCGI_RECEIVED(start->flags) || FCGI_RECEIVED(end->flags)) + { + // Wrap times relative to the current time + if (start_time < 0) + start_time += current_time; + if (end_time < 0) + end_time += current_time; + + // Print points by time range + Data_PrintByTimes(df, start_time, end_time, format); + + } + else // No time was specified; just return a recent set of points + { + pthread_mutex_lock(&(df->mutex)); + int start_index = df->num_points-DATA_BUFSIZ; + int end_index = df->num_points-1; + pthread_mutex_unlock(&(df->mutex)); + + // Bounds check + if (start_index < 0) + start_index = 0; + if (end_index < 0) + end_index = 0; + + // Print points by indexes + Data_PrintByIndexes(df, start_index, end_index, format); + } + +} + +/** + * Helper - Convert human readable format string to DataFormat + * @param fmt - FCGIValue to use + */ +DataFormat Data_GetFormat(FCGIValue * fmt) +{ + const char * fmt_str = *(const char**)(fmt->value); + // Check if format type was specified + if (FCGI_RECEIVED(fmt->flags)) + { + if (strcmp(fmt_str, "json") == 0) + return JSON; + else if (strcmp(fmt_str, "tsv") == 0) + return TSV; + else + Log(LOGERR, "Unknown format type \"%s\"", fmt_str); + } + return JSON; +} + +/** + * Binary search for index of a double in an array + * @param value - The value + * @param x - The array + * @param size - Sizeof the array + */ +int FindClosest(double value, double x[], int size) +{ + int upper = size-1; + int lower = 0; + int index = 0; + while (upper - lower > 1) + { + index = lower + ((upper - lower)/2); + double look = x[index]; + if (look > value) + upper = index; + else if (look < value) + lower = index; + else + return index; + } + + if (x[index] > value && index > 0) + --index; + return index; + +} + +/** + * Get calibrated value by interpolation in array y + * @param value - Raw measured value + * @param x - x values (raw values) of the data + * @param y - calibrated values + * @param size - Number of values in the arrays + * @returns interpolated calibrated value + */ +double Data_Calibrate(double value, double x[], double y[], int size) +{ + int i = FindClosest(value, x, size); + if (i >= size-1) + { + i = size-2; + } + double dist = (value - x[i])/(x[i+1] - x[i]); + return y[i] + dist*(y[i+1]-y[i]); +}