Skip to content

Commit

Permalink
changes to fix partial discriminators
Browse files Browse the repository at this point in the history
  • Loading branch information
jimklo committed Apr 9, 2012
1 parent 97777af commit fedfafe
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions LR/lr/controllers/extract.py
Expand Up @@ -34,7 +34,7 @@ def _getView(self,view='_all_docs',keys=[], includeDocs=True,startKey=None,endKe
return view
def _convertDateTime(self,dt):
try:
epoch = parse_date("1970-01-01T00:00:01Z")
epoch = parse_date("1970-01-01T00:00:00Z")
if isinstance(dt, str) or isinstance(dt,unicode):
dt = parse_date(dt)
dt = dt - epoch
Expand Down Expand Up @@ -114,6 +114,16 @@ def makeStartsWithEndKey(key):

return newkey

def hasParamFor(funcName):
if funcName == 'ts' and ('from' in params or 'until' in params):
return True
elif funcName == 'discriminator' and ('discriminator' in params or 'discriminator-starts-with' in params):
return True
elif funcName == 'resource' and ('resource' in params or 'resource-starts-with' in params):
return True
else:
return False

def populateTs(startKey, endKey, pos, isLast):
if 'from' in params:
startKey.append(self._convertDateTime(params['from']))
Expand All @@ -123,8 +133,10 @@ def populateTs(startKey, endKey, pos, isLast):
if 'until' in params:
endKey.append(self._convertDateTime(params['until']))
elif pos == 1:
endKey.append(self._convertDateTime(datetime.utcnow().isoformat()+"Z"))
return startKey, endKey
endKey.append(self._convertDateTime(datetime.utcnow().isoformat()+"Z"))

return startKey, endKey

def populateDiscriminator(startKey, endKey, pos, isLast):
if 'discriminator' in params:
# preserve key order!!!
Expand All @@ -135,7 +147,7 @@ def populateDiscriminator(startKey, endKey, pos, isLast):
discriminator = params['discriminator']
startKey.append(discriminator)
endKey.append(discriminator)
endKey = makeEndKey(endKey)

elif 'discriminator-starts-with' in params:
# preserve key order!!!
try:
Expand All @@ -154,7 +166,6 @@ def populateResource(startKey, endKey, pos, isLast):
if 'resource' in params:
startKey.append(params['resource'])
endKey.append(params['resource'])
endKey = makeEndKey(endKey);
elif 'resource-starts-with' in params:
startKey.append(params['resource-starts-with'])
endKey.append(params['resource-starts-with']+u'\ud7af')
Expand All @@ -174,10 +185,21 @@ def populateResource(startKey, endKey, pos, isLast):
}
queryOrderParts = view.split('-by-')
aggregate = queryOrderParts[0]
queryParams= queryOrderParts[1].split('-')
queryParams= queryOrderParts[1].split('-')

# if we don't have explicit params for this, then omit.
if hasParamFor(aggregate):
queryParams.append(aggregate)
log.error("added aggregate")

for pos, q in enumerate(queryParams,start=1):
startkey, endKey = funcs[q](startKey, endKey, pos, len(queryParams)==pos)
startkey, endKey = funcs[aggregate](startKey, endKey, len(queryParams)+1, True)

if len(endKey) > 0 and 'resource-starts-with' not in params and 'discriminator-starts-with' not in params:
log.error("making endkey")
endKey = makeEndKey(endKey)

# startkey, endKey = funcs[aggregate](startKey, endKey, len(queryParams)+1, True)
return startKey if len(startKey) > 0 else None, endKey if len(endKey) > 0 else None, includeDocs

def get(self, dataservice="",view='',list=''):
Expand Down

0 comments on commit fedfafe

Please sign in to comment.