Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #671 from keiji/risk_calculation
Browse files Browse the repository at this point in the history
Risk calculation
  • Loading branch information
kazuhiro4949 committed Jan 12, 2022
2 parents 7d696a5 + 1b8d799 commit 6af2214
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 131 deletions.
18 changes: 11 additions & 7 deletions Covid19Radar/Covid19Radar/Repository/ExposureDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Covid19Radar.Repository
public interface IExposureDataRepository
{
// ExposureWindow mode
Task SetExposureDataAsync(
Task<(List<DailySummary>, List<ExposureWindow>)> SetExposureDataAsync(
List<DailySummary> dailySummaryList,
List<ExposureWindow> exposueWindowList
);
Expand Down Expand Up @@ -72,7 +72,7 @@ ILoggerService loggerService
private readonly DailySummary.Comparer _dailySummaryComparer = new DailySummary.Comparer();
private readonly ExposureWindow.Comparer _exposureWindowComparer = new ExposureWindow.Comparer();

public async Task SetExposureDataAsync(
public async Task<(List<DailySummary>, List<ExposureWindow>)> SetExposureDataAsync(
List<DailySummary> dailySummaryList,
List<ExposureWindow> exposueWindowList
)
Expand All @@ -82,15 +82,19 @@ List<ExposureWindow> exposueWindowList
List<DailySummary> existDailySummaryList = await GetDailySummariesAsync();
List<ExposureWindow> existExposureWindowList = await GetExposureWindowsAsync();

List<DailySummary> newDailySummaryList = existDailySummaryList.Union(dailySummaryList).ToList();
newDailySummaryList.Sort(_dailySummaryComparer);
List<DailySummary> unionDailySummaryList = existDailySummaryList.Union(dailySummaryList).ToList();
List<ExposureWindow> unionExposureWindowList = existExposureWindowList.Union(exposueWindowList).ToList();
unionDailySummaryList.Sort(_dailySummaryComparer);
unionExposureWindowList.Sort(_exposureWindowComparer);

List<ExposureWindow> newExposureWindowList = existExposureWindowList.Union(exposueWindowList).ToList();
newExposureWindowList.Sort(_exposureWindowComparer);
await SaveExposureDataAsync(unionDailySummaryList, unionExposureWindowList);

await SaveExposureDataAsync(newDailySummaryList, newExposureWindowList);
List<DailySummary> newDailySummaryList = unionDailySummaryList.Except(existDailySummaryList).ToList();
List<ExposureWindow> newExposureWindowList = unionExposureWindowList.Except(existExposureWindowList).ToList();

_loggerService.EndMethod();

return (newDailySummaryList, newExposureWindowList);
}

private Task SaveExposureDataAsync(IList<DailySummary> dailySummaryList, IList<ExposureWindow> exposureWindowList)
Expand Down
13 changes: 8 additions & 5 deletions Covid19Radar/Covid19Radar/Services/ExposureDetectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ public async Task ExposureDetectedAsync(ExposureConfiguration exposureConfigurat

var enVersionStr = enVersion.ToString();

await _exposureDataRepository.SetExposureDataAsync(
var (newDailySummaries, newExposureWindows) = await _exposureDataRepository.SetExposureDataAsync(
dailySummaries.ToList(),
exposureWindows.ToList()
);

bool isHighRiskExposureDetected = dailySummaries
.Select(dailySummary => _exposureRiskCalculationService.CalcRiskLevel(dailySummary))
bool isHighRiskExposureDetected = newDailySummaries
.Select(ds => _exposureRiskCalculationService.CalcRiskLevel(
ds,
newExposureWindows.Where(ew => ew.DateMillisSinceEpoch == ds.DateMillisSinceEpoch).ToList())
)
.Any(riskLevel => riskLevel >= RiskLevel.High);

if (isHighRiskExposureDetected)
Expand All @@ -122,7 +125,7 @@ await _exposureDataCollectServer.UploadExposureDataAsync(
exposureConfiguration,
_deviceInfoUtility.Model,
enVersionStr,
dailySummaries, exposureWindows
newDailySummaries, newExposureWindows
);
}
catch (Exception e)
Expand All @@ -138,7 +141,7 @@ await _eventLogService.SendExposureDataAsync(
exposureConfiguration,
_deviceInfoUtility.Model,
enVersionStr,
dailySummaries, exposureWindows
newDailySummaries, newExposureWindows
);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Collections.Generic;
using Chino;

namespace Covid19Radar.Services
{
public interface IExposureRiskCalculationService
{
RiskLevel CalcRiskLevel(DailySummary dailySummary);
RiskLevel CalcRiskLevel(DailySummary dailySummary, List<ExposureWindow> exposureWindowList);
}

public class ExposureRiskCalculationService : IExposureRiskCalculationService
{
// TODO: We should make consideration later.
public RiskLevel CalcRiskLevel(DailySummary dailySummary)
=> RiskLevel.High;
// TODO: refine
private const double THRESHOLD_SCORE_SUM = 2000.0;

public RiskLevel CalcRiskLevel(DailySummary dailySummary, List<ExposureWindow> exposureWindowList)
{
if (dailySummary.DaySummary.ScoreSum >= THRESHOLD_SCORE_SUM)
{
return RiskLevel.High;
}
return RiskLevel.Low;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Moq;
using Newtonsoft.Json;
using Xunit;

namespace Covid19Radar.UnitTests.Repository
Expand Down Expand Up @@ -40,6 +41,173 @@ private IExposureDataRepository CreateRepository()
mockLoggerService.Object
);

[Fact]
public async void SetExposureDataTests_Once()
{
var existDailySummaries = new List<DailySummary>() {
};
var existExposureWindows = new List<ExposureWindow>()
{
};

var addDailySummaries = new List<DailySummary>() {
new DailySummary()
{
DateMillisSinceEpoch = 10,
DaySummary = new ExposureSummaryData(),
ConfirmedClinicalDiagnosisSummary = new ExposureSummaryData(),
ConfirmedTestSummary = new ExposureSummaryData(),
RecursiveSummary = new ExposureSummaryData(),
SelfReportedSummary = new ExposureSummaryData()
}
};
var addExposureWindows = new List<ExposureWindow>()
{
new ExposureWindow()
{
CalibrationConfidence = CalibrationConfidence.High,
DateMillisSinceEpoch = 0,
Infectiousness = Infectiousness.High,
ReportType = ReportType.Unknown,
ScanInstances = new List<ScanInstance>()
},
new ExposureWindow()
{
CalibrationConfidence = CalibrationConfidence.Medium,
DateMillisSinceEpoch = 0,
Infectiousness = Infectiousness.High,
ReportType = ReportType.ConfirmedTest,
ScanInstances = new List<ScanInstance>()
}
};

// Mock Setup
mockPreferencesService
.Setup(x => x.GetValue(It.Is<string>(x => x == "DailySummaries"), It.IsAny<string>()))
.Returns(JsonConvert.SerializeObject(existDailySummaries));
mockPreferencesService
.Setup(x => x.GetValue(It.Is<string>(x => x == "ExposureWindows"), It.IsAny<string>()))
.Returns(JsonConvert.SerializeObject(existExposureWindows));

var unitUnderTest = CreateRepository();

var (newDailySummaryList, newExposureWindowList) = await unitUnderTest.SetExposureDataAsync(
addDailySummaries,
addExposureWindows
);

var expectedDailySummariesJson = JsonConvert.SerializeObject(addDailySummaries);
var expectedExposureWindowsJson = JsonConvert.SerializeObject(addExposureWindows);

// Assert
Assert.Equal(addDailySummaries, newDailySummaryList);
Assert.Equal(addExposureWindows, newExposureWindowList);
mockPreferencesService.Verify(x => x.SetValue("DailySummaries", expectedDailySummariesJson), Times.Once);
mockPreferencesService.Verify(x => x.SetValue("ExposureWindows", expectedExposureWindowsJson), Times.Once);

}

[Fact]
public async void SetExposureDataTests_Append()
{
var existDailySummaries = new List<DailySummary>() {
new DailySummary()
{
DateMillisSinceEpoch = 0,
DaySummary = new ExposureSummaryData(),
ConfirmedClinicalDiagnosisSummary = new ExposureSummaryData(),
ConfirmedTestSummary = new ExposureSummaryData(),
RecursiveSummary = new ExposureSummaryData(),
SelfReportedSummary = new ExposureSummaryData()
}
};
var existExposureWindows = new List<ExposureWindow>()
{
new ExposureWindow()
{
CalibrationConfidence = CalibrationConfidence.High,
DateMillisSinceEpoch = 0,
Infectiousness = Infectiousness.High,
ReportType = ReportType.Unknown,
ScanInstances = new List<ScanInstance>()
}
};

var addDailySummaries = new List<DailySummary>() {
new DailySummary()
{
DateMillisSinceEpoch = 10,
DaySummary = new ExposureSummaryData(),
ConfirmedClinicalDiagnosisSummary = new ExposureSummaryData(),
ConfirmedTestSummary = new ExposureSummaryData(),
RecursiveSummary = new ExposureSummaryData(),
SelfReportedSummary = new ExposureSummaryData()
}
};
var addExposureWindows = new List<ExposureWindow>()
{
new ExposureWindow()
{
CalibrationConfidence = CalibrationConfidence.High,
DateMillisSinceEpoch = 0,
Infectiousness = Infectiousness.High,
ReportType = ReportType.Unknown,
ScanInstances = new List<ScanInstance>()
},
new ExposureWindow()
{
CalibrationConfidence = CalibrationConfidence.Medium,
DateMillisSinceEpoch = 0,
Infectiousness = Infectiousness.High,
ReportType = ReportType.ConfirmedTest,
ScanInstances = new List<ScanInstance>()
}
};

var expectedDailySummaries = new List<DailySummary>() {
existDailySummaries[0],
addDailySummaries[0]
};
var expectedExposureWindows = new List<ExposureWindow>()
{
existExposureWindows[0],
addExposureWindows[1]
};

var expectedNewDailySummaries = new List<DailySummary>() {
addDailySummaries[0]
};
var expectedNewExposureWindows = new List<ExposureWindow>()
{
addExposureWindows[1]
};

// Mock Setup
mockPreferencesService
.Setup(x => x.GetValue(It.Is<string>(x => x == "DailySummaries"), It.IsAny<string>()))
.Returns(JsonConvert.SerializeObject(existDailySummaries));
mockPreferencesService
.Setup(x => x.GetValue(It.Is<string>(x => x == "ExposureWindows"), It.IsAny<string>()))
.Returns(JsonConvert.SerializeObject(existExposureWindows));

var unitUnderTest = CreateRepository();

var (newDailySummaryList, newExposureWindowList) = await unitUnderTest.SetExposureDataAsync(
addDailySummaries,
addExposureWindows
);

var expectedDailySummariesJson = JsonConvert.SerializeObject(expectedDailySummaries);
var expectedExposureWindowsJson = JsonConvert.SerializeObject(expectedExposureWindows);

// Assert
Assert.Equal(expectedNewDailySummaries, newDailySummaryList);
Assert.Equal(expectedNewExposureWindows, newExposureWindowList);
mockPreferencesService.Verify(x => x.SetValue("DailySummaries", expectedDailySummariesJson), Times.Once);
mockPreferencesService.Verify(x => x.SetValue("ExposureWindows", expectedExposureWindowsJson), Times.Once);

}

[Fact]
public void GetExposureInformationListTests_Success()
{
Expand Down
Loading

0 comments on commit 6af2214

Please sign in to comment.