Esempio n. 1
0
        private async Task SendHealthReportAsync()
        {
            try
            {
                ConditionalValue <BandActorState> BandActorStateResult = await this.StateManager.TryGetStateAsync <BandActorState>("BandActorState");

                if (BandActorStateResult.HasValue)
                {
                    ActorId doctorId = new ActorId(BandActorStateResult.Value.DoctorId);

                    IDoctorActor doctor = ActorProxy.Create <IDoctorActor>(doctorId, this.doctorActorServiceUri);

                    await
                    doctor.ReportHealthAsync(
                        this.Id.GetGuidId(),
                        BandActorStateResult.Value.PatientName,
                        BandActorStateResult.Value.HealthIndex,
                        new HealthIndex(this.random.Next(0, 101)));

                    ActorEventSource.Current.Message("Health info sent from band {0} to doctor {1}", this.Id, BandActorStateResult.Value.DoctorId);
                }
            }
            catch (Exception e)
            {
                ActorEventSource.Current.Message(
                    "Band Actor failed to send health data to doctor. Exception: {0}",
                    (e is AggregateException) ? e.InnerException.ToString() : e.ToString());
            }

            return;
        }
Esempio n. 2
0
        private async Task CreateBandActorTask(BandActorGenerator bag, CancellationToken cancellationToken)
        {
            CryptoRandom random = new CryptoRandom();

            while (!cancellationToken.IsCancellationRequested && this.MaxBandsToCreatePerServiceInstance > 0)
            {
                bool created = false;
                while (!created && !cancellationToken.IsCancellationRequested)
                {
                    ActorId bandActorId;
                    ActorId doctorActorId;
                    int     randomCountyId = -1;
                    string  doctorName     = null;

                    randomCountyId = random.Next(0, bag.doctorsPerCounty.Keys.Count);
                    doctorName     = bag.GetRandomName(random);

                    CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId);
                    BandInfo     bandActorInfo      = bag.GetRandomHealthStatus(randomCountyRecord, random);

                    try
                    {
                        bandActorId   = new ActorId(Guid.NewGuid());
                        doctorActorId = new ActorId(bandActorInfo.DoctorId);

                        IDoctorActor docActor = ActorProxy.Create <IDoctorActor>(doctorActorId, this.DoctorServiceUri);
                        await docActor.NewAsync(doctorName, randomCountyRecord);

                        IBandActor bandActor = ActorProxy.Create <IBandActor>(bandActorId, this.ActorServiceUri);
                        await bandActor.NewAsync(bandActorInfo);

                        ServiceEventSource.Current.Message("Actor created {0} verifying...", bandActorId);

                        await VerifyActors(
                            new HealthIndexCalculator(this.Context),
                            bandActorId,
                            doctorName,
                            randomCountyRecord,
                            bandActorInfo,
                            docActor,
                            bandActor,
                            cancellationToken);
                    }

                    catch (Exception e)
                    {
                        ServiceEventSource.Current.ServiceMessage(this, "Failed to iniitalize band or doctor. {0}", e.ToString());
                    }

                    created = true;
                }

                this.MaxBandsToCreatePerServiceInstance--;

                ServiceEventSource.Current.ServiceMessage(this, "Created Actors, {0} remaining", this.MaxBandsToCreatePerServiceInstance);

                await Task.Delay(100, cancellationToken);
            }
        }
        private async Task CreateKnownActors(BandActorGenerator bag, ConfigurationSettings settings, CancellationToken cancellationToken, bool verify)
        {
            CryptoRandom          random = new CryptoRandom();
            FabricClient          fc     = new FabricClient();
            HealthIndexCalculator hic    = new HealthIndexCalculator(this.Context);

            KeyedCollection <string, ConfigurationProperty> serviceParameters = settings.Sections["HealthMetrics.BandCreationService.Settings"].Parameters;

            while (!cancellationToken.IsCancellationRequested)
            {
                ActorId bandActorId;
                ActorId doctorActorId;
                int     randomCountyId = -1;
                string  doctorName     = null;

                randomCountyId = int.Parse(serviceParameters["KnownCountyIdIndex"].Value);
                //(2968 is King, WA) || (2231 is Multnomah, OR) || (1870 is St. Lawrence, NY)
                doctorName = serviceParameters["KnownDoctorName"].Value;

                CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId);
                BandInfo     bandActorInfo      = bag.GetRandomHealthStatus(randomCountyRecord, random);

                try
                {
                    bandActorInfo.PersonName = serviceParameters["KnownPatientName"].Value;
                    bandActorId = new ActorId(new Guid(serviceParameters["KnownPatientId"].Value));

                    bandActorInfo.DoctorId = new Guid(serviceParameters["KnownDoctorId"].Value);
                    doctorActorId          = new ActorId(bandActorInfo.DoctorId);

                    bag.doctorsPerCounty[bag.doctorsPerCounty.Keys.ElementAt(randomCountyId)].Add(bandActorInfo.DoctorId);

                    IDoctorActor docActor = ActorProxy.Create <IDoctorActor>(doctorActorId, this.DoctorServiceUri);
                    await docActor.NewAsync(doctorName, randomCountyRecord);

                    IBandActor bandActor = ActorProxy.Create <IBandActor>(bandActorId, this.ActorServiceUri);
                    await bandActor.NewAsync(bandActorInfo);

                    if (verify)
                    {
                        await VerifyActors(hic, bandActorId, doctorName, randomCountyRecord, bandActorInfo, docActor, bandActor);

                        break;
                    }

                    await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
                }
                catch (Exception e)
                {
                    ServiceEventSource.Current.ServiceMessage(this, "Exception when creating actor {0}", e.ToString());
                }
            }
        }
