def update_ts_temp_file(cache_dir, connection, id): full_rewrite = False afilename = os.path.join(cache_dir, '%d.hts' % (id, )) if os.path.exists(afilename): if os.path.getsize(afilename) < 3: full_rewrite = True #Update the file in the case of logged data, if this is possible if os.path.exists(afilename) and not full_rewrite: with ropen(afilename) as fileobject: line = fileobject.readline() lastdate = datetime_from_iso(line.split(',')[0]) ts = Timeseries(id) ts.read_from_db(connection, bottom_only=True) if len(ts) > 0: db_start, db_end = ts.bounding_dates() if db_start > lastdate: full_rewrite = True elif db_end > lastdate: lastindex = ts.index(lastdate) with open(afilename, 'a') as fileobject: ts.write(fileobject, start=ts.keys()[lastindex + 1]) #Check for tmmp file or else create it if not os.path.exists(afilename) or full_rewrite: ts = Timeseries(id) ts.read_from_db(connection) if not os.path.exists(cache_dir): os.mkdir(cache_dir) tempfile_handle, tempfile_name = tempfile.mkstemp(dir=cache_dir) with os.fdopen(tempfile_handle, 'w') as afile: ts.write(afile) shutil.move(tempfile_name, afilename)
def testUploadTsData(self): self.assert_(self.client.login(username='******', password='******')) response = self.client.put( "/api/tsdata/1/", encode_multipart(BOUNDARY, {'timeseries_records': '2012-11-06 18:17,20,\n'}), content_type=MULTIPART_CONTENT) t = Timeseries(1) t.read_from_db(connection) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, '1') self.assertEqual(len(t), 1) self.assertEqual(t.items(0)[0], datetime(2012, 11, 06, 18, 17, 0)) self.assertEqual(t.items(0)[1], 20) self.client.logout() # Append two more records self.assert_(self.client.login(username='******', password='******')) response = self.client.put( "/api/tsdata/1/", encode_multipart( BOUNDARY, { 'timeseries_records': '2012-11-06 18:18,21,\n2012-11-07 18:18,23,\n' }), content_type=MULTIPART_CONTENT) t = Timeseries(1) t.read_from_db(connection) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, '2') self.assertEqual(len(t), 3) self.assertEqual(t.items(0)[0], datetime(2012, 11, 06, 18, 17, 0)) self.assertEqual(t.items(0)[1], 20) self.assertEqual(t.items(1)[0], datetime(2012, 11, 06, 18, 18, 0)) self.assertEqual(t.items(1)[1], 21) self.assertEqual(t.items(2)[0], datetime(2012, 11, 07, 18, 18, 0)) self.assertEqual(t.items(2)[1], 23) self.client.logout() # Try to append an earlier record; should fail self.assert_(self.client.login(username='******', password='******')) response = self.client.put( "/api/tsdata/1/", encode_multipart(BOUNDARY, {'timeseries_records': '2012-11-05 18:18,21,\n'}), content_type=MULTIPART_CONTENT) self.client.logout() t = Timeseries(1) t.read_from_db(connection) self.assertEqual(response.status_code, 409) self.assertEqual(len(t), 3) self.client.logout()
def get(self, request, pk, format=None): ts = Timeseries(id=int(pk)) self.check_object_permissions(request, ts) ts.read_from_db(connection) result = StringIO() ts.write(result) return HttpResponse(result.getvalue(), content_type="text/plain")
def testUploadTsDataUnauthenticated(self): # Attempt to upload some timeseries data, unauthenticated response = self.client.put( "/api/tsdata/1/", encode_multipart(BOUNDARY, {'timeseries_records': '2012-11-06 18:17,20,\n'}), content_type=MULTIPART_CONTENT) t = Timeseries(1) t.read_from_db(connection) self.assertEqual(response.status_code, 403) self.assertEqual(len(t), 0)
def testUploadTsDataGarbage(self): self.assert_(self.client.login(username='******', password='******')) response = self.client.put( "/api/tsdata/1/", encode_multipart(BOUNDARY, {'timeseries_records': '2012-aa-06 18:17,20,\n'}), content_type=MULTIPART_CONTENT) t = Timeseries(1) t.read_from_db(connection) self.assertEqual(response.status_code, 400) self.assertEqual(len(t), 0) self.client.logout()
def calculate_cost_timeseries(consumption_timeseries, rate=CUBIC_METER_FLAT_RATE): timestep_kwargs = dict([ (attr, getattr(consumption_timeseries.time_step, attr)) for attr in ('length_minutes', 'length_months', 'interval_type', 'nominal_offset', 'actual_offset')]) time_step = TimeStep(**timestep_kwargs) result = Timeseries(time_step = time_step) for timestamp in consumption_timeseries: result[timestamp] = monthly_cost_from_consumption( consumption_timeseries[timestamp], rate=rate) return result
def testUploadTsDataAsWrongUser(self): # Attempt to upload some timeseries data as user 2; should deny self.assert_(self.client.login(username='******', password='******')) response = self.client.put( "/api/tsdata/1/", encode_multipart(BOUNDARY, {'timeseries_records': '2012-11-06 18:17,20,\n'}), content_type=MULTIPART_CONTENT) t = Timeseries(1) t.read_from_db(connection) self.assertEqual(response.status_code, 403) self.assertEqual(len(t), 0) self.client.logout()
def put(self, request, pk, format=None): try: ts = Timeseries(id=int(pk)) self.check_object_permissions(request, ts) result_if_error = status.HTTP_400_BAD_REQUEST ts.read(StringIO(request.DATA['timeseries_records'])) result_if_error = status.HTTP_409_CONFLICT ts.append_to_db(connection, commit=False) return HttpResponse(str(len(ts)), content_type="text/plain") except ValueError as e: return HttpResponse(status=result_if_error, content=str(e), content_type="text/plain")
def _load_daily_series_data(ts_daily): time_step = ReadTimeStep(ts_daily.id, ts_daily) timeseries = Timeseries(time_step=time_step, id=ts_daily.id) timeseries.read_from_db(connection) return timeseries
def readseries(self,timeseries): time_step = ReadTimeStep(timeseries.id, timeseries) timeseries = Timeseries(time_step=time_step,id=timeseries.id) timeseries.read_from_db(connection) return timeseries.items()