/// <summary>
        /// Create or update cache.
        /// </summary>
        /// <param name="destDir">Destination directory.</param>
        /// <param name="scriptText">Script text.</param>
        /// <param name="assemblyPath">Assembly path.</param>
        /// <param name="metadata">Metadata</param>
        /// <returns>Task for downloading and updating cache.</returns>
        public async Task CreateOrUpdateCacheAsync(DirectoryInfo destDir, string scriptText, string assemblyPath, string metadata)
        {
            LogMessage($"Loading assembly : {assemblyPath}");
            var asm = Assembly.LoadFrom(assemblyPath);

            var newInvoker = new EntityInvoker(new EntityMetadata(scriptText, GetEntityType(), metadata));

            newInvoker.InitializeEntryPoint(asm);

            var cacheIdFilePath = Path.Combine(destDir.FullName, _cacheIdFileName);

            // Remove the Old Invoker from Cache
            var lastCacheId = await FileHelper.GetFileContentAsync(cacheIdFilePath);

            if (!string.IsNullOrWhiteSpace(lastCacheId) && GetCacheService().TryRemoveValue(lastCacheId, out EntityInvoker oldInvoker))
            {
                LogMessage($"Removing old invoker with id : {oldInvoker.EntryPointDefinitionAttribute.Id} from Cache");
                oldInvoker.Dispose();
            }

            // Add new invoker to Cache and update Cache Id File
            if (newInvoker.EntryPointDefinitionAttribute != null)
            {
                LogMessage($"Updating cache with  new invoker with id : {newInvoker.EntryPointDefinitionAttribute.Id}");
                GetCacheService().AddOrUpdate(newInvoker.EntryPointDefinitionAttribute.Id, newInvoker);
                await FileHelper.WriteToFileAsync(cacheIdFilePath, newInvoker.EntryPointDefinitionAttribute.Id);
            }
            else
            {
                LogWarning("Missing Entry Point Definition attribute. skipping cache update");
            }
        }
Пример #2
0
        private bool VerifyEntity(EntityInvoker invoker, ref QueryResponse <DiagnosticApiResponse> queryRes)
        {
            List <EntityInvoker> allDetectors = this._invokerCache.GetAll().ToList();

            foreach (var topicId in invoker.EntryPointDefinitionAttribute.SupportTopicList)
            {
                var existingDetector = allDetectors.FirstOrDefault(p =>
                                                                   (!p.EntryPointDefinitionAttribute.Id.Equals(invoker.EntryPointDefinitionAttribute.Id, StringComparison.OrdinalIgnoreCase) && p.EntryPointDefinitionAttribute.SupportTopicList.Contains(topicId)));
                if (existingDetector != default(EntityInvoker))
                {
                    // There exists a detector which has same support topic id.
                    queryRes.CompilationOutput.CompilationSucceeded = false;
                    queryRes.CompilationOutput.AssemblyBytes        = string.Empty;
                    queryRes.CompilationOutput.PdbBytes             = string.Empty;
                    queryRes.CompilationOutput.CompilationOutput    = queryRes.CompilationOutput.CompilationOutput.Concat(new List <string>()
                    {
                        $"Error : There is already a detector(id : {existingDetector.EntryPointDefinitionAttribute.Id}, name : {existingDetector.EntryPointDefinitionAttribute.Name})" +
                        $" that uses the SupportTopic (id : {topicId.Id}, pesId : {topicId.PesId}). System can't have two detectors for same support topic id. Consider merging these two detectors."
                    });

                    return(false);
                }
            }

            return(true);
        }
        public async void EntityInvoker_InvalidSupportTopicId(string supportTopicId, string pesId, bool isInternal)
        {
            Definition definitonAttribute = new Definition()
            {
                Id     = "TestId",
                Name   = "Test",
                Author = "User"
            };

            SupportTopic topic1 = new SupportTopic()
            {
                Id = supportTopicId, PesId = pesId
            };
            SupportTopic topic2 = new SupportTopic()
            {
                Id = "5678", PesId = "14878"
            };

            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScriptWithMultipleSupportTopics(definitonAttribute, isInternal, topic1, topic2);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                Assert.False(invoker.IsCompilationSuccessful);
                Assert.NotEmpty(invoker.CompilationOutput);
            }
        }
