public static async void Run([EventHubTrigger("%ResultsEH-Name%", Connection = "ResultsEH-ConnectionString")] string[] eventHubMessages, [CosmosDB( databaseName: "resultDataDB", collectionName: "resultCollection", ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <OffenderResult> finalResult, ILogger log) { // Final result object List <OffenderResult> finalResultObject = new List <OffenderResult>(); // Each message needs to compare thresholds foreach (var message in eventHubMessages) { log.LogInformation($"C# Event Hub trigger function processed a message: {message}"); ResultStreamObject resultObject = JsonConvert.DeserializeObject <ResultStreamObject>(message); // For every Offender Result item check the threshold foreach (var offenderResult in resultObject.ResultList) { if ((offenderResult.PI_freq1 > threshold) || (offenderResult.PI_freq2 > threshold) || (offenderResult.PI_freq3 > threshold) || (offenderResult.SIB_freq1 > threshold) || (offenderResult.SIB_freq2 > threshold) || (offenderResult.SIB_freq3 > threshold) || (offenderResult.SIB_Local > threshold)) { finalResultObject.Add(offenderResult); await finalResult.AddAsync(offenderResult); } } } var outputString = JsonConvert.SerializeObject(finalResultObject); try { await finalResult.FlushAsync(); } catch (Exception ex) { log.LogError(ex.Message); //await Task.Delay(5000); // do something else with it so its not lost } }
public static void Run([EventHubTrigger("%WorkItemEH-Name%", Connection = "WorkItemEH-ConnectionString")] EventData[] myEventHubMessages, [EventHub("%ResultsEH-Name%", Connection = "ResultsEH-ConnectionString")] IAsyncCollector <EventData> outputEventHubMessages, ILogger log) { MD5 md5 = MD5.Create(); // process messages foreach (EventData message in myEventHubMessages) { string messagePayload = Encoding.UTF8.GetString(message.Body.Array); // process each message WorkItemObject myEvent = JsonConvert.DeserializeObject <WorkItemObject>(messagePayload); try { ResultStreamObject result = new ResultStreamObject() { SchemaVersion = myEvent.SchemaVersion, JobDetails = myEvent.JobDetails, // modify the results here ResultList = new List <OffenderResult>() }; // simulated caluclation on payload Random rNum = new Random(); foreach (var offender in myEvent.OffenderList) { OffenderResult newResult = new OffenderResult() { Forensic_ID = myEvent.SuspectDetails.Specimen_ID, Offender_ID = offender.Specimen_ID, PI_freq1 = rNum.Next(), PI_freq2 = rNum.Next(), PI_freq3 = rNum.Next(), SIB_freq1 = rNum.Next(), SIB_freq2 = rNum.Next(), SIB_freq3 = rNum.Next(), SIB_Local = rNum.Next() }; result.ResultList.Add(newResult); } // craft outbound event EventData outputEvent = new EventData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result))); outputEventHubMessages.AddAsync(outputEvent).Wait(); } catch (Exception ex) { log.LogError(ex.Message); // do something else with it so its not lost } } // flush the event hub output, we're doing this and not letting the system so we can do error handling // note that output batch must not exceed maximum Event Hub batch size try { outputEventHubMessages.FlushAsync().Wait(); } catch (Exception ex) { log.LogError(ex.Message); //await Task.Delay(5000); // do something else with it so its not lost } }