IPerformanceDB.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Elasticsearch.Net;
  2. using Nest;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace XYY.PerformanceCount.Standard
  8. {
  9. /// <summary>
  10. /// 日志计数服务实现
  11. /// </summary>
  12. public interface IPerformanceDB
  13. {
  14. Task AddLog(MethodPerformanceModel menthodPerformance);
  15. Task<IReadOnlyCollection<MethodPerformanceModel>> QueryLog(Query query);
  16. }
  17. public class ESPerformanceDB : IPerformanceDB
  18. {
  19. string indexName = "performance_count_1";
  20. public ESPerformanceDB(string dbConnection)
  21. {
  22. string esNodeUrl = dbConnection;
  23. Uri node = new Uri(esNodeUrl);
  24. ConnectionSettings settings = new ConnectionSettings(node).DefaultMappingFor<MethodPerformanceModel>(x =>
  25. x.IdProperty(i => i.Id).IndexName(indexName).TypeName("doc")).DefaultIndex(indexName);
  26. //3s超时
  27. settings.RequestTimeout(new TimeSpan(0, 0, 3));
  28. IIndexState indexState = new IndexState()
  29. {
  30. Settings = new IndexSettings()
  31. {
  32. NumberOfReplicas = 0,//副本数
  33. NumberOfShards = 1//分片数
  34. }
  35. };
  36. _client = new ElasticClient(settings);
  37. if (!_client.IndexExists(indexName).Exists)
  38. {
  39. _client.CreateIndex(indexName, p => p.InitializeUsing(indexState));
  40. }
  41. }
  42. private ElasticClient _client;
  43. public async Task AddLog(MethodPerformanceModel menthodPerformance)
  44. {
  45. await _client.IndexDocumentAsync(menthodPerformance);
  46. }
  47. public async Task<IReadOnlyCollection<MethodPerformanceModel>> QueryLog(Query query)
  48. {
  49. if (!string.IsNullOrEmpty(query.SearchParameter))
  50. {
  51. var sq1 = new Func<QueryContainerDescriptor<MethodPerformanceModel>, QueryContainer>(q => q.Bool(b =>
  52. b.Filter(f =>
  53. f.DateRange(r =>
  54. r.Field("startTime")
  55. .GreaterThanOrEquals(query.StartTime)
  56. .LessThan(query.EndTime)
  57. )
  58. ,
  59. f =>
  60. f.Bool(
  61. b2 => b2.Must(
  62. m => m.MatchPhrase(
  63. mp => mp.Field("mehtodName").Query(
  64. query.MethodName)))),
  65. f =>
  66. f.Range(r =>
  67. r.Field("executionTime")
  68. .GreaterThanOrEquals(query.ExecutionTime)
  69. ),
  70. f =>
  71. f.Bool(
  72. b2 => b2.Must(
  73. m => m.MatchPhrase(
  74. mp => mp.Field("requestParameters").Query(
  75. query.SearchParameter))))
  76. )));
  77. var result = await _client.SearchAsync<MethodPerformanceModel>(s =>
  78. s.Query(sq1).Size(query.Size).From(query.Size * (query.Index - 1)));
  79. return result.Documents;
  80. }
  81. else
  82. {
  83. var sq1 = new Func<QueryContainerDescriptor<MethodPerformanceModel>, QueryContainer>(q => q.Bool(b =>
  84. b.Filter(f =>
  85. f.DateRange(r =>
  86. r.Field("startTime")
  87. .GreaterThanOrEquals(query.StartTime)
  88. .LessThan(query.EndTime)
  89. )
  90. ,
  91. f =>
  92. f.Range(r =>
  93. r.Field("executionTime")
  94. .GreaterThanOrEquals(query.ExecutionTime)
  95. ),
  96. f =>
  97. f.Bool(
  98. b2 => b2.Must(
  99. m => m.MatchPhrase(
  100. mp => mp.Query(
  101. query.MethodName))))
  102. )));
  103. var result = await _client.SearchAsync<MethodPerformanceModel>(s =>
  104. s.Query(sq1).Size(query.Size).From(query.Size * (query.Index - 1)));
  105. return result.Documents;
  106. }
  107. }
  108. }
  109. }