Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with credit expiration algorithm

So I'm stuck. I am working on a credit system with expirations. Similar to credit card miles but not exactly. By the way I am sorry for the book ahead but I needed to add enough detail to help get the whole picture.

What I need is a system where a user accumulates credits for doing activities. But they can also spend these credits on activities. The credits should expire after 30 days if they are not used. I seem to be stuck on how to accurately calculate this in a batch that will run every night. Any ideas in any language would be greatly appreciated as I seem to be stuck on just one minor detail that I can't get around. Here is an example of the data:

7/1: +5 - user signs up
7/2: +5 - user interacts with system
7/2: -3 - user purchases activity
7/3: +5 - user interacts with system

So at this point the user has received 15 credits and has spent 3. Leaving him with a total of 12 credits. (At least I got basic math down :P)

I should add that currently we are playing with the idea of having two fields: last processed, next processed. So these values at this time assuming it was a new sign up are:

Last Processed Date: 7/1
Next Process Date: 8/1

So now 8/1 comes around. The batch starts and looks at all credits that are older than 30 days. Which at this point is 5.

This is where it starts to get fuzzy.

Then the system should look at all the credits that have been spent in the last 30 days to see if they are using any credits. Because they should only expire if they haven't been used. So there are 3. So I then deduct the user 2 credits because that is the difference of credits earned older than 30 days and what has been spent. So I finish the batch and set the dates accordingly for the next day. Now assuming they haven't spent anymore I start the calculation over of credits earned older than 30, which is 5 and credits spent which again is 3. But I obviously don't want to consider the 3 credits that I considered yesterday. What is a good approach to not include those 3 credits again for consideration.

That is where I am stuck.

We are thinking about writing a debit record for the expired credits so we can track them but having a hard time seeing how I can use it in this calculation.

If you read this far thank you. If you even make a somewhat effort in the answer I will at a minimum give you an up vote for effort.

EDIT:
Ok @Greg mentioned something that I forgot to address. The idea of putting a flag on the credits considered. A valid point but not one that can work because of the following scenario:

Let's say that on a particular day a user spends 10 credits. But the expired credits that the batch is considering only accumulated to 5. Well he should still have 5 more credits left over to not have expired because he spent more than a single expiration. So the flag wouldn't work because we would have skipped those 5 extra credits. Hope that makes sense?

like image 715
spinon Avatar asked Jul 07 '10 22:07

spinon


2 Answers

For every user of the system keep an array, that stores information about the amount of credits available to the user for the next 30 consecutive days

For example the data for some user might look like this

8  | 
7  | | 
6  | | | | 
5  | | | | | | | | | | |
4  | | | | | | | | | | | | | | | | |
3  | | | | | | | | | | | | | | | | | | | | | | | | 
2  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  -------------------------------------------------------------
  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
   ^ ^                           ^          
   |  \_                         |
  today tomorrow             in 15 days

Every time the user earns some credits, You increase amounts for all days by the number of credits earned. For example if the user earns 2 credits the table changes as follows. It's like rising the whole graph up.

10 | 
9  | | 
8  | | | | 
7  | | | | | | | | | | |
6  | | | | | | | | | | | | | | | | |
5  | | | | | | | | | | | | | | | | | | | | | | | | 
4  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
2  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  -------------------------------------------------------------
  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
   ^ ^                           ^          
   |  \_                         |
  today tomorrow             in 15 days

If The user has x credits today and spends y credits, You decrease the amount of credits available to him to x - y, for every day he has an amount greater than x - y. For days he has no more than x - y, the amount stays the same. It's like cutting the top of the graph off. For example if the user spends 3 credits the graph changes to

7  | | | | | | | | | | |
6  | | | | | | | | | | | | | | | | |
5  | | | | | | | | | | | | | | | | | | | | | | | | 
4  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
2  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  -------------------------------------------------------------
  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
   ^ ^                           ^          
   |  \_                         |
  today tomorrow             in 15 days

Every day You shift the graph to the left to model expiring credits. The user will have the following amounts tomorrow

7  | | | | | | | | | |
6  | | | | | | | | | | | | | | | |
5  | | | | | | | | | | | | | | | | | | | | | | | 
4  | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
3  | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
2  | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
1  | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  -------------------------------------------------------------
  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
   ^ ^                           ^          
   |  \_                         |
  today tomorrow             in 15 days
like image 112
Maciej Hehl Avatar answered Nov 20 '22 04:11

Maciej Hehl


I wouldn't consider trying to process the data as you present it. Instead, you should keep track of how many credits the user has, and when they expire. That way you keep track of which credits were used when the purchase is made, instead of trying to work it all out later.

So when the user signs up, they have:

5 credits expiring on 8/1

After interacting with the system the next day:

5 credits expiring on 8/1
5 credits expiring on 8/2

After purchasing something:

2 credits expiring on 8/1
5 credits expiring on 8/2

And so on.

like image 3
Anon. Avatar answered Nov 20 '22 05:11

Anon.