public async Task GetGeoReport(bool isAjax, string keyword, DateTime? start, DateTime? end) { this.SetFilters(isAjax, keyword, String.Empty, start, end); /* * Note: the OpenFDA API has a bug/limitation that it will not support a count on distribution_pattern.exact * which would return complete distribution_pattern strings and allow more accurate parsing. Just counting * distribution_pattern splits on individual words, so there is no reliable way to distinguish between e.g. * IN for Indiana and the preposition "in". It will also fail to count two-word state names such as "South * Dakota" because the words wil be split. Due to the time constraints on this prototype, this view model * will accept this limitation of the API and return demonstrative but inaccurate results. * */ // create state count list with all states and zero counts int cnt = 0; this.data = new List<StateCount> (); foreach (KeyValuePair<string, string> kvp in StateNames.stateNames) { cnt++; StateCount stateCount = new StateCount (cnt, kvp.Key, kvp.Value); this.data.Add (stateCount); } OpenFDAQuery query = new OpenFDAQuery (); query.source = OpenFDAQuery.FDAReportSource.food; query.type = OpenFDAQuery.FDAReportType.enforcement; query.queryCount = "distribution_pattern"; var searchQuery = new List<string>(); if (!string.IsNullOrWhiteSpace (keyword)) searchQuery.Add ( HttpUtility.UrlEncode(keyword)); if (start != null && end != null) searchQuery.Add (string.Format ("recall_initiation_date:[{0:yyyyMMdd}+TO+{1:yyyyMMdd}]", start.Value, end.Value)); if (searchQuery.Count > 0) query.querySearch = string.Join ("+AND+", searchQuery); bool success = await query.RunQueryAsync(); if (!success) this.ErrorMsg = "An error occurred executing the query. Please try again."; else { this.ErrorMsg = String.Empty; foreach (OpenFDAResult result in query.response.results) { var statecount = this.data.Where (s => s.StateAbbr.ToLower().Equals (result.term) || s.StateName.ToLower().Equals (result.term)).FirstOrDefault (); if (statecount != null) { int c = 0; Int32.TryParse(result.count, out c); statecount.Count += c; } } } }
public async Task GetReportDetails(string id, string eventid) { OpenFDAQuery query = new OpenFDAQuery (); query.source = OpenFDAQuery.FDAReportSource.food; query.type = OpenFDAQuery.FDAReportType.enforcement; query.querySearch = String.Format("event_id:{0}", HttpUtility.HtmlEncode(eventid)); query.queryLimit = 100; bool result = await query.RunQueryAsync(); if (result) { this.Result = query.response.results.Where (r => r.id.Equals(id)).FirstOrDefault (); } }
public async Task GetFinancialReport(bool isAjax, string keyword, string state) { this.data = new List<RecallCount>(); this.SetFilters(isAjax, keyword, state, null, null); this.ErrorMsg = String.Empty; int loopStart = 2008; int loopEnd = DateTime.Today.Year; for (int yr = loopStart; yr <= loopEnd; yr++) { OpenFDAQuery query = new OpenFDAQuery(); query.source = OpenFDAQuery.FDAReportSource.food; query.type = OpenFDAQuery.FDAReportType.enforcement; var searchQuery = new List<string>(); if (!String.IsNullOrWhiteSpace(keyword)) searchQuery.Add(HttpUtility.UrlEncode(keyword)); if (!String.IsNullOrWhiteSpace(state)) searchQuery.Add(String.Format("state:{0}", HttpUtility.UrlEncode(state))); searchQuery.Add(String.Format("recall_initiation_date:[{0}0101+TO+{1}0101]", yr, yr + 1)); if (searchQuery.Count > 0) query.querySearch = string.Join("+AND+", searchQuery); bool success = await query.RunQueryAsync(); int cnt = 0; if (success && (query.response != null)) cnt = query.response.meta.results.total; this.data.Add(new RecallCount(yr, cnt)); } }
public async Task GetFinancialReport(bool isAjax, string keyword, string state, DateTime? start, DateTime? end) { this.data = new List<CompanyCount> (); this.SetFilters(isAjax, keyword, state, start, end); OpenFDAQuery query = new OpenFDAQuery (); query.source = OpenFDAQuery.FDAReportSource.food; query.type = OpenFDAQuery.FDAReportType.enforcement; query.queryCount = "recalling_firm.exact"; var searchQuery = new List<string>(); if (!String.IsNullOrWhiteSpace (keyword)) searchQuery.Add ( HttpUtility.UrlEncode(keyword)); if (!String.IsNullOrWhiteSpace(state)) searchQuery.Add(String.Format("state:{0}", HttpUtility.UrlEncode(state))); if ((start != null) && (end != null)) searchQuery.Add (String.Format ("recall_initiation_date:[{0:yyyyMMdd}+TO+{1:yyyyMMdd}]", start.Value, end.Value)); else searchQuery.Add (String.Format ("recall_initiation_date:[20080101+TO+{0}0101]", DateTime.Today.Year+1)); if (searchQuery.Count > 0) query.querySearch = string.Join ("+AND+", searchQuery); bool success = await query.RunQueryAsync(); if (!success) this.ErrorMsg = "An error occurred executing the query. Please try again."; else { this.ErrorMsg = String.Empty; // build top company list int resultLimit = 0; foreach (OpenFDAResult result in query.response.results) { string name = result.term; /* * TODO: FDA API contains a known issue which does not permit string text search queries * containing single-quote whether or not it is URL-encoded properly. For demonstration * purposes, names with single-quote/apostrophes will be excluded from the top-ten list. * Actual fix for this issue may involve making broader queries and string matching to find * the desired company within the result set. * */ if (!name.Contains("'")) { resultLimit++; if (resultLimit > 10) break; this.data.Add(new CompanyCount(name, 0)); } } // build company counts per year int loopStart = 2008; int loopEnd = DateTime.Today.Year; if ((start != null) && (end != null)) { loopStart = ((DateTime)start).Year; loopEnd = ((DateTime)end).Year; } bool yearHasHadData = false; for (int yr = loopStart; yr <= loopEnd; yr++) { bool yearHasData = false; OpenFDAQuery subquery = new OpenFDAQuery(); subquery.source = OpenFDAQuery.FDAReportSource.food; subquery.type = OpenFDAQuery.FDAReportType.enforcement; subquery.queryCount = "recalling_firm.exact"; subquery.querySearch = String.Format("recall_initiation_date:[{0}0101+TO+{1}0101]", yr, yr + 1); subquery.queryLimit = 1000; success = await subquery.RunQueryAsync(); foreach (CompanyCount company in this.data) { int cnt = 0; if ((success) && (subquery.response != null) && (subquery.response.results.Count > 0)) { var result = subquery.response.results.Where(r => r.term.Equals(company.Name)).FirstOrDefault(); if (result != null) { cnt = result.countNumeric; yearHasData = true; yearHasHadData = true; } } company.YearlyCounts.Add(yr.ToString(), cnt); } if (!yearHasData && !yearHasHadData) { // remove year from all company dictionaries if year (and no previous year) contains data foreach (CompanyCount company in this.data) { company.YearlyCounts.Remove(yr.ToString()); } } } } }
public async void QueryTest() { OpenFDAQuery query = new OpenFDAQuery (); query.baseUrl = "https://api.fda.gov"; query.apiKey = "iECPoOgnA6LVBZ5X3PHyx5Slpj1smHE6u4FuzlHl"; query.source = OpenFDAQuery.FDAReportSource.food; query.type = OpenFDAQuery.FDAReportType.enforcement; bool result = await query.RunQueryAsync(); Assert.IsTrue (result); /* * Note: mocked request TestWebRequest does not support async calls // test against a mock WebRequest WebRequest.RegisterPrefix ("test", new TestWebRequestCreate ()); TestWebRequest request = TestWebRequestCreate.CreateTestRequest (json_response); OpenFDAQuery query = new OpenFDAQuery (); query.baseUrl = "test://testUrl"; query.apiKey = "iECPoOgnA6LVBZ5X3PHyx5Slpj1smHE6u4FuzlHl"; query.source = OpenFDAQuery.FDAReportSource.food; query.type = OpenFDAQuery.FDAReportType.enforcement; bool result = await query.RunQueryAsync(); Assert.IsTrue (result); Assert.AreEqual ("F-0924-2013", query.response.results [1].recall_number); Assert.AreEqual ("US", query.response.results [0].country); */ }