|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Web.Script.Serialization; |
| 10 | + |
| 11 | +namespace Tractor.Com.QuantAsylum.Tractor.Database |
| 12 | +{ |
| 13 | + class AuditData |
| 14 | + { |
| 15 | + public string ProductId; |
| 16 | + public string SerialNumber; |
| 17 | + public string SessionName; |
| 18 | + public string TestFile; |
| 19 | + public string TestFileMd5; |
| 20 | + public string Name; |
| 21 | + public DateTime Time; |
| 22 | + public bool PassFail; |
| 23 | + public string ResultString; |
| 24 | + public float Result; |
| 25 | + public string TestLimits; |
| 26 | + public string Email; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The Audit Database is a database that lives in the cloud. It exists to provide |
| 31 | + /// easy and reliable out-of-the-box storage of test data. |
| 32 | + /// </summary> |
| 33 | + static class AuditDb |
| 34 | + { |
| 35 | + // Web service in cloud |
| 36 | + static string Url = "https://qatestcloud1.azurewebsites.net"; |
| 37 | + |
| 38 | + // https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ |
| 39 | + static HttpClient Client = new HttpClient(); |
| 40 | + |
| 41 | + static int _AuditQueueDepth; |
| 42 | + |
| 43 | + static public int AuditQueueDepth |
| 44 | + { |
| 45 | + get { return _AuditQueueDepth; } |
| 46 | + } |
| 47 | + |
| 48 | + static public void StartBackgroundWorker() |
| 49 | + { |
| 50 | + var task = new Task(() => BackgroundWorker(), TaskCreationOptions.LongRunning); |
| 51 | + task.Start(); |
| 52 | + } |
| 53 | + |
| 54 | + static public bool CheckService() |
| 55 | + { |
| 56 | + var response = Client.GetAsync(Url + "/api/CheckService").Result; |
| 57 | + Log.WriteLine(LogType.Database, string.Format("Checkservice(). Response: " + response.StatusCode.ToString())); |
| 58 | + |
| 59 | + if (response.IsSuccessStatusCode) |
| 60 | + return true; |
| 61 | + else |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + static public void SubmitAuditData(AuditData d) |
| 66 | + { |
| 67 | + string json = new JavaScriptSerializer().Serialize(d); |
| 68 | + File.WriteAllText(Constants.AuditPath + @"\" + Guid.NewGuid().ToString() + ".cache", json); |
| 69 | + Log.WriteLine(LogType.Database, string.Format("Submitted AudioData to Filesystem")); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Submits data to the cloud. Returns true if successful |
| 74 | + /// </summary> |
| 75 | + /// <param name="d"></param> |
| 76 | + /// <returns></returns> |
| 77 | + static bool SubmitAuditDataToCloud(AuditData d) |
| 78 | + { |
| 79 | + var json = new JavaScriptSerializer().Serialize(d); |
| 80 | + var body = new StringContent(json, Encoding.UTF8, "application/json"); |
| 81 | + var response = Client.PostAsync(Url + "/api/AddTest", body).Result; |
| 82 | + Log.WriteLine(LogType.Database, string.Format("PostAsync() to cloud finished. Response: " + response.StatusCode.ToString())); |
| 83 | + |
| 84 | + if (response.IsSuccessStatusCode) |
| 85 | + return true; |
| 86 | + else |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + static void BackgroundWorker() |
| 91 | + { |
| 92 | + const int fileLimit = 500; |
| 93 | + while (true) |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + string[] filePaths = Directory.GetFiles(Constants.AuditPath, "*.cache", SearchOption.TopDirectoryOnly); |
| 98 | + |
| 99 | + _AuditQueueDepth = filePaths.Length; |
| 100 | + if (filePaths.Length > fileLimit) |
| 101 | + { |
| 102 | + for (int i = 0; i <= fileLimit; i++) |
| 103 | + { |
| 104 | + SubmitFileToServer(filePaths[i]); |
| 105 | + Thread.Sleep(1100); |
| 106 | + } |
| 107 | + } |
| 108 | + else if (filePaths.Length >= 1) |
| 109 | + { |
| 110 | + SubmitFileToServer(filePaths[0]); |
| 111 | + Thread.Sleep(1100); |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + Thread.Sleep(5000); |
| 116 | + } |
| 117 | + } |
| 118 | + catch (Exception ex) |
| 119 | + { |
| 120 | + Log.WriteLine(LogType.Database, "AuditDb BackgroundWorker exception: " + ex.Message); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + static void SubmitFileToServer(string fileName) |
| 127 | + { |
| 128 | + try |
| 129 | + { |
| 130 | + string s = File.ReadAllText(fileName); |
| 131 | + AuditData d = (AuditData)new JavaScriptSerializer().Deserialize(s, typeof(AuditData)); |
| 132 | + if (SubmitAuditDataToCloud(d)) |
| 133 | + { |
| 134 | + File.Delete(fileName); |
| 135 | + Log.WriteLine(LogType.Database, "AuditDb BackgroundWorker successfully submitted a file to cloud"); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + Log.WriteLine(LogType.Database, "AuditDb BackgroundWorker failed to submit a file to cloud"); |
| 140 | + } |
| 141 | + } |
| 142 | + catch (Exception ex) |
| 143 | + { |
| 144 | + Log.WriteLine(LogType.Database, "AuditDb BackgroundWorker exception: " + ex.Message); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments