1
+ defmodule CodeSponsor.Stats.Clicks do
2
+ import Filtrex.Type.Config
3
+ import Ecto.Query , warn: false
4
+ alias CodeSponsor.Repo
5
+ alias CodeSponsor.Properties.Property
6
+ alias CodeSponsor.Campaigns.Campaign
7
+ alias CodeSponsor.Sponsorships.Sponsorship
8
+ alias CodeSponsor.Clicks.Click
9
+ alias CodeSponsor.Coherence.User
10
+
11
+ def count ( start_date , end_date ) when start_date <= end_date do
12
+ Repo . one (
13
+ from c in Click ,
14
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
15
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
16
+ select: fragment ( "count(*)" )
17
+ )
18
+ end
19
+
20
+ def count ( % Property { } = property , start_date , end_date ) when start_date <= end_date do
21
+ Repo . one (
22
+ from c in Click ,
23
+ where: c . property_id == ^ property . id ,
24
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
25
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
26
+ select: fragment ( "count(*)" )
27
+ )
28
+ end
29
+
30
+ def count ( % Sponsorship { } = sponsorship , start_date , end_date ) when start_date <= end_date do
31
+ Repo . one (
32
+ from c in Click ,
33
+ where: c . sponsorship_id == ^ sponsorship . id ,
34
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
35
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
36
+ select: fragment ( "count(*)" )
37
+ )
38
+ end
39
+
40
+ def count ( % Campaign { } = campaign , start_date , end_date ) when start_date <= end_date do
41
+ Repo . one (
42
+ from c in Click ,
43
+ where: c . campaign_id == ^ campaign . id ,
44
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
45
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
46
+ select: fragment ( "count(*)" )
47
+ )
48
+ end
49
+
50
+ def count ( % User { } = user , start_date , end_date ) when start_date <= end_date do
51
+ campaign_ids =
52
+ Repo . all ( from c in Campaign , where: c . user_id == ^ user . id )
53
+ |> Enum . map ( fn ( c ) -> c . id end )
54
+
55
+ property_ids =
56
+ Repo . all ( from p in Property , where: p . user_id == ^ user . id )
57
+ |> Enum . map ( fn ( c ) -> c . id end )
58
+
59
+ Repo . one (
60
+ from c in Click ,
61
+ where: c . campaign_id in ^ campaign_ids or c . property_id in ^ property_ids ,
62
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
63
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
64
+ select: fragment ( "count(*)" )
65
+ )
66
+ end
67
+
68
+ def count_by_day ( start_date , end_date ) when start_date <= end_date do
69
+ (
70
+ from c in Click ,
71
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
72
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
73
+ group_by: fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ,
74
+ select: [ ( fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ) , count ( "*" ) ]
75
+ ) |> to_date_map ( )
76
+ end
77
+
78
+ def count_by_day ( % Property { } = property , start_date , end_date ) when start_date <= end_date do
79
+ (
80
+ from c in Click ,
81
+ where: c . property_id == ^ property . id ,
82
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
83
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
84
+ group_by: fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ,
85
+ select: [ ( fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ) , count ( "*" ) ]
86
+ ) |> to_date_map ( )
87
+ end
88
+
89
+ def count_by_day ( % Sponsorship { } = sponsorship , start_date , end_date ) when start_date <= end_date do
90
+ (
91
+ from c in Click ,
92
+ where: c . sponsorship_id == ^ sponsorship . id ,
93
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
94
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
95
+ group_by: fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ,
96
+ select: [ ( fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ) , count ( "*" ) ]
97
+ ) |> to_date_map ( )
98
+ end
99
+
100
+ def count_by_day ( % Campaign { } = campaign , start_date , end_date ) when start_date <= end_date do
101
+ (
102
+ from c in Click ,
103
+ where: c . campaign_id == ^ campaign . id ,
104
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
105
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
106
+ group_by: fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ,
107
+ select: [ ( fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ) , count ( "*" ) ]
108
+ ) |> to_date_map ( )
109
+ end
110
+
111
+ def count_by_day ( % Campaign { } = campaign , start_date , end_date ) when start_date <= end_date do
112
+ (
113
+ from i in Impression ,
114
+ where: i . campaign_id == ^ campaign . id ,
115
+ where: fragment ( "?::date" , i . inserted_at ) >= ^ start_date ,
116
+ where: fragment ( "?::date" , i . inserted_at ) <= ^ end_date ,
117
+ group_by: fragment ( "date_trunc('day', ?)" , ( field ( i , ^ :inserted_at ) ) ) ,
118
+ select: [ ( fragment ( "date_trunc('day', ?)" , ( field ( i , ^ :inserted_at ) ) ) ) , count ( "*" ) ]
119
+ ) |> to_date_map ( )
120
+ end
121
+
122
+ def count_by_day ( % User { } = user , start_date , end_date ) when start_date <= end_date do
123
+ campaign_ids =
124
+ Repo . all ( from c in Campaign , where: c . user_id == ^ user . id )
125
+ |> Enum . map ( fn ( c ) -> c . id end )
126
+
127
+ property_ids =
128
+ Repo . all ( from p in Property , where: p . user_id == ^ user . id )
129
+ |> Enum . map ( fn ( c ) -> c . id end )
130
+
131
+ (
132
+ from c in Click ,
133
+ where: c . campaign_id in ^ campaign_ids or c . property_id in ^ property_ids ,
134
+ where: fragment ( "?::date" , c . inserted_at ) >= ^ start_date ,
135
+ where: fragment ( "?::date" , c . inserted_at ) <= ^ end_date ,
136
+ group_by: fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ,
137
+ select: [ ( fragment ( "date_trunc('day', ?)" , ( field ( c , ^ :inserted_at ) ) ) ) , count ( "*" ) ]
138
+ ) |> to_date_map ( )
139
+ end
140
+
141
+ defp to_date_map ( query ) do
142
+ query
143
+ |> Repo . all
144
+ |> Enum . map ( fn ( [ day , count ] ) ->
145
+ { :ok , date } =
146
+ day
147
+ |> Tuple . delete_at ( 1 )
148
+ |> Tuple . insert_at ( 1 , { 0 , 0 , 0 } )
149
+ |> NaiveDateTime . from_erl ( )
150
+
151
+ formatted_date = Timex . format! ( date , "%F" , :strftime )
152
+ { formatted_date , count }
153
+ end )
154
+ |> Enum . into ( % { } )
155
+ end
156
+ end
0 commit comments