예제 #1
0
        /// <inheritdoc />
        public async Task <string> GetStatisticForQuestions(int identifier)
        {
            if (!await IsUserAdmin(identifier))
            {
                return(string.Empty);
            }

            var questionResult = new List <QuestionResult>();
            var query          = $"SELECT question, wrongcount FROM dbo.{QuestionResultsTable};";

            try
            {
                await _connection.OpenAsync().ConfigureAwait(false);

                using (var command = new SqlCommand(query, _connection))
                {
                    var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);

                    while (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        questionResult.Add(new QuestionResult {
                            Question = reader.GetString(0), Quota = reader.GetInt32(1)
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error(@"Не удалось получить результаты пользователей!", exception);
                }

                return(string.Empty);
            }
            finally
            {
                _connection.Close();
            }

            var sum = questionResult.Aggregate((double)0, (current, item) => current + item.Quota);

            questionResult.ForEach(item => item.Quota = item.Quota / sum);
            if (OutputFileType.Equals(StatisticOutputFileType.Image))
            {
                return(_resultFileGenerator.GenerateAsImage(questionResult));
            }

            if (OutputFileType.Equals(StatisticOutputFileType.Text))
            {
                return(_resultFileGenerator.GenerateAsText(questionResult));
            }

            throw new ArgumentOutOfRangeException("", OutputFileType, @"Неизвестный тип выходного файла!");
        }
예제 #2
0
        /// <inheritdoc />
        public async Task <string> GetStatisticForUsers(ICollection <string> users, DateTime startDate, int identifier)
        {
            if (!await IsUserAdmin(identifier))
            {
                return(string.Empty);
            }

            var results = new List <UserResult>();
            var query   = $"SELECT username, result, finishtime FROM {UserResultsTable} WHERE (finishtime > convert(datetime,'{startDate:dd-MM-yy}', 5) AND username IS NOT NULL)";

            if (users != null && users.Any())
            {
                query += @" AND (" + string.Join(@" OR ", users.Select(user => $"username = '******'")) + @")";
            }


            query += @";";
            try
            {
                await _connection.OpenAsync().ConfigureAwait(false);

                using (var command = new SqlCommand(query, _connection))
                {
                    var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);

                    while (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        var user   = reader.GetString(0);
                        var result = (int)reader.GetDouble(1);
                        var date   = reader.GetDateTime(2);
                        if (results.Any(item => item.User == user))
                        {
                            results[results.FindIndex(item => item.User == user)].Result[date] = result;
                        }
                        else
                        {
                            results.Add(new UserResult {
                                User = user, Result = new Dictionary <DateTime, int> {
                                    { date, result }
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error(@"Не удалось получить результаты пользователей!", exception);
                }

                return(string.Empty);
            }
            finally
            {
                _connection.Close();
            }

            foreach (var result in results)
            {
                var sortedResult  = new SortedDictionary <DateTime, int>(result.Result);
                var keyValuePairs = sortedResult.OrderBy(item => item.Key);
                result.Result = keyValuePairs.ToDictionary(item => item.Key, item => item.Value);
            }

            if (OutputFileType.Equals(StatisticOutputFileType.Image))
            {
                return(_resultFileGenerator.GenerateAsImage(results));
            }

            if (OutputFileType.Equals(StatisticOutputFileType.Text))
            {
                return(_resultFileGenerator.GenerateAsText(results));
            }

            throw new ArgumentOutOfRangeException("", OutputFileType, @"Неизвестный тип выходного файла!");
        }