1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # Easy and ready to use web tracker you can apply to your popcorn application.
19 # The only thing you have to do is to use the tracker in your app routes:
22 # var config = new AppConfig
24 # app.use("/", new HomeHandler)
25 # app.use("/products", new ProductsHandler)
26 # app.use("customers/", new CustomersHandler)
28 # app.use_after("/*", new PopTracker(config)) # tracker listens to /*
31 # You can also use multiple tracker at once on different route.
32 # All the data will be aggregated for you.
35 # app.use_after("/api/*", new PopTracker(config))
36 # app.use_after("/admin/*", new PopTracker(config))
39 # To retrieve your tracker data use the `PopTrackerAPI` which serves the tracker
40 # data in JSON format.
43 # app.use("/api/tracker_data", new PopTrackerAPI(config))
48 import popcorn
::pop_config
49 import popcorn
::pop_logging
50 import popcorn
::pop_repos
55 var tracker_logs
= new TrackerRepo(db
.collection
("tracker_logs"))
58 var tracker
= new PopTracker(self)
61 # JSON API of the PopTracker
63 # Serves the collected data in JSON format.
67 # Config used to access tracker db
71 use
("/entries", new PopTrackerEntries(config
))
72 use
("/queries", new PopTrackerQueries(config
))
73 use
("/browsers", new PopTrackerBrowsers(config
))
74 use
("/times", new PopTrackerResponseTime(config
))
78 # Base tracker handler
79 abstract class TrackerHandler
82 # Config used to access tracker db
85 # Get the `limit` GET argument from `req`
87 # Return `10` by default.
88 fun limit
(req
: HttpRequest): Int do return req
.int_arg
("limit") or else 10
91 # Saves logs into a MongoDB collection
96 redef fun all
(req
, res
) do
97 config
.tracker_logs
.save
new LogEntry(req
, res
)
101 # List all tracker log entries
102 class PopTrackerEntries
105 redef fun get
(req
, res
) do
106 res
.json
new JsonArray.from
(config
.tracker_logs
.find_all
)
110 # Group and count entries by query string
111 class PopTrackerQueries
114 redef fun get
(req
, res
) do
115 var pipe
= new MongoPipeline
116 pipe
.group
((new MongoGroup("$request.uri")).
118 avg
("response_time", "$response_time").
119 addToSet
("uniq", "$session"))
120 pipe
.sort
((new MongoMatch).eq
("visits", -1))
121 pipe
.limit
(limit
(req
))
122 res
.json
new JsonArray.from
(config
.tracker_logs
.collection
.aggregate
(pipe
))
126 # Group and count entries by browser
127 class PopTrackerBrowsers
130 # MongoMatch query used for each browser key
132 # Because parsing user agent string is a pain in the nit, we go lazy on this
133 # one. We associate each broswer key like `Chromium` to the query that allows
134 # us to count the number of visits.
135 var browser_queries
: HashMap[String, MongoMatch] do
136 var map
= new HashMap[String, MongoMatch]
137 map
["Chromium"] = (new MongoMatch).regex
("user_agent", "Chromium")
138 map
["Edge"] = (new MongoMatch).regex
("user_agent", "Edge")
139 map
["Firefox"] = (new MongoMatch).regex
("user_agent", "Firefox")
140 map
["IE"] = (new MongoMatch).regex
("user_agent", "(MSIE)|(Trident)")
141 map
["Netscape"] = (new MongoMatch).regex
("user_agent", "Netscape")
142 map
["Opera"] = (new MongoMatch).regex
("user_agent", "Opera")
143 map
["Safari"] = (new MongoMatch).land
(null, [
144 (new MongoMatch).regex
("user_agent", "Safari"),
145 (new MongoMatch).regex
("user_agent", "^((?!Chrome).)*$")])
146 map
["Chrome"] = (new MongoMatch).land
(null, [
147 (new MongoMatch).regex
("user_agent", "Chrome"),
148 (new MongoMatch).regex
("user_agent", "^((?!Edge).)*$")])
153 # Apply the `query` on `TrackerRepo::count`
154 fun browser_count
(query
: MongoMatch): Int do return config
.tracker_logs
.count
(query
)
156 redef fun get
(req
, res
) do
157 var browsers
= new Array[BrowserCount]
158 for browser
, query
in self.browser_queries
do
159 var count
= new BrowserCount(browser
, browser_count
(query
))
160 if count
.count
> 0 then browsers
.add count
163 for browser
in browsers
do sum
+= browser
.count
164 var other
= config
.tracker_logs
.count
- sum
165 if other
> 0 then browsers
.add
new BrowserCount("Other", other
)
166 default_comparator
.sort
(browsers
)
167 res
.json
new JsonArray.from
(browsers
)
171 # Associate each browser to its count.
173 # Only used to serialize the results.
174 private class BrowserCount
179 redef type OTHER: SELF
184 redef fun <=>(o
) do return o
.count
<=> count
187 # Return last month response time
188 class PopTrackerResponseTime
191 redef fun get
(req
, res
) do
192 var limit
= get_time
- (3600 * 24 * 30)
193 var pipe
= new MongoPipeline
194 pipe
.match
((new MongoMatch).gte
("timestamp", limit
))
195 pipe
.group
((new MongoGroup("$timestamp")).
197 avg
("response_time", "$response_time"))
198 pipe
.sort
((new MongoMatch).eq
("_id", -1))
199 res
.json
new JsonArray.from
(config
.tracker_logs
.collection
.aggregate
(pipe
))
203 # A tracker log entry used to store HTTP requests and their given HTTP responses
208 # HTTP request that triggered that log entry
209 var request
: HttpRequest
211 # HTTP response returned by the serveur
212 var response
: HttpResponse
214 # Request user-agent shortcut (easier for db requests
215 var user_agent
: nullable String is lazy
do return request
.header
["User-Agent"]
217 # Processing time in miliseconds (or null if no clock was found in request)
218 var response_time
: nullable Int is lazy
do
219 var clock
= request
.clock
220 if clock
== null then return null
221 return (clock
.total
* 1000.0).to_i
224 # Log entry timestamp
225 var timestamp
: Int = get_time
227 # Session ID associated to this entry
228 var session
: nullable String is lazy
do
229 var session
= request
.session
230 if session
== null then return null
231 return session
.id_hash
235 # Repository to store track logs.
237 super MongoRepository[LogEntry]