Beispiel #1
0
 public void Init(DragKeyDelegate dragKeyDownHandler, DragKeyDelegate dragKeyUpHandler, LogErrorDelegate logErrorHandler, int dragAreaLayerMask)
 {
     DragKeyDownHandler = dragKeyDownHandler;
     DragKeyUpHandler   = dragKeyUpHandler;
     LogErrorHandler    = logErrorHandler;
     DragAreaLayerMask  = dragAreaLayerMask;
 }
        // save this instance of telemetrycollection to a file
        // this method first saves the data in a tmp file
        // and then replaces the existing one (if it exists) to reduce the chances of corrupting the file.
        public void SaveToFile(string filePath, LogErrorDelegate logErrorDelegate)
        {
            try
            {
                string filePathTemp = string.Format("{0}.tmp", filePath);

                // saving current collection in a json serialization format on temporary file
                using (StreamWriter file = File.CreateText(filePathTemp))
                {
                    // using converter to properly serialize dictionary keys
                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    settings.ContractResolver = new DictionaryAsArrayResolver();
                    string json = JsonConvert.SerializeObject(this, settings);

                    // writing json to file
                    file.Write(json);
                }

                // create or replace file
                File.Copy(filePathTemp, filePath, true);

                // remove temporary file
                File.Delete(filePathTemp);
            }
            catch (Exception e)
            {
                logErrorDelegate(LogSourceId, "Error when trying to save telemetry aggregation to file: {0} - Exception: {1}", filePath, e);
            }
        }
        public static TelemetryCollection LoadTelemetryCollection(string filePath, LogErrorDelegate logErrorDelegate)
        {
            TelemetryCollection telemetryCollection = null;

            if (File.Exists(filePath))
            {
                try
                {
                    using (StreamReader file = File.OpenText(filePath))
                    {
                        // using converter to properly serialize dictionary keys
                        string serializedObject         = file.ReadToEnd();
                        JsonSerializerSettings settings = new JsonSerializerSettings()
                        {
                            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                        };
                        settings.ContractResolver = new DictionaryAsArrayResolver();

                        telemetryCollection = JsonConvert.DeserializeObject <TelemetryCollection>(serializedObject, settings);
                    }
                }
                catch (JsonException e)
                {
                    logErrorDelegate(LogSourceId, "Error deserializing file :{0} - Exception {1}", filePath, e);
                }
                catch (Exception e)
                {
                    logErrorDelegate(LogSourceId, "Error loading persisted telemetry from file: {0} - Exception {1}", filePath, e);
                }
            }

            return(telemetryCollection);
        }
