internal bool ForwardTest(string uuid, out string testID) { testID = ""; var parser = new TestResultParser(); UUID = uuid; if (!parser.ValidXmlDoc(XMLResult, TargetNamespace, XMLSchema)) { Logger.LogError("This test result from {0} is invalid [{1}]", uuid, parser.ValidationError); return false; } var timeStamp = DateTime.Now.ToString(TimestampFormat); var machineGUID = GetValueFromXML(string.Concat("//", TRRConstant.ElementPrefix, ":machineID")); try { testID = GetTestID(machineGUID, timeStamp); } catch (Exception e) { Logger.LogError("[{0}] Cannot get TestID from OpsConsole - Exception: {1}", uuid, e.Message); return false; } if (string.IsNullOrEmpty(machineGUID)) machineGUID = ZeroGuid; var testResultArray = new[]{new TestResult { Product = ProductName, TimeStamp = timeStamp, UUID = uuid, InstallGuid = ZeroGuid, MachineGUID = machineGUID, Stats = GetTestCaseListFromXML(string.Concat("//", TRRConstant.ElementPrefix, ":testCases//", TRRConstant.ElementPrefix, ":testCase")) }}; var product = TestResultForwarder.ProductName; _productVersion = GetValueFromXML(string.Concat("//", TRRConstant.ElementPrefix, ":product")); var productName = GetValueFromXML(string.Concat("//", TRRConstant.ElementPrefix, ":product/@name")); var productVersion = GetValueFromXML(string.Concat("//", TRRConstant.ElementPrefix, ":product/@version")); if (IsEWM(productName, productVersion)) { Logger.LogInfo("ForwardTest: EWM Mapping {0}: {1}-{2}", uuid, productName, productVersion); product = "ewm"; MapStatCodeEWM(testResultArray[0]); } var runningMode = GetValueFromXML(string.Concat("//", TRRConstant.ElementPrefix, ":runningMode")); var xnav = XMLResult.CreateNavigator(); var total = Convert.ToInt32(GetXPathScalar(xnav, "count(/rst:rstResult/rst:testCases/rst:testCase)", NamespaceMgr)); var totalPass = Convert.ToInt32(GetXPathScalar(xnav, "count(/rst:rstResult/rst:testCases/rst:testCase[not(@shouldBeRerun!='false') and not(@ssiID!='') and rst:valid='pass'])", NamespaceMgr)); var totalInfo = Convert.ToInt32(GetXPathScalar(xnav, "count(/rst:rstResult/rst:testCases/rst:testCase[not(@shouldBeRerun!='false') and not(@ssiID!='') and rst:valid='info'])", NamespaceMgr)); var totalWarning = Convert.ToInt32(GetXPathScalar(xnav, "count(/rst:rstResult/rst:testCases/rst:testCase[not(@shouldBeRerun!='false') and not(@ssiID!='') and rst:valid='warning'])", NamespaceMgr)); var totalAlert = Convert.ToInt32(GetXPathScalar(xnav, "count(/rst:rstResult/rst:testCases/rst:testCase[not(@shouldBeRerun!='false') and not(@ssiID!='') and rst:valid='alert'])", NamespaceMgr)); var totalFail = Convert.ToInt32(GetXPathScalar(xnav, "count(/rst:rstResult/rst:testCases/rst:testCase[not(@shouldBeRerun!='false') and not(@ssiID!='') and rst:valid='fail'])", NamespaceMgr)); var other = total - totalPass - totalInfo - totalWarning - totalAlert - totalFail; testResultArray[0].Stats.Add(new TestCase { StatName = "runmode", StatVal = runningMode }); testResultArray[0].Stats.Add(new TestCase { StatName = "total", StatVal = total.ToString() }); testResultArray[0].Stats.Add(new TestCase { StatName = "pass", StatVal = totalPass.ToString() }); testResultArray[0].Stats.Add(new TestCase { StatName = "info", StatVal = totalInfo.ToString() }); AddValidationStat(xnav, testResultArray[0], "warn", totalWarning); AddValidationStat(xnav, testResultArray[0], "alert", totalAlert); AddValidationStat(xnav, testResultArray[0], "fail", totalFail); testResultArray[0].Stats.Add(new TestCase { StatName = "other", StatVal = other.ToString() }); var compName = Environment.GetEnvironmentVariable("COMPUTERNAME") ?? ""; var envName = Environment.GetEnvironmentVariable("AS_ENV") ?? ""; testResultArray[0].Stats.Add(new TestCase { StatName = "servname", StatVal = compName }); testResultArray[0].Stats.Add(new TestCase { StatName = "envname", StatVal = envName }); testResultArray[0].Stats.Add(new TestCase { StatName = "testID", StatVal = testID }); testResultArray[0].Stats.Add(new TestCase { StatName = "product", StatVal = product }); var json = JsonConvert.SerializeObject(testResultArray); // TODO: comment out after debuging /*var fileName = string.Format("{0}-{1:yyyyMMdd}.json", uuid, DateTime.Now); var offlineResultsFolderPath = Path.Combine(Environment.ExpandEnvironmentVariables("%AS_DATA%"), "RSTStat\\OfflineResults\\"); if (!Directory.Exists(offlineResultsFolderPath)) { Directory.CreateDirectory(offlineResultsFolderPath); } File.WriteAllText(Path.Combine(offlineResultsFolderPath, fileName), json); */ var compressedBytes = new MemoryStream(); var rawBytes = new MemoryStream(Encoding.UTF8.GetBytes(json)); // auto disposed by GzipStream using (var gzip = new GZipStream(compressedBytes, CompressionMode.Compress, leaveOpen: true)) { rawBytes.CopyTo(gzip); gzip.Flush(); } compressedBytes.Position = 0L; Task.Run(() => { try { using ( var client = new PlatformUsageDataCollectorClient(RouterBindings.Local, RouterAddresses.Local.RequestReply)) { client.WriteOpsConsoleDesktopMessageGzip(compressedBytes); Logger.LogInfo("ForwardTest: [{0}] test result forwarded", UUID); using ( var appHitsClient = new AppHitsClient(RouterBindings.Local, RouterAddresses.Local.RequestReply)) { AppHitsService.Response resp = null; var appHitItems = resp = appHitsClient.Process(new Request { TypeHeader = TypeHeader.LOG_HITS, LogHitsReq = new LogHitsReq { Product = "ekn", SubProduct = "app", AppHitItems = new AppHitItems { new AppHitItem { Uuid = uuid, Region = "", AppName = "SystemTest", IsEmpApp = false, FeatureName = "STR", // SubmitTestResult BusinessDate = "", GmtTime = timeStamp, Count = 1 } } } }); if (resp != null && resp.AppHitResult.ResultCode == 1) // success { Logger.LogInfo("ForwardTest: [{0}] submit test result feature hit", UUID); } else { Logger.LogWarn("ForwardTest: [{0}] cannot send apphit", UUID); } } } } catch (Exception ex) { Logger.LogError("ForwardTest: [{0}], {1}", UUID, ex.Message); } }); return true; }
public static bool LogAppHits(List<LogStatItem> stats) { using (var appHitsClient = new AppHitsClient(RouterBindings.Local,RouterAddresses.Local.RequestReply)) { var req = new Request { TypeHeader = TypeHeader.LOG_HITS, LogHitsReq = new LogHitsReq { Product = "ekn", SubProduct = "app", AppHitItems = new AppHitItems() } }; foreach (var uuid in stats.Where(x => x != null).GroupBy(u => u.uuid)) { req.LogHitsReq.AppHitItems.Add(new AppHitItem { Uuid = uuid.Key, Region = "", AppName = "SystemTest", IsEmpApp = false, FeatureName = "MTR", // Migrate Test Result BusinessDate = "", GmtTime = DateTime.UtcNow.ToString(), Count = 1 }); req.LogHitsReq.AppHitItems.Add(new AppHitItem { Uuid = uuid.Key, Region = "", AppName = "SystemTest", IsEmpApp = false, FeatureName = "MTRStat", BusinessDate = "", GmtTime = DateTime.UtcNow.ToString(), Count = uuid.Count() }); } var resp = appHitsClient.Process(req); if (resp != null && resp.AppHitResult.ResultCode == 1) // success { ALogger.LogInfo("LogAppHits: [{0}] migrate test result feature hit", req.LogHitsReq.AppHitItems.Count); return true; } else { ALogger.LogWarn("LogAppHits: [{0}] cannot send apphit", req.LogHitsReq.AppHitItems.Count); return false; } } }