Esempio n. 4
0
        public async Task <IHttpActionResult> GetDoctor(Guid doctorId)
        {
            try
            {
                ActorId           doctorActor = new ActorId(doctorId);
                ServiceUriBuilder serviceUri  = new ServiceUriBuilder(this.GetSetting("DoctorActorServiceInstanceName"));
                IDoctorActor      actor       = ActorProxy.Create <IDoctorActor>(doctorActor, serviceUri.ToUri());

                return(this.Ok(await actor.GetPatientsAsync()));
            }
            catch (AggregateException ae)
            {
                return(this.InternalServerError(ae.InnerException));
            }
        }
Esempio n. 5
0
        private async Task GenerateAndSendHealthReportAsync()
        {
            try
            {
                ConditionalValue <HealthIndex> HeatlthInfoResult = await this.StateManager.TryGetStateAsync <HealthIndex>("HealthIndex");

                ConditionalValue <string> PatientInfoResult = await this.StateManager.TryGetStateAsync <string>("PatientName");

                ConditionalValue <Guid> DoctorInfoResult = await this.StateManager.TryGetStateAsync <Guid>("DoctorId");


                if (HeatlthInfoResult.HasValue && PatientInfoResult.HasValue && DoctorInfoResult.HasValue)
                {
                    ActorId         doctorId = new ActorId(DoctorInfoResult.Value);
                    HeartRateRecord record   = new HeartRateRecord((float)this.random.NextDouble());

                    await this.SaveHealthDataAsync(record);

                    IDoctorActor doctor = ActorProxy.Create <IDoctorActor>(doctorId, this.doctorActorServiceUri);

                    await
                    doctor.ReportHealthAsync(
                        this.Id.GetGuidId(),
                        PatientInfoResult.Value,
                        HeatlthInfoResult.Value);

                    ActorEventSource.Current.Message("Health info sent from band {0} to doctor {1}", this.Id, DoctorInfoResult.Value);
                }
            }
            catch (Exception e)
            {
                ActorEventSource.Current.Message(
                    "Band Actor failed to send health data to doctor. Exception: {0}",
                    (e is AggregateException) ? e.InnerException.ToString() : e.ToString());
            }

            return;
        }
        private static async Task VerifyActors(
            HealthIndexCalculator hic, ActorId bandActorId, string doctorName, CountyRecord randomCountyRecord, BandInfo bandActorInfo, IDoctorActor docActor,
            IBandActor bandActor)
        {
            while (true)
            {
                BandDataViewModel view = await bandActor.GetBandDataAsync();

                if (view.PersonName == bandActorInfo.PersonName)
                {
                    if (view.CountyInfo == bandActorInfo.CountyInfo)
                    {
                        if (view.DoctorId == bandActorInfo.DoctorId)
                        {
                            if (view.PersonId == bandActorId.GetGuidId())
                            {
                                if (hic.ComputeIndex(bandActorInfo.HealthIndex) == view.HealthIndex)
                                {
                                    break;
                                }
                                else
                                {
                                    await bandActor.NewAsync(bandActorInfo);

                                    await Task.Delay(100);
                                }
                            }
                        }
                    }
                }
            }

            while (true)
            {
                Tuple <CountyRecord, string> info = await docActor.GetInfoAndNameAsync();

                if (info.Item2 == String.Format("Dr. {0}", doctorName) &&
                    info.Item1 == randomCountyRecord)
                {
                    break;
                }
                else
                {
                    await docActor.NewAsync(doctorName, randomCountyRecord);

                    await Task.Delay(100);
                }
            }
        }