Beispiel #4
0
        // Returns the next line in the file being parsed
        private string ReadNextLine(LogErrorDelegate logErrorDelegate)
        {
            string configLine = null;

            try
            {
                configLine = this.fileStream.ReadLine();
            }
            catch (Exception e)
            {
                logErrorDelegate(LogSourceId, "Exception while reading line from config file: {0}", e.Message);
            }

            return(configLine);
        }
        public Transform UI3DRoot = null; //3D UI组件的根节点

        public void Init(LoadUIPanelDelegate loadUIPanelHandler, LogErrorDelegate logErrorHandler,
            ButtonDownDelegate mouseLeftButtonDownHandler,
            ButtonDownDelegate mouseRightButtonDownHandler,
            ButtonDownDelegate closeUIFormKeyDownHandler,
            ButtonDownDelegate confirmKeyDownHandler,
            ButtonDownDelegate inputNavigateKeyDownHandler)
        {
            LoadUIPanelHandler = loadUIPanelHandler;
            LogErrorHandler = logErrorHandler;
            MouseLeftButtonDownHandler = mouseLeftButtonDownHandler;
            MouseRightButtonDownHandler = mouseRightButtonDownHandler;
            CloseUIFormKeyDownHandler = closeUIFormKeyDownHandler;
            ConfirmKeyDownHandler = confirmKeyDownHandler;
            InputNavigateKeyDownHandler = inputNavigateKeyDownHandler;
        }
        public ManifestEventFieldsLookup(IEnumerable <string> manifestFilePaths, LogErrorDelegate logErrorDelegate, bool verbose)
        {
            int taskEventNamesCollisionCount = 0;

            // constructing the lookup dictionary
            this.events = new Dictionary <Tuple <string, string>, Dictionary <string, FieldDefinition> >();

            // iterating over all manifest files
            foreach (var manifestFile in manifestFilePaths)
            {
                ManifestLoader manifestLoader = new ManifestLoader();
                ManifestDefinitionDescription manifestDefinitionDescription = manifestLoader.LoadManifest(manifestFile);

                // iterating over all providers in the manifest file
                foreach (var provider in manifestDefinitionDescription.Providers)
                {
                    // including all events from the provider to the dictionary
                    foreach (var ev in provider.Events.Values)
                    {
                        try
                        {
                            Dictionary <string, FieldDefinition> fieldDictionary = new Dictionary <string, FieldDefinition>();
                            this.events.Add(Tuple.Create(ev.TaskName, ev.EventName), fieldDictionary);

                            // including all fields from the event
                            foreach (var field in ev.Fields)
                            {
                                fieldDictionary.Add(field.Name, field);
                            }
                        }
                        catch (Exception)
                        {
                            if (verbose)
                            {
                                logErrorDelegate(LogSourceId, "(TaskName: {0}, EventName: {1}) collision for UnparsedEventName: {2}", ev.TaskName, ev.EventName, ev.UnparsedEventName);
                            }

                            taskEventNamesCollisionCount++;
                        }
                    }
                }

                if (taskEventNamesCollisionCount > 0)
                {
                    logErrorDelegate(LogSourceId, "Warning - Found {0} collisions on pairs (TaskName, EventName). Parser is using first occurence. For more details use verbose mode.", taskEventNamesCollisionCount);
                }
            }
        }
Beispiel #7
0
        public static async Task <(bool Success, Stack Stack)> GetStackAsync(this IAmazonCloudFormation cfnClient, string stackName, LogErrorDelegate logError)
        {
            Stack stack = null;

            try {
                var describe = await cfnClient.DescribeStacksAsync(new DescribeStacksRequest {
                    StackName = stackName
                });

                // make sure the stack is in a stable state (not updating and not failed)
                stack = describe.Stacks.FirstOrDefault();
                switch (stack?.StackStatus)
                {
                case null:
                case "CREATE_COMPLETE":
                case "ROLLBACK_COMPLETE":
                case "UPDATE_COMPLETE":
                case "UPDATE_ROLLBACK_COMPLETE":

                    // we're good to go
                    break;

                default:
                    logError?.Invoke($"{stackName} is not in a valid state; module deployment must be complete and successful (status: {stack?.StackStatus})", null);
                    return(false, null);
                }
            } catch (AmazonCloudFormationException) {
                // stack not found; nothing to do
            }
            return(true, stack);
        }
