Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tsc-sdk): add tsc-sdk for alert #125

Merged
merged 15 commits into from
Jul 19, 2022
22 changes: 22 additions & 0 deletions Masa.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.M
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.UoW.EF", "src\Data\Masa.Contrib.Data.UoW.EF\Masa.Contrib.Data.UoW.EF.csproj", "{FC4E526A-DBFC-406A-8ED3-64983B67F688}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.BasicAbility.Tsc.Tests", "test\Masa.Contrib.BasicAbility.Tsc.Test\Masa.Contrib.BasicAbility.Tsc.Tests.csproj", "{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.BasicAbility.Tsc", "src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj", "{C265268A-F311-4B6A-915E-C1AF9D1EB624}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1198,6 +1202,22 @@ Global
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|Any CPU.Build.0 = Release|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.ActiveCfg = Release|Any CPU
{FD35BEFA-512C-4483-B5C0-ADCA35680315}.Release|x64.Build.0 = Release|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.ActiveCfg = Debug|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Debug|x64.Build.0 = Debug|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|Any CPU.Build.0 = Release|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.ActiveCfg = Release|Any CPU
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22}.Release|x64.Build.0 = Release|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.ActiveCfg = Debug|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Debug|x64.Build.0 = Debug|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|Any CPU.Build.0 = Release|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.ActiveCfg = Release|Any CPU
{C265268A-F311-4B6A-915E-C1AF9D1EB624}.Release|x64.Build.0 = Release|Any CPU
{23582B50-BAEA-4A2D-82A4-345A3C30124E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23582B50-BAEA-4A2D-82A4-345A3C30124E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23582B50-BAEA-4A2D-82A4-345A3C30124E}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -1467,6 +1487,8 @@ Global
{DB2B4DA3-EEF2-49AA-93A4-B00C25210A68} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{D91C3145-C31C-4301-A493-96F94C903CF2} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68}
{FD35BEFA-512C-4483-B5C0-ADCA35680315} = {DB2B4DA3-EEF2-49AA-93A4-B00C25210A68}
{FED315CF-7CA2-4653-AD37-BC0C1D7FFD22} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{C265268A-F311-4B6A-915E-C1AF9D1EB624} = {DC578D74-98F0-4F19-A230-CFA8DAEE0AF1}
{23582B50-BAEA-4A2D-82A4-345A3C30124E} = {59DA3D5F-9E39-4173-8C31-126967CC189F}
{1371F22B-FDD7-43B0-8E7D-E68DA31F07F3} = {38E6C400-90C0-493E-9266-C1602E229F1B}
{FF3FD53D-D23E-48AC-98B6-3B028B4DE8AF} = {0D34A7F0-DC77-4789-A136-93089CBD15C3}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.Contrib.BasicAbility.Tsc.Tests")]
namespace Masa.Utils.Caller.Core;

