2026-06-03 01:43:55 +03:30
|
|
|
using JobsMedical.Web.Data;
|
|
|
|
|
using JobsMedical.Web.Models;
|
2026-06-03 08:18:19 +03:30
|
|
|
using JobsMedical.Web.Services.Scraping;
|
2026-06-03 01:43:55 +03:30
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace JobsMedical.Web.Pages.Admin;
|
|
|
|
|
|
2026-06-03 08:18:19 +03:30
|
|
|
[Authorize(Roles = "Admin")]
|
2026-06-03 01:43:55 +03:30
|
|
|
public class IndexModel : PageModel
|
|
|
|
|
{
|
|
|
|
|
private readonly AppDbContext _db;
|
2026-06-03 08:18:19 +03:30
|
|
|
private readonly IngestionService _ingest;
|
|
|
|
|
|
|
|
|
|
public IndexModel(AppDbContext db, IngestionService ingest)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
_ingest = ingest;
|
|
|
|
|
}
|
2026-06-03 01:43:55 +03:30
|
|
|
|
|
|
|
|
public List<RawListing> Queue { get; private set; } = new();
|
2026-06-03 08:18:19 +03:30
|
|
|
public List<RawListing> Flagged { get; private set; } = new();
|
2026-06-04 00:44:11 +03:30
|
|
|
public IReadOnlyList<string> SourceNames { get; private set; } = new List<string>();
|
2026-06-03 01:43:55 +03:30
|
|
|
public int PublishedShifts { get; private set; }
|
|
|
|
|
public int PublishedJobs { get; private set; }
|
2026-06-08 06:23:58 +03:30
|
|
|
public List<IngestionRun> Runs { get; private set; } = new();
|
2026-06-03 01:43:55 +03:30
|
|
|
|
|
|
|
|
[BindProperty] public string? SourceChannel { get; set; }
|
|
|
|
|
[BindProperty] public string? RawText { get; set; }
|
|
|
|
|
|
2026-06-03 08:18:19 +03:30
|
|
|
[TempData] public string? IngestMessage { get; set; }
|
|
|
|
|
|
2026-06-03 01:43:55 +03:30
|
|
|
public async Task OnGetAsync() => await LoadAsync();
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> OnPostAddAsync()
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(RawText))
|
|
|
|
|
{
|
|
|
|
|
_db.RawListings.Add(new RawListing
|
|
|
|
|
{
|
|
|
|
|
SourceChannel = string.IsNullOrWhiteSpace(SourceChannel) ? "ورود دستی" : SourceChannel.Trim(),
|
|
|
|
|
RawText = RawText.Trim(),
|
|
|
|
|
Status = RawListingStatus.New,
|
|
|
|
|
});
|
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
return RedirectToPage();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 06:41:17 +03:30
|
|
|
/// <summary>Fast triage — reject (discard) a queued/flagged item without opening the review page.</summary>
|
|
|
|
|
public async Task<IActionResult> OnPostQuickDiscardAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
var raw = await _db.RawListings.FirstOrDefaultAsync(r => r.Id == id);
|
|
|
|
|
if (raw is not null) { raw.Status = RawListingStatus.Discarded; await _db.SaveChangesAsync(); }
|
|
|
|
|
return RedirectToPage();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 08:18:19 +03:30
|
|
|
public async Task<IActionResult> OnPostRunIngestionAsync()
|
|
|
|
|
{
|
|
|
|
|
var s = await _ingest.RunAsync();
|
|
|
|
|
IngestMessage = $"جمعآوری انجام شد — {s.TotalQueued} در صف، {s.TotalFlagged} پرچمخورده، " +
|
|
|
|
|
$"{s.TotalSpam} اسپم، {s.TotalDuplicates} تکراری.";
|
|
|
|
|
return RedirectToPage();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 21:38:55 +03:30
|
|
|
/// <summary>
|
|
|
|
|
/// DESTRUCTIVE rebuild, in two distinct deletes:
|
|
|
|
|
/// 1. The DEDUPE CACHE — ALL RawListings, including any added via «افزودن دستی». These are not
|
|
|
|
|
/// published content; they're the crawl/staging rows whose ContentHash blocks re-ingesting
|
|
|
|
|
/// the same ad. Wiping them lets everything be re-fetched and re-judged by the AI.
|
|
|
|
|
/// 2. AGGREGATED listings only — Shifts/JobOpenings/TalentListings with Source==Aggregated, i.e.
|
|
|
|
|
/// produced by ingestion. Employer/admin-posted listings (Source==Direct) are left untouched.
|
|
|
|
|
/// Then re-fetch everything and re-run it through the (now AI-enabled) pipeline.
|
|
|
|
|
/// RawListings are deleted first so their LinkedShift/LinkedTalent FKs (SetNull) don't dangle;
|
|
|
|
|
/// DB cascade clears ContactMethods / Applications / InterestEvents when the posts are deleted.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<IActionResult> OnPostPurgeAndReingestAsync()
|
|
|
|
|
{
|
|
|
|
|
int rawCount, shifts, jobs, talent;
|
|
|
|
|
await using (var tx = await _db.Database.BeginTransactionAsync())
|
|
|
|
|
{
|
|
|
|
|
rawCount = await _db.RawListings.ExecuteDeleteAsync(); // clear dedupe cache
|
|
|
|
|
shifts = await _db.Shifts.Where(s => s.Source == ShiftSource.Aggregated).ExecuteDeleteAsync();
|
|
|
|
|
jobs = await _db.JobOpenings.Where(j => j.Source == ShiftSource.Aggregated).ExecuteDeleteAsync();
|
|
|
|
|
talent = await _db.TalentListings.Where(t => t.Source == ShiftSource.Aggregated).ExecuteDeleteAsync();
|
|
|
|
|
await tx.CommitAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var s = await _ingest.RunAsync(); // fresh fetch → AI audit → publish/queue
|
|
|
|
|
IngestMessage = $"پاکسازی شد (حذف: {rawCount} آیتم کش، {shifts} شیفت، {jobs} استخدام، {talent} آمادهبهکارِ جمعآوریشده). " +
|
|
|
|
|
$"جمعآوری مجدد: {s.TotalPublished} منتشر، {s.TotalQueued} در صف، {s.TotalFlagged} پرچم، {s.TotalSpam} اسپم، {s.TotalDuplicates} تکراری.";
|
|
|
|
|
return RedirectToPage();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 01:43:55 +03:30
|
|
|
private async Task LoadAsync()
|
|
|
|
|
{
|
|
|
|
|
Queue = await _db.RawListings
|
|
|
|
|
.Where(r => r.Status == RawListingStatus.New)
|
2026-06-03 08:18:19 +03:30
|
|
|
.OrderByDescending(r => r.Confidence).ThenByDescending(r => r.FetchedAt).ToListAsync();
|
|
|
|
|
Flagged = await _db.RawListings
|
|
|
|
|
.Where(r => r.Status == RawListingStatus.Flagged)
|
2026-06-03 01:43:55 +03:30
|
|
|
.OrderByDescending(r => r.FetchedAt).ToListAsync();
|
2026-06-04 00:44:11 +03:30
|
|
|
SourceNames = _ingest.SourceNames;
|
2026-06-03 01:43:55 +03:30
|
|
|
PublishedShifts = await _db.Shifts.CountAsync(s => s.Source != ShiftSource.Direct);
|
|
|
|
|
PublishedJobs = await _db.JobOpenings.CountAsync();
|
2026-06-08 06:23:58 +03:30
|
|
|
Runs = await _db.IngestionRuns.OrderByDescending(r => r.RunAt).Take(15).ToListAsync();
|
2026-06-03 01:43:55 +03:30
|
|
|
}
|
|
|
|
|
}
|