Beispiel #8
0
        public static async Task <(Stack Stack, bool Success)> TrackStackUpdateAsync(
            this IAmazonCloudFormation cfnClient,
            string stackName,
            string stackId,
            string mostRecentStackEventId,
            ModuleNameMappings nameMappings = null,
            LogErrorDelegate logError       = null
            )
        {
            var seenEventIds = new HashSet <string>();
            var foundMostRecentStackEvent = (mostRecentStackEventId == null);
            var request = new DescribeStackEventsRequest {
                StackName = stackId ?? stackName
            };
            var eventList        = new List <StackEvent>();
            var ansiLinesPrinted = 0;

            // iterate as long as the stack is being created/updated
            var active  = true;
            var success = false;

            while (active)
            {
                await Task.Delay(TimeSpan.FromSeconds(3));

                // fetch as many events as possible for the current stack
                var events = new List <StackEvent>();
                try {
                    var response = await cfnClient.DescribeStackEventsAsync(request);

                    events.AddRange(response.StackEvents);
                } catch (System.Net.Http.HttpRequestException e) when((e.InnerException is System.Net.Sockets.SocketException) && (e.InnerException.Message == "No such host is known"))
                {
                    // ignore network issues and just try again
                    continue;
                }
                events.Reverse();

                // skip any events that preceded the most recent event before the stack update operation
                while (!foundMostRecentStackEvent && events.Any())
                {
                    var evt = events.First();
                    if (evt.EventId == mostRecentStackEventId)
                    {
                        foundMostRecentStackEvent = true;
                    }
                    seenEventIds.Add(evt.EventId);
                    events.RemoveAt(0);
                }
                if (!foundMostRecentStackEvent)
                {
                    throw new ApplicationException($"unable to find starting event for stack: {stackName}");
                }

                // report only on new events
                foreach (var evt in events.Where(evt => !seenEventIds.Contains(evt.EventId)))
                {
                    UpdateEvent(evt);
                    if (!seenEventIds.Add(evt.EventId))
                    {
                        // we found an event we already saw in the past, no point in looking at more events
                        break;
                    }
                    if (IsFinalStackEvent(evt) && (evt.LogicalResourceId == stackName))
                    {
                        // event signals stack creation/update completion; time to stop
                        active  = false;
                        success = IsSuccessfulFinalStackEvent(evt);
                        break;
                    }
                }
                RenderEvents();
            }
            if (!success)
            {
                return(Stack : null, Success : false);
            }

            // describe stack and report any output values
            var description = await cfnClient.DescribeStacksAsync(new DescribeStacksRequest {
                StackName = stackName
            });

            return(Stack : description.Stacks.FirstOrDefault(), Success : success);

            // local function
            string TranslateLogicalIdToFullName(string logicalId)
            {
                var fullName = logicalId;

                nameMappings?.ResourceNameMappings?.TryGetValue(logicalId, out fullName);
                return(fullName ?? logicalId);
            }

            string TranslateResourceTypeToFullName(string awsType)
            {
                var fullName = awsType;

                nameMappings?.TypeNameMappings?.TryGetValue(awsType, out fullName);
                return(fullName ?? awsType);
            }

            void RenderEvents()
            {
                if (Settings.UseAnsiConsole)
                {
                    if (ansiLinesPrinted > 0)
                    {
                        Console.Write(AnsiTerminal.MoveLineUp(ansiLinesPrinted));
                    }
                    var maxResourceStatusLength   = eventList.Any() ? eventList.Max(evt => evt.ResourceStatus.ToString().Length) : 0;
                    var maxResourceTypeNameLength = eventList.Any() ? eventList.Max(evt => TranslateResourceTypeToFullName(evt.ResourceType).Length) : 0;
                    foreach (var evt in eventList)
                    {
                        var resourceStatus = evt.ResourceStatus.ToString();
                        var resourceType   = TranslateResourceTypeToFullName(evt.ResourceType);
                        if (_ansiStatusColorCodes.TryGetValue(evt.ResourceStatus, out var ansiColor))
                        {
                            // print resource status
                            Console.Write(ansiColor);
                            Console.Write(resourceStatus);
                            Console.Write(AnsiTerminal.Reset);
                            Console.Write("".PadRight(maxResourceStatusLength - resourceStatus.Length + 4));

                            // print resource type
                            Console.Write(resourceType);
                            Console.Write("".PadRight(maxResourceTypeNameLength - resourceType.Length + 4));

                            // print resource name
                            Console.Write(TranslateLogicalIdToFullName(evt.LogicalResourceId));

                            // print status reason
                            if ((logError == null) && (evt.ResourceStatusReason != null))
                            {
                                Console.Write($" ({evt.ResourceStatusReason})");
                            }
                        }
                        else
                        {
                            Console.Write($"{resourceStatus}    {resourceType}    {TranslateLogicalIdToFullName(evt.LogicalResourceId)}{(evt.ResourceStatusReason != null ? $" ({evt.ResourceStatusReason})" : "")}");
                        }
                        Console.Write(AnsiTerminal.ClearEndOfLine);
                        Console.WriteLine();
                    }
                    ansiLinesPrinted = eventList.Count;
                }
            }

            void UpdateEvent(StackEvent evt)
            {
                if (Settings.UseAnsiConsole)
                {
                    var index = eventList.FindIndex(e => e.LogicalResourceId == evt.LogicalResourceId);
                    if (index < 0)
                    {
                        eventList.Add(evt);
                    }
                    else
                    {
                        eventList[index] = evt;
                    }
                }
                else
                {
                    Console.WriteLine($"{evt.ResourceStatus,-35} {TranslateResourceTypeToFullName(evt.ResourceType),-55} {TranslateLogicalIdToFullName(evt.LogicalResourceId)}{(evt.ResourceStatusReason != null ? $" ({evt.ResourceStatusReason})" : "")}");
                }

                // capture failed operation as an error
                switch (evt.ResourceStatus)
                {
                case "CREATE_FAILED":
                case "ROLLBACK_FAILED":
                case "UPDATE_FAILED":
                case "DELETE_FAILED":
                case "UPDATE_ROLLBACK_FAILED":
                case "UPDATE_ROLLBACK_IN_PROGRESS":
                    if (evt.ResourceStatusReason != "Resource creation cancelled")
                    {
                        logError?.Invoke($"{evt.ResourceStatus} {TranslateLogicalIdToFullName(evt.LogicalResourceId)} [{TranslateResourceTypeToFullName(evt.ResourceType)}]: {evt.ResourceStatusReason}", /*Exception*/ null);
                    }
                    break;
                }
            }
        }
 public Log()
 {
     logger    = new Logger();
     logError += logger.LogToConsole;
     logError += logger.LogToFile;
 }