internal static class CallerProviderExtensions
{
public static async Task<TResult> GetByBodyAsync<TResult>(this ICallerProvider caller, string url, object body) where TResult : class
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
if (body != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body is not null, if body is nullable, its type is object?

{
request.Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
}
return (await caller.SendAsync<TResult>(request,default)) ?? default!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.Contrib.BasicAbility.Tsc.Tests")]
namespace Masa.Contrib.BasicAbility.Tsc;

public static class ServiceExtensions
{
private const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.tsc";

public static IServiceCollection AddTscClient(this IServiceCollection services, string tscServiceBaseUri)
{
ArgumentNullException.ThrowIfNull(tscServiceBaseUri, nameof(tscServiceBaseUri));

if (services.Any(service => service.ServiceType == typeof(ITscClient)))
return services;

services.AddCaller(builder =>
{
builder.UseHttpClient(options =>
{
options.BaseAddress = tscServiceBaseUri;
options.Name = DEFAULT_CLIENT_NAME;
});
});

services.AddSingleton<ITscClient>(serviceProvider =>
{
var caller = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
var pmCaching = new TscClient(caller);
return pmCaching;
});

return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace System.IO;

public static class StreamExtenistions
public static class StreamExtensions
{
private static readonly Encoding _defaultEncoding = Encoding.UTF8;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caller.Core" Version="0.5.0-preview.3" />
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="0.5.0-preview.3" />
<PackageReference Include="OpenTelemetry" Version="1.3.0-beta.1" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.ElasticsearchClient" Version="1.0.0-beta2" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta2" />
Expand All @@ -19,4 +21,8 @@
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Tsc.Service;

public class LogService : ILogService
{
private readonly ICallerProvider _caller;
internal const string AGGREGATION_URI = "/api/log/aggregation";
internal const string LATEST_URI = "/api/log/latest";
internal const string FIELD_URI = "/api/log/field";

public LogService(ICallerProvider caller)
{
_caller = caller;
}

public async Task<IEnumerable<KeyValuePair<string, string>>> GetAggregationAsync(LogAggregationRequest query)
{
return (await _caller.GetAsync<IEnumerable<KeyValuePair<string, string>>>(AGGREGATION_URI, query)) ?? default!;
}

public async Task<IEnumerable<string>> GetFieldsAsync()
{
return (await _caller.GetAsync<IEnumerable<string>>(FIELD_URI)) ?? default!;
}

public async Task<object> GetLatestAsync(LogLatestRequest query)
{
return (await _caller.GetAsync<object>(LATEST_URI, query)) ?? default!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Tsc.Service;

internal class MetricService : IMetricService
{
private readonly ICallerProvider _caller;
internal const string RANGEVALUES_URL = "/api/metric/range-values";
internal const string NAMES_URI = "/api/metric/names";
internal const string LABELVALUES_URI = "/api/metric/label-values";

public MetricService(ICallerProvider caller)
{
_caller = caller;
}

public async Task<IEnumerable<string>> GetMetricNamesAsync(IEnumerable<string>? match = default)
{
string param = default!;
if (match != null && match.Any(s => !string.IsNullOrEmpty(s)))
{
param = string.Join(',', match.Where(s => !string.IsNullOrEmpty(s)));
}
return (await _caller.GetAsync<IEnumerable<string>>(NAMES_URI, new { match = param })) ?? default!;
}

public async Task<Dictionary<string, List<string>>> GetLabelAndValuesAsync(MetricLableValuesRequest query)
{
var data = await _caller.GetByBodyAsync<Dictionary<string, Dictionary<string, List<string>>>>(LABELVALUES_URI, query);
if (data == null || !data.ContainsKey(query.Match))
return default!;

return data[query.Match];
}

public async Task<string> GetMetricValuesAsync(MetricRangeValueRequest query)
{
if (query.Lables != null && !string.IsNullOrEmpty(query.Match))
{
query.Match = $"{query.Match}{{{string.Join(',',query.Lables)}}}";
}

return (await _caller.GetByBodyAsync<string>(RANGEVALUES_URL, query)) ?? default!;
}
}
18 changes: 18 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/TscClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

[assembly: InternalsVisibleTo("Masa.Contrib.BasicAbility.Tsc.Tests")]
namespace Masa.Contrib.BasicAbility.Tsc;

internal class TscClient : ITscClient
{
public TscClient(ICallerProvider callerProvider)
{
LogService = new LogService(callerProvider);
MetricService = new MetricService(callerProvider);
}

public ILogService LogService { get; }

public IMetricService MetricService { get; }
}
8 changes: 8 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Tsc/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.BasicAbility.Tsc;
global using Masa.BuildingBlocks.BasicAbility.Tsc.Model;
global using Masa.BuildingBlocks.BasicAbility.Tsc.Service;
global using Masa.Contrib.BasicAbility.Tsc;
global using Masa.Contrib.BasicAbility.Tsc.Service;
global using Masa.Utils.Caller.Core;
global using Masa.Utils.Caller.HttpClient;
global using Microsoft.AspNetCore.Http;
global using Microsoft.Extensions.DependencyInjection;
global using OpenTelemetry.Contrib.Instrumentation.ElasticsearchClient;
global using OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore;
global using OpenTelemetry.Instrumentation.AspNetCore;
Expand All @@ -15,4 +22,5 @@
global using System.Diagnostics;
global using System.Net;
global using System.Net.Http.Headers;
global using System.Runtime.CompilerServices;
global using System.Text;
2 changes: 1 addition & 1 deletion src/BuildingBlocks/MASA.BuildingBlocks
Submodule MASA.BuildingBlocks updated 19 files
+1 −1 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Auth.Contracts/Model/UserModel.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Enums/LogAggTypes.cs
+11 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/ITscClient.cs
+9 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Masa.BuildingBlocks.BasicAbility.Tsc.csproj
+13 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Model/Log/FieldAggregationRequest.cs
+15 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Model/Log/LogAggregationRequest.cs
+15 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Model/Log/LogLatestRequest.cs
+19 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Model/Metric/MetricLableValuesRequest.cs
+16 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Model/Metric/MetricRangeValueRequest.cs
+13 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Service/ILogService.cs
+13 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/Service/IMetricService.cs
+7 −0 src/BasicAbility/Masa.BuildingBlocks.BasicAbility.Tsc/_Imports.cs
+0 −7 src/Data/Masa.BuildingBlocks.Data.UoW/IUnitOfWork.cs
+1 −1 src/Data/Masa.BuildingBlocks.Data/IdGenerator/IIdGenerator.cs
+0 −2 src/Ddd/Masa.BuildingBlocks.Ddd.Domain/Events/IDomainEventBus.cs
+41 −19 src/Ddd/Masa.BuildingBlocks.Ddd.Domain/Repositories/BaseRepository.cs
+1 −0 src/Ddd/Masa.BuildingBlocks.Ddd.Domain/_Imports.cs
+1 −7 src/Dispatcher/Masa.BuildingBlocks.Dispatcher.Events/IMiddleware.cs
+0 −11 src/Dispatcher/Masa.BuildingBlocks.Dispatcher.Events/Middleware.cs
21 changes: 21 additions & 0 deletions test/Masa.Contrib.BasicAbility.Tsc.Test/Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using System.Net.Http;
using System.Text;

namespace Masa.Contrib.BasicAbility.Tsc.Tests;

internal class Common
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete blank lines

public static HttpRequestMessage CreateMessage(string url, object body)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
if (body != null)
{
request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
}
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Tsc.Tests.Extensions;

[TestClass]
public class CallerProviderExtensionsTests
{
[TestMethod]
public async Task GetByBodyAsyncTest()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name should reflect what you want to test, it can be longer

{
var callerProvider = new Mock<ICallerProvider>();
string url = "http://locahost:80/test";
var param = new { name = "name" };
var result = "ok";
callerProvider.Setup(provider => provider.SendAsync<string>(It.IsAny<HttpRequestMessage>(), default)).ReturnsAsync(result);
var str = await callerProvider.Object.GetByBodyAsync<string>(url, "name");
Assert.IsNotNull(str);
Assert.AreEqual(result, str);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" />
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" />
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" />
<PackageReference Include="coverlet.collector" Version="$(CoverletPackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\BasicAbility\Masa.Contrib.BasicAbility.Tsc\Masa.Contrib.BasicAbility.Tsc.csproj" />
<ProjectReference Include="..\..\src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Tsc\Masa.BuildingBlocks.BasicAbility.Tsc.csproj" />
</ItemGroup>

</Project>
Loading