Пример #4
0
        public async void TestMdmGetDimensionValuesAsync()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<IEnumerable<string>> Run(DataProviders dataProviders) {
                    var filter = new List<Tuple<string, IEnumerable<string>>>
                    {
                        new Tuple<string, IEnumerable<string>>(""StampName"", new List<string>())
                    };

                    return await dataProviders.Mdm.GetDimensionValuesAsync(""Microsoft/Web/WebApps"", ""CpuTime"", filter, ""ServerName"", DateTime.UtcNow.AddMinutes(-30), DateTime.UtcNow);
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = await invoker.Invoke(new object[] { dataProviders }) as IEnumerable <string>;

                Assert.NotNull(result);
                Assert.True(result.Count() == 3);
            }
        }
        public async void EntityInvoker_TestSupportTopicAttributeResolution()
        {
            Definition definitonAttribute = new Definition()
            {
                Id     = "TestId",
                Name   = "Test",
                Author = "User"
            };

            SupportTopic topic1 = new SupportTopic()
            {
                Id = "1234", PesId = "14878"
            };
            SupportTopic topic2 = new SupportTopic()
            {
                Id = "5678", PesId = "14878"
            };

            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScriptWithMultipleSupportTopics(definitonAttribute, false, topic1, topic2);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                Assert.True(invoker.IsCompilationSuccessful);
                Assert.Contains <SupportTopic>(topic1, invoker.EntryPointDefinitionAttribute.SupportTopicList);
                Assert.Contains <SupportTopic>(topic2, invoker.EntryPointDefinitionAttribute.SupportTopicList);
            }
        }
