Update StatisticsHandler.cs

pull/70/head
2dust 2019-08-29 17:30:44 +08:00
parent 3cdff624ba
commit 3154df1bde
1 changed files with 116 additions and 66 deletions

View File

@ -36,7 +36,7 @@ namespace v2rayN.Handler
public ulong TotalDown { get; private set; }
public List<Mode.ServerStatistics> Statistic{ get; set; }
public List<Mode.ServerStatistics> Statistic { get; set; }
public ulong Up { get; private set; }
@ -71,7 +71,7 @@ namespace v2rayN.Handler
workThread_.Start();
}
private void grpcInit ()
private void grpcInit()
{
channel_ = new Channel($"127.0.0.1:{Global.InboundAPIPort}", ChannelCredentials.Insecure);
channel_.ConnectAsync();
@ -80,9 +80,17 @@ namespace v2rayN.Handler
public void Close()
{
try
{
exitFlag_ = true;
channel_.ShutdownAsync();
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
public void run()
{
@ -92,11 +100,22 @@ namespace v2rayN.Handler
{
if (enabled_ && channel_.State == ChannelState.Ready)
{
var res = client_.QueryStats(new QueryStatsRequest() { Pattern = "", Reset = true });
QueryStatsResponse res = null;
try
{
res = client_.QueryStats(new QueryStatsRequest() { Pattern = "", Reset = true });
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
if (res != null)
{
var addr = config_.address();
var port = config_.port();
var cur = Statistic.FindIndex(item => item.address == addr && item.port == port);
var path = config_.path();
var cur = Statistic.FindIndex(item => item.address == addr && item.port == port && item.path == path);
ulong up = 0,
down = 0;
@ -109,7 +128,7 @@ namespace v2rayN.Handler
TotalUp += up;
TotalDown += down;
if(cur != -1)
if (cur != -1)
{
Statistic[cur].todayUp += up;
Statistic[cur].todayDown += down;
@ -119,11 +138,16 @@ namespace v2rayN.Handler
if (UpdateUI)
updateFunc_(TotalUp, TotalDown, Up, Down, Statistic);
}
Thread.Sleep(config_.statisticsFreshRate);
channel_.ConnectAsync();
}
}
catch { }
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
}
@ -131,8 +155,10 @@ namespace v2rayN.Handler
{
up = 0; down = 0;
try
{
foreach(var stat in source)
foreach (var stat in source)
{
var name = stat.Name;
var value = stat.Value;
@ -157,10 +183,15 @@ namespace v2rayN.Handler
}
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
public void saveToFile()
{
if(!Directory.Exists(logPath_))
if (!Directory.Exists(logPath_))
{
Directory.CreateDirectory(logPath_);
}
@ -184,13 +215,16 @@ namespace v2rayN.Handler
overallWriter.WriteLine($"LastUpdate {DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()}");
overallWriter.WriteLine($"UP {string.Format("{0:f2}", up_amount)}{up_unit} {TotalUp}");
overallWriter.WriteLine($"DOWN {string.Format("{0:f2}", down_amount)}{down_unit} {TotalDown}");
foreach(var s in Statistic)
foreach (var s in Statistic)
{
overallWriter.WriteLine($"* {s.name} {s.address} {s.port} {s.path} {s.host} {s.totalUp} {s.totalDown}");
}
}
}
catch { }
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
// 当天流量记录文件
var dailyPath = Path.Combine(logPath_, $"{DateTime.Now.ToLongDateString()}.txt");
@ -209,7 +243,10 @@ namespace v2rayN.Handler
}
}
}
catch { }
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
public void loadFromFile()
@ -278,7 +315,10 @@ namespace v2rayN.Handler
}
}
}
catch { }
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
// 当天流量记录文件
@ -324,12 +364,17 @@ namespace v2rayN.Handler
}
}
}
catch { }
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
}
private void DeleteExpiredLog()
{
try
{
if (!Directory.Exists(logPath_)) return;
var dirInfo = new DirectoryInfo(logPath_);
@ -341,11 +386,16 @@ namespace v2rayN.Handler
var ft = DateTime.Parse(name);
var ct = DateTime.Now;
var dur = ct - ft;
if(dur.Days > config_.CacheDays)
if (dur.Days > config_.CacheDays)
{
file.Delete();
}
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
}
}