REORG+histo

This commit is contained in:
2021-09-05 15:44:47 +01:00
parent 459f8ea422
commit 6dd76ac1a5
28 changed files with 948 additions and 159 deletions

View File

@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using isnd.Controllers;
using isnd.Data;
using isnd.Data.Catalog;
using isnd.Entities;
using isnd.Helpers;
using isnd.Interfaces;
using isnd.ViewModels;
using Microsoft.EntityFrameworkCore;
@ -68,6 +72,15 @@ namespace isnd.Services
type = "SearchQueryService/3.5.0",
comment = "Search Query service"
});
if (defaultActivation || unleashClient.IsEnabled("pkg-catalog", false))
res.Add(
new Resource
{
id = extUrl + ApiConfig.Catalog,
type = "Catalog/3.0.0",
comment = "Package Catalog Index"
});
return res;
}
@ -80,7 +93,7 @@ namespace isnd.Services
var scope = dbContext.Packages
.Include(p => p.Versions)
.Where(
p => (CamelCaseMatch(p.Id, query) || SeparatedByMinusMatch(p.Id, query))
p => (PackageIdHelpers.CamelCaseMatch(p.Id, query) || PackageIdHelpers.SeparatedByMinusMatch(p.Id, query))
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
);
@ -146,15 +159,17 @@ namespace isnd.Services
}
return CurrentCatalogIndex;
}
public void ÛpdateCatalogFor(Commit last = null)
public void ÛpdateCatalogFor(Commit reason = null)
{
int i = 0;
int p = 0;
var oldIndex = CurrentCatalogIndex;
var oldPages = CurrentCatalogPages;
string baseid= extUrl + ApiConfig.Catalog;
string basepageid= extUrl + ApiConfig.CatalogPage;
CurrentCatalogIndex = new CatalogIndex
{
Id = extUrl,
Id = baseid,
Items = new List<PageRef>()
};
CurrentCatalogPages = new List<Page>();
@ -170,16 +185,18 @@ namespace isnd.Services
{
page = new Page
{
Parent = isndSettings.ExternalUrl + "/package",
Id = basepageid + "-" + p++,
Parent = baseid,
CommitId = commit.CommitId,
CommitTimeStamp = commit.CommitTimeStamp,
Id = this.isndSettings.ExternalUrl + "/package/index-" + p++,
Items = new List<PackageRef>()
};
CurrentCatalogPages.Add(page);
pageRef = new PageRef
{
Id = page.Id
Id = page.Id,
CommitId = commit.CommitId,
CommitTimeStamp = commit.CommitTimeStamp
};
CurrentCatalogIndex.Items.Add(pageRef);
i = 0;
@ -197,54 +214,61 @@ namespace isnd.Services
Where (cv => cv.CommitId == commit.CommitId)
.OrderByDescending(vc => vc.CommitNId).First();
StringBuilder refid = new StringBuilder(extUrl);
refid.AppendFormat("{0}/{1}/{2}",ApiConfig.CatalogLeaf, v.PackageId
, v.FullString);
if (v.Type!=null)
refid.AppendFormat("/{0}",v.Type);
var pkgref = new PackageRef
{
Version = v.FullString,
LastCommit = v.LatestCommit,
CommitId = v.LatestCommit.CommitId,
CommitTimeStamp = v.LatestCommit.CommitTimeStamp,
RefId = isndSettings.ExternalUrl + v.NugetLink,
Id = v.PackageId
RefId = refid.ToString(),
Id = v.PackageId,
RefType = v.LatestCommit.Action == PackageAction.PublishPackage
? "nuget:PackageDetails" :
"nuget:PackageDelete"
};
page.Items.Add(pkgref);
}
last = commit;
reason = commit;
i++;
}
if (last != null)
if (reason != null)
{
CurrentCatalogIndex.CommitId = last.CommitId;
CurrentCatalogIndex.CommitId = reason.CommitId;
}
else
else
{
// From a fresh db
CurrentCatalogIndex.CommitId = "none";
}
}
protected static bool CamelCaseMatch(string id, string query)
public async Task<PackageDeletionReport> DeletePackageAsync(string pkgId, string fullVersion, string pkgType)
{
// Assert.False (q==null);
if (string.IsNullOrEmpty(query)) return true;
while (id.Length > 0)
// TODO package deletion on disk
var commit = new Commit{
Action = PackageAction.DeletePackage,
TimeStamp = DateTime.Now
};
var pkg = await dbContext.PackageVersions.SingleOrDefaultAsync(
v => v.PackageId == pkgId &&
v.FullString == fullVersion &&
v.Type == pkgType
);
if (pkg == null)
{
int i = 0;
while (id.Length > i && char.IsLower(id[i])) i++;
if (i == 0) break;
id = id.Substring(i);
if (id.StartsWith(query, System.StringComparison.OrdinalIgnoreCase)) return true;
return new PackageDeletionReport{ Deleted = false };
}
return false;
dbContext.PackageVersions.Remove(pkg);
await dbContext.SaveChangesAsync();
ÛpdateCatalogFor(commit);
return new PackageDeletionReport{ Deleted = true };
}
protected static bool SeparatedByMinusMatch(string id, string q)
{
foreach (var part in id.Split('-'))
{
if (part.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
}
return false;
}
}
}