模型原型:伺服器的配置和運行狀態信息。 設計要求:Json格式數據解析後,判斷配置信息是否是新數據或者是否更新。如是新數據,則直接添加到資料庫;若是數據更新,則更新資料庫配置信息並更新運行狀態信息;都不是則僅將運行狀態添加到資料庫。最後從資料庫取數據並展示。 模型難點:每個伺服器會搭載多個網卡和最多 ...
模型原型:伺服器的配置和運行狀態信息。
設計要求:Json格式數據解析後,判斷配置信息是否是新數據或者是否更新。如是新數據,則直接添加到資料庫;若是數據更新,則更新資料庫配置信息並更新運行狀態信息;都不是則僅將運行狀態添加到資料庫。最後從資料庫取數據並展示。
模型難點:每個伺服器會搭載多個網卡和最多44個硬碟。
(1)View層如何同時展示所有硬碟和其他設備屬性的信息。
(2)單伺服器配多網卡多硬碟必定會設計多個表(即伺服器配置表、運行狀態表、網卡配置表、硬碟配置表),MVC框架下如何同時將其Model傳到View層。
(3)本文程式使用PageList進行分頁,通常為了程式運行速度,會在查詢時進行分頁,及var servers = db.Servers.OrderByDescending(e => e.ID).ToPagedList(pageNumber, 7),但本設計考慮到多Model傳到View層,此方法不適用。如何使程式適應上萬條數據的資料庫。
Model層:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.ComponentModel.DataAnnotations; 5 using System.Linq; 6 using System.Web; 7 8 namespace monitoring.Models 9 { 10 public class Servers 11 { 12 public int ID { get; set; } 13 [Required] 14 [DisplayName("主板序列號")] 15 public string AMDCDkey { get; set; } 16 [Required] 17 [DisplayName("伺服器名")] 18 public string HostName { get; set; } 19 [Required] 20 [DisplayName("CPU型號")] 21 public string CPUType { get; set; } 22 [Required] 23 [DisplayName("CPU數量")] 24 public int CPUNum { get; set; } 25 [Required] 26 [DisplayName("記憶體大小")] 27 public string RAMSize { get; set; } 28 [DisplayName("Raid卡型號")] 29 public string RaidType { get; set; } 30 [Required] 31 [DisplayName("硬碟個數")] 32 public int HDDNum { get; set; } 33 [Required] 34 [DisplayName("機箱型號")] 35 public string CaseType { get; set; } 36 } 37 }Servers
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.ComponentModel.DataAnnotations; 5 using System.Linq; 6 using System.Web; 7 8 namespace monitoring.Models 9 { 10 public class Servers 11 { 12 public int ID { get; set; } 13 [Required] 14 [DisplayName("主板序列號")] 15 public string AMDCDkey { get; set; } 16 [Required] 17 [DisplayName("伺服器名")] 18 public string HostName { get; set; } 19 [Required] 20 [DisplayName("CPU型號")] 21 public string CPUType { get; set; } 22 [Required] 23 [DisplayName("CPU數量")] 24 public int CPUNum { get; set; } 25 [Required] 26 [DisplayName("記憶體大小")] 27 public string RAMSize { get; set; } 28 [DisplayName("Raid卡型號")] 29 public string RaidType { get; set; } 30 [Required] 31 [DisplayName("硬碟個數")] 32 public int HDDNum { get; set; } 33 [Required] 34 [DisplayName("機箱型號")] 35 public string CaseType { get; set; } 36 } 37 }ServersUsing
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.ComponentModel.DataAnnotations; 5 using System.Linq; 6 using System.Web; 7 8 namespace monitoring.Models 9 { 10 public class NIC 11 { 12 public int ID { get; set; } 13 public string AMDCDkey { get; set; } 14 [Required] 15 [DisplayName("網卡型號")] 16 public string NICType { get; set; } 17 [Required] 18 [DisplayName("網卡數量")] 19 public int NICNum { get; set; } 20 } 21 }NIC
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.ComponentModel.DataAnnotations; 5 using System.Linq; 6 using System.Web; 7 8 namespace monitoring.Models 9 { 10 public class HDD 11 { 12 public int ID { get; set; } 13 public string AMDCDkey { get; set; } 14 public int RootHDD { get; set; } 15 [Required] 16 [DisplayName("硬碟標識")] 17 public string HDDID { get; set; } 18 [Required] 19 [DisplayName("硬碟型號")] 20 public string HDDType { get; set; } 21 [Required] 22 [DisplayName("硬碟容量")] 23 public string HDDCap { get; set; } 24 } 25 }HDD
為了將多Model傳到View層,單獨建立一個List,將多個model的信息添加到List中。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace monitoring.Models 7 { 8 public class ViewModel 9 { 10 public int ID { get; set; } 11 public Servers SView { get; set; } 12 public ServersUsing SUView { get; set; } 13 public List<NIC> NICView { get; set; } 14 public List<HDD> HDDView { get; set; } 15 } 16 }ViewModel
如需實現,還要有上下文:
1 using System.Data.Entity; 2 3 namespace monitoring.Models 4 { 5 public class ServersContext : DbContext 6 { 7 // 您可以向此文件中添加自定義代碼。更改不會被覆蓋。 8 // 9 // 如果您希望只要更改模型架構,Entity Framework 10 // 就會自動刪除並重新生成資料庫,則將以下 11 // 代碼添加到 Global.asax 文件中的 Application_Start 方法。 12 // 註意: 這將在每次更改模型時銷毀並重新創建資料庫。 13 // 14 // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<monitoring.Models.ServersContext>()); 15 16 public ServersContext() : base("name=ServersContext") 17 { 18 19 } 20 21 public DbSet<Servers> Servers { get; set; } 22 public DbSet<ServersUsing> ServersUsings { get; set; } 23 public DbSet<NIC> NICs { get; set; } 24 public DbSet<HDD> HDDs { get; set; } 25 } 26 }ServerContext
Controller層:
實現瞭解析Json並添加資料庫的方法,實現了查詢和分頁功能,完成了將多Model傳到View層的方法。
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.Entity; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Mvc; 8 using monitoring.Models; 9 using Newtonsoft.Json.Linq; 10 using Newtonsoft.Json; 11 using System.IO; 12 using monitoring.Controllers; 13 using System.Web.Script.Serialization; 14 using PagedList; 15 16 namespace monitoring.Controllers 17 { 18 public class ServersController : Controller 19 { 20 private ServersContext db = new ServersContext(); 21 public void ParseJson(StreamReader reader) 22 { 23 // 配置信息 24 JToken jobject = JToken.ReadFrom(new JsonTextReader(reader)); 25 string jsonText = jobject.ToString(); 26 JObject roots = (JObject)JsonConvert.DeserializeObject(jsonText); 27 JArray servers = (JArray)roots["Servers"]; 28 for (int i = 0; i < servers.Count; i++) 29 { 30 JObject root = (JObject)servers[i]; 31 Servers newdate = new Servers(); 32 newdate.AMDCDkey = (string)root["AMDCDkey"]; 33 newdate.HostName = (string)root["HostName"]; 34 newdate.CPUType = (string)root["CPUType"]; 35 newdate.CPUNum = (int)root["CPUNum"]; 36 newdate.RAMSize = (string)root["RAMSize"]; 37 newdate.RaidType = (string)root["RaidType"]; 38 newdate.HDDNum = (int)root["HDDNum"]; 39 newdate.CaseType = (string)root["CaseType"]; 40 bool NewDate = false; 41 try 42 { 43 var server = db.Servers.Single(x => x.AMDCDkey == newdate.AMDCDkey); 44 if (server != newdate) 45 { 46 server = newdate; 47 UpdateModel<Servers>(newdate); 48 db.SaveChanges(); 49 } 50 } 51 catch 52 { 53 NewDate = true; 54 db.Servers.Add(newdate); 55 db.SaveChanges(); 56 } 57 58 // 運行狀態信息 59 ServersUsing newdateusi = new ServersUsing(); 60 newdateusi.AMDCDkey = (string)root["AMDCDkey"]; 61 newdateusi.CPUUsRate = (string)root["CPUUsRate"]; 62 newdateusi.RAMUsRate = (string)root["RAMUsRate"]; 63 newdateusi.HDDUsRate = (string)root["HDDUsRate"]; 64 newdateusi.HDDIO = (string)root["HDDIO"]; 65 newdateusi.HostcomputerLoad = (string)root["HostcomputerLoad"]; 66 newdateusi.TheRootPartitionUsageRate = (string)root["TheRootPartitionUsageRate"]; 67 newdateusi.Time = DateTime.Now; 68 db.ServersUsings.Add(newdateusi); 69 db.SaveChanges(); 70 71 // 網卡信息 72 JArray NICs = (JArray)root["NIC"]; 73 if (NewDate == true) 74 { 75 for (int b = 0; b < NICs.Count; b++) 76 { 77 JObject nics = (JObject)NICs[b]; 78 NIC newdatenic = new NIC(); 79 newdatenic.AMDCDkey = (string)root["AMDCDkey"]; 80 newdatenic.NICType = (string)nics["NICType"]; 81 newdatenic.NICNum = (int)nics["NICNum"]; 82 db.NICs.Add(newdatenic); 83 db.SaveChanges(); 84 } 85 } 86 else 87 { 88 JavaScriptSerializer Serializer = new JavaScriptSerializer(); 89 List<NIC> objs = Serializer.Deserialize<List<NIC>>(NICs.ToString()); 90 List<NIC> nic = db.NICs.Where(x => x.AMDCDkey == newdate.AMDCDkey).ToList(); 91 bool b = false; 92 foreach (var a in nic) { if (!objs.Contains(a)) b = true; } 93 if (b && nic.Count != objs.Count) 94 { 95 var remove = db.NICs.Where(x => x.AMDCDkey == newdate.AMDCDkey); 96 foreach (var a in remove) 97 { 98 db.NICs.Remove(a); 99 } 100 for (int d = 0; d < NICs.Count; d++) 101 { 102 JObject nics = (JObject)NICs[d]; 103 NIC newdatenic = new NIC(); 104 newdatenic.AMDCDkey = (string)root["AMDCDkey"]; 105 newdatenic.NICType = (string)nics["NICType"]; 106 newdatenic.NICNum = (int)nics["NICNum"]; 107 db.NICs.Add(newdatenic); 108 } 109 db.SaveChanges(); 110 } 111 } 112 113 // 硬碟信息 114 JArray HDDs = (JArray)root["HDD"]; 115 116 if (NewDate == true) 117 { 118 for (int f = 0; f < HDDs.Count; f++) 119 { 120 JObject hdds = (JObject)HDDs[f]; 121 HDD newdatehdd = new HDD(); 122 newdatehdd.HDDID = (string)hdds["HDDID"]; 123 newdatehdd.RootHDD = (int)hdds["RootHDD"]; 124 newdatehdd.AMDCDkey = (string)root["AMDCDkey"]; 125 newdatehdd.HDDType = (string)hdds["HDDType"]; 126 newdatehdd.HDDCap = (string)hdds["HDDCap"]; 127 db.HDDs.Add(newdatehdd); 128 db.SaveChanges(); 129 } 130 } 131 else 132 { 133 JavaScriptSerializer Serializer = new JavaScriptSerializer(); 134 List<HDD> objs = Serializer.Deserialize<List<HDD>>(HDDs.ToString()); 135 List<HDD> hdd = db.HDDs.Where(x => x.AMDCDkey == newdate.AMDCDkey).ToList(); 136 bool b = false; 137 foreach (var a in hdd) { if (!objs.Contains(a)) b = true; } 138 if (b && hdd.Count != objs.Count) 139 { 140 var remove = db.HDDs.Where(x => x.AMDCDkey == newdate.AMDCDkey); 141 foreach (var a in remove) 142 { 143 db.HDDs.Remove(a); 144 } 145 for (int f = 0; f < HDDs.Count; f++) 146 { 147 JObject hdds = (JObject)HDDs[f]; 148 HDD newdatehdd = new HDD(); 149 newdatehdd.HDDID = (string)hdds["HDDID"]; 150 newdatehdd.RootHDD = (int)hdds["RootHDD"]; 151 newdatehdd.AMDCDkey = (string)root["AMDCDkey"]; 152 newdatehdd.HDDType = (string)hdds["HDDType"]; 153 newdatehdd.HDDCap = (string)hdds["HDDCap"]; 154 db.HDDs.Add(newdatehdd); 155 } 156 db.SaveChanges(); 157 } 158 } 159 } 160 } 161 public ActionResult Index(string searchString, int? page) 162 { 163 // 遍歷文件夾中的文件 164 //string path = "C:\\Users\\edong\\Desktop\\json"; 165 //DirectoryInfo dir = new DirectoryInfo(path); 166 //FileInfo[] fil = dir.GetFiles(); 167 //foreach (FileInfo f in fil) 168 //{ 169 // using (StreamReader reader = System.IO.File.OpenText("" + f.FullName + "")) 170 // { 171 // ParseJson(reader); 172 // } 173 // // 存入資料庫後刪除json文件 174 // // System.IO.File.Delete(@""+ f.FullName +""); 175 //} 176 int pageNumber = page ?? 1; 177 178 var servers = db.Servers.OrderByDescending(e => e.ID); 179 180 var listallview = new List<ViewModel>(); 181 foreach (var s in servers) 182 { 183 listallview.Add(new ViewModel() 184 { 185 SView = s, 186 SUView = db.ServersUsings.Where(o => o.AMDCDkey == s.AMDCDkey).First(), 187 NICView = db.NICs.Where(o => o.AMDCDkey == s.AMDCDkey).ToList(), 188 HDDView = db.HDDs.Where(o => o.AMDCDkey == s.AMDCDkey).ToList() 189 }); 190 } 191 var pagelist = listallview.OrderByDescending(e => e.ID).ToPagedList(pageNumber, 7); 192 // 查詢 193 if (!string.IsNullOrEmpty(searchString)) 194 { 195 pagelist = listallview.Where(s => s.SView.HostName.Contains(searchString)).ToPagedList(pageNumber, 7); 196 } 197 // 伺服器網卡/硬碟數量最大值 198 int NICNum = 0; 199 int HDDNum = 0; 200 foreach (var item in pagelist) 201 { 202 if (NICNum < item.NICView.Count) 203 NICNum = item.NICView.Count; 204 if (HDDNum < item.HDDView.Count) 205 HDDNum = item.HDDView.Count; 206 } 207 ViewBag.NICNum = NICNum; 208 ViewBag.HDDNum = HDDNum; 209 210 return View(pagelist); 211 } 212 213 protected override void Dispose(bool