summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/metrics/MetricsStarter.java
blob: 2c531414245c0959a732f7c57367376f554f6aa3 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package net.ess3.metrics;

import java.util.Locale;
import java.util.logging.Level;
import static net.ess3.I18n._;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import net.ess3.economy.register.Method;
import net.ess3.economy.register.methods.VaultEco;
import net.ess3.metrics.Metrics.Graph;
import net.ess3.metrics.Metrics.Plotter;


public class MetricsStarter implements Runnable
{
	private final IEssentials ess;
	private transient Boolean start;


	private enum Modules
	{
		Essentials,
		Essentials2Compat,
		EssentialsChat,
		EssentialsExtra,
		EssentialsSigns,
		EssentialsUpdate,
		EssentialsAntiBuild,
		EssentialsSpawn,
		EssentialsProtect,
		EssentialsGeoIP,
		EssentialsXMPP
	}


	;

	public MetricsStarter(final IEssentials plugin)
	{
		ess = plugin;

		try
		{

			final Metrics metrics = new Metrics(ess.getPlugin());
			ess.setMetrics(metrics);

			ISettings settings = ess.getSettings();
			if (!metrics.isOptOut())
			{
				if (settings.getData().getGeneral().getMetricsEnabled() == true)
				{
					start = true;
				}
				else if (settings.getData().getGeneral().getMetricsEnabled() == null)
				{
					ess.getLogger().info(_("metrics1"));
					ess.getLogger().info(_("metrics2"));
					ess.getLogger().info(_("metrics4"));
					start = false;
				}
			}
		}
		catch (Exception ex)
		{
			metricsError(ex);
		}
	}

	@Override
	public void run()
	{
		try
		{
			final Metrics metrics = ess.getMetrics();

			final Graph moduleGraph = metrics.createGraph("Modules Used");
			for (Modules module : Modules.values())
			{
				final String moduleName = module.toString();
				if (ess.getServer().getPluginManager().isPluginEnabled(moduleName))
				{
					moduleGraph.addPlotter(new SimplePlotter(moduleName));
				}
			}

			final Graph localeGraph = metrics.createGraph("Locale");
			localeGraph.addPlotter(new SimplePlotter(ess.getI18n().getCurrentLocale().getDisplayLanguage(Locale.ENGLISH)));

			final Graph featureGraph = metrics.createGraph("Features");
			featureGraph.addPlotter(
					new Plotter("Unique Accounts")
					{
						@Override
						public int getValue()
						{
							return ess.getUserMap().getUniqueUsers();
						}
					});
			featureGraph.addPlotter(
					new Plotter("Jails")
					{
						@Override
						public int getValue()
						{
							return ess.getJails().getCount();
						}
					});
			featureGraph.addPlotter(
					new Plotter("Kits")
					{
						@Override
						public int getValue()
						{
							int size = 0;
							try
							{
								size = ess.getKits().getList().size();
							}
							catch (Exception ex)
							{
							}
							return size;

						}
					});
			featureGraph.addPlotter(
					new Plotter("Warps")
					{
						@Override
						public int getValue()
						{
							return ess.getWarps().getList().size();
						}
					});

			final Graph enabledGraph = metrics.createGraph("EnabledFeatures");
			enabledGraph.addPlotter(new SimplePlotter("Total"));

			ISettings settings = ess.getSettings();
			final String BKcommand = settings.getData().getGeneral().getBackup().getCommand();
			;
			if (BKcommand != null && !"".equals(BKcommand))
			{
				enabledGraph.addPlotter(new SimplePlotter("Backup"));
			}
			if (ess.getJails().getCount() > 0)
			{
				enabledGraph.addPlotter(new SimplePlotter("Jails"));
			}
			if (ess.getKits().getList().size() > 0)
			{
				enabledGraph.addPlotter(new SimplePlotter("Kits"));
			}
			if (ess.getWarps().getList().size() > 0)
			{
				enabledGraph.addPlotter(new SimplePlotter("Warps"));
			}
			if (ess.getSettings().getData().getCommands().getAfk().getAutoAFK() > 0)
			{
				enabledGraph.addPlotter(new SimplePlotter("AutoAFK"));
			}
			if (ess.getSettings().getData().getChat().getChangeDisplayname())
			{
				enabledGraph.addPlotter(new SimplePlotter("DisplayName"));
			}
			if (ess.getSettings().getData().getChat().getLocalRadius() >= 1)
			{
				enabledGraph.addPlotter(new SimplePlotter("LocalChat"));
			}

			final Graph depGraph = metrics.createGraph("Dependencies");
			final Method method = ess.getPaymentMethod().getMethod();
			if (method != null)
			{
				String version;
				if (method instanceof VaultEco)
				{
					version = ((VaultEco)method).getEconomy();
				}
				else
				{
					version = method.getVersion();
					final int dashPosition = version.indexOf('-');
					if (dashPosition > 0)
					{
						version = version.substring(0, dashPosition);
					}
				}
				depGraph.addPlotter(new SimplePlotter(method.getName() + " " + version));
			}
			metrics.start();

		}
		catch (Exception ex)
		{
			metricsError(ex);
		}
	}

	public void metricsError(final Exception ex)
	{
		if (ess.getSettings().isDebug())
		{
			ess.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage(), ex);
		}
		else
		{
			ess.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
		}
	}

	public Boolean getStart()
	{
		return start;
	}


	private class SimplePlotter extends Plotter
	{
		public SimplePlotter(final String name)
		{
			super(name);
		}

		@Override
		public int getValue()
		{
			return 1;
		}
	}
}