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->write_file = fopen(filename, "w");
39 if (df->write_file == NULL)
41 Fatal("Error opening DataFile %s - %s", filename, strerror(errno));
45 df->read_file = fopen(filename, "r");
46 if (df->read_file == NULL)
48 Fatal("Error opening DataFile %s - %s", filename, strerror(errno));
56 * @param df - The DataFile to close
58 void Data_Close(DataFile * df)
62 //TODO: Write data to TSV?
66 df->write_file = NULL;
73 * Save DataPoints to a DataFile
74 * @param df - The DataFile to save to
75 * @param buffer - Array of DataPoint(s) to save
76 * @param amount - Number of DataPoints in the buffer
78 void Data_Save(DataFile * df, DataPoint * buffer, int amount)
81 assert(buffer != NULL);
84 // Go to the end of the file
85 if (fseek(df->write_file, 0, SEEK_END) < 0)
87 Fatal("Error seeking to end of DataFile %s - %s", df->filename, strerror(errno));
90 // Attempt to write the DataPoints
91 int amount_written = fwrite(buffer, sizeof(DataPoint), amount, df->write_file);
93 // Check if the correct number of points were written
94 if (amount_written != amount)
96 Fatal("Wrote %d points instead of %d to DataFile %s - %s", amount_written, amount, df->filename, strerror(errno));
99 // Update number of DataPoints
100 pthread_mutex_lock(&(df->mutex));
101 df->num_points += amount_written;
102 pthread_mutex_unlock(&(df->mutex));
107 * Read DataPoints from a DataFile
108 * @param df - The DataFile to read from
109 * @param buffer - Array to fill with DataPoints
110 * @param index - Index to start reading at (inclusive)
111 * @param amount - Maximum number of DataPoints to read
112 * @returns - Actual number of points read (If smaller than amount, the end of the file was reached)
114 int Data_Read(DataFile * df, DataPoint * buffer, int index, int amount)
117 assert(buffer != NULL);
121 // If we would read past the end of the file, reduce the amount of points to read
122 pthread_mutex_lock(&(df->mutex));
123 if (index + amount >= df->num_points)
125 Log(LOGDEBUG, "Requested %d points but will only read %d to get to EOF", amount, df->num_points - index);
126 amount = df->num_points - index;
128 pthread_mutex_unlock(&(df->mutex));
130 // Go to position in file
131 if (fseek(df->read_file, index*sizeof(DataPoint), SEEK_SET))
133 Fatal("Error seeking to position %d in DataFile %s - %s", index, df->filename, strerror(errno));
136 // Attempt to read the DataPoints
137 int amount_read = fread(buffer, sizeof(DataPoint), amount, df->read_file);
139 // Check if correct number of points were read
140 if (amount_read != amount)
142 Fatal("Read %d points instead of %d from DataFile %s - %s", amount_read, amount, df->filename, strerror(errno));
149 * Print data points between two indexes using a given format
150 * @param df - DataFile to print
151 * @param start_index - Index to start at (inclusive)
152 * @param end_index - Index to end at (inclusive)
153 * @param format - The format to use
155 void Data_PrintByIndexes(DataFile * df, int start_index, int end_index, DataFormat format)
158 assert(start_index >= 0);
159 assert(end_index <= df->num_points-1);
161 const char * fmt_string; // Format for each data point
162 char seperator; // Character used to seperate successive data points
164 // Determine what format string and seperator character to use
168 fmt_string = "[%f,%f]";
170 // For JSON we need an opening bracket
174 fmt_string = "%f\t%f";
179 DataPoint buffer[DATA_BUFSIZ]; // Buffer
181 int index = start_index;
183 // Repeat until all DataPoints are printed
184 while (index <= end_index)
186 // Fill the buffer from the DataFile
187 int amount_read = Data_Read(df, buffer, index, DATA_BUFSIZ);
189 // Print all points in the buffer
190 for (int i = 0; i < amount_read; ++i)
192 // Print individual DataPoint
193 FCGI_PrintRaw(fmt_string, buffer[i].time_stamp, buffer[i].value);
195 // Last seperator is not required
196 if (index+1 < end_index)
197 FCGI_PrintRaw("%c", seperator);
199 // Advance the position in the DataFile
207 // For JSON we need a closing bracket
216 * Print data points between two time stamps using a given format.
217 * Prints nothing if the time stamp
218 * @param df - DataFile to print
219 * @param start_time - Time to start from (inclusive)
220 * @param end_time - Time to end at (inclusive)
221 * @param format - The format to use
223 void Data_PrintByTimes(DataFile * df, double start_time, double end_time, DataFormat format)
226 assert(start_time > 0);
227 assert(end_time > 0);
228 assert(end_time > start_time);
232 // Get starting index
233 int start_index = Data_FindByTime(df, start_time, &closest);
235 // Start time is greater than most recent time stamp
236 if (start_index >= df->num_points-1 && closest.time_stamp < start_time)
238 Data_PrintByIndexes(df, 0, 0, format); // Will print "empty" dataset
242 // Get finishing index
243 int end_index = Data_FindByTime(df, end_time, &closest);
245 // Print data between the indexes
246 Data_PrintByIndexes(df, start_index, end_index, format);
250 * Get the index of the DataPoint closest to a given time stamp
251 * @param df - DataFile to search
252 * @param time_stamp - The time stamp to search for
253 * @param closest - If not NULL, will be filled with the DataPoint chosen
254 * @returns index of DataPoint with the *closest* time stamp to that given
256 int Data_FindByTime(DataFile * df, double time_stamp, DataPoint * closest)
259 assert(time_stamp >= 0);
260 assert(closest != NULL);
262 DataPoint tmp; // Current DataPoint in binary search
264 int lower = 0; // lower index in binary search
265 pthread_mutex_lock(&(df->mutex));
266 int upper = df->num_points - 1; // upper index in binary search
267 pthread_mutex_unlock(&(df->mutex));
268 int index = 0; // current index in binary search
270 // Commence binary search:
271 while (upper - lower > 1)
274 index = lower + ((upper - lower)/2);
277 if (Data_Read(df, &tmp, index, 1) != 1)
279 Fatal("Couldn't read DataFile %s at index %d", df->filename, index);
282 // Change search interval to either half appropriately
283 if (tmp.time_stamp > time_stamp)
287 else if (tmp.time_stamp < time_stamp)
293 // Store closest DataPoint