Commit a5ee1d20 authored by Tatuya Kamada's avatar Tatuya Kamada

Invenotory: Change to use inventory cache even if we do not specify the date.

This is not a new feature, this behavior is compatible with the old caching
implementation that was using inventory_stock table.
In other words, this brings backs the backword compatibility in invetory
caching.
parent d8289e10
...@@ -60,8 +60,10 @@ FROM \n ...@@ -60,8 +60,10 @@ FROM \n
inventory_cache \n inventory_cache \n
WHERE \n WHERE \n
inventory_cache.query=<dtml-sqlvar query type="string"> \n inventory_cache.query=<dtml-sqlvar query type="string"> \n
<dtml-if date>\n
AND \n AND \n
inventory_cache.date <= <dtml-sqlvar date type="datetime"> \n inventory_cache.date <= <dtml-sqlvar date type="datetime">\n
</dtml-if>\n
ORDER BY \n ORDER BY \n
date DESC date DESC
......
13 14
\ No newline at end of file \ No newline at end of file
...@@ -1303,7 +1303,6 @@ class SimulationTool(BaseTool): ...@@ -1303,7 +1303,6 @@ class SimulationTool(BaseTool):
# Get cached data # Get cached data
if getattr(self, "Resource_zGetInventoryCacheResult", None) is not None and \ if getattr(self, "Resource_zGetInventoryCacheResult", None) is not None and \
optimisation__ and (not kw.get('from_date')) and \ optimisation__ and (not kw.get('from_date')) and \
(bool(kw.get("at_date")) ^ bool(kw.get("to_date"))) and \
'transformed_resource' not in kw: 'transformed_resource' not in kw:
# Here is the different kind of date # Here is the different kind of date
# from_date : >= # from_date : >=
...@@ -1314,7 +1313,7 @@ class SimulationTool(BaseTool): ...@@ -1314,7 +1313,7 @@ class SimulationTool(BaseTool):
# of the same line # of the same line
at_date = kw.pop("at_date", None) at_date = kw.pop("at_date", None)
if at_date is None: if at_date is None:
to_date = kw.pop("to_date") to_date = kw.pop("to_date", None)
else: else:
# add one second so that we can use to_date # add one second so that we can use to_date
to_date = at_date + MYSQL_MIN_DATETIME_RESOLUTION to_date = at_date + MYSQL_MIN_DATETIME_RESOLUTION
...@@ -1402,10 +1401,9 @@ class SimulationTool(BaseTool): ...@@ -1402,10 +1401,9 @@ class SimulationTool(BaseTool):
**kw)).digest() **kw)).digest()
# Try to get result from cache # Try to get result from cache
Resource_zGetInventoryCacheResult = self.Resource_zGetInventoryCacheResult Resource_zGetInventoryCacheResult = self.Resource_zGetInventoryCacheResult
inventory_cache_kw = { inventory_cache_kw = {'query': sql_text_hash}
'query': sql_text_hash, if to_date is not None:
'date': to_date, inventory_cache_kw['date'] = to_date
}
try: try:
cached_sql_result = Resource_zGetInventoryCacheResult(**inventory_cache_kw) cached_sql_result = Resource_zGetInventoryCacheResult(**inventory_cache_kw)
except ProgrammingError: except ProgrammingError:
...@@ -1434,10 +1432,10 @@ class SimulationTool(BaseTool): ...@@ -1434,10 +1432,10 @@ class SimulationTool(BaseTool):
else: else:
cached_result = [] cached_result = []
cache_lag = self.getInventoryCacheLag() cache_lag = self.getInventoryCacheLag()
if cached_sql_result and to_date - DateTime(cached_sql_result[0].date) < cache_lag: if cached_sql_result and (to_date is None or (to_date - DateTime(cached_sql_result[0].date) < cache_lag)):
cached_date = DateTime(cached_sql_result[0].date) cached_date = DateTime(cached_sql_result[0].date)
result = cached_result result = cached_result
else: elif to_date is not None:
# Cache miss, or hit with old data: store a new entry in cache. # Cache miss, or hit with old data: store a new entry in cache.
# Don't store it at to_date, as it risks being flushed soon (ie, when # Don't store it at to_date, as it risks being flushed soon (ie, when
# any document older than to_date gets reindexed in stock table). # any document older than to_date gets reindexed in stock table).
...@@ -1475,6 +1473,12 @@ class SimulationTool(BaseTool): ...@@ -1475,6 +1473,12 @@ class SimulationTool(BaseTool):
'data': result._data, 'data': result._data,
}), }),
) )
else:
# Cache miss and this getInventory() not specifying to_date,
# and other getInventory() have not created usable caches.
# In such case, do not create cache, do not use cache.
result = []
cached_date = None
if src__: if src__:
result = sql_source_list result = sql_source_list
return result, cached_date return result, cached_date
......
...@@ -3022,6 +3022,45 @@ class TestInventoryCacheTable(InventoryAPITestCase): ...@@ -3022,6 +3022,45 @@ class TestInventoryCacheTable(InventoryAPITestCase):
} }
) )
def test_17_GetInventoryWithoutDate(self):
"""
Check getInventory without date. Without date means specifying
infinite future date. So it try to retrieve the latest cache.
Note: This behavior is compatible with the old caching implementation
that was using inventory_stock table. In other words, this brings backs
the backword compatibility in invetory caching.
"""
self.assertInventoryEquals(
self._fillCache(),
inventory_kw={
'node_uid': self.node_uid,
},
)
def test_18_GetInventoryWithoutDate(self):
"""
Check that getInventoryList without date retrives the latest caches.
"""
self._fillCache(True)
# Check we got all results
self._checkInventoryList(
self.getInventoryList(node_uid=self.node_uid),
[{
'date': self.INVENTORY_DATE_3,
'inventory': self.INVENTORY_QUANTITY_3,
'node_uid': self.node_uid,
}, {
'date': self.INVENTORY_DATE_2,
'inventory': self.INVENTORY_QUANTITY_2,
'node_uid': self.node_uid,
}, {
'date': self.INVENTORY_DATE_1,
'inventory': self.INVENTORY_QUANTITY_1,
'node_uid': self.node_uid,
}],
)
class BaseTestUnitConversion(InventoryAPITestCase): class BaseTestUnitConversion(InventoryAPITestCase):
QUANTITY_UNIT_DICT = {} QUANTITY_UNIT_DICT = {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment