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

Code before:
﻿using ApiService.OneFuzzLib.Orm;
using Microsoft.Extensions.Logging;
using Polly;
namespace Microsoft.OneFuzz.Service;
using System.Net;

public interface IJobResultOperations : IOrm<JobResult> {

    Async.Task<JobResult?> GetJobResult(Guid taskId, string machineIdMetric);
    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 taskId, string machineIdMetric) {
        var data = QueryAsync(Query.SingleEntity(taskId.ToString(), machineIdMetric));
        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 machineIdMetric = string.Concat(machineId, resultType);

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

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

        ResultVoid<(HttpStatusCode Status, string Reason)> r;
        switch (resultType) {
            case COVERAGE_DATA when oldEntry.MetricValue["rate"] < newEntry.MetricValue["rate"]:
                r = await Replace(newEntry);
                break;

Review comment (suggestion):
I'd make this method take machine ID + metric type separately - the way the data is stored in the table is a concern of this class only

Code after:
﻿using ApiService.OneFuzzLib.Orm;
using Microsoft.Extensions.Logging;
using Polly;
namespace Microsoft.OneFuzz.Service;
using System.Net;

public interface IJobResultOperations : IOrm<JobResult> {

    Async.Task<JobResult?> GetJobResult(Guid jobId, Guid taskId, Guid machineId, string metricType);
    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) {