Пример #6
0
        public async void E2E_Test_RuntimeSlotMapData()
        {
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            //read a sample csx file from local directory
            metadata.ScriptText = await File.ReadAllTextAsync("GetRuntimeSiteSlotMapData.csx");

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var appResource = new App(string.Empty, string.Empty, "my-api")
                {
                    Stamp = new HostingEnvironment(string.Empty, string.Empty, "waws-prod-bn1-71717c45")
                    {
                        Name = "waws-prod-bn1-71717c45"
                    }
                };

                var operationContext = new OperationContext <App>(appResource, string.Empty, string.Empty, true, string.Empty);
                var response         = new Response();

                Response result = (Response)await invoker.Invoke(new object[] { dataProviders, operationContext, response });

                Assert.Equal("my-api__a88nf", result.Dataset.First().Table.Rows[1][1]);
            }
        }
        public async void TestDetectorWithMDMConfigurationGists()
        {
            var references = new Dictionary <string, string>
            {
                { "mdm", GetMDMConfigurationGist() },
            };

            var metadata = new EntityMetadata(GetMDMDetector(), EntityType.Detector);

            var dataSourceConfiguration = new MockDataProviderConfigurationFactory();

            var config = dataSourceConfiguration.LoadConfigurations();

            ArmResource resource = new ArmResource("751A8C1D-EA9D-4FE7-8574-3096A81C2C08", "testResourceGroup", "Microsoft.AppPlatform", "Spring", "testResource", "FakeLocation");
            OperationContext <ArmResource> context = new OperationContext <ArmResource>(resource, "2019-12-09T00:10", "2019-12-09T23:54", true, "A9854948-807B-4371-B834-3EC78BB6635C");
            Response response = new Response();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, incomingHeaders: new HeaderDictionary()
            {
                [HeaderConstants.LocationHeader] = resource.Location
            }));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports(), references.ToImmutableDictionary()))
            {
                await invoker.InitializeEntryPointAsync().ConfigureAwait(false);

                await invoker.Invoke(new object[] { dataProviders, context, response }).ConfigureAwait(false);

                Assert.Equal("Diagnostics.DataProviders.MdmLogDecorator", response.Insights[0].Message);
            }
        }
        public async Task <IActionResult> Post([FromBody] JToken jsonBody)
        {
            if (jsonBody == null)
            {
                return(BadRequest("Missing body"));
            }

            string script = jsonBody.Value <string>("script");

            if (string.IsNullOrWhiteSpace(script))
            {
                return(BadRequest("Missing script from body"));
            }

            EntityMetadata   metaData         = new EntityMetadata(script);
            CompilerResponse compilerResponse = new CompilerResponse();

            using (var invoker = new EntityInvoker(metaData, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                compilerResponse.CompilationOutput    = invoker.CompilationOutput;
                compilerResponse.CompilationSucceeded = invoker.IsCompilationSuccessful;

                if (compilerResponse.CompilationSucceeded)
                {
                    Tuple <string, string> asmBytes = await invoker.GetAssemblyBytesAsync();

                    compilerResponse.AssemblyBytes = asmBytes.Item1;
                    compilerResponse.PdbBytes      = asmBytes.Item2;
                }
            }

            return(Ok(compilerResponse));
        }
        /// <summary>
        /// Create or update cache.
        /// </summary>
        /// <param name="subDir">Directory info.</param>
        /// <returns>Task for adding item to cache.</returns>
        public virtual async Task CreateOrUpdateCacheAsync(DirectoryInfo subDir)
        {
            try
            {
                var cacheId = await FileHelper.GetFileContentAsync(subDir.FullName, _cacheIdFileName);

                if (string.IsNullOrWhiteSpace(cacheId) || !GetCacheService().TryGetValue(cacheId, out EntityInvoker invoker))
                {
                    LogMessage($"Folder : {subDir.FullName} missing in invoker cache.");

                    // Check if delete marker file exists.
                    var deleteMarkerFile = new FileInfo(Path.Combine(subDir.FullName, _deleteMarkerName));
                    if (deleteMarkerFile.Exists)
                    {
                        LogMessage("Folder marked for deletion. Skipping cache update");
                        return;
                    }

                    var mostRecentAssembly = GetMostRecentFileByExtension(subDir, ".dll");
                    var csxScriptFile      = GetMostRecentFileByExtension(subDir, ".csx");
                    var metadataFile       = Path.Combine(subDir.FullName, "metadata.json");
                    if (mostRecentAssembly == default(FileInfo) || csxScriptFile == default(FileInfo))
                    {
                        LogWarning("No Assembly file (.dll) or Csx File found (.csx). Skipping cache update");
                        return;
                    }

                    var scriptText = await FileHelper.GetFileContentAsync(csxScriptFile.FullName);

                    var metadata = string.Empty;
                    if (File.Exists(metadataFile))
                    {
                        metadata = await FileHelper.GetFileContentAsync(metadataFile);
                    }

                    LogMessage($"Loading assembly : {mostRecentAssembly.FullName}");
                    var asm = Assembly.LoadFrom(mostRecentAssembly.FullName);
                    invoker = new EntityInvoker(new EntityMetadata(scriptText, GetEntityType(), metadata));
                    invoker.InitializeEntryPoint(asm);

                    if (invoker.EntryPointDefinitionAttribute != null)
                    {
                        LogMessage($"Updating cache with new invoker with id : {invoker.EntryPointDefinitionAttribute.Id}");
                        GetCacheService().AddOrUpdate(invoker.EntryPointDefinitionAttribute.Id, invoker);
                        await FileHelper.WriteToFileAsync(subDir.FullName, _cacheIdFileName, invoker.EntryPointDefinitionAttribute.Id);
                    }
                    else
                    {
                        LogWarning("Missing Entry Point Definition attribute. skipping cache update");
                    }
                }
            }
            catch (Exception ex)
            {
                LogException(ex.Message, ex);
            }
        }
        public async void EntityInvoker_TestInvokeMethod()
        {
            using (EntityInvoker invoker = new EntityInvoker(ScriptTestDataHelper.GetRandomMetadata(), ImmutableArray.Create <string>()))
            {
                await invoker.InitializeEntryPointAsync();

                int result = (int)await invoker.Invoke(new object[] { 3 });

                Assert.Equal(9, result);
            }
        }
Пример #11
0
        private async Task AddInvokerToCacheIfNeeded(DirectoryInfo subDir)
        {
            try
            {
                string cacheId = await FileHelper.GetFileContentAsync(subDir.FullName, _cacheIdFileName);

                if (string.IsNullOrWhiteSpace(cacheId) || !_invokerCache.TryGetValue(cacheId, out EntityInvoker invoker))
                {
                    LogMessage($"Folder : {subDir.FullName} missing in invoker cache.");
                    FileInfo mostRecentAssembly = GetMostRecentFileByExtension(subDir, ".dll");
                    FileInfo csxScriptFile      = GetMostRecentFileByExtension(subDir, ".csx");
                    FileInfo deleteMarkerFile   = new FileInfo(Path.Combine(subDir.FullName, _deleteMarkerName));

                    if (mostRecentAssembly == default(FileInfo) || csxScriptFile == default(FileInfo))
                    {
                        LogWarning($"No Assembly file (.dll) or Csx File found (.csx). Skipping cache update");
                        return;
                    }

                    if (deleteMarkerFile.Exists)
                    {
                        LogMessage("Folder marked for deletion. Skipping cache update");
                        return;
                    }

                    string scriptText = await FileHelper.GetFileContentAsync(csxScriptFile.FullName);

                    LogMessage($"Loading assembly : {mostRecentAssembly.FullName}");
                    Assembly asm = Assembly.LoadFrom(mostRecentAssembly.FullName);
                    invoker = new EntityInvoker(new EntityMetadata(scriptText));
                    invoker.InitializeEntryPoint(asm);

                    if (invoker.EntryPointDefinitionAttribute != null)
                    {
                        LogMessage($"Updating cache with  new invoker with id : {invoker.EntryPointDefinitionAttribute.Id}");
                        _invokerCache.AddOrUpdate(invoker.EntryPointDefinitionAttribute.Id, invoker);
                        await FileHelper.WriteToFileAsync(subDir.FullName, _cacheIdFileName, invoker.EntryPointDefinitionAttribute.Id);
                    }
                    else
                    {
                        LogWarning("Missing Entry Point Definition attribute. skipping cache update");
                    }
                }
            }
            catch (Exception ex)
            {
                LogException(ex.Message, ex);
            }
        }
        private async Task StartWatcherInternal()
        {
            try
            {
                LogMessage("SourceWatcher : Start");

                DirectoryInfo srcDirectoryInfo = new DirectoryInfo(_localScriptsPath);
                foreach (DirectoryInfo srcSubDirInfo in srcDirectoryInfo.GetDirectories())
                {
                    LogMessage($"Scanning in folder : {srcSubDirInfo.FullName}");
                    var files   = srcSubDirInfo.GetFiles().OrderByDescending(p => p.LastWriteTimeUtc);
                    var csxFile = files.FirstOrDefault(p => p.Extension.Equals(".csx", StringComparison.OrdinalIgnoreCase));
                    var asmFile = files.FirstOrDefault(p => p.Extension.Equals(".dll", StringComparison.OrdinalIgnoreCase));

                    string scriptText = string.Empty;
                    if (csxFile != default(FileInfo))
                    {
                        scriptText = await File.ReadAllTextAsync(csxFile.FullName);
                    }

                    EntityMetadata scriptMetadata = new EntityMetadata(scriptText);
                    EntityInvoker  invoker        = new EntityInvoker(scriptMetadata);

                    if (asmFile == default(FileInfo))
                    {
                        LogWarning($"No Assembly file (.dll). Skipping cache update");
                        continue;
                    }

                    LogMessage($"Loading assembly : {asmFile.FullName}");
                    Assembly asm = Assembly.LoadFrom(asmFile.FullName);
                    invoker.InitializeEntryPoint(asm);
                    LogMessage($"Updating cache with  new invoker with id : {invoker.EntryPointDefinitionAttribute.Id}");
                    _invokerCache.AddOrUpdate(invoker.EntryPointDefinitionAttribute.Id, invoker);
                }
            }
            catch (Exception ex)
            {
                LogException(ex.Message, ex);
            }
            finally
            {
                LogMessage("SourceWatcher : End");
            }
        }
        public async void EntityInvoker_TestInvokeWithCompilationError(ScriptErrorType errorType)
        {
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = ScriptTestDataHelper.GetInvalidCsxScript(errorType);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ImmutableArray.Create <string>()))
            {
                ScriptCompilationException ex = await Assert.ThrowsAsync <ScriptCompilationException>(async() =>
                {
                    await invoker.InitializeEntryPointAsync();
                    int result = (int)await invoker.Invoke(new object[] { 3 });
                    Assert.Equal(9, result);
                });

                Assert.NotEmpty(ex.CompilationOutput);
            }
        }
        public async void EntityInvoker_InvalidDetectorId(string idValue)
        {
            Definition def = new Definition()
            {
                Id = idValue
            };
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScript(def, ResourceType.App);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                Assert.False(invoker.IsCompilationSuccessful);
                Assert.NotEmpty(invoker.CompilationOutput);
            }
        }
        public async Task <IActionResult> Post([FromBody] JToken jsonBody)
        {
            if (jsonBody == null)
            {
                return(BadRequest("Missing body"));
            }

            // Get script and reference
            var script = jsonBody.Value <string>("script");

            if (string.IsNullOrWhiteSpace(script))
            {
                return(BadRequest("Missing script from body"));
            }

            var references = TryExtract(jsonBody, "reference") ?? new Dictionary <string, string>();

            if (!Enum.TryParse(jsonBody.Value <string>("entityType"), true, out EntityType entityType))
            {
                entityType = EntityType.Signal;
            }

            var metaData         = new EntityMetadata(script, entityType);
            var compilerResponse = new CompilerResponse();

            using (var invoker = new EntityInvoker(metaData, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports(), references.ToImmutableDictionary()))
            {
                await invoker.InitializeEntryPointAsync();

                compilerResponse.CompilationTraces    = invoker.CompilationOutput;
                compilerResponse.CompilationSucceeded = invoker.IsCompilationSuccessful;
                compilerResponse.References           = invoker.References;

                if (compilerResponse.CompilationSucceeded)
                {
                    var asmBytes = await invoker.GetAssemblyBytesAsync();

                    compilerResponse.AssemblyBytes = asmBytes.Item1;
                    compilerResponse.PdbBytes      = asmBytes.Item2;
                }
            }

            return(Ok(compilerResponse));
        }
