Files
isn/src/isnd/Services/PackageManager.cs

247 lines
8.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using isnd.Controllers;
using isnd.Data;
using isnd.Data.Catalog;
using isnd.Entities;
using isnd.Interfaces;
using isnd.ViewModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using NuGet.Versioning;
using Unleash;
namespace isnd.Services
{
public class PackageManager : IPackageManager
{
ApplicationDbContext dbContext;
public PackageManager(ApplicationDbContext dbContext,
IOptions<IsndSettings> siteConfigOptionsOptions)
{
this.dbContext = dbContext;
isndSettings = siteConfigOptionsOptions.Value;
extApiUrl = isndSettings.ExternalUrl + "/package";
CurrentCatalogIndex = GetCatalogIndex();
}
public PackageIndexViewModel SearchByName(string query,
int skip, int take, bool prerelease = false,
string packageType = null)
{
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))
);
var total = scope.Count();
var pkgs = scope.Skip(skip).Take(take).ToArray();
return new PackageIndexViewModel
{
query = query,
totalHits = total,
data = pkgs
};
}
const int maxPageLen = 512;
public AutoCompleteResult AutoComplete(string id,
int skip, int take, bool prerelease = false,
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
{
totalHits = scope.Count(),
data = scope.Select(v => v.FullString)
.Skip(skip).Take(take).ToArray()
};
}
// 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();
}
public static CatalogIndex CurrentCatalogIndex { get; protected set; }
public static List<Page> CurrentCatalogPages { get; protected set; }
private IsndSettings isndSettings;
private string extApiUrl;
public virtual CatalogIndex GetCatalogIndex()
{
if (CurrentCatalogIndex == null)
{
ÛpdateCatalogFor();
}
return CurrentCatalogIndex;
}
public void ÛpdateCatalogFor(Commit last = null)
{
int i = 0;
int p = 0;
var oldIndex = CurrentCatalogIndex;
var oldPages = CurrentCatalogPages;
CurrentCatalogIndex = new CatalogIndex
{
Id = extApiUrl,
Items = new List<PageRef>()
};
CurrentCatalogPages = new List<Page>();
var scope = dbContext.Commits.OrderBy(c => c.TimeStamp);
PageRef pageRef = null;
Page page = null;
i = isndSettings.CatalogPageLen;
foreach (var commit in scope)
{
if (i >= this.isndSettings.CatalogPageLen)
{
page = new Page
{
Parent = isndSettings.ExternalUrl + "/package",
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
};
CurrentCatalogIndex.Items.Add(pageRef);
i = 0;
}
var validPkgs = dbContext.Packages
.Include(pkg => pkg.Versions)
.Include(pkg => pkg.LatestVersion)
.Where(
pkg => pkg.Versions.Count() > 0 && pkg.CommitId == commit.CommitId
);
// pkg.Versions.OrderByDescending(vi => vi.CommitNId).First().FullString
foreach (var pkg in validPkgs)
{
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 = isndSettings.ExternalUrl + v.NugetLink,
Id = v.PackageId
};
page.Items.Add(pkgref);
}
last = commit;
i++;
}
if (last != null)
{
CurrentCatalogIndex.CommitId = last.CommitId;
}
else
{
CurrentCatalogIndex.CommitId = "none";
}
}
protected static bool CamelCaseMatch(string id, string query)
{
// Assert.False (q==null);
if (string.IsNullOrEmpty(query)) return true;
while (id.Length > 0)
{
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 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;
}
public IEnumerable<Resource> GetResources(IUnleash unleashClient)
{
var res = new List<Resource>();
if (unleashClient.IsEnabled("pkg-push", true))
res.Add(
new Resource
{
id = extApiUrl,
type = "PackagePublish/2.0.0",
comment = "Package Publish service"
});
if (unleashClient.IsEnabled("pkg-get", false))
res.Add(
new Resource
{
id = extApiUrl,
type = "PackageBaseAddress/3.0.0",
comment = "Package Base Address service"
});
if (unleashClient.IsEnabled("pkg-autocomplete", false))
res.Add(
new Resource
{
id = extApiUrl,
type = "SearchAutocompleteService/3.5.0",
comment = "Auto complete service"
});
if (unleashClient.IsEnabled("pkg-search", false))
res.Add(
new Resource
{
id = extApiUrl,
type = "SearchQueryService/3.5.0",
comment = "Search Query service"
});
return res;
}
}
}