isn/src/isnd/Services/PackageManager.cs

288 lines
9.5 KiB
C#
Raw Normal View History

2021-08-23 03:21:08 +01:00
using System;
2021-07-05 12:55:52 +01:00
using System.Collections.Generic;
using System.Linq;
2021-08-15 19:09:01 +01:00
using isnd.Controllers;
using isnd.Data;
2021-08-23 03:21:08 +01:00
using isnd.Data.Catalog;
using isnd.Entities;
using isnd.Interfaces;
2021-08-11 23:25:15 +01:00
using isnd.ViewModels;
2021-07-05 12:55:52 +01:00
using Microsoft.EntityFrameworkCore;
2021-08-23 03:21:08 +01:00
using Microsoft.Extensions.Options;
2021-07-05 12:55:52 +01:00
using NuGet.Versioning;
using Unleash;
namespace isnd.Services
{
2021-08-28 18:41:22 +01:00
2021-08-23 03:21:08 +01:00
public class PackageManager : IPackageManager
2021-07-05 12:55:52 +01:00
{
ApplicationDbContext dbContext;
2021-08-28 18:41:22 +01:00
public PackageManager(ApplicationDbContext dbContext,
2021-08-27 21:22:34 +01:00
IOptions<IsndSettings> siteConfigOptionsOptions)
2021-07-05 12:55:52 +01:00
{
this.dbContext = dbContext;
2021-08-23 03:21:08 +01:00
CurrentCatalogIndex = GetCatalogIndex();
2021-08-27 21:22:34 +01:00
pmConfigOptions = siteConfigOptionsOptions.Value;
extApiUrl = pmConfigOptions.ExternalUrl + "/package";
2021-07-05 12:55:52 +01:00
}
2021-08-23 03:21:08 +01:00
public PackageIndexViewModel SearchByName(string query,
int skip, int take, bool prerelease = false,
2021-07-05 12:55:52 +01:00
string packageType = null)
{
2021-08-10 00:10:26 +01:00
2021-07-05 12:55:52 +01:00
var scope = dbContext.Packages
.Include(p => p.Versions)
.Where(
p => (CamelCaseMatch(p.Id, query) || SeparatedByMinusMatch(p.Id, query))
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
);
2021-08-10 00:10:26 +01:00
var total = scope.Count();
var pkgs = scope.Skip(skip).Take(take).ToArray();
2021-08-11 23:25:15 +01:00
return new PackageIndexViewModel
2021-08-23 03:21:08 +01:00
{
query = query,
totalHits = total,
data = pkgs
};
}
const int maxPageLen = 512;
public CatalogIndex GenerateCatalogIndex(string commitId)
{
var catalog = new CatalogIndex
{
CommitId = commitId,
CommitTimeStamp = DateTime.Now
};
var scope = dbContext.Packages.Where(p => p.Public)
.OrderBy(p => p.CommitTimeStamp);
catalog.Items = new List<PageRef>();
int pagecount = (int)(scope.Count() / maxPageLen);
for (int pagelen = 0, pagenum = 0; pagelen < pagecount; pagenum++)
{
Page p = new Page
2021-07-05 12:55:52 +01:00
{
2021-08-23 03:21:08 +01:00
2021-07-05 12:55:52 +01:00
};
2021-08-23 03:21:08 +01:00
}
dbContext.Packages.Where(p => p.Public)
.OrderBy(p => p.CommitTimeStamp);
return catalog;
2021-07-05 12:55:52 +01:00
}
2021-08-23 03:21:08 +01:00
public Page CatalogPage()
{
var scope = dbContext.Packages
.Where(P => P.Versions.Count > 0)
.Select(
p => new PackageRef { Id = p.Id, Version = p.Versions.Max().FullString });
return new Page
{
Items = scope.ToList()
};
}
public void PublishCatalog()
{
}
public AutoCompleteResult AutoComplete(string id,
int skip, int take, bool prerelease = false,
2021-07-05 12:55:52 +01:00
string packageType = null)
{
var scope = dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
)
.OrderBy(v => v.FullString);
return new AutoCompleteResult
2021-08-23 03:21:08 +01:00
{
totalHits = scope.Count(),
data = scope.Select(v => v.FullString)
2021-07-05 12:55:52 +01:00
.Skip(skip).Take(take).ToArray()
2021-08-23 03:21:08 +01:00
};
2021-07-05 12:55:52 +01:00
}
// TODO stocker MetaData plutôt que FullString en base,
// et en profiter pour corriger ce listing
public string[] GetVersions(
string id,
NuGetVersion parsedVersion,
bool prerelease = false,
string packageType = null,
int skip = 0,
int take = 25)
{
return dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
&& (parsedVersion.CompareTo(new SemanticVersion(v.Major, v.Minor, v.Patch)) < 0)
)
.OrderBy(v => v.FullString)
.Select(v => v.FullString)
.Skip(skip).Take(take).ToArray();
}
2021-08-23 03:21:08 +01:00
public static CatalogIndex CurrentCatalogIndex { get; protected set; }
2021-08-27 21:22:34 +01:00
public static List<Page> CurrentCatalogPages { get; protected set; }
2021-08-23 03:21:08 +01:00
private IsndSettings pmConfigOptions;
2021-08-27 21:22:34 +01:00
private string extApiUrl;
2021-08-23 03:21:08 +01:00
public virtual CatalogIndex GetCatalogIndex()
{
if (CurrentCatalogIndex == null)
{
LoadCatalogFromDb();
}
return CurrentCatalogIndex;
}
void LoadCatalogFromDb()
{
2021-08-28 18:41:22 +01:00
int i = 0;
int p = 0;
2021-08-27 21:22:34 +01:00
var oldIndex = CurrentCatalogIndex;
var oldPages = CurrentCatalogPages;
CurrentCatalogIndex = new CatalogIndex
{
2021-08-28 18:41:22 +01:00
2021-08-27 21:22:34 +01:00
};
CurrentCatalogPages = new List<Page>();
var scope = dbContext.Commits.OrderBy(c => c.TimeStamp);
Commit last = null;
PageRef pageRef = null;
Page page = null;
2021-08-28 18:41:22 +01:00
i = pmConfigOptions.CatalogPageLen;
2021-08-27 21:22:34 +01:00
foreach (var commit in scope)
{
if (i >= this.pmConfigOptions.CatalogPageLen)
{
2021-08-28 18:41:22 +01:00
page = new Page
{
Parent = pmConfigOptions.ExternalUrl + "/package",
2021-08-27 21:22:34 +01:00
CommitId = commit.CommitId,
CommitTimeStamp = commit.CommitTimeStamp,
Id = this.pmConfigOptions.ExternalUrl + "/package/index-" + p++
};
CurrentCatalogPages.Add(page);
2021-08-28 18:41:22 +01:00
pageRef = new PageRef
{
Id = page.Id
2021-08-27 21:22:34 +01:00
};
CurrentCatalogIndex.Items.Add(pageRef);
i = 0;
}
2021-08-28 18:41:22 +01:00
var validPkgs = dbContext.Packages
.Include(pkg => pkg.Versions)
.Where(
pkg => pkg.Versions.Count() > 0
);
// pkg.Versions.OrderByDescending(vi => vi.CommitNId).First().FullString
foreach (var pkg in validPkgs)
2021-08-27 21:22:34 +01:00
{
2021-08-28 18:41:22 +01:00
var v = pkg.Versions.OrderByDescending(vc => vc.CommitNId).First();
var pkgref = new PackageRef
{
Version = v.FullString,
LastCommit = v.LatestCommit,
CommitId = v.LatestCommit.CommitId,
CommitTimeStamp = v.LatestCommit.CommitTimeStamp,
RefId = page.Id,
Id = v.PackageId
};
page.Items.Add(pkgref);
}
2021-08-27 21:22:34 +01:00
last = commit;
i++;
}
2021-08-28 18:41:22 +01:00
if (last != null)
2021-08-27 21:22:34 +01:00
{
CurrentCatalogIndex.CommitId = last.CommitId;
}
2021-08-28 18:41:22 +01:00
else
{
CurrentCatalogIndex.CommitId = "none";
}
2021-08-23 03:21:08 +01:00
}
protected static bool CamelCaseMatch(string id, string query)
2021-07-05 12:55:52 +01:00
{
// Assert.False (q==null);
2021-08-23 03:21:08 +01:00
if (string.IsNullOrEmpty(query)) return true;
2021-07-05 12:55:52 +01:00
while (id.Length > 0)
{
int i = 0;
while (id.Length > i && char.IsLower(id[i])) i++;
if (i == 0) break;
id = id.Substring(i);
2021-08-23 03:21:08 +01:00
if (id.StartsWith(query, System.StringComparison.OrdinalIgnoreCase)) return true;
2021-07-05 12:55:52 +01:00
}
return false;
}
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;
}
2021-08-23 03:21:08 +01:00
public IEnumerable<Resource> GetResources(IUnleash unleashClient)
2021-07-05 12:55:52 +01:00
{
var res = new List<Resource>();
2021-08-27 21:22:34 +01:00
if (unleashClient.IsEnabled("pkg-push", true))
2021-07-05 12:55:52 +01:00
res.Add(
new Resource
{
2021-08-27 21:22:34 +01:00
id = extApiUrl,
2021-07-05 12:55:52 +01:00
type = "PackagePublish/2.0.0",
comment = "Package Publish service"
});
2021-08-27 21:22:34 +01:00
if (unleashClient.IsEnabled("pkg-get", false))
2021-07-05 12:55:52 +01:00
res.Add(
new Resource
{
2021-08-27 21:22:34 +01:00
id = extApiUrl,
2021-07-05 12:55:52 +01:00
type = "PackageBaseAddress/3.0.0",
comment = "Package Base Address service"
});
2021-08-27 21:22:34 +01:00
if (unleashClient.IsEnabled("pkg-autocomplete", false))
2021-07-05 12:55:52 +01:00
res.Add(
new Resource
{
2021-08-27 21:22:34 +01:00
id = extApiUrl,
2021-07-05 12:55:52 +01:00
type = "SearchAutocompleteService/3.5.0",
comment = "Auto complete service"
});
2021-08-27 21:22:34 +01:00
if (unleashClient.IsEnabled("pkg-search", false))
2021-07-05 12:55:52 +01:00
res.Add(
new Resource
{
2021-08-27 21:22:34 +01:00
id = extApiUrl,
2021-07-05 12:55:52 +01:00
type = "SearchQueryService/3.5.0",
comment = "Search Query service"
});
return res;
}
}
}