Example #1
0
        public NamedValueSet GetAllValues(bool includeUncommitted)
        {
            var results = new NamedValueSet(_currentSettings);

            if (includeUncommitted)
            {
                results.Add(_unsavedSettings);
            }
            return(results);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FXCurveTerms"/> class.
        /// </summary>
        /// <param name="terms">The terms.</param>
        public FXCurveTerms(IDictionary <string, object> terms)
        {
            Terms = terms;
            var nvs = new NamedValueSet();

            nvs.Add(terms);
            Market                   = nvs.GetValue <string>("Market", null);
            Currency1                = nvs.GetValue <string>("Currency1", null);
            Currency2                = nvs.GetValue <string>("Currency2", null);
            ReferenceKey             = BuildKey();
            CurveReferenceIdentifier = null;
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VolatilitySurfaceTerms"/> class.
        /// </summary>
        /// <param name="terms">The terms.</param>
        public VolatilitySurfaceTerms(IDictionary <string, object> terms)
        {
            Terms = terms;
            var nvs = new NamedValueSet();

            nvs.Add(terms);
            if (nvs.Get(CurveProp.Market, false) != null)
            {
                Market = nvs.GetValue <string>(CurveProp.Market, null);
            }
            else if (nvs.Get("Market", false) != null)
            {
                Market = nvs.GetValue <string>("Market", null);
            }
            Currency     = nvs.GetValue <string>(CurveProp.Currency1, null);
            BaseDate     = nvs.GetValue(CurveProp.BaseDate, DateTime.MinValue);
            ReferenceKey = BuildKey();
            SurfaceReferenceIdentifier = null;
        }
        ///<summary>
        ///</summary>
        ///<param name="terms"></param>
        public RateCurveTerms(IDictionary <string, object> terms)
        {
            Terms = terms;
            var nvs = new NamedValueSet();

            nvs.Add(terms);

            if (nvs.Get("Version", false) != null)
            {
                Version = Convert.ToInt32(nvs.GetValue("Version", 0.0));
            }

            if (nvs.Get(CurveProp.Market, false) != null)
            {
                Market = nvs.GetValue <string>(CurveProp.Market, null);
            }
            else if (nvs.Get("Market", false) != null)
            {
                Market = nvs.GetValue <string>("Market", null);
            }

            if (nvs.Get(CurveProp.IndexTenor, false) != null)
            {
                Tenor = nvs.GetValue <string>(CurveProp.IndexTenor, null);
            }
            else if (nvs.Get("Tenor", false) != null)
            {
                Tenor = nvs.GetValue <string>("Tenor", null);
            }

            Currency = nvs.GetValue <string>(CurveProp.Currency1, null);


            BaseDate                 = nvs.GetValue(CurveProp.BaseDate, DateTime.MinValue);
            ReferenceKey             = BuildKey();
            CurveReferenceIdentifier = null;
        }
Example #5
0
        private void ProcessAllRules()
        {
            // note: no locks required - as we are inside main Dispatcher
            try
            {
                // publish latest rule status
                DateTimeOffset asAtTime = DateTimeOffset.Now;
                foreach (InternalRule rule in _ruleStore.Values)
                {
                    var props = new NamedValueSet();
                    try
                    {
                        if ((rule.LastMonitored + rule.MonitorPeriod) < asAtTime)
                        {
                            // update LastMonitored first to prevent infinite looping
                            // when there are repeated expression evaluation exceptions
                            rule.LastMonitored = asAtTime;
                            // build property set
                            string   smtpServer = rule.Properties.GetString(AlertRule.MailHost, null) ?? _defaultSmtpServer;
                            string   mailFrom   = rule.Properties.GetString(AlertRule.MailFrom, null) ?? _defaultMailFrom;
                            string[] recipients = rule.Properties.GetArray <string>(AlertRule.MailTo) ?? _defaultMailTo;
                            props.Set(AlertRule.MailHost, smtpServer);
                            props.Set(AlertRule.MailFrom, mailFrom);
                            props.Set(AlertRule.MailTo, recipients);
                            props.Set("host.AsAtTime", asAtTime);
                            props.Set("host.HostName", IntClient.Target.ClientInfo.HostName);
                            props.Set("rule.DebugEnabled", rule.Properties.GetValue <bool>("DebugEnabled", false));
                            props.Set("rule.RuleName", rule.RuleName);
                            props.Set("rule.Disabled", rule.Disabled);
                            props.Set("rule.DataSubsExpr", rule.DataSubsExpr.DisplayString());
                            props.Set("rule.Constraint", rule.Constraint.DisplayString());
                            props.Set("rule.Condition", rule.Condition.DisplayString());
                            props.Set("rule.MonitorPeriod", rule.MonitorPeriod);
                            props.Add(rule.Properties);
                            if (rule.CurrentReceivedItem != null)
                            {
                                props.Add(rule.CurrentReceivedItem.AppProps);
                                props.Set("item.IsNotNull", true);
                            }
                            else
                            {
                                // no item received
                                props.Set("item.IsNotNull", false);
                            }
                            AlertStatus newStatus;
                            string      statusMessage;
                            var         signal = new InternalSignal
                            {
                                RuleName    = rule.RuleName,
                                AlertServer = IntClient.Target.ClientInfo.HostName
                            };
                            if (rule.Disabled)
                            {
                                newStatus     = AlertStatus.Disabled;
                                statusMessage = "the rule is disabled";
                            }
                            else
                            {
                                // extend constraint
                                ICoreItem item       = rule.CurrentReceivedItem;
                                object    constraint = (item == null)
                                    ? rule.Constraint.Evaluate(props, null, DateTimeOffset.MinValue, DateTimeOffset.MinValue, asAtTime)
                                    : rule.Constraint.Evaluate(props, item.Name, item.Created, item.Expires, asAtTime);
                                if (Expr.CastTo(constraint, false))
                                {
                                    object condition = item == null
                                        ? rule.Condition.Evaluate(props, null, DateTimeOffset.MinValue, DateTimeOffset.MinValue, asAtTime)
                                        : rule.Condition.Evaluate(props, item.Name, item.Created, item.Expires, asAtTime);

                                    if (Expr.CastTo(condition, false))
                                    {
                                        newStatus     = AlertStatus.Alerted;
                                        statusMessage = rule.SignalFormat;
                                    }
                                    else
                                    {
                                        newStatus     = AlertStatus.AllClear;
                                        statusMessage = "the condition is no longer active";
                                    }
                                }
                                else
                                {
                                    newStatus     = AlertStatus.Inactive;
                                    statusMessage = "the constraint is no longer satisfied";
                                }
                            }
                            props.Set("rule.Status", newStatus.ToString());
                            signal.Status        = newStatus;
                            signal.LastMonitored = asAtTime;
                            signal.ReminderCount = 0;
                            if (rule.LastSignal != null)
                            {
                                signal.ReminderCount = rule.LastSignal.ReminderCount;
                            }
                            if (newStatus == AlertStatus.Alerted)
                            {
                                signal.SignalMessage = props.ReplaceTokens(statusMessage);
                            }
                            else
                            {
                                signal.SignalMessage =
                                    $"The previous alert message sent from this server ({signal.AlertServer}) " +
                                    $"for this rule ({signal.RuleName}) has been withdrawn because {statusMessage}.";
                            }
                            // set instance (repeat) count
                            if (newStatus != rule.LastStatus)
                            {
                                signal.ReminderCount = 0;
                            }
                            if ((newStatus != rule.LastStatus) || ((rule.LastPublished + rule.PublishPeriod) < asAtTime))
                            {
                                rule.LastPublished = asAtTime;
                                // publish signal
                                PublishAlertSignal(signal);
                                // email signal if status is or was alerted
                                if ((newStatus == AlertStatus.Alerted) || (rule.LastStatus == AlertStatus.Alerted))
                                {
                                    Logger.LogInfo(
                                        "Rule '{0}' {1}: {2}", signal.RuleName, signal.Status, signal.SignalMessage);
                                    SendAlertSignalEmail(signal, props);
                                }
                            }
                            // done
                            rule.LastStatus = newStatus;
                            rule.LastSignal = signal;
                        }
                    }
                    catch (Exception ex)
                    {
                        ReportUncaughtError("MainCallback", rule.RuleName, ex, props);
                    }
                } // foreach rule
            }
            catch (Exception ex)
            {
                ReportUncaughtError("MainCallback", null, ex, null);
            }
        }
Example #6
0
        public string CommentSuffix = null; // use "-->" for XML/XSD

        public string[] ProcessTemplate(string[] template, TemplateIterator outerIterator)
        {
            var output      = new List <string>();
            var savedScopes = new Stack <TemplateScope>();
            TemplateIterator currentIterator = outerIterator;
            int iterationStartLine           = 0;
            int iterationNumber = 0;
            var currentTokens   = new NamedValueSet(currentIterator.Iterations[iterationNumber].Tokens);
            int currentLine     = 0;
            int nestLevel       = 0;

            while (currentLine < template.Length)
            {
                string trimmedLine = template[currentLine].Trim();
                // check for template directive
                bool specialLine = false;
                if (((CommentPrefix == null) || trimmedLine.StartsWith(CommentPrefix)) &&
                    ((CommentSuffix == null) || trimmedLine.EndsWith(CommentSuffix)))
                {
                    // comment line - might have a template directive in it
                    if (CommentPrefix != null)
                    {
                        trimmedLine = trimmedLine.Substring(CommentPrefix.Length);
                    }
                    if (CommentSuffix != null)
                    {
                        trimmedLine = trimmedLine.Substring(0, trimmedLine.Length - CommentSuffix.Length);
                    }
                    trimmedLine = trimmedLine.Trim();
                    if (trimmedLine.StartsWith("##"))
                    {
                        // directive found
                        specialLine = true;
                        string[] directiveParts = trimmedLine.Substring(2).Split(':');
                        string   directive      = directiveParts[0].ToLower().Trim();
                        switch (directive)
                        {
                        case "foreach":
                            // start of for loop block
                            // - save scope and loop
                        {
                            string iteratorName = directiveParts[1].Trim();
                            savedScopes.Push(new TemplateScope(nestLevel, currentTokens,
                                                               currentIterator, iterationStartLine, iterationNumber));
                            iterationStartLine = currentLine;
                            if (!currentIterator.Iterations[iterationNumber].SubIterators.ContainsKey(iteratorName))
                            {
                                throw new ApplicationException(
                                          $"Unknown iterator name '{iteratorName}' in template (line {currentLine}).");
                            }
                            currentIterator = currentIterator.Iterations[iterationNumber].SubIterators[iteratorName];
                            // 1st iteration at new level
                            nestLevel++;
                            iterationNumber = 0;
                            currentTokens.Add(currentIterator.Iterations[iterationNumber].Tokens);
                        }
                        break;

                        case "end":
                            // end of block - exit or loop
                            if ((currentIterator != null) && ((iterationNumber + 1) < currentIterator.Iterations.Length))
                            {
                                // restart loop
                                currentLine = iterationStartLine;
                                iterationNumber++;
                                currentTokens.Add(currentIterator.Iterations[iterationNumber].Tokens);
                            }
                            else
                            {
                                // pop scope
                                TemplateScope oldScope = savedScopes.Pop();
                                nestLevel          = oldScope.NestLevel;
                                currentTokens      = oldScope.Tokens;
                                currentIterator    = oldScope.Iterator;
                                iterationStartLine = oldScope.IterationStartLine;
                                iterationNumber    = oldScope.IterationNumber;
                            }
                            break;

                        default:
                            throw new ApplicationException(
                                      $"Unknown directive '{directive}' in template (line {currentLine}).");
                        }
                    }
                }
                if (!specialLine)
                {
                    // ordinary input line
                    currentTokens.Set("SourceLine", currentLine);
                    currentTokens.Set("OutputLine", output.Count);
                    currentTokens.Set("NestLevel", nestLevel);
                    output.Add(currentTokens.ReplaceTokens(template[currentLine]));
                }
                currentLine++;
            }
            if (savedScopes.Count > 0)
            {
                throw new ApplicationException("Not enough matching ##end directives in template!");
            }
            return(output.ToArray());
        }
        private void ProcessRule(ICoreClient client, InternalRule rule, DateTimeOffset currentTime)
        {
            RuleStatusEnum ruleStatus;
            ILogger        logger = new FilterLogger(
                Logger, $"Rule {rule.RuleName}: ",
                rule.DebugEnabled ? LogSeverity.Debug : LogSeverity.Info);

            using (var settings = new SettingsTracker(client, "FileImporter." + rule.RuleName))
            {
                bool           lastCheckFailed    = false;
                string         lastCheckException = "(null)";
                DateTimeOffset lastCheckDateTime  = currentTime;
                //logger.LogDebug("Processing...");
                try
                {
                    ruleStatus = RuleStatusEnum.Disabled;
                    if (!rule.Disabled)
                    {
                        // evaluate rule constraint and condition
                        var properties = new NamedValueSet(settings.GetAllValues(true));
                        properties.Add(rule.Properties);
                        // last import date/time (default to 4 days ago)
                        var lastImportDateTime = settings.GetSetValue(RuleConst.LastImportDateTime, DateTimeOffset.Now.AddDays(-4));
                        properties.Set(RuleConst.LastImportDateTime, lastImportDateTime);
                        // calculate effective "as at" date
                        var thisImportDateTime = Expr.CastTo(rule.EffectiveDateExpr.Evaluate(properties, currentTime), currentTime);
                        properties.Set(RuleConst.EffectiveDateTime, thisImportDateTime);
                        // add useful date/time tokens
                        //foreach (string token in new string[] { "dd", "ddd", "MM", "MMM", "yyyy" })
                        //{
                        //    properties.Set(token, thisImportDateTime.ToString(token));
                        //}
                        // calculate import delay
                        var thisImportDelay = Expr.CastTo(rule.ImportDelayExpr.Evaluate(properties, currentTime), TimeSpan.Zero);
                        properties.Set(RuleConst.ImportDelay, thisImportDelay);
                        // evaluate rule check constraint and import condition
                        logger.LogDebug("Evaluation Params :");
                        properties.LogValues(text => logger.LogDebug("    " + text));
                        logger.LogDebug("Check Constraint  : {0}", rule.CheckConstraint);
                        ruleStatus = RuleStatusEnum.Inactive;
                        if (Expr.CastTo(rule.CheckConstraint.Evaluate(properties, currentTime), false))
                        {
                            logger.LogDebug("Import Condition  : {0}", rule.ImportCondition);
                            ruleStatus = RuleStatusEnum.NotReady;
                            if (Expr.CastTo(rule.ImportCondition.Evaluate(properties, currentTime), false))
                            {
                                ruleStatus = RuleStatusEnum.Failed;
                                // import condition is true
                                // process date/time tokens
                                string targetLocation = StringHelper.ReplaceDateTimeTokens(rule.TargetLocation, thisImportDateTime);
                                string sourceLocation = StringHelper.ReplaceDateTimeTokens(rule.SourceLocation, thisImportDateTime);
                                var    importedFiles  = new List <string>();
                                logger.LogInfo("Source Location  : {0}", sourceLocation);
                                logger.LogInfo("Target Location  : {0}", targetLocation);
                                logger.LogInfo("Filenames to copy: {0}", rule.CopyFilePatterns);
                                string thisImportException = "(null)";
                                try
                                {
                                    // import the file
                                    // - optionally clean up old files aged more than 7 days
                                    if (rule.RemoveOldTargetFiles)
                                    {
                                        try
                                        {
                                            string[] oldTargetFiles = Directory.GetFiles(targetLocation, "*.*", SearchOption.TopDirectoryOnly);
                                            foreach (string oldTargetFile in oldTargetFiles)
                                            {
                                                var targetFileInfo = new FileInfo(oldTargetFile);
                                                if ((currentTime - targetFileInfo.LastWriteTime) > TimeSpan.FromDays(2))
                                                {
                                                    File.Delete(oldTargetFile);
                                                }
                                            }
                                        }
                                        catch (IOException removeExcp)
                                        {
                                            logger.LogWarning("Error removing old files: {0}", removeExcp.GetType().Name);
                                            // ignored
                                        }
                                    }
                                    // - create target directory if required
                                    if (!Directory.Exists(targetLocation))
                                    {
                                        Directory.CreateDirectory(targetLocation);
                                    }
                                    // - copy file(s) from source to target
                                    foreach (string ruleFilePattern in rule.CopyFilePatterns.Split(';'))
                                    {
                                        string   filePattern = StringHelper.ReplaceDateTimeTokens(ruleFilePattern, thisImportDateTime);
                                        string[] sourceFiles = Directory.GetFiles(sourceLocation, filePattern, SearchOption.TopDirectoryOnly);
                                        logger.LogInfo("Copying file(s): {0} ({1} found)", filePattern, sourceFiles.Length);
                                        int copiedCount  = 0;
                                        int skippedCount = 0;
                                        foreach (string sourceFileFullname in sourceFiles)
                                        {
                                            string sourceFileBaseName = Path.GetFileName(sourceFileFullname);
                                            string targetFileFullname = $@"{targetLocation}\{sourceFileBaseName}";
                                            bool   copyRequired       = true;
                                            if (File.Exists(targetFileFullname) && rule.OnlyCopyUpdatedFiles)
                                            {
                                                var sourceFileInfo = new FileInfo(sourceFileFullname);
                                                var targetFileInfo = new FileInfo(targetFileFullname);
                                                copyRequired = (sourceFileInfo.LastWriteTime > targetFileInfo.LastWriteTime);
                                            }
                                            if (copyRequired)
                                            {
                                                logger.LogInfo("Copying file : {0}", sourceFileBaseName);
                                                logger.LogInfo("  From source: {0}", sourceLocation);
                                                logger.LogInfo("    To target: {0}", targetLocation);
                                                DateTime copyCommenced = DateTime.Now;
                                                File.Copy(sourceFileFullname, targetFileFullname, true);
                                                TimeSpan copyDuration = DateTime.Now - copyCommenced;
                                                importedFiles.Add(sourceFileBaseName);
                                                var targetFileInfo = new FileInfo(targetFileFullname);
                                                copiedCount++;
                                                logger.LogInfo("  Copied {0}MB in {1}s ({2}KB/sec)",
                                                               (targetFileInfo.Length / 1000000.0).ToString("N"),
                                                               copyDuration.TotalSeconds.ToString("N"),
                                                               (targetFileInfo.Length / (1000.0 * copyDuration.TotalSeconds)).ToString("N"));
                                                // publish rule import status
                                                var importFileResult = new ImportFileResult
                                                {
                                                    hostEnvName     = EnvHelper.EnvName(IntClient.Target.ClientInfo.ConfigEnv),
                                                    hostComputer    = IntClient.Target.ClientInfo.HostName,
                                                    hostInstance    = null,
                                                    hostUserName    = client.ClientInfo.UserName,
                                                    RuleName        = rule.RuleName,
                                                    FileName        = sourceFileBaseName,
                                                    DebugEnabled    = rule.DebugEnabled,
                                                    DebugProperties = rule.DebugProperties.Serialise(),
                                                    FileContentType = rule.FileContentType,
                                                    ImportResult    = RuleStatusEnum.Completed.ToString(),
                                                    ImportException = null,
                                                    ImportDateTime  = currentTime.ToString("o"),
                                                    SourceSystem    = rule.SourceSystem,
                                                    SourceLocation  = sourceLocation,
                                                    TargetLocation  = targetLocation
                                                };
                                                IntClient.Target.SaveObject(importFileResult, true, TimeSpan.FromDays(30));
                                            }
                                            else
                                            {
                                                skippedCount++;
                                                logger.LogDebug("Skipping file : {0}", sourceFileBaseName);
                                            }
                                        } // foreach file
                                        logger.LogInfo("Copied {0} file(s), skipped {1} file(s).", copiedCount, skippedCount);
                                    }
                                    // - optionally decompress target
                                    // todo
                                    // done
                                    ruleStatus         = RuleStatusEnum.Completed;
                                    lastImportDateTime = Expr.CastTo(rule.DateUpdateExpr.Evaluate(properties, currentTime), currentTime);
                                }
                                catch (Exception e2)
                                {
                                    logger.Log(e2);
                                    thisImportException = e2.ToString();
                                    ruleStatus          = RuleStatusEnum.Failed;
                                }
                                finally
                                {
                                    settings.SetNewValue(RuleConst.LastImportResult, ruleStatus.ToString());
                                    settings.SetNewValue(RuleConst.LastImportException, thisImportException);
                                    settings.SetNewValue(RuleConst.LastImportDateTime, lastImportDateTime);
                                }
                                // publish rule import status
                                var importRuleResult = new ImportRuleResult
                                {
                                    hostEnvName     = EnvHelper.EnvName(IntClient.Target.ClientInfo.ConfigEnv),
                                    hostComputer    = IntClient.Target.ClientInfo.HostName,
                                    hostInstance    = null,
                                    hostUserName    = client.ClientInfo.UserName,
                                    RuleName        = rule.RuleName,
                                    ImportResult    = ruleStatus.ToString(),
                                    ImportException = thisImportException,
                                    ImportDateTime  = currentTime.ToString("o"),
                                    SourceSystem    = rule.SourceSystem,
                                    SourceLocation  = sourceLocation,
                                    TargetLocation  = targetLocation,
                                    FileNames       = importedFiles.ToArray()
                                };
                                IntClient.Target.SaveObject(importRuleResult, true, TimeSpan.FromDays(30));
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Log(e);
                    lastCheckFailed    = true;
                    lastCheckException = e.ToString();
                    ruleStatus         = RuleStatusEnum.Failed;
                }
                settings.SetNewValue(RuleConst.LastCheckFailed, lastCheckFailed);
                settings.SetNewValue(RuleConst.LastCheckException, lastCheckException);
                settings.SetNewValue(RuleConst.LastCheckDateTime, lastCheckDateTime);
            } // commit unsaved settings
            logger.LogDebug("Status={0}", ruleStatus);
        }