Guest User

Untitled

a guest
Mar 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. We have `user_scores_daily` created every day for every user. Each of these records has a `_user_id` tag and some numeric fields.
  2.  
  3. Each user belongs to multiple "groups" (i.e. teams).
  4.  
  5. We also want to get `group_scores_daily`, which containns the sum of `user_scores_daily` fields for all users in each group (per day).
  6.  
  7. We currently do this in a background job with Ruby. It runs every day and combines data using two separate Influx queries.
  8.  
  9. 1. We get a mapping of group_id => user_ids
  10.  
  11. ```
  12. SELECT
  13. DISTINCT(group_id) AS group_id
  14. FROM
  15. "groups"
  16. WHERE
  17. event='group_user_added'
  18. AND
  19. time > 1451606400
  20. GROUP BY
  21. _user_id
  22. ```
  23.  
  24. 2. We get all `user_daily_scores` for the last day:
  25.  
  26. ```
  27. SELECT
  28. *
  29. FROM
  30. user_scores_daily
  31. WHERE
  32. time > now() - 1d
  33. ```
  34.  
  35. Using this data, we need to create two sets of records.
  36.  
  37. First, we need to score the same user/score/day for each of the user groups.
  38. This is so we can query for the top users in a particular group.
  39.  
  40. ```
  41. group_id | user_id | score
  42. ------------------------------
  43. 1 1 5
  44. 1 2 6
  45. 2 1 5
  46. 2 2 6
  47. ```
  48.  
  49. Second, we need to get the total score for each group/day:
  50.  
  51. ```
  52. group_id | score
  53. ------------------------------
  54. 1 11
  55. 2 11
  56. ```
  57.  
  58. I'm not sure how we would either of these in a continuous query, I think we need to do some processing in Ruby.
Add Comment
Please, Sign In to add comment