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 # Load statistics for this `MEntity` if any.
30 fun load_statistics
: nullable GameStats do
31 var key
= self.key
/ "statistics"
32 if not game
.store
.has_key
(key
) then return null
33 var json
= game
.store
.load_object
(key
)
34 return new GameStats.from_json
(game
, json
)
37 # Save statistics under this `MEntity`.
38 fun save_statistics
(stats
: GameStats) do
39 game
.store
.store_object
(key
/ stats
.key
, stats
.to_json
)
45 # Statistics for this game instance.
46 var stats
: GameStats is lazy
do
47 return load_statistics
or else new GameStats(game
)
52 save_statistics
(stats
)
56 var res
= new FlatBuffer
58 res
.append
"# stats:\n"
59 res
.append stats
.pretty
60 return res
.write_to_string
66 # Statistics for this player.
67 var stats
: GameStats is lazy
do
68 return load_statistics
or else new GameStats(game
)
73 save_statistics
(stats
)
77 var res
= new FlatBuffer
79 res
.append
"# stats:\n"
80 res
.append stats
.pretty
81 return res
.write_to_string
85 # Game statistics structure that can be saved as a `GameEntity`.
92 redef var key
= "statistics"
94 # Load `self` from saved data
95 init from_json
(game
: Game, json
: JsonObject) do
97 for k
, v
in json
do self[k
] = v
.as(Int)
101 var obj
= new JsonObject
102 for k
, v
in self do obj
[k
] = v
106 # Decrements the value of `key` statistic entry by 1.
107 fun dec
(key
: String) do
108 if not has_key
(key
) then
116 var res
= new FlatBuffer
118 res
.append
"# {v} {k}\n"
120 return res
.write_to_string
124 # `GameReactor` that computes statistics about the game.
125 class StatisticsReactor
128 redef fun react_event
(game
, e
) do
130 e
.react_stats_event
(game
)
134 redef class GithubEvent
135 # Reacts to a statistics related event.
137 # Called by `StatisticsReactor::react_event`.
139 private fun react_stats_event
(game
: Game) do end
142 redef class IssuesEvent
144 # Count opened and closed issues.
145 redef fun react_stats_event
(game
) do
146 var player
= issue
.user
.player
(game
)
147 if action
== "opened" then
148 game
.stats
.inc
("issues")
149 game
.stats
.inc
("issues_open")
151 player
.stats
.inc
("issues")
152 player
.stats
.inc
("issues_open")
154 else if action
== "reopened" then
155 game
.stats
.inc
("issues_open")
157 player
.stats
.inc
("issues_open")
159 else if action
== "closed" then
160 game
.stats
.dec
("issues_open")
162 player
.stats
.dec
("issues_open")
168 redef class PullRequestEvent
170 # Count opened and closed pull requests.
171 redef fun react_stats_event
(game
) do
172 var player
= pull
.user
.player
(game
)
173 if action
== "opened" then
174 game
.stats
.inc
("pulls")
175 game
.stats
.inc
("pulls_open")
177 player
.stats
.inc
("pulls")
178 player
.stats
.inc
("pulls_open")
180 else if action
== "reopened" then
181 game
.stats
.inc
("pulls_open")
183 player
.stats
.inc
("pulls_open")
185 else if action
== "closed" then
186 game
.stats
.dec
("pulls_open")
187 player
.stats
.dec
("pulls_open")
189 game
.stats
["commits"] += pull
.commits
190 player
.stats
["commits"] += pull
.commits