Views with variable date logic

Hi,

I am working on a reverse auction system. Listings can be set to be visible for 3,5 or 7 days and I store this data, along with the created and end epoch in the object. What I want a view to do however is only bring back listings which have not ended, if i work this date calculation out in the map then the view result becomes stale as the map does not get re run on every query, neither can it for scalability. Currently I’m returning the max period of 7 days and trimming out the lesser days that have ended in code. But this is horrible, i was wondering what you would suggest?

To mitigate the extra data i gather by always getting 7 days worth of data, even if a listing may have ended in 3 days I have implemented the following:

A cron process which grabs all ‘active’ listings using the current method but rather than excluding expired items, updates their type to a finished listing type. This means these types will not be returned for the view as the type will not match an active listing type.

I will still be selecting a little bit of extra data…so ideally id like a better solution, but if i do this every hour it will only include an extra hours worth of expired items to filter out in code rather than having a few extra days.
Any better ideas?

Thanks!

Hi,

Thanks for the reply, this is what I am doing now, but this gets extra data. What I need is to return only active listings. Seeing as the listings run for X number of days i either do not know how far back in time to start my query or how far into the future to end it depending on whether i output the created dates or end dates as the key. All i can do is set the query start or end to be the longest number of listing days and then trip out the listings that were set to less days.

What I is a query that will take into account the number of days the listing should run for when returning the data, but this seems to be the old problem, what comes first…the chicken or the egg? I cant select only the active listings because my query is set to a date range, i cant alter the date range based on information inside the object because i only know that after the query is done.

Thanks,
Tom

One solution that comes to mind is to have an index that emits the end date in its key, either as ISO8601 (sortable strings) or using the built-in function dateToArray() to get an array.

Before querying you take the current date and adds e.g. 3 days to it so you get an end date.

Then you can query the index with endkey=“enddate”

Hi,

Thanks, i think your first suggestion is what will work and I can easily add this as I have the end time and created time in Unix epoch.

Thanks,
Tom

I’m not sure I understand.

I’m assuming you don’t have items with startdate in the future, e.g. not started yet. If you do that’s a bit more problematic. Do you?

If not, do you have the end date in the objects? E.g. {startdate:‘2013-01-01’, length:3, enddate:‘2013-01-04’}

If you do, you can emit this as your key, and all that has an enddate in the future are active. E.g. your view would be “emit(doc.enddate, null)” and you can query with startkey=

If you don’t have the enddate in your objects, you can calculate it in the index, e.g. if your startdate is an epoch value:

function (doc, meta) {
var end = doc.startdate + doc.length * 60 * 60 * 24 * 1000; // or similar
emit(end, null);
}

and query just as above.