Beispiel #10
0
        public static async Task <(bool Success, Stack Stack)> GetStackAsync(this IAmazonCloudFormation cfnClient, string stackName, LogErrorDelegate logError)
        {
            Stack stack    = null;
            var   attempts = 0;

            try {
                var describe = await cfnClient.DescribeStacksAsync(new DescribeStacksRequest {
                    StackName = stackName
                });

                // make sure the stack is in a stable state (not updating and not failed)
                stack = describe.Stacks.FirstOrDefault();
                switch (stack?.StackStatus)
                {
                case null:
                case "CREATE_COMPLETE":
                case "ROLLBACK_COMPLETE":
                case "UPDATE_COMPLETE":
                case "UPDATE_ROLLBACK_COMPLETE":

                    // we're good to go
                    break;

                default:
                    logError?.Invoke($"{stackName} is not in a valid state; CloudFormation stack must be complete and successful (status: {stack?.StackStatus})", null);
                    return(false, null);
                }
            } catch (AmazonCloudFormationException) {
                // stack not found; nothing to do
            } catch (HttpRequestException e) when(e.Message == "The requested name is valid, but no data of the requested type was found")
            {
                // NOTE (2020-03-31, bjorg): avoid sporadic DNS issues by waiting an trying again
                if (++attempts < 3)
                {
                    await Task.Delay(TimeSpan.FromSeconds(attempts));
                }
            }
            return(true, stack);
        }
Beispiel #11
0
 public static extern bool UWK_Initialize(LogCallbackDelegate logcb, LogErrorDelegate errorcb, ProcessUWKEventDelegate processcb, UWKBetaDelegate betacb, IntPtr initJson);
 //--- Constructors ---
 public ParameterStoreFunctionNodeDeserializer(string workingDirectory, LogErrorDelegate logError, Settings settings)
 {
     _workingDirectory = workingDirectory ?? throw new ArgumentNullException(nameof(workingDirectory));
     LogError          = logError ?? throw new ArgumentNullException(nameof(logError));
     Settings          = settings ?? throw new ArgumentNullException(nameof(settings));
 }
Beispiel #13
0
 public static extern int UWK_Initialize(LogCallbackDelegate logcb, LogErrorDelegate errorcb, ProcessMessageDelegate processcb, IntPtr initJson);
Beispiel #14
0
 // private constructor
 // the only public exposed method is the static method Parse
 private SyntaxAnalyzer(StreamReader fileStream, IEnumerable <string> manifestFilesPaths, LogErrorDelegate logErrorDelegate, bool verbose)
 {
     this.fileStream        = fileStream;
     this.lexAnalyzer       = null;
     this.semanticsAnalyzer = new SemanticsAnalyzer(manifestFilesPaths, manifestFilesPaths.Any(), logErrorDelegate, verbose);
 }
