Skip to content

Commit

Permalink
Adds playtime_metric
Browse files Browse the repository at this point in the history
  • Loading branch information
kinduff committed May 8, 2021
1 parent 21460c3 commit 8366533
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ This exporter uses environment variables, there are no CLI support for now. The
| -------------------------- | ------------------------------------------------------------------------------------------------------ |
| `csgo_stats_metric` | All the stats from the player, it includes last_match data, totals per weapon, among other cool things |
| `csgo_achievements_metric` | All achievements done by the player, with value `1` or `0` for achieved or not |
| `csgo_playtime_metric` | Hours spent playing the game in minutes in different types, includes stats for each OS |
| `csgo_news_metric` | The latest news from the CS:GO community, can be used in a table. Value is an epoch |

## Footnotes
Expand Down
5 changes: 5 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func getAPIEndpoint(endpoint string) string {
path = "/ISteamUser/ResolveVanityURL/v0001"
case "news":
path = "/ISteamNews/GetNewsForApp/v0002"
case "gameInfo":
path = "/IPlayerService/GetOwnedGames/v1"
}

return baseUrl + path
Expand All @@ -86,6 +88,9 @@ func getAPIQueryParams(endpoint string, config *model.Config, req *http.Request)
q.Add("vanityurl", config.SteamName)
case "news":
q.Add("maxlength", "240")
case "gameInfo":
q.Add("steamid", config.SteamID)
q.Add("appids_filter[0]", "730")
}

q.Add(gameIdKey, "730")
Expand Down
39 changes: 29 additions & 10 deletions internal/collector/player_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type playerCollector struct {
config *model.Config
statsMetric *prometheus.Desc
achievementsMetric *prometheus.Desc
playtimeMetric *prometheus.Desc
newsMetric *prometheus.Desc
}

Expand All @@ -32,6 +33,11 @@ func NewPlayerCollector(config *model.Config) *playerCollector {
[]string{"name", "player", "title", "description"},
nil,
),
playtimeMetric: prometheus.NewDesc("playtime_metric",
"Shows the playtime the user has in the game in minutes",
[]string{"type", "player"},
nil,
),
newsMetric: prometheus.NewDesc("news_metric",
"Shows the latest news from CSGO",
[]string{"title", "url", "feedlabel"},
Expand All @@ -43,6 +49,7 @@ func NewPlayerCollector(config *model.Config) *playerCollector {
func (collector *playerCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.statsMetric
ch <- collector.achievementsMetric
ch <- collector.playtimeMetric
ch <- collector.newsMetric
}

Expand All @@ -60,6 +67,11 @@ func (collector *playerCollector) Collect(ch chan<- prometheus.Metric) {
collector.config.SteamID = ResolveVanityUrl.Response.Steamid
}

player := collector.config.SteamName
if player == "" {
player = collector.config.SteamID
}

playerStats := model.PlayerStats{}
if err := client.DoAPIRequest("stats", collector.config, &playerStats); err != nil {
log.Fatal(err)
Expand All @@ -75,6 +87,16 @@ func (collector *playerCollector) Collect(ch chan<- prometheus.Metric) {
log.Fatal(err)
}

gameInfo := model.GameInfo{}
if err := client.DoAPIRequest("gameInfo", collector.config, &gameInfo); err != nil {
log.Fatal(err)
}

achievementsDetails := model.AchievementsDetails{}
if err := client.DoXMLRequest("achievementsDetails", collector.config, &achievementsDetails); err != nil {
log.Fatal(err)
}

for _, s := range archivements.AchievementPercentages.Achievements {
allPlayerAchievements[s.Name] = 0
}
Expand All @@ -84,16 +106,6 @@ func (collector *playerCollector) Collect(ch chan<- prometheus.Metric) {
allPlayerAchievements[s.Name] = 1
}

player := collector.config.SteamName
if player == "" {
player = collector.config.SteamID
}

achievementsDetails := model.AchievementsDetails{}
if err := client.DoXMLRequest("achievementsDetails", collector.config, &achievementsDetails); err != nil {
log.Fatal(err)
}

for _, s := range achievementsDetails.Achievements.Achievement {
inner, ok := allPlayerAchievementsDetails[s.Apiname]
if !ok {
Expand All @@ -116,6 +128,13 @@ func (collector *playerCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(collector.achievementsMetric, prometheus.GaugeValue, float64(count), name, player, allPlayerAchievementsDetails[strings.ToLower(name)]["title"], allPlayerAchievementsDetails[strings.ToLower(name)]["description"])
}

playData := gameInfo.Response.Games[0]
ch <- prometheus.MustNewConstMetric(collector.playtimeMetric, prometheus.CounterValue, float64(playData.Playtime2Weeks), "last_2_weeks", player)
ch <- prometheus.MustNewConstMetric(collector.playtimeMetric, prometheus.CounterValue, float64(playData.PlaytimeForever), "forever", player)
ch <- prometheus.MustNewConstMetric(collector.playtimeMetric, prometheus.CounterValue, float64(playData.PlaytimeWindowsForever), "windows_forever", player)
ch <- prometheus.MustNewConstMetric(collector.playtimeMetric, prometheus.CounterValue, float64(playData.PlaytimeMacForever), "mac_forever", player)
ch <- prometheus.MustNewConstMetric(collector.playtimeMetric, prometheus.CounterValue, float64(playData.PlaytimeLinuxForever), "linux_forever", player)

for _, s := range news.Appnews.Newsitems {
ch <- prometheus.MustNewConstMetric(collector.newsMetric, prometheus.GaugeValue, float64(s.Date)*1000, s.Title, s.URL, s.Feedlabel)
}
Expand Down
14 changes: 14 additions & 0 deletions internal/model/game_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package model

type GameInfo struct {
Response struct {
Games []struct {
Appid int `json:"appid"`
Playtime2Weeks int `json:"playtime_2weeks"`
PlaytimeForever int `json:"playtime_forever"`
PlaytimeWindowsForever int `json:"playtime_windows_forever"`
PlaytimeMacForever int `json:"playtime_mac_forever"`
PlaytimeLinuxForever int `json:"playtime_linux_forever"`
} `json:"games"`
} `json:"response"`
}

0 comments on commit 8366533

Please sign in to comment.