public ActionResult GetMaterialsByCategory(string categories) { var selectedCategories = new JavaScriptSerializer().Deserialize<IEnumerable<string>>(categories); var materials = GetMarketingMaterials(); var idsResult = materials.Select(a => new { a.Id, Visible = selectedCategories.Contains(a.Category) }).ToArray(); return Json(idsResult); }
//Tests the value returned by the LoadVideoMgmt method private void TestReturnValue(ActionResult result, string expectedMrdvrResult, string expectedDeviceActionResponse) { //Assert Assert.IsTrue(result != null, "result is null"); //A partial view result is returned var resultPartial = result as PartialViewResult; Assert.IsTrue(resultPartial != null, "resultPartial is null"); //It was the right type of partial view result Assert.IsTrue(resultPartial.ViewName.Equals("VideoManagement_Partial"), "Expected resultPartial.ViewName to be VideoManagement_Partial. It was actually " + resultPartial.ViewName + "."); //It has the correct information //Model is not null var model = resultPartial.Model; Assert.IsNotNull(model); //Model response is correct var modelResponse = model.ToJSON(); string modelResponseString = new JavaScriptSerializer().Serialize(modelResponse); modelResponseString = modelResponseString.Replace(@"\", " "); modelResponseString = modelResponseString.Replace(@"""", @" "); Assert.IsTrue(modelResponseString.Contains(expectedMrdvrResult), "MRDVR Service was not found"); //Compare the expected DeviceActionResponse int startResponse = modelResponseString.IndexOf("DeviceActionResponse", System.StringComparison.Ordinal); int endResponse = modelResponseString.IndexOf("NewDeviceSerial", System.StringComparison.Ordinal); string actualDeviceResponse = modelResponseString.Substring(startResponse, (endResponse - startResponse) - 1); Assert.IsTrue(modelResponseString.Contains(expectedDeviceActionResponse), "DeviceActionResponse had an unexpected value. Expected: " + expectedDeviceActionResponse + ", Actual: " + actualDeviceResponse + "."); //Model is correct type Assert.IsTrue(model is VideoManagementModel, "Model is not a VideoManagementModel as expected"); }
public override void ExecuteCommand() { int lastNhour = 24; List<WorkInstanceDetail> jobDetail = new List<WorkInstanceDetail>(); List<WorkJobInstanceDetails> instanceDetails = getWorkjobInstance(); foreach (WorkJobInstanceDetails job in instanceDetails) { int invocationCount = 0; double totalRunTime = 0; int faultCount = 0; int faultRate = 0; int runtime = 0; Dictionary<string, List<string>> ErrorList = new Dictionary<string, List<string>>(); string AdminKey = WorkServiceAdminKey; if (job.url.Contains("api-work-1")) { AdminKey = WorkServiceFailoverAdminKey; } NetworkCredential nc = new NetworkCredential(WorkServiceUserName, AdminKey); //get all invocations in last 24 hours or last 10 invocations int no = (lastNhour * 60) / job.FrequencyInMinutes; if (no < 10) no = 10; WebRequest request = WebRequest.Create(string.Format("{0}/instances/{1}?limit={2}", job.url, job.JobInstanceName, no)); request.Credentials = nc; request.PreAuthenticate = true; request.Method = "GET"; WebResponse respose = request.GetResponse(); using (var reader = new StreamReader(respose.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); js.MaxJsonLength = Int32.MaxValue; var objects = js.Deserialize<List<WorkJobInvocation>>(reader.ReadToEnd()); WorkJobInvocation lastJob; bool alert = false; string lastCompleted = string.Empty; if (objects.Any((item => item.status.Equals("Executed") && item.result.Equals("Completed")))) { lastJob = objects.Where(item => item.status.Equals("Executed") && item.result.Equals("Completed")).ToList().FirstOrDefault(); } else { lastJob = objects.FirstOrDefault(); } if (lastJob != null) { lastCompleted = string.Format("{0} mins ago", Convert.ToInt32(DateTime.Now.Subtract(lastJob.completedAt).TotalMinutes)); } else { lastCompleted = "N/A"; } foreach (WorkJobInvocation each in objects) { if (each.result.Equals("Incomplete", StringComparison.OrdinalIgnoreCase)) continue; if (each.completedAt >= DateTime.Now.AddHours(-1)) alert = true; // check there is any failure happened in last one hour invocationCount++; totalRunTime += each.completedAt.Subtract(each.queuedAt).TotalSeconds; if (each.result.Equals("Faulted")) { faultCount++; string message = getResultMessage(each.resultMessage); if (ErrorList.ContainsKey(message)) { if (ErrorList[message].Count < 5) ErrorList[message].Add(each.logUrl); } else { List<string> LogUrl = new List<string>(); LogUrl.Add(each.logUrl); ErrorList.Add(message, LogUrl); } } } if (invocationCount != 0) { faultRate = (faultCount * 100 / invocationCount); runtime = ((int)(totalRunTime / invocationCount)); } jobDetail.Add(new WorkInstanceDetail(job.JobInstanceName, job.FrequencyInMinutes+ "mins",lastCompleted , runtime.ToString() + "s", invocationCount.ToString(), faultCount.ToString(), faultRate, ErrorList)); AlertThresholds thresholdValues = new JavaScriptSerializer().Deserialize<AlertThresholds>(ReportHelpers.Load(StorageAccount, "Configuration.AlertThresholds.json", ContainerName)); string[] Igonored = new JavaScriptSerializer().Deserialize<string[]>(ReportHelpers.Load(StorageAccount, "Configuration.WorkerJobToBeIgnored.json", ContainerName)); if (Igonored.Contains(job.JobInstanceName, StringComparer.OrdinalIgnoreCase)) continue; if (faultRate > thresholdValues.WorkJobErrorThreshold && alert) { new SendAlertMailTask { AlertSubject = string.Format("Error: Alert for Nuget Work Service : {0} failure", job.JobInstanceName), Details = string.Format("Rate of failure exceeded Error threshold for {0}. Threshold count : {1}%, failure in last 24 hour : {2}", job.JobInstanceName,thresholdValues.WorkJobErrorThreshold , faultCount), AlertName = string.Format("Error: Nuget Work Service {0}", job.JobInstanceName), Component = "Nuget Work Service", Level = "Error" }.ExecuteCommand(); } else if (faultRate > thresholdValues.WorkJobWarningThreshold && alert) { new SendAlertMailTask { AlertSubject = string.Format("Warning: Alert for Nuget Work Service: {0} failure", job.JobInstanceName), Details = string.Format("Rate of failure exceeded Warning threshold for {0}. Threshold count : {1}%, failure in last 24 hour : {2}", job.JobInstanceName, thresholdValues.WorkJobWarningThreshold, faultCount), AlertName = string.Format("Warning: Nuget Work Service {0}", job.JobInstanceName), Component = "Nuget Work Service", Level = "Warning" }.ExecuteCommand(); } } //check to make sure that the jobs that are not queued as part of scheduler are being invoked properly if (invocationCount < ((lastNhour * 60 / job.FrequencyInMinutes) / 2)) { new SendAlertMailTask { AlertSubject = string.Format("Error: Alert for Nuget Work Service : {0} failure", job.JobInstanceName), Details = string.Format("In last 24 hours, invocation of {0} is only {1}, it's less than half of scheduled jobs", job.JobInstanceName, invocationCount), AlertName = string.Format("Error: Nuget Work Service {0}", job.JobInstanceName), Component = "Nuget Work Service", Level = "Error" }.ExecuteCommand(); } } List<WorkServiceAdmin> allkey = new List<WorkServiceAdmin>(); allkey.Add(new WorkServiceAdmin(WorkServiceUserName, WorkServiceAdminKey)); allkey.Add(new WorkServiceAdmin(WorkServiceUserName,WorkServiceFailoverAdminKey)); var json = new JavaScriptSerializer().Serialize(jobDetail); var key = new JavaScriptSerializer().Serialize(allkey); ReportHelpers.CreateBlob(StorageAccount, "WorkJobDetail.json", ContainerName, "application/json", ReportHelpers.ToStream(json)); ReportHelpers.CreateBlob(StorageAccount, "WorkServiceAdminKey.json", ContainerName, "application/json", ReportHelpers.ToStream(key)); }