Skip to content

Commit

Permalink
multi site scan
Browse files Browse the repository at this point in the history
  • Loading branch information
koskedk committed May 18, 2020
1 parent a083714 commit 86a4419
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
dotnet-version: 3.1.201
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/tag-proj.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: DWAPI Bot Tag & Release

on:
push:
tags:
- 'v*'


jobs:
build:
name: Create Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal

- name: Pub .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
- name: Publish
run: dotnet publish --configuration Release -o dwapi-bot
- name: Zip Folder
run: zip -r dwapi-bot.zip dwapi-bot

- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false

- name: Upload Release Asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./dwapi-bot.zip
asset_name: dwapi-bot.zip
asset_content_type: application/zip
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public async Task<Result> Handle(ScanSubject request, CancellationToken cancella

while (page <= pageCount)
{
Log.Debug($"Scanning page {page}/{pageCount}...");
// Subjects
List<SubjectIndex> subjects;

Expand All @@ -66,8 +67,12 @@ public async Task<Result> Handle(ScanSubject request, CancellationToken cancella
subjects = await _repository.Read(page, request.Size);
}

int subIndex = 0;
var subjectsCount = subjects.Count;
foreach (var subject in subjects)
{
subIndex++;
Log.Debug($"Scanning page {page}/{pageCount} | Subject {subIndex} of {subjectsCount}...");
var scores = new List<SubjectIndexScore>();
// Block

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ScanSubject : IRequest<Result>
public int BlockSize { get; }
public SubjectField Field { get; }



public ScanSubject(SubjectField field = SubjectField.PKV, int size = 500, int blockSize = 500)
{
Level = ScanLevel.InterSite;
Expand Down
32 changes: 32 additions & 0 deletions src/Dwapi.Bot.Core/Domain/Indices/Dto/ScanDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using Dwapi.Bot.Core.Application.Matching.Commands;
using Dwapi.Bot.SharedKernel.Enums;

namespace Dwapi.Bot.Core.Domain.Indices.Dto
{
public class ScanDto
{
public bool AllSites { get; set; }
public int[] Sites { get; set; }

public List<ScanSubject> GenerateCommands(List<int> siteCodes)
{
var commands=new List<ScanSubject>();
if (AllSites)
{
foreach (var site in siteCodes)
{
commands.Add(new ScanSubject(site.ToString()));
}
}
else
{
foreach (var site in Sites)
{
commands.Add(new ScanSubject(site.ToString()));
}
}
return commands;
}
}
}
13 changes: 13 additions & 0 deletions src/Dwapi.Bot.Core/Domain/Indices/Dto/SubjectSiteDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Dwapi.Bot.Core.Domain.Indices.Dto
{
public class SubjectSiteDto
{
public int SiteCode { get; set; }
public string FacilityName { get; set; }

public override string ToString()
{
return $"{SiteCode},{FacilityName}";
}
}
}
2 changes: 2 additions & 0 deletions src/Dwapi.Bot.Core/Domain/Indices/ISubjectIndexRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Dwapi.Bot.Core.Domain.Indices.Dto;
using Dwapi.Bot.SharedKernel.Enums;
using Dwapi.Bot.SharedKernel.Interfaces.Data;

Expand All @@ -19,5 +20,6 @@ public interface ISubjectIndexRepository:IRepository<SubjectIndex,Guid>
Task CreateOrUpdate(IEnumerable<SubjectIndex> indices);
Task CreateOrUpdateScores(IEnumerable<SubjectIndexScore> scores);
Task CreateOrUpdateStages(IEnumerable<SubjectIndexStage> stages);
Task<IEnumerable<SubjectSiteDto>> GetSubjectSiteDtos();
}
}
9 changes: 9 additions & 0 deletions src/Dwapi.Bot.Infrastructure/Data/SubjectIndexRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Dapper;
using Dwapi.Bot.Core.Domain.Indices;
using Dwapi.Bot.Core.Domain.Indices.Dto;
using Dwapi.Bot.SharedKernel.Enums;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -140,5 +141,13 @@ public Task CreateOrUpdateStages(IEnumerable<SubjectIndexStage> stages)
{
return CreateOrUpdateAsync<SubjectIndexStage, Guid>(stages);
}

public async Task<IEnumerable<SubjectSiteDto>> GetSubjectSiteDtos()
{
var sql = $@"SELECT DISTINCT {nameof(SubjectSiteDto.SiteCode)},{nameof(SubjectSiteDto.FacilityName)}
FROM {nameof(BotContext.SubjectIndices)}";

return await GetConnection().QueryAsync<SubjectSiteDto>(sql);
}
}
}
100 changes: 100 additions & 0 deletions src/Dwapi.Bot/Controllers/WorkerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSharpFunctionalExtensions;
using Dwapi.Bot.Core.Application.Configs.Queries;
using Dwapi.Bot.Core.Application.Indices.Commands;
using Dwapi.Bot.Core.Application.Matching.Commands;
using Dwapi.Bot.Core.Domain.Configs;
using Dwapi.Bot.Core.Domain.Indices;
using Dwapi.Bot.Core.Domain.Indices.Dto;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Dwapi.Bot.Controllers
{
[Route("api/[controller]")]
public class WorkerController : Controller
{
private readonly IMediator _mediator;
private readonly ISubjectIndexRepository _repository;

public WorkerController(IMediator mediator, ISubjectIndexRepository repository)
{
_mediator = mediator;
_repository = repository;
}

[HttpPost("Refresh")]
public async Task<ActionResult> Get(RefreshIndex command)
{
if (command.BatchSize <= 0)
return BadRequest();

try
{
var results = await _mediator.Send(command);

if (results.IsSuccess)
return Ok("Refreshing...");

throw new Exception(results.Error);
}
catch (Exception e)
{
var msg = $"Error executing {nameof(RefreshIndex)}(s)";
Log.Error(e, msg);
return StatusCode(500, $"{msg} {e.Message}");
}
}

[HttpPost("Scan")]
public async Task<ActionResult> Get(ScanDto command)
{
var results = new List<Result>();
var siteCodes = new List<int>();

if (null==command)
return BadRequest();

try
{
if (command.AllSites)
{
var sites = await _repository.GetSubjectSiteDtos();
siteCodes.AddRange(sites.Select(x=>x.SiteCode).ToList());
}
else
{
siteCodes = command.Sites.ToList();
}

var commands = command.GenerateCommands(siteCodes);
foreach (var scanCommand in commands)
{
var result= await _mediator.Send(scanCommand);
results.Add(result);
}

if (results.Any(x=>!x.IsFailure))
return Ok("Scanning...");

var scb=new StringBuilder("Errors scanning:");
foreach (var result in results)
{
scb.AppendLine(result.Error);
}
throw new Exception(scb.ToString());
}
catch (Exception e)
{
var msg = $"Error executing {nameof(ScanSubject)}(s)";
Log.Error(e, msg);
return StatusCode(500, $"{msg} {e.Message}");
}
}
}
}
9 changes: 4 additions & 5 deletions src/Dwapi.Bot/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
{
"profiles": {
"Dwapi.Bot": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ public void should_Save_Stages()
Assert.True(data == 2);
}

[Test,Order(1)]
public void should_Get_SubjectSites()
{
var sites = _repository.GetSubjectSiteDtos().Result.ToList();
Assert.True((sites.Count > 0));
foreach (var site in sites)
Log.Debug($"{site}");
}

[Test,Order(99)]
public void should_Clear()
{
Expand Down

0 comments on commit 86a4419

Please sign in to comment.