2

I have a collection of responses sent from the application to different users. Each response has a user_id and the keen created time-stamp.

I want to run a regular job that returns the user_ids for those users where their last response was greater than seven days (or whatever) ago.

I can't just filter on the timestamp GTE seven days ago because a user can have multiple responses over a period of time. All I want to know is when was the last response by users.

I can't use the extract latest events because that appears to be across the whole collection. The last 10 events, in this case, could all have been by one user.

I want to know for each user, the latest event that is greater than seven days ago.

Satchel
  • 16,414
  • 23
  • 106
  • 192
  • I think there's a select first or top function available to you via most strictured query languages. That might be in the ball park of what you are looking for. – Thomas Feb 15 '15 at 02:25

1 Answers1

4

Try to remove the active users by using two different queries. One for active_users and the other for all_users. See example below:

active_users = Keen.select_unique("responses", :target_property => "user_id", :timeframe => "this_7_days")

all_users = Keen.select_unique("responses", :target_property => "user_id")

inactive_users = all_users - active_users

puts inactive_users
Kyle Wild
  • 8,845
  • 2
  • 36
  • 36
simplyaubs
  • 56
  • 4
  • Cool, I'm implementing now and will let you know. But this makes alot of sense. – Satchel Feb 24 '15 at 19:20
  • Hmm, I tried it and have been looking into the user_id's that are surfaced. Many of those are ones I know have not been using the system so they shouldn't come up with any events in the last 7 days....is there a way I can verify why this isn't working right? – Satchel Mar 23 '15 at 18:32
  • To test I switched the timeframe to this minute and still get a bunch of old target_properties listed, even those that I know have not been active: `active_users = Keen.select_unique(:bot_response, :target_property => "bot_client_id", :time_frame => "this_minute")` – Satchel Mar 23 '15 at 18:37
  • okay, some new data: when I run it in the query workbench, the results seem to be more accurate, so something about the client is off..... – Satchel Mar 23 '15 at 18:40
  • I have run what l looks like the same query from the UI and from the CLI, and get different results. I get the right response from the UI. @simplyaubs – Satchel Mar 23 '15 at 19:10
  • I found the error, mistyped :timeframe as :time_frame -- works as intended, now! – Satchel Mar 23 '15 at 19:20