Пример #16
0
        public async Task E2E_Test_WAWSObserverAsync()
        {
            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            //read a sample csx file from local directory
            metadata.ScriptText = await File.ReadAllTextAsync("BackupCheckDetector.csx");

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var appResource = new App(string.Empty, string.Empty, "my-api")
                {
                    Stamp = new HostingEnvironment(string.Empty, string.Empty, "waws-prod-bn1-71717c45")
                };

                appResource.Stamp.TenantIdList = new List <string>()
                {
                    Guid.NewGuid().ToString()
                };

                var operationContext = new OperationContext <App>(appResource, null, null, true, null);

                var response = new Response();

                try
                {
                    Response result = (Response)await invoker.Invoke(new object[] { dataProviders, operationContext, response });
                }
                catch (ScriptCompilationException ex)
                {
                    foreach (var output in ex.CompilationOutput)
                    {
                        Trace.WriteLine(output);
                    }
                }
            }
        }
        public async void EntityInvoker_TestGistResourceAttributeResolution(ResourceType resType, Type filterType)
        {
            Definition definitonAttribute = new Definition()
            {
                Id = "TestId"
            };

            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata(EntityType.Gist);

            metadata.ScriptText = await ScriptTestDataHelper.GetGistScript(definitonAttribute, resType);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                Assert.Equal(definitonAttribute, invoker.EntryPointDefinitionAttribute);
                Assert.Equal(filterType, invoker.ResourceFilter.GetType());
            }
        }
