0

I have a state history table, which contains states of an activity. These states can be repetitive. Sample table :

id    activity_name   state     sub_state
1      firstTask       Check     INPROGRESS
2      firstTask       Check     FAIL
3      secondTask      Check     INPROGRESS
4      secondTask      Check     SUCCESS
5      seccondTask    Initiated  INPROGRESS
6      secondTask     Initiated  SUCCESS

Any new activity with new state can be introduced anytime in the system.

These states in the table are for debugging purpose. Now I need to expose an API which reads this table and convert these states to meaningful states ignoring the repetitions.

Eg, the above table should be exposed as

First TASK: 
CHECKED --> FAILED

Second Task:
CHECKED --> INITIATED

What approach/design pattern should I follow which will handle newly introduced states, their compaction and conversion to meaningful state.

Thanks in advance.

Mak
  • 165
  • 3
  • 11

1 Answers1

0

Unusal Approach

If you dont have queries like: Give me all Task which state is check and substate is failed try this approach. Save the state and substate in a json string in the database.

id    activity_name   state
1      firstTask      {'Check' : ['INPROGRESS', 'FAIL']}
2      secondTask     {'Check' : ['INPROGRESS', 'SUCCESS'], {'Initiate' : ['INPROGRESS', 'SUCCESS']}

Relational Approach

If you like it the rational way then try to save it like that way, or the criteria above don't fit.

id    activity_name   state     sub_state
1      firstTask       Check     01-INPROGRESS
2      firstTask       Check     02-FAIL
3      secondTask      Check     01-INPROGRESS
4      secondTask      Check     02-SUCCESS
5      seccondTask    Initiate   01-INPROGRESS
6      secondTask     Initiate   02-SUCCESS

So you can just have sql queries like

SELECT MAX(SUB_STATE) FROM STATES WHERE activity_name = 'firstTask' and state = 'Check';

Max is here a string comparison, so if you have more than 99 sub_states for one state then use 003-SUCCESS, etc. You can use normal int number too, but than its hard to read the table without further knowledge from the sql console. So if data size doesn't matter I would prefer to append the meaningful string.

In both approaches you can easily introduce new states.

Kordi
  • 2,405
  • 1
  • 14
  • 13
  • Actually sub_state are more then INPROGRESS or FAIL, sub_state will many times save the 1 level high description of corresponding state. Also my table can have more than 10 dynamic task, getting max will not be possible in that case – Mak Mar 08 '16 at 06:52
  • @Mak thats true but thats no problem at all. Then you just create a new one. In both approaches you can create unlimited substates. the 01- infront is just which substate for this state occured first, that you can have a nice query optimization. Max(SUB_STATE) for a state is than just the last substate for this state which occured. You know what I mean? – Kordi Mar 08 '16 at 07:08