Repository: microsoft/onefuzz
File: src/ApiService/ApiService/onefuzzlib/JobResultOperations.cs (C#)

Code before:
    Async.Task<JobResult?> GetJobResults(Guid jobId);
    Async.Task<OneFuzzResultVoid> CreateOrUpdate(Guid jobId, Guid taskId, Guid machineId, string resultType, Dictionary<string, double> resultValue);

}
public class JobResultOperations : Orm<JobResult>, IJobResultOperations {

    const string COVERAGE_DATA = "CoverageData";
    const string RUNTIME_STATS = "RuntimeStats";

    public JobResultOperations(ILogger<JobResultOperations> log, IOnefuzzContext context)
        : base(log, context) {
    }

    public async Async.Task<JobResult?> GetJobResult(Guid jobId, Guid taskId, Guid machineId, string metricType) {
        var data = QueryAsync(Query.SingleEntity(jobId.ToString(), string.Concat(taskId, machineId, metricType)));
        return await data.FirstOrDefaultAsync();
    }

    public async Async.Task<JobResult?> GetJobResults(Guid jobId) {
        var data = QueryAsync(Query.PartitionKey(jobId.ToString()));
        return await data.FirstOrDefaultAsync();
    }

    private async Async.Task<bool> TryUpdate(Job job, Guid taskId, Guid machineId, string resultType, Dictionary<string, double> resultValue) {
        var jobId = job.JobId;
        var taskIdMachineIdMetric = string.Concat(taskId, machineId, resultType);

        var oldEntry = await GetJobResult(jobId, taskId, machineId, resultType);
        var newEntry = new JobResult(JobId: jobId, TaskIdMachineIdMetric: taskIdMachineIdMetric, TaskId: taskId, MachineId: machineId, Project: job.Config.Project, Name: job.Config.Name, resultType, resultValue);

        if (oldEntry == null) {
            _logTracer.LogInformation($"attempt to insert new job result {taskId} and taskId+machineId+metricType {taskIdMachineIdMetric}");
            var result = await Insert(newEntry);
            if (!result.IsOk) {
                throw new InvalidOperationException($"failed to insert job result with taskId {taskId} and taskId+machineId+metricType {taskIdMachineIdMetric}");
            }
            return true;
        }

        ResultVoid<(HttpStatusCode Status, string Reason)> r;
        switch (resultType) {
            case COVERAGE_DATA:
                if (oldEntry.MetricValue["rate"] < newEntry.MetricValue["rate"]) {
                    r = await Replace(newEntry);
                    if (!r.IsOk) {
                        throw new InvalidOperationException($"failed to replace job result with taskId {taskId} and machineId+metricType {taskIdMachineIdMetric}");
                    }
                }
                break;
            case RUNTIME_STATS:
                if (oldEntry.MetricValue["total_count"] < newEntry.MetricValue["total_count"]) {

Review comment (suggestion):
For readability in the table it would probably be good to separate these values with `|` or something.

Code after:
    Async.Task<OneFuzzResultVoid> CreateOrUpdate(Guid jobId, Guid taskId, Guid machineId, DateTime createdAt, double version, string resultType, Dictionary<string, double> resultValue);

}
public class JobResultOperations : Orm<JobResult>, IJobResultOperations {

    const string COVERAGE_DATA = "CoverageData";
    const string RUNTIME_STATS = "RuntimeStats";

    public JobResultOperations(ILogger<JobResultOperations> log, IOnefuzzContext context)
        : base(log, context) {
    }

    public async Async.Task<JobResult?> GetJobResult(Guid jobId, Guid taskId, Guid machineId, string metricType) {
        var data = QueryAsync(Query.SingleEntity(jobId.ToString(), string.Concat(taskId, "-", machineId, "-", metricType)));
        return await data.FirstOrDefaultAsync();
    }

    private async Async.Task<bool> TryUpdate(Job job, Guid taskId, Guid machineId, DateTime createdAt, double version, string resultType, Dictionary<string, double> resultValue) {
        var jobId = job.JobId;
        var taskIdMachineIdMetric = string.Concat(taskId, "-", machineId, "-", resultType);

        var oldEntry = await GetJobResult(jobId, taskId, machineId, resultType);

        if (oldEntry == null) {
            _logTracer.LogInformation($"attempt to insert new job result {taskId} and taskId+machineId+metricType {taskIdMachineIdMetric}");
            var newEntry = new JobResult(JobId: jobId, TaskIdMachineIdMetric: taskIdMachineIdMetric, TaskId: taskId, MachineId: machineId, CreatedAt: createdAt, Project: job.Config.Project, Name: job.Config.Name, resultType, Version: version, resultValue);
            var result = await Insert(newEntry);
            if (!result.IsOk) {
                throw new InvalidOperationException($"failed to insert job result with taskId {taskId} and taskId+machineId+metricType {taskIdMachineIdMetric}");
            }
            return true;
        }

        ResultVoid<(HttpStatusCode Status, string Reason)> r;
        switch (resultType) {
            case COVERAGE_DATA:
            case RUNTIME_STATS:
                if (oldEntry.CreatedAt < createdAt) {
                    oldEntry = oldEntry with { CreatedAt = createdAt, MetricValue = resultValue };
                    r = await Update(oldEntry);
                    if (!r.IsOk) {
                        throw new InvalidOperationException($"failed to replace job result with taskId {taskId} and machineId+metricType {taskIdMachineIdMetric}");
                    }
                } else {
                    _logTracer.LogInformation($"received an out-of-date metric. skipping.");
                }
                break;
            default:
                _logTracer.LogInformation($"attempt to update job result {taskId} and taskId+machineId+metricType {taskIdMachineIdMetric}");
                oldEntry.MetricValue["count"]++;
                oldEntry = oldEntry with { MetricValue = oldEntry.MetricValue };