Beispiel #15
0
        // Static method which returns a list with the white-listed traces and
        //  respective aggregation configurations parsed from the config file provided
        public static List <ParseResult> Parse(
            string filePath,
            IEnumerable <string> manifestFilesPaths,
            LogErrorDelegate logErrorDelegate,
            bool verbose,
            out List <TraceAggregationConfig> telemetryConfigEntryList)
        {
            telemetryConfigEntryList = new List <TraceAggregationConfig>();
            List <ParseResult> parseResultsList = new List <ParseResult>();

            try
            {
                using (StreamReader fileStream = File.OpenText(filePath))
                {
                    SyntaxAnalyzer telemetryParser = new SyntaxAnalyzer(fileStream, manifestFilesPaths, logErrorDelegate, verbose);

                    // parse line by line using the grammar and stores the parsed trace configurations
                    int currentLine = 0;
                    while (true)
                    {
                        TraceAggregationConfig traceConfigEntry;

                        currentLine++;
                        string configLine = telemetryParser.ReadNextLine(logErrorDelegate);
                        if (configLine == null)
                        {
                            // EOL
                            break;
                        }

                        telemetryParser.lexAnalyzer = new LexicalAnalyzer(configLine);

                        var parseResult = telemetryParser.L(out traceConfigEntry);
                        parseResult.LineNumber = currentLine;

                        if (parseResult.ParseCode != ParseCode.EmptyLine &&
                            parseResult.ParseCode != ParseCode.Comment)
                        {
                            // checking if something went wrong during parsing and a null object was created
                            if (traceConfigEntry == null && parseResult.ParseCode == ParseCode.Success)
                            {
                                logErrorDelegate(LogSourceId, "Unexpected null entry generated on parsing of file - {0} - ParseResult: {1}", filePath, parseResult);
                                parseResult = new ParseResult(ParseCode.ParserError, string.Format("Previous ParseResult Value: {0}", parseResult), 0, currentLine);
                            }

                            parseResultsList.Add(parseResult);

                            if (parseResult.ParseCode == ParseCode.Success)
                            {
                                telemetryConfigEntryList.Add(traceConfigEntry);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                parseResultsList.Add(new ParseResult(ParseCode.ParserError, string.Format("Exception while opening or parsing config file - {0} - Exception: {1}", filePath, e)));
            }

            return(parseResultsList);
        }
        // tries to read the persisted serialization. If fails returns a new instance (empty)
        public static TelemetryCollection LoadOrCreateTelemetryCollection(string filePath, TelemetryIdentifiers telemetryIdentifier, int dailyNumberOfTelemetryPushes, out bool telCollectionloadedFromDisk, LogErrorDelegate logErrorDelegate)
        {
            TelemetryCollection telemetryCollection = TelemetryCollection.LoadTelemetryCollection(filePath, logErrorDelegate);

            // if loaded from file and configuration on the push rate not changed add telemetryIdentifier, otherwise create a new one
            if (telemetryCollection != null && telemetryCollection.DailyNumberOfTelemetryPushes == dailyNumberOfTelemetryPushes)
            {
                telemetryCollection.TelemetryIds = telemetryIdentifier;
                telCollectionloadedFromDisk      = true;
            }
            else
            {
                telemetryCollection         = new TelemetryCollection(telemetryIdentifier, dailyNumberOfTelemetryPushes);
                telCollectionloadedFromDisk = false;
            }

            return(telemetryCollection);
        }
Beispiel #17
0
 public SemanticsAnalyzer(IEnumerable <string> manifestFilePaths, bool enableManifestSemanticsCheck, LogErrorDelegate logErrorDelegate, bool verbose)
 {
     this.manifestSemanticsCheckEnabled = enableManifestSemanticsCheck;
     this.manifestLookup = new ManifestEventFieldsLookup(manifestFilePaths, logErrorDelegate, verbose);
 }
Beispiel #18
0
 private void LogError(string msg)
 {
     LogErrorDelegate?.BeginInvoke($"ImageProcessor Error: {msg}", (r) => { }, null);
 }
Beispiel #19
0
 public static extern int UWK_Initialize(LogCallbackDelegate logcb, LogErrorDelegate errorcb, ProcessMessageDelegate processcb, IntPtr initJson);