Esempio n. 7
0
        private static async Task VerifyActors(
            HealthIndexCalculator hic, ActorId bandActorId, string doctorName, CountyRecord randomCountyRecord, BandInfo bandActorInfo, IDoctorActor docActor,
            IBandActor bandActor, CancellationToken ct)
        {
            ServiceEventSource.Current.Message("Verifying Actor {0}", bandActorId);

            bool bandVerified     = false;
            bool doctorVerified   = false;
            int  bandErrorCount   = 0;
            int  doctorErrorCount = 0;

            while (!ct.IsCancellationRequested && !bandVerified && !doctorVerified)
            {
                await Task.Delay(100);

                if (!bandVerified)
                {
                    try
                    {
                        BandDataViewModel view = await bandActor.GetBandDataAsync();

                        if (view.PersonName == bandActorInfo.PersonName)
                        {
                            if (view.CountyInfo == bandActorInfo.CountyInfo)
                            {
                                if (view.DoctorId == bandActorInfo.DoctorId)
                                {
                                    if (view.PersonId == bandActorId.GetGuidId())
                                    {
                                        if (view.HealthIndexValue == bandActorInfo.HealthIndex)
                                        {
                                            bandVerified = true;
                                            ServiceEventSource.Current.Message("Band actor verified.");
                                        }
                                        else
                                        {
                                            await bandActor.NewAsync(bandActorInfo);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        bandErrorCount++;
                        ServiceEventSource.Current.Message("Exception Count {0} verifying band actor, retrying. {1}", bandErrorCount, e);
                    }
                }
                else
                {
                    ServiceEventSource.Current.Message("band already verified, skipping");
                }


                if (!doctorVerified)
                {
                    try
                    {
                        Tuple <CountyRecord, string> info = await docActor.GetInfoAndNameAsync();

                        if (info.Item2 == String.Format("Dr. {0}", doctorName) &&
                            info.Item1 == randomCountyRecord)
                        {
                            doctorVerified = true;
                            ServiceEventSource.Current.Message("Doctor actor verified.");
                        }
                        else
                        {
                            await docActor.NewAsync(doctorName, randomCountyRecord);
                        }
                    }
                    catch (Exception e)
                    {
                        doctorErrorCount++;
                        ServiceEventSource.Current.Message("Exception Count {0} verifying doctor actor, retrying. {1}", doctorErrorCount, e);
                    }
                }
                else
                {
                    ServiceEventSource.Current.Message("doctor already verified, skipping");
                }
            }
        }