public void SubmitData() { QuestionResults userInput = new QuestionResults(); userInput.livingArrangement = SelectedLivingArrangement.ToString(); userInput.ageGroup = SelectedAgeGroup.ToString(); userInput.pax = SelectedPax; userInput.affordable = (SelectedAffordable == 0 ? false : true); userInput.requiredRooms[RequiredRooms.SingleBedroom.ToString()] = SelectedRequiredRooms[RequiredRooms.SingleBedroom]; userInput.requiredRooms[RequiredRooms.SharedBedroom.ToString()] = SelectedRequiredRooms[RequiredRooms.SharedBedroom]; userInput.requiredRooms[RequiredRooms.Study.ToString()] = SelectedRequiredRooms[RequiredRooms.Study]; //bug fix to allow algorithm to allocate rooms if (SelectedRequiredRooms[RequiredRooms.SingleBedroom] + SelectedRequiredRooms[RequiredRooms.SharedBedroom] + SelectedRequiredRooms[RequiredRooms.Study] == 0) { SelectedRequiredRooms[RequiredRooms.SingleBedroom] = 1; } int row = SelectedLocation / colSt.Length; int col = SelectedLocation - row * colSt.Length; //print(string.Format("[{0},{1}]", row, col)); userInput.location[0] = rowSt[row]; //row userInput.location[1] = colSt[col]; //col foreach (SharedSpace ss in SelectedSharedSpaces) { userInput.preferredSharedSpaces.Add(ss.ToString()); } string inputJson = Newtonsoft.Json.JsonConvert.SerializeObject(userInput); ConnectionManager.Instance.UploadUserInput(inputJson); //UiCanvasManager.Instance.CongratulatoryScreen(); }
public IDataResult <QuestionForResponseDto> GetQuestionsAndAnswerByProjectCode(string pCode) { var projectQuestions = _projectQuestionDal.GetProjectQuestionsByProjectCode(pCode); var qIds = new List <int>(); foreach (var pq in projectQuestions) { qIds.Add(pq.Id); } var questions = _questionDal.GetQuestionsByProjectCode(qIds); var answers = _answerDal.GetAnswerByProjectCode(pCode); var resp = new QuestionForResponseDto(); resp.response_code = 0; if (questions.Count > 0 && answers.Count > 0) { foreach (var q in questions) { var qresult = new QuestionResults(); qresult.category = "Cities"; qresult.type = "multiple"; qresult.difficulty = "medium"; qresult.questionImage = q.QuestionImage; qresult.question = q.QuestionText; qresult.answerType = q.AnswerType; qresult.correct_answer = answers.Find(x => x.QuestionId == q.Id && x.IsTrue == true); var incorrects = answers.FindAll(x => x.QuestionId == q.Id && x.IsTrue == false); foreach (var ic in incorrects) { qresult.incorrect_answers.Add(ic); } resp.results.Add(qresult); } } return(new SuccessDataResult <QuestionForResponseDto>(resp, Messages.RegisterSuccessfull)); }
public static async Task RunPartitionedChangeFeed( [CosmosDBTrigger( databaseName: "%cosmosdbname%", collectionName: "%cosmoscollectionname%", LeaseCollectionPrefix = "partitionedfeed", LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true, ConnectionStringSetting = "cosmosconnectionstring", FeedPollDelay = 1000, MaxItemsPerInvocation = 100) ] IReadOnlyList <Document> input, [CosmosDB( databaseName: "%cosmosdbname%", collectionName: "%cosmoscollectionname%", ConnectionStringSetting = "cosmosconnectionstring" )] DocumentClient client, [SignalR(HubName = "%Region%")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log) { log.LogInformation("ChangeFeedProcessorPartitioned was called."); string _databaseName = Environment.GetEnvironmentVariable("cosmosdbname"); string _collectionName = Environment.GetEnvironmentVariable("cosmoscollectionname"); if (input != null && input.Count > 0) { log.LogInformation("Documents modified " + input.Count); log.LogInformation("First document Id " + input[0].Id); SessionResponseStats stats = new SessionResponseStats(); var answerDocs = input.Where(doc => doc.GetPropertyValue <string>("type").ToLower() == "answer").ToList(); log.LogInformation($"Found {answerDocs.Count} answer docs in the current changefeed processing iteration"); var answers = answerDocs.Select(a => JsonConvert.DeserializeObject <Answer>(a.ToString())).ToList(); var answersBySession = answers.GroupBy(a => a.SessionId); foreach (var sessionQuestions in answersBySession) { var sessionId = sessionQuestions.Key; log.LogInformation($"Processing session: {sessionId}"); // fetch session from cosmos db Document sessionDoc = null; Session session = null; var sessionDocPartitionKey = new PartitionKey($"session-{sessionId}"); var sessionDocId = $"{sessionId}"; var sessionDocUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, sessionDocId); try{ sessionDoc = await client.ReadDocumentAsync(sessionDocUri, new RequestOptions() { PartitionKey = sessionDocPartitionKey }); session = JsonConvert.DeserializeObject <Session>(sessionDoc.ToString()); } catch (DocumentClientException ex) { log.LogError($"Error in retrieving session doc from cosmos for session {sessionId}."); log.LogError(ex.Message); continue; // session does not seem to exist or not retrievable, so just skip/drop the answer submissions for this session } // fetch session results from cosmos db Document sessionResultsdoc = null; SessionResults sessionResults = null; var sessionResultsDocPartitionKey = new PartitionKey($"session-{sessionId}"); var sessionResultsDocId = $"sessionresults-{sessionId}"; var sessionResultsDocUri = UriFactory.CreateDocumentUri(_databaseName, _collectionName, sessionResultsDocId); try{ sessionResultsdoc = await client.ReadDocumentAsync(sessionResultsDocUri, new RequestOptions() { PartitionKey = sessionResultsDocPartitionKey }); sessionResults = JsonConvert.DeserializeObject <SessionResults>(sessionResultsdoc.ToString()); } catch (DocumentClientException ex) { if (ex.StatusCode != System.Net.HttpStatusCode.NotFound) { log.LogError("Error in retrieving results doc from cosmos."); log.LogError(ex.Message); throw; } } if (sessionResultsdoc == null) { log.LogInformation($" Results for session {sessionId} not found yet in cosmos db. Need to create new results doc."); sessionResultsdoc = new Document(); sessionResults = new SessionResults() { Id = $"sessionresults-{session.SessionId}", PartitionId = $"session-{session.SessionId}", SessionId = session.SessionId, SessionName = session.SessionName }; } // process all questions (and their answers) for this session: log.LogInformation($" Processing questions for session: {sessionId}"); var answersByQuestionId = sessionQuestions.GroupBy(s => s.QuestionId); foreach (var questionAnswers in answersByQuestionId) { var questionId = questionAnswers.Key; log.LogInformation($" Processing answers for question {questionId} within session {sessionId}"); if (session.Questions.Any(q => q.Id == questionId)) { var question = session.Questions.First(q => q.Id == questionId); var questionResults = sessionResults.Questions.FirstOrDefault(q => q.QuestionId == questionId); if (questionResults == null) { questionResults = new QuestionResults() { QuestionId = questionId, Title = question.Title }; sessionResults.Questions.Add(questionResults); } // TODO: deduplicate multiple entries for the same answer on the same question by the same user: // var answersByAnswerId = questionAnswers.GroupBy(a => a.AnswerId, a => new { AnswerId = a.AnswerId, QuestionId = a.QuestionId, UserId = a.UserId }, (answerId, answersByAllUsers) => answersByAllUsers.Distinct()); var answersByAnswerId = questionAnswers.GroupBy(a => a.AnswerId); foreach (var answersWithSameId in answersByAnswerId) { var answerId = answersWithSameId.Key; log.LogInformation($" Processing answers with answer id {answersWithSameId.Key} for question {questionId} within session {sessionId}"); var answerCount = answersWithSameId.Count(); log.LogInformation($" Answercount is {answerCount} for answer with id {answersWithSameId.Key} for question {questionId} within session {sessionId}"); // add votes to the votecount for this answer: var votes = questionResults.Votes.FirstOrDefault(v => v.Id == answerId); if (votes == null) { votes = new VoteCount() { Id = answerId, Votes = answerCount }; questionResults.Votes.Add(votes); } else { votes.Votes += answerCount; } } } } // write back updated session results to cosmos: var collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName); try{ var result = await client.UpsertDocumentAsync(collectionUri, sessionResults, new RequestOptions() { PartitionKey = sessionDocPartitionKey }); } catch (DocumentClientException ex) { log.LogError($"Failed to update session results for session {sessionId} in cosmos"); throw; } } } }