This commit is contained in:
2024-02-06 14:54:46 -07:00
parent e671024a4e
commit c48046a97e
12 changed files with 551 additions and 65 deletions

View File

@@ -7,12 +7,14 @@ namespace Management.Services.Canvas;
public class CanvasQuizService(
IWebRequestor webRequestor,
CanvasServiceUtils utils,
CanvasAssignmentService assignments
CanvasAssignmentService assignments,
ILogger<CanvasQuizService> logger
)
{
private readonly IWebRequestor webRequestor = webRequestor;
private readonly CanvasServiceUtils utils = utils;
private readonly CanvasAssignmentService assignments = assignments;
private readonly ILogger<CanvasQuizService> logger = logger;
public async Task<IEnumerable<CanvasQuiz>> GetAll(ulong courseId)
{
@@ -33,6 +35,9 @@ public class CanvasQuizService(
ulong? canvasAssignmentGroupId
)
{
using var activity = DiagnosticsConfig.Source.StartActivity("Creating all canvas quiz");
activity?.SetCustomProperty("localQuiz", localQuiz);
activity?.SetTag("canvas syncronization", true);
Console.WriteLine($"Creating Quiz {localQuiz.Name}");
var url = $"courses/{canvasCourseId}/quizzes";
@@ -59,6 +64,7 @@ public class CanvasQuizService(
var (canvasQuiz, response) = await webRequestor.PostAsync<CanvasQuiz>(request);
if (canvasQuiz == null)
throw new Exception("Created canvas quiz was null");
activity?.SetCustomProperty("canvasQuizId", canvasQuiz.Id);
await CreateQuizQuestions(canvasCourseId, canvasQuiz.Id, localQuiz);
return canvasQuiz.Id;
@@ -70,15 +76,27 @@ public class CanvasQuizService(
LocalQuiz localQuiz
)
{
var tasks = localQuiz.Questions.Select(createQuestion(canvasCourseId, canvasQuizId)).ToArray();
await Task.WhenAll(tasks);
using var activity = DiagnosticsConfig.Source.StartActivity("Creating all quiz questions");
activity?.SetCustomProperty("canvasQuizId", canvasQuizId);
activity?.SetTag("canvas syncronization", true);
var tasks = localQuiz.Questions.Select(
async (q, i) => await createQuestionOnly(canvasCourseId, canvasQuizId, q, i)
).ToArray();
var questionAndPositions = await Task.WhenAll(tasks);
await hackFixQuestionOrdering(canvasCourseId, canvasQuizId, questionAndPositions);
await hackFixRedundantAssignments(canvasCourseId);
}
private async Task hackFixRedundantAssignments(ulong canvasCourseId)
{
var canvasAssignments = await assignments.GetAll(canvasCourseId);
using var activity = DiagnosticsConfig.Source.StartActivity("hack fixing redundant quiz assignments that are auto-created");
activity?.SetTag("canvas syncronization", true);
var canvasAssignments = await assignments.GetAll(canvasCourseId);
var assignmentsToDelete = canvasAssignments
.Where(
assignment =>
@@ -99,24 +117,44 @@ public class CanvasQuizService(
await Task.WhenAll(tasks);
}
private Func<LocalQuizQuestion, Task> createQuestion(
ulong canvasCourseId,
ulong canvasQuizId
)
private async Task hackFixQuestionOrdering(ulong canvasCourseId, ulong canvasQuizId, IEnumerable<(CanvasQuizQuestion question, int position)> questionAndPositions )
{
return async (question) => await createQuestionOnly(canvasCourseId, canvasQuizId, question);
using var activity = DiagnosticsConfig.Source.StartActivity("hack fixing question ordering with reorder");
activity?.SetCustomProperty("canvasQuizId", canvasQuizId);
activity?.SetTag("canvas syncronization", true);
var order = questionAndPositions.OrderBy(t => t.position).Select(tuple => {
return new {
type = "question",
id = tuple.question.Id.ToString(),
};
}).ToArray();
var url = $"courses/{canvasCourseId}/quizzes/{canvasQuizId}/reorder";
var request = new RestRequest(url);
request.AddBody(new { order });
var response = await webRequestor.PostAsync(request);
if (!response.IsSuccessStatusCode)
throw new NullReferenceException("error re-ordering questions, reorder response is not successfull");
}
private async Task<CanvasQuizQuestion> createQuestionOnly(
private async Task<(CanvasQuizQuestion question, int position)> createQuestionOnly(
ulong canvasCourseId,
ulong canvasQuizId,
LocalQuizQuestion q
LocalQuizQuestion q,
int position
)
{
using var activity = DiagnosticsConfig.Source.StartActivity("creating quiz question");
activity?.SetTag("canvas syncronization", true);
activity?.SetTag("localQuestion", q);
activity?.SetCustomProperty("localQuestion", q);
activity?.SetTag("success", false);
var url = $"courses/{canvasCourseId}/quizzes/{canvasQuizId}/questions";
var answers = q.Answers
.Select(a => new { answer_html = a.HtmlText, answer_weight = a.Correct ? 100 : 0 })
.ToArray();
var answers = getAnswers(q);
var body = new
{
question = new
@@ -124,16 +162,36 @@ public class CanvasQuizService(
question_text = q.HtmlText,
question_type = q.QuestionType + "_question",
points_possible = q.Points,
// position
position,
answers
}
};
var request = new RestRequest(url);
request.AddBody(body);
var (newQuestion, response) = await webRequestor.PostAsync<CanvasQuizQuestion>(request);
if (newQuestion == null)
throw new NullReferenceException("error creating new question, created question is null");
return newQuestion;
activity?.SetCustomProperty("canvasQuizId", newQuestion.Id);
activity?.SetTag("success", true);
return (newQuestion, position);
}
private static object[] getAnswers(LocalQuizQuestion q)
{
if(q.QuestionType == QuestionType.MATCHING)
return q.Answers
.Select(a => new {
answer_match_left = a.Text,
answer_match_right = a.MatchedText
})
.ToArray();
return q.Answers
.Select(a => new { answer_html = a.HtmlText, answer_weight = a.Correct ? 100 : 0 })
.ToArray();
}
}

View File

@@ -1,4 +1,3 @@
using System.Diagnostics;
using LocalModels;
using Management.Services;
@@ -58,10 +57,3 @@ public class FileStorageManager
}
}
public static class DiagnosticsConfig
{
public const string SourceName = "canvas-management-source";
public static ActivitySource Source = new ActivitySource(SourceName);
}

View File

@@ -1,3 +1,4 @@
using System.Net;
using Microsoft.Extensions.Configuration;
using RestSharp;
@@ -7,10 +8,12 @@ public class WebRequestor : IWebRequestor
private string token;
private RestClient client;
private readonly IConfiguration _config;
private readonly ILogger<WebRequestor> logger;
public WebRequestor(IConfiguration config)
public WebRequestor(IConfiguration config, ILogger<WebRequestor> logger)
{
_config = config;
this.logger = logger;
token =
_config["CANVAS_TOKEN"]
?? throw new Exception("CANVAS_TOKEN not in environment");
@@ -33,22 +36,41 @@ public class WebRequestor : IWebRequestor
public async Task<RestResponse> PostAsync(RestRequest request)
{
using var activity = DiagnosticsConfig.Source.StartActivity("sending post");
activity?.AddTag("success", false);
request.AddHeader("Content-Type", "application/json");
var response = await client.ExecutePostAsync(request);
if (!response.IsSuccessful)
try
{
Console.WriteLine(response.Content);
Console.WriteLine(response.ResponseUri);
Console.WriteLine("error with response");
throw new Exception("error with response");
var response = await client.ExecutePostAsync(request);
activity?.AddTag("url", response.ResponseUri);
if (isRateLimited(response))
logger.LogInformation("hit rate limit");
if (!response.IsSuccessful)
{
logger.LogError($"Error with response, response content: {response.Content}", response);
throw new Exception("error with response");
}
activity?.AddTag("success", true);
return response;
}
catch (Exception e)
{
Console.WriteLine("inside post catch block");
throw e;
}
return response;
}
private static bool isRateLimited(RestResponse response) =>
response.StatusCode == HttpStatusCode.Forbidden && response.Content?.Contains("403 Forbidden (Rate Limit Exceeded)") != null;
public async Task<(T?, RestResponse)> PostAsync<T>(RestRequest request)
{
request.AddHeader("Content-Type", "application/json");
var response = await client.ExecutePostAsync(request);
var response = await PostAsync(request);
return (deserialize<T>(response), response);
}
@@ -75,11 +97,38 @@ public class WebRequestor : IWebRequestor
public async Task<RestResponse> DeleteAsync(RestRequest request)
{
return await client.DeleteAsync(request);
// using var activity = DiagnosticsConfig.Source.StartActivity($"sending delete web request");
// activity?.AddTag("success", false);
try
{
var response = await client.DeleteAsync(request);
if (isRateLimited(response))
Console.WriteLine("after delete response in rate limited");
// Console.WriteLine(response.Content);
// activity?.AddTag("url", response.ResponseUri);
// activity?.AddTag("success", true);
return response;
}
catch (HttpRequestException e)
{
if (e.StatusCode == HttpStatusCode.Forbidden) // && response.Content == "403 Forbidden (Rate Limit Exceeded)"
logger.LogInformation("hit rate limit in delete");
Console.WriteLine(e.StatusCode);
// Console.WriteLine();
throw e;
}
}
private static T? deserialize<T>(RestResponse response)
{
// using var activity = DiagnosticsConfig.Source.StartActivity("deserializing response");
// activity?.AddTag("url", response.ResponseUri);
if (!response.IsSuccessful)
{
Console.WriteLine(response.Content);