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`.
28 # Statistics for this game instance.
29 var stats
= new GameStats
31 redef fun from_json
(json
) do
33 if json
.has_key
("statistics") then
34 stats
.from_json
(json
["statistics"].as(JsonObject))
40 obj
["statistics"] = stats
.to_json
45 var res
= new FlatBuffer
47 res
.append
"# stats:\n"
48 res
.append stats
.pretty
49 return res
.write_to_string
58 # Game statistics structure that can be saved as a `GameEntity`.
62 redef var key
= "statistics"
64 # Used internally to stats values.
65 private var stats
= new HashMap[String, Int]
69 # Load `self` from saved data
70 private fun from_json
(json
: JsonObject) do
71 for k
, v
in json
do stats
[k
] = v
.as(Int)
75 var obj
= new JsonObject
76 for k
, v
in stats
do obj
[k
] = v
80 # Retrieves the current value of `key` statistic entry.
81 fun [](key
: String): Int do return stats
[key
]
83 # Increments the value of `key` statistic entry by 1.
84 fun incr
(key
: String) do stats
[key
] += 1
86 # Decrements the value of `key` statistic entry by 1.
87 fun decr
(key
: String) do stats
[key
] -= 1
92 stats
["issues_open"] = 0
94 stats
["pulls_open"] = 0
98 var res
= new FlatBuffer
100 res
.append
"# {v} {k}\n"
102 return res
.write_to_string
106 # `GameReactor` that computes statistics about the game.
107 class StatisticsReactor
110 redef fun react_event
(game
, e
) do
112 e
.react_stats_event
(game
)
117 redef class GithubEvent
118 # Reacts to a statistics related event.
120 # Called by `StatisticsReactor::react_event`.
122 private fun react_stats_event
(game
: Game) do end
125 redef class IssuesEvent
127 # Count opened and closed issues.
128 redef fun react_stats_event
(game
) do
129 if action
== "opened" then
130 game
.stats
.incr
("issues")
131 game
.stats
.incr
("issues_open")
132 else if action
== "reopened" then
133 game
.stats
.incr
("issues_open")
134 else if action
== "closed" then
135 game
.stats
.decr
("issues_open")
140 redef class PullRequestEvent
142 # Count opened and closed pull requests.
143 redef fun react_stats_event
(game
) do
144 if action
== "opened" then
145 game
.stats
.incr
("pulls")
146 game
.stats
.incr
("pulls_open")
147 else if action
== "reopened" then
148 game
.stats
.incr
("pulls_open")
149 else if action
== "closed" then
150 game
.stats
.decr
("pulls_open")