aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/TelemetryProvider.kt
blob: d9c7108c9d61ea5cc0c0d0752a61b1b89db2c0ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package moe.nea.ledger

import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import moe.nea.ledger.gen.BuildConfig
import moe.nea.ledger.utils.di.DI
import moe.nea.ledger.utils.di.DIProvider
import moe.nea.ledger.utils.telemetry.CommonKeys
import moe.nea.ledger.utils.telemetry.ContextValue
import moe.nea.ledger.utils.telemetry.EventRecorder
import moe.nea.ledger.utils.telemetry.JsonElementContext
import moe.nea.ledger.utils.telemetry.LoggingEventRecorder
import moe.nea.ledger.utils.telemetry.Span
import net.minecraft.client.Minecraft
import net.minecraft.util.Session
import net.minecraftforge.fml.common.Loader

object TelemetryProvider {
	fun injectTo(di: DI) {
		di.register(
			EventRecorder::class.java,
			if (DevUtil.isDevEnv) DIProvider.singeleton(LoggingEventRecorder(Ledger.logger, true))
			else DIProvider.singeleton(
				LoggingEventRecorder(Ledger.logger, false)) // TODO: replace with upload to server
		)
	}

	val USER = "minecraft_user"
	val MINECRAFT_VERSION = "minecraft_version"
	val MODS = "mods"

	class MinecraftUser(val session: Session) : ContextValue {
		override fun serialize(): JsonElement {
			val obj = JsonObject()
			obj.addProperty("uuid", session.playerID)
			obj.addProperty("name", session.username)
			return obj
		}
	}

	fun setupDefaultSpan() {
		val sp = Span.current()
		sp.add(USER, MinecraftUser(Minecraft.getMinecraft().session))
		sp.add(MINECRAFT_VERSION, ContextValue.compound(
			"static" to "1.8.9",
			"rt" to Minecraft.getMinecraft().version,
		))
		val mods = JsonArray()
		Loader.instance().activeModList.map {
			val obj = JsonObject()
			obj.addProperty("id", it.modId)
			obj.addProperty("version", it.version)
			obj.addProperty("displayVersion", it.displayVersion)
			obj
		}.forEach(mods::add)
		sp.add(MODS, JsonElementContext(mods))
		sp.add(CommonKeys.VERSION, ContextValue.string(BuildConfig.FULL_VERSION))
		sp.add(CommonKeys.COMMIT_VERSION, ContextValue.string(BuildConfig.GIT_COMMIT))
	}

	fun setupFor(di: DI) {
		injectTo(di)
		setupDefaultSpan()
	}
}