Skip to content

Commit

Permalink
Romanchuk/statistic reports (#889)
Browse files Browse the repository at this point in the history
* Init

* Response for GetDataById was fixed

* Add view to migration

* Fix some code smells

* Fix some smells

* Code smells

* Code smells

* Code smells

* Smell

* Migration was deleted

* Add migration after merge

* Delete migration

* Add new migration
  • Loading branch information
alrom authored Jan 4, 2023
1 parent 94aceb9 commit 3aff235
Show file tree
Hide file tree
Showing 37 changed files with 3,957 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OutOfSchool.Services.Contexts.Configuration;

/// <summary>
/// Contains a configuration that is essential for operations with files in Google Cloud Storage.
/// </summary>
public class GcpStorageFilesSourceConfig : GcpStorageSourceConfig
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OutOfSchool.Services.Enums;

public enum StatisticReportDataTypes
{
CSV,
XLSX,
HTML,
}
20 changes: 20 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Enums/StatisticReportTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace OutOfSchool.Services.Enums;

public enum StatisticReportTypes
{
WorkshopsYear,
WorkshopsDaily,
}

public static class StatisticReportTypesExtensions
{
public static string GetReportTitle(this StatisticReportTypes value)
{
return value switch
{
StatisticReportTypes.WorkshopsYear => "Річний звіт по гуртках: {0:dd-MM-yyyy}",
StatisticReportTypes.WorkshopsDaily => "Поточний звіт по гуртках: {0:dd-MM-yyyy}",
_ => string.Empty
};
}
}
15 changes: 15 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/FileInDb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OutOfSchool.Services.Models;
public class FileInDb : IKeyedEntity<string>
{
public string Id { get; set; }

public string ContentType { get; set; }

public byte[] Data { get; set; }
}
26 changes: 26 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/StatisticReport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.ComponentModel.DataAnnotations;
using OutOfSchool.Services.Enums;

namespace OutOfSchool.Services.Models;

public class StatisticReport : IKeyedEntity<Guid>
{
public Guid Id { get; set; }

[Required]
public DateTime Date { get; set; }

[Required]
public StatisticReportTypes ReportType { get; set; }

[Required]
public StatisticReportDataTypes ReportDataType { get; set; }

[Required]
[MaxLength(250)]
public string Title { get; set; }

[Required]
public string ExternalStorageId { get; set; }
}
112 changes: 112 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/StatisticReportCSV.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using CsvHelper.Configuration.Attributes;
using Microsoft.EntityFrameworkCore;

namespace OutOfSchool.Services.Models;

[Keyless]
public class StatisticReportCSV
{
[Name("Рік звітності")]
public int Year { get; set; }

[Name("ID закладу позашкільної освіти")]
public Guid ProviderId { get; set; }

[Name("Повна назва закладу")]
public string ProviderName { get; set; }

[Name("ЄДРПОУ")]
public string EDRPOU { get; set; }

[Name("Форма власності")]
public string ProviderType { get; set; }

[Name("Підпорядкування(Назва міністерства)")]
public string InstitutionTitle { get; set; }

[Name("КАТОТТГ")]
public string CATOTTGCode { get; set; }

[Name("Область")]
public string Region { get; set; }

[Name("Громада")]
public string TerritorialCommunity { get; set; }

[Name("Населений пункт")]
public string Settlement { get; set; }

[Name("Тип місцевості")]
public string CATOTTGCategory { get; set; }

[Name("Комплексний/профільний")]
public string Complex { get; set; }

[Name("Статус(окрема юридична особа/структурний підрозділ)")]
public string Status { get; set; }

[Name("Загальна кількість гуртків")]
public int WorkshopsAmount { get; set; }

[Name("Загальна кількість поданих заяв(всі які є для цього надавача, за звітний період)")]
public int ApplicationsAmount { get; set; }

[Name("Загальна кількість зарахованих заяв(заразовані/навчаються, за звітній період)")]
public int ApplicationsApproved { get; set; }

[Name("Загальна кількість здобувачів освіти(загалом, крім тих хто завершив навчання)")]
public int ChildrenStudying { get; set; }

[Name("Загальна кількість здобувачів освіти жіночої статі(загалом, крім тих хто завершив навчання)")]
public int ChildrenStudyingFemale { get; set; }

[Name("Загальна кількість здобувачів освіти віком до 18 років(загалом, крім тих хто завершив навчання)")]
public int ChildrenStudyingLess18 { get; set; }

[Name("Загальна кількість викладачів")]
public int Teachers { get; set; }

// By InstitutionHierarchy
[Name("Кількість гуртків (в розрізі напрямків)")]
public int WorkshopsAmountInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти (в розрізі напрямків)")]
public int ChildrenStudyingInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти жіночої статі (в розрізі напрямків)")]
public int ChildrenStudyingFemaleInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти із досягненнями (в розрізі напрямків)")]
public int ChildrenStudyingAchievementsInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти із багатодітних сімей (в розрізі напрямків)")]
public int ChildrenStudyingLargeFamilyInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти із малозабезпечених сімей (в розрізі напрямків)")]
public int ChildrenStudyingPoorFamilyInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти з інвалідністю (в розрізі напрямків)")]
public int ChildrenStudyingDisabilityInstitutionHierarchy { get; set; }

[Name("Кількість здобувачів освіти, що є сиротами або позбавленими батьківського піклування (в розрізі напрямків)")]
public int ChildrenStudyingOrphanInstitutionHierarchy { get; set; }

[Name("Загальна кількість викладачів (в розрізі напрямків)")]
public int TeachersInstitutionHierarchy { get; set; }

[Name("Кількість викладачів віком до 30 (в розрізі напрямків)")]
public int TeachersLess30InstitutionHierarchy { get; set; }

[Name("Кількість викладачів віком 31 - 40 (в розрізі напрямків)")]
public int TeachersFrom31To40InstitutionHierarchy { get; set; }

[Name("Кількість викладачів віком 41 - 50 (в розрізі напрямків)")]
public int TeachersFrom41To50InstitutionHierarchy { get; set; }

[Name("Кількість викладачів віком 51 - 55 (в розрізі напрямків)")]
public int TeachersFrom51To55InstitutionHierarchy { get; set; }

[Name("Кількість викладачів віком 55 + (в розрізі напрямків)")]
public int TeachersFrom55InstitutionHierarchy { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CsvHelper" Version="29.0.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="3.7.0" />
<PackageReference Include="H3Lib" Version="3.7.2" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="6.0.6" />
Expand Down
6 changes: 6 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public OutOfSchoolDbContext(DbContextOptions<OutOfSchoolDbContext> options)
public DbSet<Achievement> Achievements { get; set; }
public DbSet<ProviderType> ProviderTypes { get; set; }

public DbSet<StatisticReport> StatisticReports { get; set; }

public DbSet<StatisticReportCSV> StatisticReportsCSV { get; set; }

public DbSet<FileInDb> FilesInDb { get; set; }

public async Task<int> CompleteAsync() => await this.SaveChangesAsync();

public int Complete() => this.SaveChanges();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using OutOfSchool.Services.Models;

namespace OutOfSchool.Services.Repository.Files;

public class FileInDbRepository : EntityRepositoryBase<string, FileInDb>, IFileInDbRepository
{
public FileInDbRepository(OutOfSchoolDbContext dbContext)
: base(dbContext)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Google;
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using OutOfSchool.Services.Common.Exceptions;
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Models;

namespace OutOfSchool.Services.Repository.Files;

public abstract class FileInDbStorageBase<TFile> : IFilesStorage<TFile, string>
where TFile : FileModel, new()
{
private readonly IFileInDbRepository fileInDbRepository;

protected FileInDbStorageBase(
IFileInDbRepository fileInDbRepository)
{
this.fileInDbRepository = fileInDbRepository;
}

public async Task DeleteAsync(string fileId, CancellationToken cancellationToken = default)
{
var fileInDb = await fileInDbRepository.GetById(fileId).ConfigureAwait(false);

if (fileInDb != null)
{
await fileInDbRepository.Delete(fileInDb).ConfigureAwait(false);
}
}

public IAsyncEnumerable<Objects> GetBulkListsOfObjectsAsync(string prefix = null, ListObjectsOptions options = null)
{
throw new NotImplementedException();
}

public async Task<TFile> GetByIdAsync(string fileId, CancellationToken cancellationToken = default)
{
_ = fileId ?? throw new ArgumentNullException(nameof(fileId));

try
{
var fileInDb = await fileInDbRepository.GetById(fileId).ConfigureAwait(false);

var memoryStream = new MemoryStream();
memoryStream.Write(fileInDb.Data, 0, fileInDb.Data.Length);
memoryStream.Position = 0;
return new TFile { ContentStream = memoryStream, ContentType = fileInDb.ContentType };
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}

public async Task<string> UploadAsync(TFile file, CancellationToken cancellationToken = default)
{
_ = file ?? throw new ArgumentNullException(nameof(file));

var fileInDb = new FileInDb()
{
Id = GenerateFileId(),
ContentType = file.ContentType,
Data = ((MemoryStream)file.ContentStream).ToArray(),
};

await fileInDbRepository.Create(fileInDb).ConfigureAwait(false);

return fileInDb.Id;
}

protected virtual string GenerateFileId()
{
return Guid.NewGuid().ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using OutOfSchool.Services.Contexts;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Models.Images;

namespace OutOfSchool.Services.Repository.Files;

public class FileStatisticReportStorage : FileInDbStorageBase<FileModel>, IStatisticReportFileStorage
{
public FileStatisticReportStorage(IFileInDbRepository fileInDbRepository)
: base(fileInDbRepository)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using OutOfSchool.Services.Models;

namespace OutOfSchool.Services.Repository.Files;

public interface IFileInDbRepository : IEntityRepositoryBase<string, FileInDb>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using OutOfSchool.Services.Models;

namespace OutOfSchool.Services.Repository.Files;

public interface IStatisticReportFileStorage : IFilesStorage<FileModel, string>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using OutOfSchool.Services.Models;

namespace OutOfSchool.Services.Repository;

public interface IStatisticReportRepository : ISensitiveEntityRepository<StatisticReport>
{
public Task<List<StatisticReportCSV>> GetDataForReport();
}
Loading

0 comments on commit 3aff235

Please sign in to comment.