1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Statistics about the Game.
19 # This module uses `GameReactor` to extract statistics about the game from
20 # triggered `Github::Event`.
27 redef class GameEntity
29 # Statistics for this entity.
30 fun stats
: GameStats is abstract
32 # Load statistics for this `MEntity` if any.
33 fun load_statistics
: nullable GameStats do
34 var key
= self.key
/ "statistics"
35 if not game
.store
.has_key
(key
) then return null
36 var json
= game
.store
.load_object
(key
)
37 return new GameStats.from_json
(game
, json
)
43 redef var stats
is lazy
do
44 return load_statistics
or else new GameStats(game
)
53 var res
= new FlatBuffer
55 res
.append
"# stats:\n"
56 res
.append stats
.pretty
57 return res
.write_to_string
63 redef var stats
is lazy
do
64 return load_statistics
or else new GameStats(game
)
73 var res
= new FlatBuffer
75 res
.append
"# stats:\n"
76 res
.append stats
.pretty
77 return res
.write_to_string
81 # Game statistics structure that can be saved as a `GameEntity`.
88 redef var key
= "statistics"
90 # Load `self` from saved data
91 init from_json
(game
: Game, json
: JsonObject) do
93 for k
, v
in json
do self[k
] = v
.as(Int)
97 var obj
= new JsonObject
98 for k
, v
in self do obj
[k
] = v
102 # Decrements the value of `key` statistic entry by 1.
103 fun dec
(key
: String) do
104 if not has_key
(key
) then
112 var res
= new FlatBuffer
114 res
.append
"# {v} {k}\n"
116 return res
.write_to_string
120 # `GameReactor` that computes statistics about the game.
121 class StatisticsReactor
124 redef fun react_event
(game
, e
) do e
.react_stats_event
(game
)
127 redef class GithubEvent
128 # Reacts to a statistics related event.
130 # Called by `StatisticsReactor::react_event`.
132 private fun react_stats_event
(game
: Game) do end
135 redef class IssuesEvent
137 # Count opened and closed issues.
138 redef fun react_stats_event
(game
) do
139 var player
= issue
.user
.player
(game
)
140 if action
== "opened" then
141 game
.stats
.inc
("issues")
142 game
.stats
.inc
("issues_open")
144 player
.stats
.inc
("issues")
145 player
.stats
.inc
("issues_open")
147 else if action
== "reopened" then
148 game
.stats
.inc
("issues_open")
150 player
.stats
.inc
("issues_open")
152 else if action
== "closed" then
153 game
.stats
.dec
("issues_open")
155 player
.stats
.dec
("issues_open")
161 redef class PullRequestEvent
163 # Count opened and closed pull requests.
164 redef fun react_stats_event
(game
) do
165 var player
= pull
.user
.player
(game
)
166 if action
== "opened" then
167 game
.stats
.inc
("pulls")
168 game
.stats
.inc
("pulls_open")
170 player
.stats
.inc
("pulls")
171 player
.stats
.inc
("pulls_open")
173 else if action
== "reopened" then
174 game
.stats
.inc
("pulls_open")
176 player
.stats
.inc
("pulls_open")
178 else if action
== "closed" then
179 game
.stats
.dec
("pulls_open")
180 player
.stats
.dec
("pulls_open")
182 game
.stats
["commits"] += pull
.commits
183 player
.stats
["commits"] += pull
.commits