Skip to content

Commit

Permalink
FIX query filter, toDate +14 days, datetime and int and W
Browse files Browse the repository at this point in the history
  • Loading branch information
quimnuss committed Jun 10, 2021
1 parent 438cea4 commit 662e327
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ORM/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ class ForecastMetadata(database.Entity):

@classmethod
def create(cls, plant, forecastdate, errorcode, variable='prod', predictor='aggregated', granularity='60'):
if errorcode:
if errorcode != 'OK':
return ForecastMetadata(
plant=plant,
errorcode=errorcode,
Expand Down
10 changes: 5 additions & 5 deletions meteologica/forecasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def getForecast(api, facility):
lastDownload = plant.lastForecastDownloaded()

now = dt.datetime.now(dt.timezone.utc)
toDate = now

if not lastDownload:
fromDate = now - dt.timedelta(days=14)
Expand All @@ -75,6 +74,8 @@ def getForecast(api, facility):
else:
fromDate = lastDownload

toDate = now + dt.timedelta(days=14)

try:
meterDataForecast = api.getForecast(facility, fromDate, toDate)
status = ForecastStatus.OK
Expand Down Expand Up @@ -160,7 +161,6 @@ def downloadMeterForecasts(configdb, test_env=True):
return statuses

def getMeterReadings(facility, fromDate=None, toDate=None):
toDate = toDate or dt.datetime.now(dt.timezone.utc)
plant = Plant.get(codename=facility)
if not plant:
logger.warning("Plant codename {} is unknown to db".format(facility))
Expand All @@ -172,9 +172,9 @@ def getMeterReadings(facility, fromDate=None, toDate=None):

query = orm.select(r for r in MeterRegistry if r.meter == meter)
if fromDate:
query.filter(lambda r: fromDate <= r.time)
query = query.filter(lambda r: fromDate <= r.time)
if toDate:
query.filter(lambda r: r.time <= toDate)
query = query.filter(lambda r: r.time <= toDate)

data = [(r.time, r.export_energy_wh) for r in query]

Expand All @@ -191,7 +191,7 @@ def getMeterReadingsFromLastUpload(api, facility):
else:
# TODO refactor this undo the hour shift due to api understanding start-hours instead of end-hours (see below @101)
fromDate = lastUploadDT + dt.timedelta(hours=1)
meterReadings = getMeterReadings(facility, fromDate)
meterReadings = getMeterReadings(facility=facility, fromDate=fromDate)

if not meterReadings:
logger.warning("No meter readings for facility {} since {}".format(facility, lastUploadDT))
Expand Down
6 changes: 4 additions & 2 deletions meteologica/meteologica_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def uploadProduction(self, facility, data):
self._checkFacility(facility)
facility_data = self._data.setdefault(facility, [])
facility_data += data
return 'OK'

def downloadProduction(self, facility, fromDate, toDate, variableId='prod',
predictorId='aggregated', granularity='60', forecastDate=None):
Expand Down Expand Up @@ -155,17 +156,18 @@ def withinSession(f, self, *args, **kwds):

@withinSession
def uploadProduction(self, facility, data):

response = self._client.service.setObservation(dict(
header = self._session.header,
facilityId = facility,
variableId = 'prod',
measurementType ='MEAN',
measurementTime = 60, # minutes
unit = 'kW',
unit = 'W',
observationData = dict(item=[
dict(
startTime=startTime,
data=str(value),
data=value,
)
for startTime, value in data
]),
Expand Down
10 changes: 6 additions & 4 deletions meteologica/test_forecasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,12 @@ def test__downloadMeterForecasts__newForecasts(self):

self.assertNotEqual(forecastMetadata, oldForecastMetadata)
self.assertEqual(statuses['SomEnergia_Alcolea'], ForecastStatus.OK)
forecasts = [f for f in forecastMetadata.forecasts]
expectedTime = time.replace(minute=0, second=0, microsecond=0) + datetime.timedelta(hours=1)
self.assertEqual(len(forecasts), 1)
self.assertEqual(forecasts[0].time, expectedTime)
forecasts = orm.select(f for f in forecastMetadata.forecasts).order_by(lambda: f.time)[:]
print(forecasts)
expectedTime = time.replace(minute=0, second=0, microsecond=0) + datetime.timedelta(days=14, hours=1)
expectedNumForecasts = 14*24+1
self.assertEqual(len(forecasts), expectedNumForecasts)
self.assertEqual(forecasts[-1].time, expectedTime)

def test__getMeterReadings__None(self):

Expand Down
9 changes: 5 additions & 4 deletions meteologica/test_meteologica_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ def createApi(self):
def mainFacility(self):
return "MyPlant"

def otherFacility(slf):
def otherFacility(self):
return "OtherPlant"

def test_uploadProduction_singleData(self):
facility = self.mainFacility()
api = self.createApi()
api.uploadProduction(facility, [
(todt("2040-01-01 00:00:00"), 10),
response = api.uploadProduction(facility, [
(todtaware("2040-01-01 00:00:00"), 10),
])
self.assertEqual(response, 'OK')
self.assertEqual(
api.lastDateUploaded(facility),
todt("2040-01-01 00:00:00")
todtaware("2040-01-01 00:00:00")
)

def test_uploadProduction_noData(self):
Expand Down

0 comments on commit 662e327

Please sign in to comment.