예제 #1
0
        public bool AddVMInDB(VMDetails VMData, string BlobLocation, List<SummaryUtil> UtilPattern)
        {
            using (OpsIntel1Entities1 context = new OpsIntel1Entities1())
            {
                VMThreshold details = new VMThreshold
                {

                    VMName = VMData.Name,
                    Location = VMData.Location,
                    Description = VMData.Description,
                    UtilFileLocation = VMData.UtilFileLocation,
                    Status = "Created"

                };

                context.VMThresholds.Add(details);
                context.SaveChanges();
                var id = details.Id;


                foreach (var u in UtilPattern)
                {

                    // Console.WriteLine(u.UtilizationType);
                    foreach (var tr in u.TimeRanges)
                    {
                        //string InsertUtilPatternQuery = "INSERT into VMAutomationRules (VMID, UtilizationType, StartTime, EndTime) ";
                        //InsertUtilPatternQuery += " VALUES(@VMID, @UtilizationType, @StartTime, @EndTime);";
                        //SqlCommand InsertUtilData = new SqlCommand(InsertUtilPatternQuery, conn);

                        double Start = tr.StartTime ?? 0;
                        double Stop = tr.StopTime ?? 0;

                        TimeSpan StartTime = TimeSpan.FromHours(Start);
                        TimeSpan StopTime = TimeSpan.FromHours(Stop);

                        VMAutomationRule InsertUtilData = new VMAutomationRule
                        {
                            VMID = id,
                            UtilizationType = u.UtilizationType,
                            StartTime = StartTime,
                            EndTime = StopTime
                        };

                        context.VMAutomationRules.Add(InsertUtilData);
                        context.SaveChanges();
                        //InsertUtilData.Parameters.AddWithValue("@VMID", id);
                        //InsertUtilData.Parameters.AddWithValue("@UtilizationType", u.UtilizationType);
                        //InsertUtilData.Parameters.AddWithValue("@StartTime", StartTime);
                        //InsertUtilData.Parameters.AddWithValue("@EndTime", StopTime);

                        //var res = InsertUtilData.ExecuteNonQuery();
                        //Console.WriteLine("{0}, {1}", tr.StartTime, tr.StopTime);
                    }
                }


            }
            //using (var conn = new SqlConnection(ConnString))
            //{
            //    conn.Open();

            //    string InsertVMQuery = "Insert into VMThreshold (VMName, CloudServiceName, Description, UtilFileLocation, Status)";
            //    InsertVMQuery += "values (@VMName, @Location, @Description, @UtilFileLocation, @Status); select SCOPE_IDENTITY();";
            //    //InsertVMQuery += "Select @Id = Scope_Identity()";

            //    SqlCommand InsertVMCommand = new SqlCommand(InsertVMQuery, conn);
            //    InsertVMCommand.Parameters.AddWithValue("@VMName", VMData.Name);
            //    InsertVMCommand.Parameters.AddWithValue("@Location", VMData.Location);
            //    InsertVMCommand.Parameters.AddWithValue("@Description", VMData.Description);
            //    InsertVMCommand.Parameters.AddWithValue("@UtilFileLocation", BlobLocation);
            //    InsertVMCommand.Parameters.AddWithValue("@Status", "Created");
            //    //myCommand.Parameters.Add("@Id", SqlDbType.Int);
            //    var id = InsertVMCommand.ExecuteScalar();

            //    foreach (var u in UtilPattern)
            //    {

            //        Console.WriteLine(u.UtilizationType);
            //        foreach (var tr in u.TimeRanges)
            //        {
            //            string InsertUtilPatternQuery = "INSERT into VMAutomationRules (VMID, UtilizationType, StartTime, EndTime) ";
            //            InsertUtilPatternQuery += " VALUES(@VMID, @UtilizationType, @StartTime, @EndTime);";
            //            SqlCommand InsertUtilData = new SqlCommand(InsertUtilPatternQuery, conn);

            //            double Start = tr.StartTime ?? 0;
            //            double Stop = tr.StopTime ?? 0;

            //            TimeSpan StartTime = TimeSpan.FromHours(Start);
            //            TimeSpan StopTime = TimeSpan.FromHours(Stop);

            //            InsertUtilData.Parameters.AddWithValue("@VMID", id);
            //            InsertUtilData.Parameters.AddWithValue("@UtilizationType", u.UtilizationType);
            //            InsertUtilData.Parameters.AddWithValue("@StartTime", StartTime);
            //            InsertUtilData.Parameters.AddWithValue("@EndTime", StopTime);

            //            var res = InsertUtilData.ExecuteNonQuery();
            //            Console.WriteLine("{0}, {1}", tr.StartTime, tr.StopTime);
            //        }
            //    }

            //}

            return true;
        }
예제 #2
0
        public bool AddVM(VMDetails VMData)
        {

            var FP = VMData.UtilFileLocation;
            var FU = new FileUploader();
            var blob = FU.UploadFileToStorage(FP, true);
            var comm = new MLCommunicator();
            var result = comm.GetUtilDataFromFile(blob);
            var d = result.Result;
            var UtilData = new UtilizationCalculator();
            var UD = UtilData.getUtilPattern(d);

            AddVMInDB(VMData, blob, UD);

            // now we need to insert this data in SQL Azure for the given VM

            foreach (var u in UD)
            {
                Console.WriteLine(u.UtilizationType);
                foreach (var tr in u.TimeRanges)
                {
                    Console.WriteLine("{0}, {1}", tr.StartTime, tr.StopTime);
                }
            }
            //  populateTreeView();
            return true;

            // return Json(true, JsonRequestBehavior.AllowGet);

        }