Пример #18
0
        public async void DataProvders_TestKusto()
        {
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = GetDataProviderScript("TestA");

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                DataTable result = (DataTable)await invoker.Invoke(new object[] { dataProviders });

                Assert.NotNull(result);
            }
        }
        public async void EntityInvoker_TestSaveAssemblyToInvalidPath()
        {
            Definition definitonAttribute = new Definition()
            {
                Id = "TestId"
            };

            string         assemblyPath = string.Empty;
            EntityMetadata metadata     = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScript(definitonAttribute);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                await Assert.ThrowsAsync <ArgumentNullException>(async() =>
                {
                    await invoker.SaveAssemblyToDiskAsync(assemblyPath);
                });
            }
        }
        public async void EntityInvoker_TestSaveAssemblyToDisk()
        {
            Definition definitonAttribute = new Definition()
            {
                Id = "TestId"
            };

            string         assemblyPath = $@"{Directory.GetCurrentDirectory()}\{Guid.NewGuid().ToString()}";
            EntityMetadata metadata     = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScript(definitonAttribute);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                await invoker.SaveAssemblyToDiskAsync(assemblyPath);

                Assert.True(File.Exists($"{assemblyPath}.dll"));
                Assert.True(File.Exists($"{assemblyPath}.pdb"));
            }
        }
        public async void EntityInvoker_TestDetectorWithGists()
        {
            var gist       = ScriptTestDataHelper.GetGist();
            var references = new Dictionary <string, string>
            {
                { "xxx", gist },
                { "yyy", "" },
                { "zzz", "" }
            };

            var metadata = new EntityMetadata(ScriptTestDataHelper.GetSentinel(), EntityType.Detector);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports(), references.ToImmutableDictionary()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = (string)await invoker.Invoke(new object[] { });

                Assert.Equal(2, invoker.References.Count());
                Assert.False(string.IsNullOrWhiteSpace(result));
            }
        }
        public async void EntityInvoker_TestInitializationUsingAssembly()
        {
            // First Create and Save a assembly for test purposes.
            Definition definitonAttribute = new Definition()
            {
                Id = "TestId"
            };

            string         assemblyPath = $@"{Directory.GetCurrentDirectory()}/{Guid.NewGuid().ToString()}";
            EntityMetadata metadata     = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScript(definitonAttribute);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                await invoker.SaveAssemblyToDiskAsync(assemblyPath);

                Assert.True(File.Exists($"{assemblyPath}.dll"));
                Assert.True(File.Exists($"{assemblyPath}.pdb"));
            }

            // Now test initializing Entry Point of Invoker using assembly
            Assembly asm = Assembly.LoadFrom($"{assemblyPath}.dll");

            using (EntityInvoker invoker = new EntityInvoker(metadata))
            {
                Exception ex = Record.Exception(() =>
                {
                    invoker.InitializeEntryPoint(asm);
                });

                Assert.Null(ex);
                Assert.True(invoker.IsCompilationSuccessful);
                Assert.Equal(definitonAttribute.Id, invoker.EntryPointDefinitionAttribute.Id);
            }
        }
Пример #23
0
        public async void DataProvders_TestKusto()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<DataTable> Run(DataProviders dataProviders) {
                    return await dataProviders.Kusto.ExecuteQuery(""TestA"", ""waws-prod-mockstamp"");
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = (DataTable)await invoker.Invoke(new object[] { dataProviders });

                Assert.NotNull(result);
            }
        }
        public async void EntityInvoker_TestSystemFilterAttributeResolution()
        {
            Definition definitonAttribute = new Definition()
            {
                Id     = "TestId",
                Name   = "Test",
                Author = "User"
            };

            SystemFilter   filter   = new SystemFilter();
            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetSystemInvokerScript(definitonAttribute);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                Assert.True(invoker.IsCompilationSuccessful);
                Assert.NotNull(invoker.SystemFilter);
                Assert.Equal(filter, invoker.SystemFilter);
            }
        }
Пример #25
0
        public async void TestMdmGetTimeSeriesValuesAsync()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<IEnumerable<DataTable>> Run(DataProviders dataProviders) {
                    var dimensions = new Dictionary<string, string> { { ""StampName"", ""kudu1"" } };
                    return await dataProviders.Mdm.GetTimeSeriesAsync(DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow, Sampling.Average | Sampling.Max | Sampling.Count, ""Microsoft/Web/WebApps"", ""CpuTime"", dimensions);
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = await invoker.Invoke(new object[] { dataProviders }) as IEnumerable <DataTable>;

                Assert.NotNull(result);
            }
        }
        public async void EntityInvoker_TestGetAssemblyBytes()
        {
            Definition definitonAttribute = new Definition()
            {
                Id = "TestId"
            };

            EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = await ScriptTestDataHelper.GetDetectorScript(definitonAttribute);

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                Tuple <string, string> asmPair = await invoker.GetAssemblyBytesAsync();

                string assemblyBytes = asmPair.Item1;
                string pdbBytes      = asmPair.Item2;

                Assert.False(string.IsNullOrWhiteSpace(assemblyBytes));
                Assert.False(string.IsNullOrWhiteSpace(pdbBytes));
            }
        }
Пример #27
0
        public async void TestMdmGetMetricNamesAsync()
        {
            var metadata = ScriptTestDataHelper.GetRandomMetadata();

            metadata.ScriptText = @"
                public async static Task<IEnumerable<string>> Run(DataProviders dataProviders) {
                    return await dataProviders.Mdm.GetMetricNamesAsync(""Microsoft/Web/WebApps"");
                }";

            var configFactory = new MockDataProviderConfigurationFactory();
            var config        = configFactory.LoadConfigurations();

            var dataProviders = new DataProviders.DataProviders(new DataProviderContext(config, Guid.NewGuid().ToString()));

            using (EntityInvoker invoker = new EntityInvoker(metadata, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
            {
                await invoker.InitializeEntryPointAsync();

                var result = await invoker.Invoke(new object[] { dataProviders }) as IEnumerable <string>;

                Assert.NotNull(result);
                Assert.True(result.Count() == 3);
            }
        }
Пример #28
0
        protected async Task <IActionResult> ExecuteQuery <TPostBodyResource>(TResource resource, CompilationBostBody <TPostBodyResource> jsonBody, string startTime, string endTime, string timeGrain)
        {
            if (jsonBody == null)
            {
                return(BadRequest("Missing body"));
            }

            if (string.IsNullOrWhiteSpace(jsonBody.Script))
            {
                return(BadRequest("Missing script in body"));
            }

            if (!DateTimeHelper.PrepareStartEndTimeWithTimeGrain(startTime, endTime, timeGrain, out DateTime startTimeUtc, out DateTime endTimeUtc, out TimeSpan timeGrainTimeSpan, out string errorMessage))
            {
                return(BadRequest(errorMessage));
            }

            await this._sourceWatcherService.Watcher.WaitForFirstCompletion();

            EntityMetadata metaData      = new EntityMetadata(jsonBody.Script);
            var            dataProviders = new DataProviders.DataProviders(_dataSourcesConfigService.Config);

            QueryResponse <DiagnosticApiResponse> queryRes = new QueryResponse <DiagnosticApiResponse>
            {
                InvocationOutput = new DiagnosticApiResponse()
            };

            Assembly tempAsm = null;

            this.Request.Headers.TryGetValue(HeaderConstants.RequestIdHeaderName, out StringValues requestIds);
            var compilerResponse = await _compilerHostClient.GetCompilationResponse(jsonBody.Script, requestIds.FirstOrDefault() ?? string.Empty);

            queryRes.CompilationOutput = compilerResponse;

            if (queryRes.CompilationOutput.CompilationSucceeded)
            {
                byte[] asmData = Convert.FromBase64String(compilerResponse.AssemblyBytes);
                byte[] pdbData = Convert.FromBase64String(compilerResponse.PdbBytes);

                tempAsm = Assembly.Load(asmData, pdbData);

                using (var invoker = new EntityInvoker(metaData, ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports()))
                {
                    invoker.InitializeEntryPoint(tempAsm);

                    // Verify Detector with other detectors in the system in case of conflicts
                    if (!VerifyEntity(invoker, ref queryRes))
                    {
                        return(Ok(queryRes));
                    }
                    OperationContext <TResource> cxt = PrepareContext(resource, startTimeUtc, endTimeUtc);
                    List <DataProviderMetadata>  dataProvidersMetadata = null;

                    try
                    {
                        var responseInput = new Response()
                        {
                            Metadata = RemovePIIFromDefinition(invoker.EntryPointDefinitionAttribute, cxt.IsInternalCall)
                        };
                        var invocationResponse = (Response)await invoker.Invoke(new object[] { dataProviders, cxt, responseInput });

                        invocationResponse.UpdateDetectorStatusFromInsights();

                        if (cxt.IsInternalCall)
                        {
                            dataProvidersMetadata = GetDataProvidersMetadata(dataProviders);
                        }

                        queryRes.RuntimeSucceeded = true;
                        queryRes.InvocationOutput = DiagnosticApiResponse.FromCsxResponse(invocationResponse, dataProvidersMetadata);
                    }
                    catch (Exception ex)
                    {
                        if (cxt.IsInternalCall)
                        {
                            queryRes.RuntimeSucceeded = false;
                            queryRes.InvocationOutput = CreateQueryExceptionResponse(ex, invoker.EntryPointDefinitionAttribute, cxt.IsInternalCall, GetDataProvidersMetadata(dataProviders));
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            return(Ok(queryRes));
        }
Пример #29
0
 public void AddOrUpdate(string key, EntityInvoker value)
 {
     _collection.AddOrUpdate(key.ToLower(), value, (existingKey, oldValue) => value);
 }
Пример #30
0
 public bool TryGetValue(string key, out EntityInvoker value)
 {
     return(_collection.TryGetValue(key.ToLower(), out value));
 }