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
129 e
.react_stats_event
(game
)
133 redef class GithubEvent
134 # Reacts to a statistics related event.
136 # Called by `StatisticsReactor::react_event`.
138 private fun react_stats_event
(game
: Game) do end
141 redef class IssuesEvent
143 # Count opened and closed issues.
144 redef fun react_stats_event
(game
) do
145 var player
= issue
.user
.player
(game
)
146 if action
== "opened" then
147 game
.stats
.inc
("issues")
148 game
.stats
.inc
("issues_open")
150 player
.stats
.inc
("issues")
151 player
.stats
.inc
("issues_open")
153 else if action
== "reopened" then
154 game
.stats
.inc
("issues_open")
156 player
.stats
.inc
("issues_open")
158 else if action
== "closed" then
159 game
.stats
.dec
("issues_open")
161 player
.stats
.dec
("issues_open")
167 redef class PullRequestEvent
169 # Count opened and closed pull requests.
170 redef fun react_stats_event
(game
) do
171 var player
= pull
.user
.player
(game
)
172 if action
== "opened" then
173 game
.stats
.inc
("pulls")
174 game
.stats
.inc
("pulls_open")
176 player
.stats
.inc
("pulls")
177 player
.stats
.inc
("pulls_open")
179 else if action
== "reopened" then
180 game
.stats
.inc
("pulls_open")
182 player
.stats
.inc
("pulls_open")
184 else if action
== "closed" then
185 game
.stats
.dec
("pulls_open")
186 player
.stats
.dec
("pulls_open")
188 game
.stats
["commits"] += pull
.commits
189 player
.stats
["commits"] += pull
.commits