|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Linq; |
| 5 | +using LaunchDarkly.Logging; |
| 6 | +using LaunchDarkly.Sdk.Client.Hooks; |
| 7 | +using LaunchDarkly.Sdk.Client.Internal.Hooks.Interfaces; |
| 8 | + |
| 9 | +namespace LaunchDarkly.Sdk.Client.Internal.Hooks.Series |
| 10 | +{ |
| 11 | + using SeriesData = ImmutableDictionary<string, object>; |
| 12 | + |
| 13 | + internal class IdentifyStage |
| 14 | + { |
| 15 | + protected enum Stage |
| 16 | + { |
| 17 | + BeforeIdentify, |
| 18 | + AfterIdentify |
| 19 | + } |
| 20 | + |
| 21 | + protected readonly EvaluationStage.Order _order; |
| 22 | + private readonly Logger _logger; |
| 23 | + |
| 24 | + protected IdentifyStage(Logger logger, EvaluationStage.Order order) |
| 25 | + { |
| 26 | + _logger = logger; |
| 27 | + _order = order; |
| 28 | + } |
| 29 | + |
| 30 | + protected void LogFailure(IdentifySeriesContext context, Hook h, Stage stage, Exception e) |
| 31 | + { |
| 32 | + _logger.Error("During identify of context \"{0}\", stage \"{1}\" of hook \"{2}\" reported error: {3}", |
| 33 | + context.Context.Key, stage.ToString(), h.Metadata.Name, e.Message); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + internal sealed class BeforeIdentify : IdentifyStage, IStageExecutor<IdentifySeriesContext> |
| 38 | + { |
| 39 | + private readonly IEnumerable<Hook> _hooks; |
| 40 | + |
| 41 | + public BeforeIdentify(Logger logger, IEnumerable<Hook> hooks, EvaluationStage.Order order) : base(logger, order) |
| 42 | + { |
| 43 | + _hooks = (order == EvaluationStage.Order.Forward) ? hooks : hooks.Reverse(); |
| 44 | + } |
| 45 | + |
| 46 | + public IEnumerable<SeriesData> Execute(IdentifySeriesContext context, IEnumerable<SeriesData> _) |
| 47 | + { |
| 48 | + return _hooks.Select(hook => |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + return hook.BeforeIdentify(context, SeriesData.Empty); |
| 53 | + } |
| 54 | + catch (Exception e) |
| 55 | + { |
| 56 | + LogFailure(context, hook, Stage.BeforeIdentify, e); |
| 57 | + return SeriesData.Empty; |
| 58 | + } |
| 59 | + }).ToList(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + internal sealed class AfterIdentify : IdentifyStage, IStageExecutor<IdentifySeriesContext, IdentifySeriesResult> |
| 64 | + { |
| 65 | + private readonly IEnumerable<Hook> _hooks; |
| 66 | + |
| 67 | + public AfterIdentify(Logger logger, IEnumerable<Hook> hooks, EvaluationStage.Order order) : base(logger, order) |
| 68 | + { |
| 69 | + _hooks = (order == EvaluationStage.Order.Forward) ? hooks : hooks.Reverse(); |
| 70 | + } |
| 71 | + |
| 72 | + public IEnumerable<SeriesData> Execute(IdentifySeriesContext context, IdentifySeriesResult result, |
| 73 | + IEnumerable<SeriesData> seriesData) |
| 74 | + { |
| 75 | + return _hooks.Zip((_order == EvaluationStage.Order.Reverse ? seriesData.Reverse() : seriesData), (hook, data) => |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + return hook.AfterIdentify(context, data, result); |
| 80 | + } |
| 81 | + catch (Exception e) |
| 82 | + { |
| 83 | + LogFailure(context, hook, Stage.AfterIdentify, e); |
| 84 | + return SeriesData.Empty; |
| 85 | + } |
| 86 | + }).ToList(); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments