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: support team filter rules data & default hidden scheduler create ruels #359

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class GetAlarmHistoryInputDto : PaginatedOptionsDto<GetAlarmHistoryInputD

public Guid Handler { get; set; }

public Guid TeamId { get; set; }

public GetAlarmHistoryInputDto()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public class AlarmRuleDto : AuditEntityDto<Guid, Guid>
public List<AlarmRuleItemDto> Items { get; set; } = new();

public string ModifierName { get; set; } = string.Empty;

public string Source { get; set; } = default!;

public bool Show { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public class AlarmRuleUpsertDto
public List<MetricMonitorItemDto> MetricMonitorItems { get; set; } = new();

public List<AlarmRuleItemDto> Items { get; set; } = new();

public string Source { get; set; } = default!;

public bool Show { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class GetAlarmRuleInputDto : PaginatedOptionsDto<GetAlarmRuleInputDto>

public string MetricId { get; set; } = default!;

public Guid TeamId { get; set; }

public bool Show { get; set; } = true;

public GetAlarmRuleInputDto()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ public class AlarmRuleQueryModel : ISoftDelete
public DateTime ModificationTime { get; set; }

public Guid Modifier { get; set; }

public string Source { get; set; } = default!;

public bool Show { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,19 @@ private async Task<Expression<Func<AlarmHistoryQueryModel, bool>>> CreateFiltere
condition = condition.And(options.AlertSeverity != default, x => x.AlertSeverity == options.AlertSeverity);
condition = condition.And(options.HandleStatus != default, x => x.HandleStatus == options.HandleStatus);
condition = condition.And(options.AlarmRuleId.HasValue, x => x.AlarmRuleId == options.AlarmRuleId);
condition = condition.And(options.Handler != default, x => x.Handler == options.Handler);
return await Task.FromResult(condition); ;
condition = condition.And(options.Handler != Guid.Empty, x => x.Handler == options.Handler);
return await Task.FromResult(condition);
}



private async Task FillAlarmHistoryDtos(List<AlarmHistoryDto> dtos)
{
var userIds = dtos.Where(x => x.Handle.Handler != default).Select(x => x.Handle.Handler).Distinct().ToArray();
var userIds = dtos.Where(x => x.Handle.Handler != Guid.Empty).Select(x => x.Handle.Handler).Distinct().ToArray();
var userInfos = await _authClient.UserService.GetListByIdsAsync(userIds);
foreach (var item in dtos)
{
var handler = userInfos.FirstOrDefault(x => x.Id == item.Handle.Handler);
var handler = userInfos.Find(x => x.Id == item.Handle.Handler);
item.Handle.HandlerName = handler?.RealDisplayName ?? string.Empty;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task UpdateAsync(UpdateAlarmRuleCommand updateCommand)
var queryable = await _repository.WithDetailsAsync();
var entity = await queryable.FirstOrDefaultAsync(x => x.Id == updateCommand.AlarmRuleId);

MasaArgumentException.ThrowIfNull(entity, _i18n.T("AlarmRule"));
MasaArgumentException.ThrowIfNull(entity, _i18n.T("AlarmRule"));

updateCommand.AlarmRule.Adapt(entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Masa.Alert.Application.AlarmRules.Commands;

public record CreateAlarmRuleCommand(AlarmRuleUpsertDto AlarmRule) : Command
{
public Guid Result { get; set; } = default;
public Guid Result { get; set; } = Guid.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task HandleEventAsync(UpsertAlarmRuleJobEvent eto)
ScheduleBlockStrategy = ScheduleBlockStrategyTypes.Cover
};

if (alarmRule.SchedulerJobId == default)
if (alarmRule.SchedulerJobId == Guid.Empty)
{
var jobId = await _schedulerClient.SchedulerJobService.AddAsync(request);
alarmRule.SetSchedulerJobId(jobId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ public class AlarmRuleQueryHandler
{
private readonly IAlertQueryContext _context;
private readonly IAuthClient _authClient;
private readonly IPmClient _pmClient;
private IMultiEnvironmentContext _multiEnvironment;
private readonly II18n<DefaultResource> _i18n;

public AlarmRuleQueryHandler(IAlertQueryContext context, IAuthClient authClient, II18n<DefaultResource> i18n)
public AlarmRuleQueryHandler(IAlertQueryContext context, IAuthClient authClient, II18n<DefaultResource> i18n, IPmClient pmClient, IMultiEnvironmentContext multiEnvironment)
{
_context = context;
_authClient = authClient;
_pmClient = pmClient;
_multiEnvironment = multiEnvironment;
_i18n = i18n;
}

Expand Down Expand Up @@ -52,6 +56,7 @@ private async Task<Expression<Func<AlarmRuleQueryModel, bool>>> CreateFilteredPr
Expression<Func<AlarmRuleQueryModel, bool>> condition = x => true;
condition = condition.And(!string.IsNullOrEmpty(options.Filter), x => x.DisplayName.Contains(options.Filter));
condition = condition.And(options.Type != default, x => x.Type == options.Type);
condition = condition.And(x => x.Show == options.Show);
if (options.TimeType == AlarmRuleSearchTimeTypes.ModificationTime)
{
condition = condition.And(options.StartTime.HasValue, x => x.ModificationTime >= options.StartTime);
Expand All @@ -64,17 +69,48 @@ private async Task<Expression<Func<AlarmRuleQueryModel, bool>>> CreateFilteredPr
}
condition = condition.And(!string.IsNullOrEmpty(options.ProjectIdentity), x => x.ProjectIdentity == options.ProjectIdentity);
condition = condition.And(!string.IsNullOrEmpty(options.AppIdentity), x => x.AppIdentity == options.AppIdentity);
await AppendProjectAppFilterAsync(options, condition);
condition = condition.And(!string.IsNullOrEmpty(options.MetricId), x => x.MetricMonitorItems.Any(x => x.Aggregation.Name == options.MetricId));
return await Task.FromResult(condition); ;
return condition;
}

private async Task AppendProjectAppFilterAsync(GetAlarmRuleInputDto options, Expression<Func<AlarmRuleQueryModel, bool>> condition)
{
var projects = await _pmClient.ProjectService.GetListByTeamIdsAsync(new List<Guid>() { options.TeamId }, _multiEnvironment.CurrentEnvironment);
if (projects == null || !projects.Any())
{
condition = condition.And(x => false);
return;
}

if (!string.IsNullOrEmpty(options.ProjectIdentity))
{
if (!projects.Exists(project => project.Identity == options.ProjectIdentity))
{
condition = condition.And(y => false);
return;
}

if (!string.IsNullOrEmpty(options.AppIdentity))
{
var project = projects.Find(p => p.Identity == options.ProjectIdentity)!;
var apps = await _pmClient.AppService.GetListByProjectIdsAsync(new List<int>() { project.Id });
if (apps == null || !apps.Exists(app => app.Identity == options.AppIdentity))
{
condition = condition.And(y => false);
return;
}
}
}
}

private async Task FillAlarmRuleDtos(List<AlarmRuleDto> dtos)
{
var modifierUserIds = dtos.Where(x => x.Modifier != default).Select(x => x.Modifier).Distinct().ToArray();
var modifierUserIds = dtos.Where(x => x.Modifier != Guid.Empty).Select(x => x.Modifier).Distinct().ToArray();
var userInfos = await _authClient.UserService.GetListByIdsAsync(modifierUserIds);
foreach (var item in dtos)
{
var modifier = userInfos.FirstOrDefault(x => x.Id == item.Modifier);
var modifier = userInfos.Find(x => x.Id == item.Modifier);
item.ModifierName = modifier?.RealDisplayName ?? string.Empty;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Application/Masa.Alert.Application/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
global using Masa.BuildingBlocks.Dispatcher.Events;
global using Masa.BuildingBlocks.Extensions.BackgroundJobs;
global using Masa.BuildingBlocks.Globalization.I18n;
global using Masa.BuildingBlocks.Isolation;
global using Masa.BuildingBlocks.ReadWriteSplitting.Cqrs.Commands;
global using Masa.BuildingBlocks.ReadWriteSplitting.Cqrs.Queries;
global using Masa.BuildingBlocks.RulesEngine;
global using Masa.BuildingBlocks.StackSdks.Auth;
global using Masa.BuildingBlocks.StackSdks.Config;
global using Masa.BuildingBlocks.StackSdks.Pm;
global using Masa.BuildingBlocks.StackSdks.Scheduler;
global using Masa.BuildingBlocks.StackSdks.Scheduler.Enum;
global using Masa.BuildingBlocks.StackSdks.Scheduler.Model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Update(AlertSeverity alertSeverity, bool isNotification, List<RuleRe

public void AddAlarmRuleRecord(DateTimeOffset excuteTime, ConcurrentDictionary<string, long> aggregateResult, bool isTrigger, int consecutiveCount, List<RuleResultItem> ruleResultItems)
{
if (Id == default)
if (Id == Guid.Empty)
{
Id = IdGeneratorFactory.SequentialGuidGenerator.NewId();
}
Expand Down
17 changes: 12 additions & 5 deletions src/Domain/Masa.Alert.Domain/AlarmRules/Aggregates/AlarmRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public class AlarmRule : FullAggregateRoot<Guid, Guid>

public ICollection<AlarmRuleItem> Items { get; protected set; } = new Collection<AlarmRuleItem>();

public Guid SchedulerJobId { get; protected set; } = default;
public Guid SchedulerJobId { get; protected set; } = Guid.Empty;

public string Source { get; protected set; } = default!;

public bool Show { get; protected set; }

private AlarmRule() { }

public AlarmRule(string displayName, AlarmRuleTypes type, string projectIdentity, string appIdentity, string chartYAxisUnit
, bool isGetTotal, string totalVariable, string whereExpression, int continuousTriggerThreshold, SilenceCycle silenceCycle)
, bool isGetTotal, string totalVariable, string whereExpression, int continuousTriggerThreshold, SilenceCycle silenceCycle,
string source, bool show)
{
DisplayName = displayName;
Type = type;
Expand All @@ -46,6 +51,8 @@ public AlarmRule(string displayName, AlarmRuleTypes type, string projectIdentity
IsGetTotal = isGetTotal;
TotalVariable = totalVariable;
WhereExpression = whereExpression;
Source = source ?? string.Empty;
Show = show;

SetChartConfig(chartYAxisUnit);
SetAdvancedConfig(continuousTriggerThreshold, silenceCycle);
Expand Down Expand Up @@ -186,21 +193,21 @@ public void SetAdvancedConfig(int continuousTriggerThreshold, SilenceCycle silen
SilenceCycle = silenceCycle;
}

public void TriggerAlarm(DateTimeOffset excuteTime, ConcurrentDictionary<string, long> aggregateResult, List<RuleResultItem> ruleResult,int consecutiveCount)
public void TriggerAlarm(DateTimeOffset excuteTime, ConcurrentDictionary<string, long> aggregateResult, List<RuleResultItem> ruleResult, int consecutiveCount)
{
var alertSeverity = ruleResult.Where(x => x.IsValid).Min(x => x.AlarmRuleItem.AlertSeverity);

AddDomainEvent(new TriggerAlarmEvent(Id, alertSeverity, ruleResult, excuteTime, aggregateResult, consecutiveCount));
}

public void RecoveryAlarm(DateTimeOffset excuteTime, ConcurrentDictionary<string, long> aggregateResult, List<RuleResultItem> ruleResult)
public void RecoveryAlarm(DateTimeOffset excuteTime, ConcurrentDictionary<string, long> aggregateResult, List<RuleResultItem> ruleResult)
{
AddDomainEvent(new RecoveryAlarmEvent(Id, excuteTime, aggregateResult, ruleResult));
}

public bool IsRuleValid(List<RuleResultItem> ruleResult)
{
return ruleResult.Any(x => x.IsValid);
return ruleResult.Exists(x => x.IsValid);
}

public bool CheckIsNotification()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public void Configure(EntityTypeBuilder<AlarmRule> builder)
builder.Property(x => x.AppIdentity).HasMaxLength(128);
builder.Property(x => x.ChartYAxisUnit).HasMaxLength(128);
builder.Property(x => x.TotalVariable).HasMaxLength(64);
builder.Property(x => x.Source).IsRequired().HasMaxLength(40).HasDefaultValue(string.Empty);
builder.Property(x => x.Show).HasDefaultValue(true).IsRequired();
builder.OwnsMany(x => x.LogMonitorItems, b =>
{
b.ToTable(AlertConsts.DB_TABLE_PREFIX + "AlarmRuleLogMonitors", AlertConsts.DB_SCHEMA);
Expand Down Expand Up @@ -51,7 +53,7 @@ public void Configure(EntityTypeBuilder<AlarmRule> builder)
});
b.Property(x => x.CronExpression).HasMaxLength(128).HasColumnName("CheckFrequencyCron");
b.Property(x => x.Type).HasColumnName("CheckFrequencyType");

});
builder.OwnsOne(x => x.SilenceCycle, b =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using Masa.Alert.Domain.WebHooks.Aggregates;

namespace Masa.Alert.EntityFrameworkCore.EntityTypeConfigurations;

public class WebHookConfiguration : IEntityTypeConfiguration<WebHook>
Expand Down
Loading