public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", "options", Route = null)] HttpRequestMessage req, TraceWriter log) { // CosmosDB Parameters, retrieved via environment variables string databaseName = Environment.GetEnvironmentVariable("cosmosdbDatabaseName"); string collectionName = Environment.GetEnvironmentVariable("cosmosdbCollectionName"); string mongodbConnectionString = Environment.GetEnvironmentVariable("cosmosdbMongodbConnectionString"); // Set BSON AutoMap //BsonClassMap.RegisterClassMap<VmSize>(); // This endpoint is valid for all MongoDB var client = new MongoClient(mongodbConnectionString); var database = client.GetDatabase(databaseName); var collection = database.GetCollection <BsonDocument>(collectionName); // Get Parameters dynamic contentdata = await req.Content.ReadAsAsync <object>(); // Cores (Min) # decimal cores = Convert.ToDecimal(GetParameter("cores", "0", req)); cores = SetMinimum(cores, 0); log.Info("Cores : " + cores.ToString()); // Physical Cores (Min) # decimal pcores = Convert.ToDecimal(GetParameter("pcores", "0", req)); pcores = SetMinimum(pcores, -127); log.Info("PCores : " + pcores.ToString()); // Azure Compute Unit (Min) # decimal acu = Convert.ToDecimal(GetParameter("acu", "-127", req)); acu = SetMinimum(acu, -127); log.Info("ACU : " + acu.ToString()); // DataBricks DBU (Min) # decimal dbu = Convert.ToDecimal(GetParameter("dbu", "-127", req)); dbu = SetMinimum(dbu, -127); log.Info("DBU : " + dbu.ToString()); // Memory (Min) # decimal memory = Convert.ToDecimal(GetParameter("memory", "0", req)); memory = SetMinimum(memory, 0); log.Info("Memory : " + memory.ToString()); // NICS (Min) # decimal nics = Convert.ToDecimal(GetParameter("nics", "-127", req)); nics = SetMinimum(nics, -127); log.Info("NICs : " + nics.ToString()); // IOPS (Min) # decimal iops = Convert.ToDecimal(GetParameter("iops", "-127", req)); iops = SetMinimum(iops, -127); log.Info("IOPS : " + iops.ToString()); // Throughput (Min) # decimal throughput = Convert.ToDecimal(GetParameter("throughput", "-127", req)); throughput = SetMinimum(throughput, -127); log.Info("Throughput : " + throughput.ToString()); // Data (Disk Capacity) (Min) # decimal data = Convert.ToDecimal(GetParameter("data", "-127", req)); data = SetMinimum(data, -127); log.Info("Data : " + data.ToString()); // Temp (Disk Capacity) (Min) # decimal temp = Convert.ToDecimal(GetParameter("temp", "-127", req)); temp = SetMinimum(temp, -127); log.Info("Temp : " + temp.ToString()); // Hyperthreading string ht = GetParameter("ht", "all", req).ToLower(); string[] htfilter = new string[2]; htfilter = YesNoAll(ht); log.Info("HyperTreading : " + ht.ToString()); log.Info("HyperTreading[0] : " + htfilter[0]); log.Info("HyperTreading[1] : " + htfilter[1]); // Tier string tier = GetParameter("tier", "standard", req).ToLower(); log.Info("Tier : " + tier.ToString()); // SAP HANA string saphana = GetParameter("saphana", "all", req).ToLower(); string[] saphanafilter = new string[2]; saphanafilter = YesNoAll(saphana); log.Info("SAPHANA : " + saphana.ToString()); log.Info("SAPHANA[0] : " + saphanafilter[0]); log.Info("SAPHANA[1] : " + saphanafilter[1]); // SAPS 2T # decimal saps2t = Convert.ToDecimal(GetParameter("saps2t", "-127", req)); if (saps2t <= 0) { saps2t = -127; } saps2t = SetMinimum(saps2t, -127); log.Info("SAPS2T : " + saps2t.ToString()); // SAPS 3T # decimal saps3t = Convert.ToDecimal(GetParameter("saps3t", "-127", req)); if (saps3t <= 0) { saps3t = -127; } saps3t = SetMinimum(saps3t, -127); log.Info("SAPS3T : " + saps3t.ToString()); // Ssd string ssd = GetParameter("ssd", "all", req).ToLower(); string[] ssdfilter = new string[2]; ssdfilter = YesNoAll(ssd); log.Info("SSD : " + ssd.ToString()); log.Info("SSD[0] : " + ssdfilter[0]); log.Info("SSD[1] : " + ssdfilter[1]); // Burstable string burstable = GetParameter("burstable", "all", req).ToLower(); string[] burstablefilter = new string[2]; burstablefilter = YesNoAll(burstable); log.Info("Burstable : " + burstable.ToString()); log.Info("Burstable[0] : " + burstablefilter[0]); log.Info("Burstable[1] : " + burstablefilter[1]); // Isolated string isolated = GetParameter("isolated", "all", req).ToLower(); string[] isolatedfilter = new string[2]; isolatedfilter = YesNoAll(isolated); log.Info("Isolated : " + isolated.ToString()); log.Info("Isolated[0] : " + isolatedfilter[0]); log.Info("Isolated[1] : " + isolatedfilter[1]); // Constrained string constrained = GetParameter("constrained", "all", req).ToLower(); string[] constrainedfilter = new string[2]; constrainedfilter = YesNoAll(constrained); log.Info("Constrained : " + constrained.ToString()); log.Info("Constrained[0] : " + constrainedfilter[0]); log.Info("Constrained[1] : " + constrainedfilter[1]); // Infiniband string infiniband = GetParameter("infiniband", "all", req).ToLower(); string[] infinibandfilter = new string[2]; infinibandfilter = YesNoAll(infiniband); log.Info("Infiniband : " + infiniband.ToString()); log.Info("Infiniband[0] : " + infinibandfilter[0]); log.Info("Infiniband[1] : " + infinibandfilter[1]); // GPU string gpu = GetParameter("gpu", "all", req).ToLower(); string[] gpufilter = new string[2]; gpufilter = YesNoAll(gpu); log.Info("GPU : " + gpu.ToString()); log.Info("GPU[0] : " + gpufilter[0]); log.Info("GPU[1] : " + gpufilter[1]); // SGX string sgx = GetParameter("sgx", "all", req).ToLower(); string[] sgxfilter = new string[2]; sgxfilter = YesNoAll(sgx); log.Info("SGX : " + sgx.ToString()); log.Info("SGX[0] : " + sgxfilter[0]); log.Info("SGX[1] : " + sgxfilter[1]); // Region string region = GetParameter("region", "europe-west", req).ToLower(); log.Info("Region : " + region.ToString()); // Currency string currency = GetParameter("currency", "EUR", req).ToUpper(); log.Info("Currency : " + currency.ToString()); // Contract string contract = GetParameter("contract", "payg", req).ToLower(); log.Info("Contract : " + contract.ToString()); // Results (Max) # decimal results = Convert.ToDecimal(GetParameter("maxresults", "5", req)); results = SetMinimum(results, 1); results = SetMaximum(results, 100); log.Info("Results : " + results.ToString()); // Right Sizing CPU 95pct (Peak Util) % decimal avgcpupeak = Convert.ToDecimal(GetParameter("avgcpupeak", "100", req)); avgcpupeak = SetMinimum(avgcpupeak, 0); avgcpupeak = SetMaximum(avgcpupeak, 100); log.Info("AvgCpuPeak : " + avgcpupeak.ToString()); // Right Sizing Memory 95pct (Peak GB) % decimal avgmempeak = Convert.ToDecimal(GetParameter("avgmempeak", "100", req)); avgmempeak = SetMinimum(avgmempeak, 0); avgmempeak = SetMaximum(avgmempeak, 100); log.Info("AvgMemPeak : " + avgmempeak.ToString()); // Right Sizing CPU cores = cores * avgcpupeak / 100; pcores = pcores * avgcpupeak / 100; log.Info("Cores* : " + cores.ToString()); log.Info("PCores* : " + pcores.ToString()); // Right Sizing Memory memory = memory * avgmempeak / 100; log.Info("Memory* : " + memory.ToString()); // os string os = GetParameter("os", "linux", req).ToLower(); log.Info("OS : " + os.ToString()); var filterBuilder = Builders <BsonDocument> .Filter; var filter = filterBuilder.Eq("type", "vm") & filterBuilder.Gte("cores", Convert.ToInt16(cores)) & filterBuilder.Gte("mem", Convert.ToInt16(memory)) & filterBuilder.Gte("pcores", Convert.ToInt16(pcores)) & filterBuilder.Gte("ACU", Convert.ToInt16(acu)) & filterBuilder.Gte("dbu", Convert.ToInt16(dbu)) & filterBuilder.Gte("MaxNics", Convert.ToInt16(nics)) & filterBuilder.Gte("MaxVmIops", Convert.ToInt16(iops)) & filterBuilder.Gte("MaxVmThroughputMBs", Convert.ToInt16(throughput)) & filterBuilder.Gte("MaxDataDiskSizeGB", Convert.ToInt16(data)) & filterBuilder.Gte("TempDiskSizeInGB", Convert.ToInt16(temp)) & filterBuilder.Eq("region", region) & filterBuilder.Eq("tier", tier) & filterBuilder.In("Hyperthreaded", htfilter) & filterBuilder.In("isolated", isolatedfilter) & filterBuilder.In("constrained", constrainedfilter) & filterBuilder.In("burstable", burstablefilter) & filterBuilder.In("infiniband", infinibandfilter) & filterBuilder.In("gpu", gpufilter) & filterBuilder.In("sgx", sgxfilter) & filterBuilder.In("SSD", ssdfilter) & filterBuilder.Eq("contract", contract) & filterBuilder.Eq("os", os) & filterBuilder.In("SAPHANA", saphanafilter) & filterBuilder.Gte("SAPS2T", Convert.ToInt16(saps2t)) & filterBuilder.Gte("SAPS3T", Convert.ToInt16(saps3t)) ; var sort = Builders <BsonDocument> .Sort.Ascending("price"); var cursor = collection.Find <BsonDocument>(filter).Sort(sort).Limit(Convert.ToInt16(results)).ToCursor(); // Load Application Insights string ApplicationInsightsKey = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process); TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey = ApplicationInsightsKey }; // Get results and put them into a list of objects List <VmSize> documents = new List <VmSize>(); foreach (var document in cursor.ToEnumerable()) { // Get RequestCharge var LastRequestStatistics = database.RunCommand <BsonDocument>(new BsonDocument { { "getLastRequestStatistics", 1 } }); double RequestCharge = (double)LastRequestStatistics["RequestCharge"]; telemetry.TrackMetric("RequestCharge", RequestCharge); // Get Document log.Info(document.ToString()); VmSize myVmSize = BsonSerializer.Deserialize <VmSize>(document); myVmSize.setCurrency(currency); documents.Add(myVmSize); } // Convert to JSON & return it var json = JsonConvert.SerializeObject(documents, Formatting.Indented); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8, "application/json") }); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", "options", Route = null)] HttpRequestMessage req, TraceWriter log) { // CosmosDB Parameters, retrieved via environment variables string databaseName = Environment.GetEnvironmentVariable("cosmosdbDatabaseName"); string collectionName = Environment.GetEnvironmentVariable("cosmosdbCollectionName"); string mongodbConnectionString = Environment.GetEnvironmentVariable("cosmosdbMongodbConnectionString"); // Set BSON AutoMap //BsonClassMap.RegisterClassMap<VmSize>(); // This endpoint is valid for all MongoDB var client = new MongoClient(mongodbConnectionString); var database = client.GetDatabase(databaseName); var collection = database.GetCollection <BsonDocument>(collectionName); // Get Parameters dynamic contentdata = await req.Content.ReadAsAsync <object>(); // Tier # string tier = GetParameter("tier", "standard", req).ToLower(); log.Info("Tier : " + tier.ToString()); // Region # string region = GetParameter("region", "europe-west", req).ToLower(); log.Info("Region : " + region.ToString()); // Currency # string currency = GetParameter("currency", "EUR", req).ToUpper(); log.Info("Currency : " + currency.ToString()); // Name string vmsize = GetParameter("vmsize", "a0", req).ToLower(); log.Info("Name : " + vmsize.ToString()); // Get price for Linux var filterBuilder = Builders <BsonDocument> .Filter; var filter = filterBuilder.Eq("type", "vm") & filterBuilder.Eq("region", region) & filterBuilder.Eq("tier", tier) & filterBuilder.Eq("name", vmsize) ; var cursor = collection.Find <BsonDocument>(filter).ToCursor(); // Get results and put them into a list of objects var results = new VmSizeOptimizer(); results.Currency = currency; results.Name = vmsize; results.Region = region; results.Tier = tier; // Load Application Insights string ApplicationInsightsKey = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process); TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey = ApplicationInsightsKey }; foreach (var document in cursor.ToEnumerable()) { // Get RequestCharge var LastRequestStatistics = database.RunCommand <BsonDocument>(new BsonDocument { { "getLastRequestStatistics", 1 } }); double RequestCharge = (double)LastRequestStatistics["RequestCharge"]; telemetry.TrackMetric("RequestCharge", RequestCharge); // Get Document log.Info(document.ToString()); VmSize myVmSize = BsonSerializer.Deserialize <VmSize>(document); myVmSize.setCurrency(currency); log.Info("Price :" + myVmSize.Price + " - Contract : " + myVmSize.Contract + " - OS : " + myVmSize.OperatingSystem); results.SetPrice(myVmSize.Price, myVmSize.Contract, myVmSize.OperatingSystem); } results.SetDifferences(); // Convert to JSON & return it var json = JsonConvert.SerializeObject(results, Formatting.Indented); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8, "application/json") }); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "options", Route = null)] HttpRequest req, ILogger log) { // CosmosDB Parameters, retrieved via environment variables string databaseName = Environment.GetEnvironmentVariable("cosmosdbDatabaseName"); string collectionName = Environment.GetEnvironmentVariable("cosmosdbCollectionName"); string mongodbConnectionString = Environment.GetEnvironmentVariable("cosmosdbMongodbConnectionString"); // Set BSON AutoMap //BsonClassMap.RegisterClassMap<VmSize>(); // This endpoint is valid for all MongoDB var client = new MongoClient(mongodbConnectionString); var database = client.GetDatabase(databaseName); var collection = database.GetCollection <BsonDocument>(collectionName); // Tier # string tier = GetParameter("tier", "standard", req).ToLower(); log.LogInformation("Tier : " + tier.ToString()); // Region # string region = GetParameter("region", "europe-west", req).ToLower(); log.LogInformation("Region : " + region.ToString()); // Currency # string currency = GetParameter("currency", "EUR", req).ToUpper(); log.LogInformation("Currency : " + currency.ToString()); // Name string vmsize = GetParameter("vmsize", "a0", req).ToLower(); log.LogInformation("Name : " + vmsize.ToString()); // Get price for Linux var filterBuilder = Builders <BsonDocument> .Filter; var filter = filterBuilder.Eq("type", "vm") & filterBuilder.Eq("region", region) & filterBuilder.Eq("tier", tier) & filterBuilder.Eq("name", vmsize) ; var cursor = collection.Find <BsonDocument>(filter).ToCursor(); // Get results and put them into a list of objects List <VmSize> documents = new List <VmSize>(); foreach (var document in cursor.ToEnumerable()) { log.LogInformation(document.ToString()); VmSize myVmSize = BsonSerializer.Deserialize <VmSize>(document); log.LogInformation(myVmSize.OperatingSystem); myVmSize.setCurrency(currency); documents.Add(myVmSize); } // Convert to JSON & return it var json = JsonConvert.SerializeObject(documents, Formatting.Indented); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8, "application/json") }); }