From c611239c781394c7a4fd8e5184061143982bc2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Wed, 20 Mar 2019 13:09:30 -0300 Subject: [PATCH] Plugin CoreMetrics (#68) * Plugin coreMetrics. * Organize project files * metric prefix --- CoreMetrics/CoreMetrics.cs | 90 ++++++++++++++++++++++++++++++++++ CoreMetrics/CoreMetrics.csproj | 13 +++++ neo-plugins.sln | 6 +++ 3 files changed, 109 insertions(+) create mode 100644 CoreMetrics/CoreMetrics.cs create mode 100644 CoreMetrics/CoreMetrics.csproj diff --git a/CoreMetrics/CoreMetrics.cs b/CoreMetrics/CoreMetrics.cs new file mode 100644 index 000000000..a1c10af5d --- /dev/null +++ b/CoreMetrics/CoreMetrics.cs @@ -0,0 +1,90 @@ +using Microsoft.AspNetCore.Http; +using Neo.IO.Json; +using Neo.Ledger; +using Neo.Network.P2P.Payloads; +using Neo.Persistence; + +namespace Neo.Plugins +{ + public class CoreMetrics : Plugin, IRpcPlugin + { + public override void Configure() + { + } + + public void PreProcess(HttpContext context, string method, JArray _params) + { + } + + public JObject OnProcess(HttpContext context, string method, JArray _params) + { + switch (method) + { + case "getmetricblocktimestamp": + { + + uint nBlocks = (uint)_params[0].AsNumber(); + uint lastHeight = _params.Count >= 2 ? lastHeight = (uint)_params[1].AsNumber() : 0; + return GetBlocksTime(nBlocks, lastHeight); + } + default: + return null; + } + } + + public void PostProcess(HttpContext context, string method, JArray _params, JObject result) + { + } + + private JObject GetBlocksTime(uint nBlocks, uint lastHeight) + { + // It is currently limited to query blocks generated in the last 24hours (86400 seconds) + uint maxNBlocksPerDay = 86400 / Blockchain.SecondsPerBlock; + if (lastHeight != 0) + { + if (lastHeight >= Blockchain.Singleton.Height) + { + JObject json = new JObject(); + return json["error"] = "Requested height to start " + lastHeight + " exceeds " + Blockchain.Singleton.Height; + } + + if (nBlocks > lastHeight) + { + JObject json = new JObject(); + return json["error"] = "Requested " + nBlocks + " blocks timestamps is greater than starting at height " + lastHeight; + } + } + + if (nBlocks > maxNBlocksPerDay) + { + JObject json = new JObject(); + return json["error"] = "Requested number of blocks timestamps exceeds " + maxNBlocksPerDay; + } + + if (nBlocks >= Blockchain.Singleton.Height) + { + JObject json = new JObject(); + return json["error"] = "Requested number of blocks timestamps exceeds quantity of known blocks " + Blockchain.Singleton.Height; + } + + if (nBlocks <= 0) + { + JObject json = new JObject(); + return json["error"] = "Requested number of block times can not be <= 0"; + } + + JArray array = new JArray(); + uint heightToBegin = lastHeight > 0 ? lastHeight - nBlocks : Blockchain.Singleton.Height - nBlocks; + for (uint i = heightToBegin; i <= Blockchain.Singleton.HeaderHeight; i++) + { + JObject json = new JObject(); + Header header = Blockchain.Singleton.Store.GetHeader(i); + json["timestamp"] = header.Timestamp; + json["height"] = i; + array.Add(json); + } + + return array; + } + } +} diff --git a/CoreMetrics/CoreMetrics.csproj b/CoreMetrics/CoreMetrics.csproj new file mode 100644 index 000000000..a88f11518 --- /dev/null +++ b/CoreMetrics/CoreMetrics.csproj @@ -0,0 +1,13 @@ + + + + 2.10.0 + netstandard2.0 + Neo.Plugins + + + + + + + diff --git a/neo-plugins.sln b/neo-plugins.sln index f6e27ed6b..08db236ee 100644 --- a/neo-plugins.sln +++ b/neo-plugins.sln @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcWallet", "RpcWallet\RpcW EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcNep5Tracker", "RpcNep5Tracker\RpcNep5Tracker.csproj", "{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreMetrics", "CoreMetrics\CoreMetrics.csproj", "{AEFFF003-3500-416B-AD9B-8C838C33C1F4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -56,6 +58,10 @@ Global {BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Debug|Any CPU.Build.0 = Debug|Any CPU {BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Release|Any CPU.ActiveCfg = Release|Any CPU {BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}.Release|Any CPU.Build.0 = Release|Any CPU + {AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AEFFF003-3500-416B-AD9B-8C838C33C1F4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE