3 * @purpose Implementation of data handling functions; saving, loading, displaying, selecting.
7 #include <assert.h> //TODO: Remove asserts
10 * One off initialisation of DataFile
11 * @param df - The DataFile
13 void Data_Init(DataFile * df)
18 df->write_file = NULL;
22 * Initialise a DataFile from a filename; opens read/write FILE*
23 * @param df - DataFile to initialise
24 * @param filename - Name of file; overwritten if it exists
26 void Data_Open(DataFile * df, const char * filename)
28 assert(filename != NULL);
32 df->filename = filename;
34 // Set number of DataPoints
38 df->read_file = fopen(filename, "r");
39 if (df->read_file == NULL)
41 Fatal("Error opening DataFile %s - %s", filename, strerror(errno));
45 df->write_file = fopen(filename, "w");
46 if (df->write_file == NULL)
48 Fatal("Error opening DataFile %s - %s", filename, strerror(errno));
54 * @param df - The DataFile to close
56 void Data_Close(DataFile * df)
60 //TODO: Write data to TSV?
64 df->write_file = NULL;
71 * Save DataPoints to a DataFile
72 * @param df - The DataFile to save to
73 * @param buffer - Array of DataPoint(s) to save
74 * @param amount - Number of DataPoints in the buffer
76 void Data_Save(DataFile * df, DataPoint * buffer, int amount)
79 assert(buffer != NULL);
82 // Go to the end of the file
83 if (fseek(df->write_file, 0, SEEK_END) < 0)
85 Fatal("Error seeking to end of DataFile %s - %s", df->filename, strerror(errno));
88 // Attempt to write the DataPoints
89 int amount_written = fwrite(buffer, sizeof(DataPoint), amount, df->write_file);
91 // Check if the correct number of points were written
92 if (amount_written != amount)
94 Fatal("Wrote %d points instead of %d to DataFile %s - %s", amount_written, amount, df->filename, strerror(errno));
97 // Update number of DataPoints
98 pthread_mutex_lock(&(df->mutex));
99 df->num_points += amount_written;
100 pthread_mutex_unlock(&(df->mutex));
105 * Read DataPoints from a DataFile
106 * @param df - The DataFile to read from
107 * @param buffer - Array to fill with DataPoints
108 * @param index - Index to start reading at (inclusive)
109 * @param amount - Maximum number of DataPoints to read
110 * @returns - Actual number of points read (If smaller than amount, the end of the file was reached)
112 int Data_Read(DataFile * df, DataPoint * buffer, int index, int amount)
115 assert(buffer != NULL);
119 // If we would read past the end of the file, reduce the amount of points to read
120 pthread_mutex_lock(&(df->mutex));
121 if (index + amount >= df->num_points)
123 Log(LOGDEBUG, "Requested %d points but will only read %d to get to EOF", amount, df->num_points - index);
124 amount = df->num_points - index;
126 pthread_mutex_unlock(&(df->mutex));
128 // Go to position in file
129 if (fseek(df->read_file, index*sizeof(DataPoint), SEEK_SET))
131 Fatal("Error seeking to position %d in DataFile %s - %s", index, df->filename, strerror(errno));
134 // Attempt to read the DataPoints
135 int amount_read = fread(buffer, sizeof(DataPoint), amount, df->read_file);
137 // Check if correct number of points were read
138 if (amount_read != amount)
140 Fatal("Read %d points instead of %d from DataFile %s - %s", amount_read, amount, df->filename, strerror(errno));
147 * Print data points between two indexes using a given format
148 * @param df - DataFile to print
149 * @param start_index - Index to start at (inclusive)
150 * @param end_index - Index to end at (inclusive)
151 * @param format - The format to use
153 void Data_Print(DataFile * df, int start_index, int end_index, DataFormat format)
156 assert(start_index >= 0);
157 assert(end_index <= df->num_points-1);
159 const char * fmt_string; // Format for each data point
160 char seperator; // Character used to seperate successive data points
162 // Determine what format string and seperator character to use
166 fmt_string = "[%f,%f]";
168 // For JSON we need an opening bracket
172 fmt_string = "%f\t%f";
177 DataPoint buffer[DATA_BUFSIZ]; // Buffer
179 int index = start_index;
181 // Repeat until all DataPoints are printed
182 while (index <= end_index)
184 // Fill the buffer from the DataFile
185 int amount_read = Data_Read(df, buffer, index, DATA_BUFSIZ);
187 // Print all points in the buffer
188 for (int i = 0; i < amount_read; ++i)
190 // Print individual DataPoint
191 FCGI_PrintRaw(fmt_string, buffer[i].time_stamp, buffer[i].value);
193 // Last seperator is not required
194 if (index+1 < end_index)
195 FCGI_PrintRaw("%c", seperator);
197 // Advance the position in the DataFile
205 // For JSON we need a closing bracket
214 * Print data points between two time stamps using a given format.
215 * Prints nothing if the time stamp
216 * @param df - DataFile to print
217 * @param start_time - Time to start from (inclusive)
218 * @param end_time - Time to end at (inclusive)
219 * @param format - The format to use
221 void Data_PrintTimes(DataFile * df, double start_time, double end_time, DataFormat format)
224 assert(start_time > 0);
225 assert(end_time > 0);
226 assert(end_time > start_time);
230 // Get starting index
231 int start_index = Data_FindByTime(df, start_time, &closest);
233 // Start time is greater than most recent time stamp
234 if (start_index >= df->num_points-1 && closest.time_stamp < start_time)
236 Data_Print(df, 0, 0, format); // Will print "empty" dataset
240 // Get finishing index
241 int end_index = Data_FindByTime(df, end_time, &closest);
243 // Print data between the indexes
244 Data_Print(df, start_index, end_index, format);
248 * Get the index of the DataPoint closest to a given time stamp
249 * @param df - DataFile to search
250 * @param time_stamp - The time stamp to search for
251 * @param closest - If not NULL, will be filled with the DataPoint chosen
252 * @returns index of DataPoint with the *closest* time stamp to that given
254 int Data_FindByTime(DataFile * df, double time_stamp, DataPoint * closest)
257 assert(time_stamp >= 0);
258 assert(closest != NULL);
260 DataPoint tmp; // Current DataPoint in binary search
262 int lower = 0; // lower index in binary search
263 pthread_mutex_lock(&(df->mutex));
264 int upper = df->num_points - 1; // upper index in binary search
265 pthread_mutex_unlock(&(df->mutex));
266 int index = 0; // current index in binary search
268 // Commence binary search:
269 while (upper - lower > 1)
272 index = lower + ((upper - lower)/2);
275 if (Data_Read(df, &tmp, index, 1) != 1)
277 Fatal("Couldn't read DataFile %s at index %d", df->filename, index);
280 // Change search interval to either half appropriately
281 if (tmp.time_stamp > time_stamp)
285 else if (tmp.time_stamp < time_stamp)
291 // Store closest DataPoint