Add usleep to strain.c just in case...
[matches/MCTX3420.git] / server / data.c
index 254b7bc..19828bc 100644 (file)
@@ -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,9 +13,8 @@
 void Data_Init(DataFile * df)
 {
        // Everything is NULL
-       df->filename = NULL;
-       df->read_file = NULL;
-       df->write_file = NULL;
+       memset(df, 0, sizeof(DataFile));
+       pthread_mutex_init(&(df->mutex), NULL);
 }
 
 /**
@@ -29,21 +28,14 @@ void Data_Open(DataFile * df, const char * filename)
        assert(df != NULL);
 
        // Set the filename
-       df->filename = filename;
+       df->filename = strdup(filename);
 
        // Set number of DataPoints
        df->num_points = 0; 
-       
-       // Set read FILE*
-       df->read_file = fopen(filename, "r");
-       if (df->read_file == NULL)
-       {
-               Fatal("Error opening DataFile %s - %s", filename, strerror(errno));
-       }
 
-       // Set write FILE*
-       df->write_file = fopen(filename, "w");
-       if (df->write_file == NULL)
+       // Set file pointer
+       df->file = fopen(filename, "wb+");
+       if (df->file == NULL)
        {
                Fatal("Error opening DataFile %s - %s", filename, strerror(errno));
        }
@@ -59,11 +51,13 @@ void Data_Close(DataFile * df)
 
        //TODO: Write data to TSV?
 
+       fclose(df->file);
+
        // Clear the FILE*s
-       df->read_file = NULL;
-       df->write_file = NULL;
+       df->file = NULL;
 
        // Clear the filename
+       free(df->filename);
        df->filename = NULL;
 }
 
@@ -75,18 +69,19 @@ void Data_Close(DataFile * df)
  */
 void Data_Save(DataFile * df, DataPoint * buffer, int amount)
 {
+       pthread_mutex_lock(&(df->mutex));
        assert(df != NULL);
        assert(buffer != NULL);
        assert(amount >= 0);
 
        // Go to the end of the file
-       if (fseek(df->write_file, 0, SEEK_END) < 0)
+       if (fseek(df->file, 0, SEEK_END) < 0)
        {
                Fatal("Error seeking to end of DataFile %s - %s", df->filename, strerror(errno));
        }
 
        // Attempt to write the DataPoints
-       int amount_written = fwrite(buffer, sizeof(DataPoint), amount, df->write_file);
+       int amount_written = fwrite(buffer, sizeof(DataPoint), amount, df->file);
        
        // Check if the correct number of points were written
        if (amount_written != amount)
@@ -95,10 +90,9 @@ void Data_Save(DataFile * df, DataPoint * buffer, int amount)
        }
 
        // Update number of DataPoints
-       pthread_mutex_lock(&(df->mutex));
-               df->num_points += amount_written;
+       df->num_points += amount_written;
+
        pthread_mutex_unlock(&(df->mutex));
-       
 }
 
 /**
@@ -111,88 +105,91 @@ void Data_Save(DataFile * df, DataPoint * buffer, int amount)
  */
 int Data_Read(DataFile * df, DataPoint * buffer, int index, int amount)
 {
+       pthread_mutex_lock(&(df->mutex));
+
        assert(df != NULL);
        assert(buffer != NULL);
        assert(index >= 0);
        assert(amount > 0);
        
        // If we would read past the end of the file, reduce the amount of points to read
-       pthread_mutex_lock(&(df->mutex));
-               if (index + amount >= df->num_points)
-               {
-                       Log(LOGDEBUG, "Requested %d points but will only read %d to get to EOF", amount, df->num_points - index);
-                       amount = df->num_points - index;
-               }
-       pthread_mutex_unlock(&(df->mutex));
+       
+       if (index + amount > df->num_points)
+       {
+               Log(LOGDEBUG, "Requested %d points but will only read %d to get to EOF (%d)", amount, df->num_points - index, df->num_points);
+               amount = df->num_points - index;
+       }
+       
 
        // Go to position in file
-       if (fseek(df->read_file, index*sizeof(DataPoint), SEEK_SET))
+       if (fseek(df->file, index*sizeof(DataPoint), SEEK_SET))
        {
                Fatal("Error seeking to position %d in DataFile %s - %s", index, df->filename, strerror(errno));
        }
 
        // Attempt to read the DataPoints
-       int amount_read = fread(buffer, sizeof(DataPoint), amount, df->read_file);
+       int amount_read = fread(buffer, sizeof(DataPoint), amount, df->file);
 
        // Check if correct number of points were read
        if (amount_read != amount)
        {
-               Fatal("Read %d points instead of %d from DataFile %s - %s", amount_read, amount, df->filename, strerror(errno));
+               Log(LOGERR,"Read %d points instead of %d from DataFile %s - %s", amount_read, amount, df->filename, strerror(errno));
        }
 
-       return amount;
+       pthread_mutex_unlock(&(df->mutex));
+       return amount_read;
 }
 
 /**
  * Print data points between two indexes using a given format
  * @param df - DataFile to print
  * @param start_index - Index to start at (inclusive)
- * @param end_index - Index to end at (inclusive)
+ * @param end_index - Index to end at (exclusive)
  * @param format - The format to use
  */
