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

Code before:
    }

    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 "CoverageData" when oldEntry.MetricValue["rate"] < newEntry.MetricValue["rate"]:
                r = await Replace(newEntry);
                break;
            case "RuntimeStats" when oldEntry.MetricValue["total_count"] < newEntry.MetricValue["total_count"]:
                r = await Replace(newEntry);
                break;
            default:
                _logTracer.LogInformation($"attempt to update job result {taskId} and machineId+metricType {machineIdMetric}");
                oldEntry.MetricValue["count"]++;
                var newResult = oldEntry with { MetricValue = oldEntry.MetricValue };
                r = await Update(newResult);
                break;
        }

        if (!r.IsOk) {
            throw new InvalidOperationException($"failed to replace or update job result with taskId {taskId} and machineId+metricType {machineIdMetric}");
        }

        return true;

    }

    public async Async.Task<OneFuzzResultVoid> CreateOrUpdate(Guid jobId, Guid taskId, Guid machineId, string resultType, Dictionary<string, double> resultValue) {

        var job = await _context.JobOperations.Get(jobId);
        if (job == null) {

Review comment (suggestion):
use constants or an enum type for the type name.

Feel free to create an issue if you prefer to address it later.

Code after:

    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 };
                r = await Update(oldEntry);
                if (!r.IsOk) {
                    throw new InvalidOperationException($"failed to update job result with taskId {taskId} and machineId+metricType {taskIdMachineIdMetric}");
                }
                break;
        }