Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.

Commit 340e2b8

Browse files
committed
Improve regex and logging
1 parent 572376a commit 340e2b8

File tree

2 files changed

+82
-69
lines changed

2 files changed

+82
-69
lines changed

APITracker/APITracker.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ public override void OnApplicationStart()
1010
Patches.Init(HarmonyInstance, LoggerInstance);
1111
}
1212

13-
uint updates = 0;
14-
public override void OnLateUpdate()
15-
{
16-
if (updates++ >= 600)
17-
{
18-
updates = 0;
19-
RateTracker.Log(LoggerInstance);
20-
}
21-
}
22-
}
13+
public override void OnApplicationQuit()
14+
{
15+
RateTracker.Stop();
16+
}
17+
18+
}
2319
}

APITracker/RateTracker.cs

+76-59
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Text.RegularExpressions;
7+
using System.Threading;
78

89
namespace APITracker
910
{
@@ -23,37 +24,32 @@ class SpecificRateTracker
2324
[JsonProperty(PropertyName = "max")]
2425
uint maxTriggers = uint.MinValue;
2526
[JsonIgnore]
26-
bool updated = false;
27+
bool changed = false;
2728

2829
public SpecificRateTracker(int mins, int secs)
2930
{
3031
FrameStart = DateTime.Now;
3132
FrameSize = new TimeSpan(0, mins, secs);
3233
}
3334

34-
public void Trigger(DateTime now)
35-
{
36-
if (now > FrameStart + FrameSize)
37-
{
38-
if (Triggers < minTriggers) { minTriggers = Triggers; updated = true; }
39-
if (Triggers > maxTriggers) { maxTriggers = Triggers; updated = true; }
40-
41-
FrameStart = now;
42-
Triggers = 0;
43-
}
44-
else
45-
{
46-
Triggers++;
47-
}
48-
}
49-
50-
public string GetStatus()
51-
{
52-
if (!updated) return null;
53-
updated = false;
54-
55-
return $"Time({FrameSize}) Min({minTriggers}) Max({maxTriggers})";
56-
}
35+
public bool Update(DateTime now)
36+
{
37+
if (now > FrameStart + FrameSize)
38+
{
39+
if (Triggers < minTriggers) { minTriggers = Triggers; changed = true; }
40+
if (Triggers > maxTriggers) { maxTriggers = Triggers; changed = true; }
41+
42+
FrameStart = now;
43+
Triggers = 0;
44+
}
45+
return changed;
46+
}
47+
48+
public void Trigger(DateTime now)
49+
{
50+
Update(now);
51+
Triggers++;
52+
}
5753
}
5854
class EndPointInfo
5955
{
@@ -83,37 +79,26 @@ public EndPointInfo(string url)
8379
};
8480
}
8581

86-
8782
public void Trigger(DateTime now)
8883
{
8984
foreach (SpecificRateTracker tracker in Trackers) {
9085
tracker.Trigger(now);
9186
}
9287
}
9388

94-
public bool Log(MelonLogger.Instance loggerInstance)
89+
public bool Update(DateTime now)
9590
{
96-
uint updates = 0;
97-
string msg = $"Endpoint {Url}\n";
91+
bool updated = false;
9892
foreach (SpecificRateTracker tracker in Trackers)
9993
{
100-
string status = tracker.GetStatus();
101-
if (status != null)
102-
{
103-
updates++;
104-
msg += $"\t\t{status}\n";
105-
}
106-
}
107-
if (updates != 0)
108-
{
109-
loggerInstance.Msg(msg);
110-
return true;
94+
updated |= tracker.Update(now);
11195
}
112-
return false;
96+
return updated;
11397
}
11498
}
11599
static Dictionary<string, EndPointInfo> endpointTimings = new Dictionary<string, EndPointInfo>();
116-
100+
static bool run;
101+
static Thread updateThread;
117102
public static void Init()
118103
{
119104
try
@@ -124,16 +109,36 @@ public static void Init()
124109
{
125110
endpointTimings = new Dictionary<string, EndPointInfo>();
126111
}
112+
run = true;
113+
updateThread = new Thread(Update);
114+
updateThread.Start();
127115
}
128116

129117
public static void RequestTriggered(String url)
130118
{
131119
var requestTime = DateTime.Now;
132120

133-
url = Regex.Replace(url.Split('?')[0], @"(\w*_[0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})", "[VRCID]");
134-
url = Regex.Replace(url, @"(\[VRCID\]:[0-9]*)", "[VRCID]:[INSTANCEID]");
135-
url = Regex.Replace(url, @"(~region\([a-z]*\))", "~region([REGION])");
136-
url = Regex.Replace(url, @"([A-Z0-9]{48})", "[NONCE]");
121+
url = Regex.Replace(
122+
Regex.Replace(
123+
Regex.Replace(
124+
Regex.Replace(
125+
Regex.Replace(
126+
url.Split('?')[0],
127+
128+
@"([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})",
129+
"[GUID]"),
130+
131+
@"(\w+_\[GUID\])",
132+
"[VRCID]"),
133+
134+
@"(\[VRCID\]:[^~/]+)",
135+
"[VRCID]:[INSTANCEID]"),
136+
137+
@"(~region\([^\)]+\))",
138+
"~region([REGION])"),
139+
140+
@"(~nonce\([^\)]+\))",
141+
"~nonce([NONCE])");
137142

138143
lock (endpointTimings)
139144
{
@@ -147,20 +152,32 @@ public static void RequestTriggered(String url)
147152
}
148153
}
149154

150-
public static void Log(MelonLogger.Instance loggerInstance)
151-
{
152-
bool updated = false;
153-
lock (endpointTimings)
154-
{
155-
foreach (var endpoint in endpointTimings)
156-
{
157-
updated |= endpoint.Value.Log(loggerInstance);
158-
}
159-
if (updated)
160-
{
161-
File.WriteAllText("endpointlimits.json", JsonConvert.SerializeObject(endpointTimings));
162-
}
163-
}
155+
public static void Stop()
156+
{
157+
run = false;
158+
updateThread.Join();
159+
}
160+
161+
private static void Update()
162+
{
163+
while (run)
164+
{
165+
var updateTime = DateTime.Now;
166+
167+
bool updated = false;
168+
lock (endpointTimings)
169+
{
170+
foreach (var endpoint in endpointTimings)
171+
{
172+
updated |= endpoint.Value.Update(updateTime);
173+
}
174+
if (updated)
175+
{
176+
File.WriteAllText("endpointlimits.json", JsonConvert.SerializeObject(endpointTimings, Formatting.Indented));
177+
}
178+
}
179+
Thread.Sleep(10);
180+
}
164181
}
165182
}
166183
}

0 commit comments

Comments
 (0)