-void Data_Print(DataFile * df, int start_index, int end_index, DataFormat format)
+void Data_PrintByIndexes(DataFile * df, int start_index, int end_index, DataFormat format)
 {
        assert(df != NULL);
        assert(start_index >= 0);
-       assert(end_index <= df->num_points-1);
+       assert(end_index >= 0);
+       assert(end_index <= df->num_points || df->num_points == 0);
 
        const char * fmt_string; // Format for each data point
-       char seperator; // Character used to seperate successive data points
+       char separator; // Character used to seperate successive data points
        
-       // Determine what format string and seperator character to use
+       // Determine what format string and separator character to use
        switch (format)
        {
                case JSON:
-                       fmt_string = "[%f,%f]";
-                       seperator = ',';
+                       fmt_string = "[%.9f,%f]";
+                       separator = ',';
                        // For JSON we need an opening bracket
                        FCGI_PrintRaw("["); 
                        break;
                case TSV:
-                       fmt_string = "%f\t%f";
-                       seperator = '\n';
+                       fmt_string = "%.9f\t%f";
+                       separator = '\n';
                        break;
        }
 
-       DataPoint buffer[DATA_BUFSIZ]; // Buffer
-
+       DataPoint buffer[DATA_BUFSIZ] = {{0}}; // Buffer
        int index = start_index;
 
        // Repeat until all DataPoints are printed
-       while (index <= end_index)
+       while (index < end_index)
        {
                // Fill the buffer from the DataFile
                int amount_read = Data_Read(df, buffer, index, DATA_BUFSIZ);
 
                // Print all points in the buffer
-               for (int i = 0; i < amount_read; ++i)
+               for (int i = 0; i < amount_read && index < end_index; ++i)
                {
                        // Print individual DataPoint
                        FCGI_PrintRaw(fmt_string, buffer[i].time_stamp, buffer[i].value);
                        
-                       // Last seperator is not required
+                       // Last separator is not required
                        if (index+1 < end_index)
-                               FCGI_PrintRaw("%c", seperator);
+                               FCGI_PrintRaw("%c", separator);
 
                        // Advance the position in the DataFile
                        ++index;
@@ -215,33 +212,26 @@ void Data_Print(DataFile * df, int start_index, int end_index, DataFormat format
  * Prints nothing if the time stamp
  * @param df - DataFile to print
  * @param start_time - Time to start from (inclusive)
- * @param end_time - Time to end at (inclusive)
+ * @param end_time - Time to end at (exclusive)
  * @param format - The format to use
  */
-void Data_PrintTimes(DataFile * df, double start_time, double end_time, DataFormat format)
+void Data_PrintByTimes(DataFile * df, double start_time, double end_time, DataFormat format)
 {
        assert(df != NULL);
-       assert(start_time > 0);
-       assert(end_time > 0);
-       assert(end_time > start_time);
-       
-       DataPoint closest;
-
-       // Get starting index
-       int start_index = Data_FindByTime(df, start_time, &closest);
-
-       // Start time is greater than most recent time stamp
-       if (start_index >= df->num_points-1 && closest.time_stamp < start_time)
+       //Clamp boundaries
+       if (start_time < 0)
+               start_time = 0;
+       if (end_time < 0)
+               end_time = 0;
+
+       int start_index = 0, end_index = 0;
+       if (start_time < end_time)
        {
-               Data_Print(df, 0, 0, format); // Will print "empty" dataset
-               return;
+               start_index = Data_FindByTime(df, start_time, NULL);
+               end_index = Data_FindByTime(df, end_time, NULL);
        }
 
-       // Get finishing index
-       int end_index = Data_FindByTime(df, end_time, &closest);
-
-       // Print data between the indexes
-       Data_Print(df, start_index, end_index, format);
+       Data_PrintByIndexes(df, start_index, end_index, format);
 }
 
 /**
@@ -255,7 +245,7 @@ int Data_FindByTime(DataFile * df, double time_stamp, DataPoint * closest)
 {
        assert(df != NULL);
        assert(time_stamp >= 0);
-       assert(closest != NULL);
+       //assert(closest != NULL);
 
        DataPoint tmp; // Current DataPoint in binary search
 
@@ -295,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]);
+}

UCC git Repository :: git.ucc.asn.au