No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

136 líneas
6.4KB

  1. <%
  2. '=======================================================================================================================
  3. ' STATS REPOSITORY
  4. ' Read-only counts for the dashboard.
  5. '=======================================================================================================================
  6. Class StatsRepository_Class
  7. ' Dictionary of counts about the list itself
  8. Public Function ListCounts()
  9. Dim rs, d
  10. Set d = CreateObject("Scripting.Dictionary")
  11. Set rs = Db_Query( _
  12. "SELECT COUNT(*) AS total, " & _
  13. "Sum(IIf(status = 'removed', 1, 0)) AS removed, " & _
  14. "Sum(IIf(status <> 'removed' AND (is_customer = 0 OR is_customer IS NULL), 1, 0)) AS prospects, " & _
  15. "Sum(IIf(status <> 'removed' AND is_customer = 1, 1, 0)) AS customers, " & _
  16. "Sum(IIf(status <> 'removed' AND (is_customer = 0 OR is_customer IS NULL) AND last_contacted_at IS NULL, 1, 0)) AS never_contacted, " & _
  17. "Sum(IIf(status = 'new', 1, 0)) AS st_new, " & _
  18. "Sum(IIf(status = 'contacted', 1, 0)) AS st_contacted, " & _
  19. "Sum(IIf(status = 'follow_up', 1, 0)) AS st_follow_up, " & _
  20. "Sum(IIf(status <> 'removed' AND next_action_date < Date(), 1, 0)) AS overdue, " & _
  21. "Sum(IIf(status <> 'removed' AND next_action_date = Date(), 1, 0)) AS due_today, " & _
  22. "Sum(IIf(status <> 'removed' AND next_action_date > Date() AND next_action_date <= DateAdd('d', 7, Date()), 1, 0)) AS this_week, " & _
  23. "Sum(IIf(status <> 'removed' AND phone_digits IS NULL, 1, 0)) AS no_phone " & _
  24. "FROM businesses", Array())
  25. Dim i
  26. For i = 0 To rs.Fields.Count - 1
  27. d(rs.Fields(i).Name) = N(rs.Fields(i).Value)
  28. Next
  29. rs.Close
  30. d("active") = d("total") - d("removed")
  31. Set ListCounts = d
  32. End Function
  33. ' Counts of logged contacts: today, last 7 days (including today), last 30 days
  34. Public Function ActivityCounts()
  35. Dim rs, d
  36. Set d = CreateObject("Scripting.Dictionary")
  37. Set rs = Db_Query( _
  38. "SELECT Sum(IIf(contacted_at = Date(), 1, 0)) AS n_today, " & _
  39. "Sum(IIf(contacted_at >= DateAdd('d', -6, Date()), 1, 0)) AS n_week, " & _
  40. "Sum(IIf(contacted_at >= DateAdd('d', -29, Date()), 1, 0)) AS n_month, " & _
  41. "COUNT(*) AS everything FROM interactions", Array())
  42. d("today") = N(rs("n_today")) : d("week") = N(rs("n_week")) : d("month") = N(rs("n_month")) : d("all") = N(rs("everything"))
  43. rs.Close
  44. Set ActivityCounts = d
  45. End Function
  46. ' Contacts per day for the last `days` days, oldest first: list of Array(date, count, methods) including zero days.
  47. ' methods is a Dictionary of method key -> count for that day.
  48. Public Function DailyActivity(days)
  49. Dim rs, counts, byMethod, list, i, dt, key, m
  50. Set counts = CreateObject("Scripting.Dictionary")
  51. Set byMethod = CreateObject("Scripting.Dictionary")
  52. Set rs = Db_Query("SELECT contacted_at, method, COUNT(*) FROM interactions WHERE contacted_at >= DateAdd('d', " & CLng(1 - days) & ", Date()) GROUP BY contacted_at, method", Array())
  53. Do While Not rs.EOF
  54. If Not IsNull(rs(0)) Then
  55. key = CStr(CLng(CDate(rs(0))))
  56. counts(key) = N(counts(key)) + CLng(rs(2))
  57. If Not byMethod.Exists(key) Then Set byMethod(key) = CreateObject("Scripting.Dictionary")
  58. m = rs(1) & ""
  59. byMethod(key)(m) = N(byMethod(key)(m)) + CLng(rs(2))
  60. End If
  61. rs.MoveNext
  62. Loop
  63. rs.Close
  64. Set list = CreateObject("Scripting.Dictionary")
  65. For i = days - 1 To 0 Step -1
  66. dt = Date - i
  67. key = CStr(CLng(dt))
  68. If counts.Exists(key) Then
  69. list.Add list.Count, Array(dt, counts(key), byMethod(key))
  70. Else
  71. list.Add list.Count, Array(dt, 0, CreateObject("Scripting.Dictionary"))
  72. End If
  73. Next
  74. Set DailyActivity = list
  75. End Function
  76. ' Last 30 days grouped by a column ("method" or "outcome"): list of Array(key, count), biggest first
  77. Public Function ActivityBy(column)
  78. Dim rs, list
  79. If column <> "method" And column <> "outcome" Then Err.Raise 5, "StatsRepository.ActivityBy", "Unsupported column"
  80. Set list = CreateObject("Scripting.Dictionary")
  81. Set rs = Db_Query("SELECT " & column & ", COUNT(*) AS n FROM interactions WHERE contacted_at >= DateAdd('d', -29, Date()) GROUP BY " & column & " ORDER BY COUNT(*) DESC", Array())
  82. Do While Not rs.EOF
  83. list.Add list.Count, Array(rs(0) & "", CLng(rs(1)))
  84. rs.MoveNext
  85. Loop
  86. rs.Close
  87. Set ActivityBy = list
  88. End Function
  89. ' Biggest industries (active businesses only) with how many have been contacted at least once
  90. Public Function TopIndustries(maxRows)
  91. Dim rs, list
  92. Set list = CreateObject("Scripting.Dictionary")
  93. Set rs = Db_Query( _
  94. "SELECT TOP " & CLng(maxRows) & " i.id, i.name, COUNT(b.id) AS n, Sum(IIf(b.last_contacted_at IS NULL, 0, 1)) AS contacted " & _
  95. "FROM industries i INNER JOIN businesses b ON b.industry_id = i.id WHERE b.status <> 'removed' " & _
  96. "GROUP BY i.id, i.name ORDER BY COUNT(b.id) DESC, i.name", Array())
  97. Do While Not rs.EOF
  98. list.Add list.Count, Array(CLng(rs(0)), rs(1) & "", CLng(rs(2)), N(rs(3)))
  99. rs.MoveNext
  100. Loop
  101. rs.Close
  102. Set TopIndustries = list
  103. End Function
  104. Public Function RecentImports(maxRows)
  105. Dim rs, list
  106. Set list = CreateObject("Scripting.Dictionary")
  107. Set rs = Db_Query("SELECT TOP " & CLng(maxRows) & " id, file_name, imported_at, rows_read, rows_added FROM import_batches ORDER BY id DESC", Array())
  108. Do While Not rs.EOF
  109. list.Add list.Count, Array(CLng(rs(0)), rs(1) & "", rs(2), N(rs(3)), N(rs(4)))
  110. rs.MoveNext
  111. Loop
  112. rs.Close
  113. Set RecentImports = list
  114. End Function
  115. ' Null -> 0
  116. Private Function N(v)
  117. If IsNull(v) Or IsEmpty(v) Then N = 0 Else N = CLng(v)
  118. End Function
  119. End Class
  120. Dim StatsRepository_Class__Singleton
  121. Function StatsRepository()
  122. If IsEmpty(StatsRepository_Class__Singleton) Then
  123. Set StatsRepository_Class__Singleton = New StatsRepository_Class
  124. End If
  125. Set StatsRepository = StatsRepository_Class__Singleton
  126. End Function
  127. %>

Powered by TurnKey Linux.