Guest User

Untitled

a guest
Dec 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. CREATE TEMP FUNCTION CALC_CHURN(ar ARRAY<STRUCT<
  2. skill_id FLOAT64,
  3. question_id FLOAT64,
  4. total_classes FLOAT64,
  5. was_correct BOOL,
  6. resp_time FLOAT64,
  7. timestamp TIMESTAMP,
  8. total_chances FLOAT64,
  9. curriculum_grade_num FLOAT64,
  10. curriculum_id FLOAT64,
  11. curriculum_location_id FLOAT64,
  12. current_strand_id FLOAT64,
  13. current_theta FLOAT64,
  14. algorithm_type STRING,
  15. play_location STRING,
  16. was_answered_on_mobile BOOL,
  17. algorithm_type_version FLOAT64,
  18. is_a_member BOOL,
  19. question_type STRING,
  20. delay_to_hb_toggle FLOAT64,
  21. hb_toggled_manually BOOL>>)
  22. RETURNS ARRAY<STRUCT<
  23. last_skill_of_session INT64,
  24. last_question_of_session INT64,
  25. number_of_class_at_session_end INT64,
  26. number_of_corrects INT64,
  27. average_session_response_time FLOAT64,
  28. curriculum_grade_at_session_end INT64,
  29. curriculum_id_at_session_end INT64,
  30. curriculum_location_at_session_end INT64,
  31. strand_at_session_end INT64,
  32. theta_at_session_end FLOAT64,
  33. algorithm_type_at_session_end STRING,
  34. was_on_mobile_at_session_end BOOL,
  35. chosen_grade_at_session_end INT64,
  36. was_member_at_session_end BOOL,
  37. average_delay_to_hint_on_last_chance FLOAT64,
  38. number_of_hints_opened_manually_on_last_chance INT64,
  39. number_of_hints_opened_on_last_chance INT64,
  40. day_of_week_session_ended INT64,
  41. month_session_ended INT64,
  42. grind_duration INT64,
  43. time_between_grind INT64,
  44. total_answered_in_session INT64
  45. >>
  46. LANGUAGE js AS """
  47. sessions = []
  48. sessionStartedTimestamp = ar[0].timestamp
  49. lastTimestamp = ar[0].timestamp
  50. lastChurnDuration = 0
  51.  
  52. sessionSkillQuestions = []
  53. numberHintBubbleUsedInSession = 0
  54. numberOfCorrectsInSession = 0
  55. numberOfAnswersInSession = 0
  56. cumulativeResponseTime = 0
  57. cumulativeTotalChances = 0
  58. cumulativeDelayToHintOnLastChance = 0
  59. numberTimesHintBubbleOpenedManuallyOnLastChance = 0
  60. numberTimesHintWasOpenedOnLastChance = 0
  61. for (let inc = 0; inc < ar.length; inc++) {
  62. current_answer = ar[inc]
  63. churnDuration = current_answer.timestamp - lastTimestamp
  64.  
  65. if (churnDuration >= (60 * 60000) || inc + 1 === ar.length) {
  66. session_ended_answer = ar[inc-1]
  67. sessions.push({
  68. last_skill_of_session: session_ended_answer.skill_id,
  69. last_question_of_session: session_ended_answer.question_id,
  70. number_of_class_at_session_end: session_ended_answer.total_classes,
  71. number_of_corrects: numberOfCorrectsInSession,
  72. cumulative_response_time: cumulativeResponseTime,
  73. average_session_response_time: cumulativeResponseTime*1.0/cumulativeTotalChances,
  74. curriculum_grade_at_session_end: session_ended_answer.curriculum_grade_num,
  75. curriculum_id_at_session_end: session_ended_answer.curriculum_id,
  76. curriculum_location_at_session_end: session_ended_answer.curriculum_location_id,
  77. strand_at_session_end: session_ended_answer.current_strand_id,
  78. theta_at_session_end: session_ended_answer.current_theta,
  79. algorithm_type_at_session_end: session_ended_answer.algorithm_type,
  80. was_on_mobile_at_session_end: session_ended_answer.was_answered_on_mobile,
  81. chosen_grade_at_session_end: session_ended_answer.chosen_grade,
  82. was_member_at_session_end: session_ended_answer.is_a_member,
  83. average_delay_to_hint_on_last_chance: cumulativeDelayToHintOnLastChance*1.0/numberTimesHintWasOpenedOnLastChance,
  84. number_of_hints_opened_manually_on_last_chance: numberTimesHintBubbleOpenedManuallyOnLastChance,
  85. number_of_hints_opened_on_last_chance: numberTimesHintWasOpenedOnLastChance,
  86. day_of_week_session_ended: session_ended_answer.timestamp.getDay(),
  87. month_session_ended: session_ended_answer.timestamp.getMonth(),
  88. skill_questions: JSON.stringify(sessionSkillQuestions),
  89. grind_duration: lastTimestamp-sessionStartedTimestamp,
  90. time_between_grind: lastChurnDuration,
  91. total_answered_in_session: numberOfAnswersInSession
  92. })
  93. sessionStartedTimestamp = current_answer.timestamp
  94. lastChurnDuration = churnDuration
  95.  
  96. sessionSkillQuestions = []
  97. numberHintBubbleUsedInSession = 0
  98. numberOfCorrectsInSession = 0
  99. numberOfAnswersInSession = 0
  100. cumulativeResponseTime = 0
  101. cumulativeTotalChances = 0
  102. cumulativeDelayToHintOnLastChance = 0
  103. numberTimesHintBubbleOpenedManuallyOnLastChance = 0
  104. numberTimesHintWasOpenedOnLastChance = 0
  105. }
  106.  
  107. numberTimesHintWasOpenedOnLastChance += current_answer.delay_to_hb_toggle != null ? 1 : 0
  108. numberTimesHintBubbleOpenedManuallyOnLastChance += current_answer.hb_toggled_manually ? 1 : 0
  109. cumulativeDelayToHintOnLastChance += current_answer.delay_to_hb_toggle || 0
  110. cumulativeTotalChances += current_answer.total_chances
  111. cumulativeResponseTime += current_answer.resp_time
  112. numberOfCorrectsInSession += current_answer.was_correct ? 1 : 0;
  113. numberOfAnswersInSession += 1
  114. sessionSkillQuestions.push({skill_id: current_answer.skill_id, question_id: current_answer.question_id})
  115. lastTimestamp = current_answer.timestamp
  116. }
  117. return sessions;
  118. """;
  119.  
  120. SELECT user_id, CALC_CHURN(
  121. ARRAY_AGG(
  122. (
  123. CAST(skill_id AS FLOAT64),
  124. CAST(question_id AS FLOAT64),
  125. CAST(total_classes AS FLOAT64),
  126. was_correct,
  127. CAST(resp_time AS FLOAT64),
  128. timestamp,
  129. CAST(total_chances AS FLOAT64),
  130. CAST(curriculum_grade_num AS FLOAT64),
  131. CAST(curriculum_id AS FLOAT64),
  132. CAST(curriculum_location_id AS FLOAT64),
  133. CAST(current_strand_id AS FLOAT64),
  134. current_theta,
  135. algorithm_type,
  136. play_location,
  137. was_answered_on_mobile,
  138. algorithm_type_version,
  139. is_a_member,
  140. question_type,
  141. CAST(delay_to_hb_toggle AS FLOAT64),
  142. hb_toggled_manually
  143. )
  144. ORDER BY timestamp)) AS sessions
  145. FROM educational_data.answers
  146. GROUP BY user_id
  147. HAVING COUNT(*)>10
Add Comment
Please, Sign In to add comment