BuildFeed/BuildFeed.Model/BuildRepository.cs

386 lines
16 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using BuildFeed.Model.View;
using MongoDB.Bson;
using MongoDB.Driver;
namespace BuildFeed.Model
{
2017-01-23 06:50:19 +08:00
public partial class BuildRepository
{
public const int CURRENT_LONG_TERM = (int)ProjectFamily.Redstone5;
public const int CURRENT_RELEASE = (int)ProjectFamily.Redstone5;
public const int CURRENT_XBOX = (int)ProjectFamily.Redstone5;
2017-01-23 06:50:19 +08:00
private const string BUILD_COLLECTION_NAME = "builds";
private static readonly BsonDocument sortByAddedDate = new BsonDocument(nameof(Build.Added), -1);
private static readonly BsonDocument sortByCompileDate = new BsonDocument(nameof(Build.BuildTime), -1);
private static readonly BsonDocument sortByLeakedDate = new BsonDocument(nameof(Build.LeakDate), -1);
private static readonly BsonDocument sortByOrder = new BsonDocument
{
new BsonElement(nameof(Build.MajorVersion), -1),
new BsonElement(nameof(Build.MinorVersion), -1),
new BsonElement(nameof(Build.Number), -1),
new BsonElement(nameof(Build.Revision), -1),
new BsonElement(nameof(Build.BuildTime), -1)
};
private readonly IMongoCollection<Build> _buildCollection;
2018-10-07 02:20:54 +08:00
public BuildRepository(MongoConfig config)
2017-01-23 06:50:19 +08:00
{
var settings = new MongoClientSettings
{
2018-10-07 02:20:54 +08:00
Server = new MongoServerAddress(config.Host, config.Port)
};
2017-01-23 06:50:19 +08:00
2018-10-07 02:20:54 +08:00
if (!string.IsNullOrEmpty(config.Username) && !string.IsNullOrEmpty(config.Password))
2017-01-23 06:50:19 +08:00
{
settings.Credential =
2018-10-07 02:20:54 +08:00
MongoCredential.CreateCredential(config.Database, config.Username, config.Password);
2017-01-23 06:50:19 +08:00
}
var dbClient = new MongoClient(settings);
2017-01-23 06:50:19 +08:00
2018-10-07 02:20:54 +08:00
IMongoDatabase buildDatabase = dbClient.GetDatabase(config.Database);
2017-01-23 06:50:19 +08:00
_buildCollection = buildDatabase.GetCollection<Build>(BUILD_COLLECTION_NAME);
}
public async void SetupIndexes() => await SetupIndexesAsync();
private async Task SetupIndexesAsync()
2017-01-23 06:50:19 +08:00
{
var indexes = await (await _buildCollection.Indexes.ListAsync()).ToListAsync();
2017-01-23 06:50:19 +08:00
if (indexes.All(i => i["name"] != "_idx_group"))
{
await
2018-10-27 05:41:11 +08:00
_buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Combine(
Builders<Build>.IndexKeys.Descending(b => b.MajorVersion),
2017-01-23 06:50:19 +08:00
Builders<Build>.IndexKeys.Descending(b => b.MinorVersion),
Builders<Build>.IndexKeys.Descending(b => b.Number),
Builders<Build>.IndexKeys.Descending(b => b.Revision)),
new CreateIndexOptions
{
Name = "_idx_group"
}));
2017-01-23 06:50:19 +08:00
}
if (indexes.All(i => i["name"] != "_idx_legacy"))
{
2018-10-27 05:41:11 +08:00
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.LegacyId),
2017-01-23 06:50:19 +08:00
new CreateIndexOptions
{
Name = "_idx_legacy"
}));
2017-01-23 06:50:19 +08:00
}
if (indexes.All(i => i["name"] != "_idx_lab"))
{
2018-10-27 05:41:11 +08:00
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.Lab),
2017-01-23 06:50:19 +08:00
new CreateIndexOptions
{
Name = "_idx_lab"
}));
2017-01-23 06:50:19 +08:00
}
if (indexes.All(i => i["name"] != "_idx_date"))
{
2018-10-27 05:41:11 +08:00
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Descending(b => b.BuildTime),
2017-01-23 06:50:19 +08:00
new CreateIndexOptions
{
Name = "_idx_date"
}));
2017-01-23 06:50:19 +08:00
}
if (indexes.All(i => i["name"] != "_idx_bstr"))
{
2018-10-27 05:41:11 +08:00
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.FullBuildString),
2017-01-23 06:50:19 +08:00
new CreateIndexOptions
{
Name = "_idx_bstr"
}));
2017-01-23 06:50:19 +08:00
}
if (indexes.All(i => i["name"] != "_idx_alt_bstr"))
{
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(
Builders<Build>.IndexKeys.Ascending(b => b.AlternateBuildString),
2017-01-23 06:50:19 +08:00
new CreateIndexOptions
{
Name = "_idx_alt_bstr"
}));
2017-01-23 06:50:19 +08:00
}
2017-05-17 06:49:21 +08:00
if (indexes.All(i => i["name"] != "_idx_source"))
{
2018-10-27 05:41:11 +08:00
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.SourceType),
2017-05-17 06:49:21 +08:00
new CreateIndexOptions
{
Name = "_idx_source"
}));
2017-05-17 06:49:21 +08:00
}
if (indexes.All(i => i["name"] != "_idx_family"))
{
2018-10-27 05:41:11 +08:00
await _buildCollection.Indexes.CreateOneAsync(new CreateIndexModel<Build>(Builders<Build>.IndexKeys.Ascending(b => b.Family),
2017-05-17 06:49:21 +08:00
new CreateIndexOptions
{
Name = "_idx_family"
}));
2017-05-17 06:49:21 +08:00
}
2017-01-23 06:50:19 +08:00
}
public async Task<IReadOnlyCollection<Build>> Select() => await _buildCollection.Find(new BsonDocument()).ToListAsync();
2017-01-23 06:50:19 +08:00
public async Task<Build> SelectById(Guid id)
=> await _buildCollection.Find(Builders<Build>.Filter.Eq(b => b.Id, id)).SingleOrDefaultAsync();
2017-01-23 06:50:19 +08:00
public async Task<Build> SelectByLegacyId(long id) => await _buildCollection
.Find(Builders<Build>.Filter.Eq(b => b.LegacyId, id))
.SingleOrDefaultAsync();
2017-01-23 06:50:19 +08:00
public async Task<IReadOnlyCollection<Build>> SelectBuildsByOrder(int limit = -1, int skip = 0)
2017-01-23 06:50:19 +08:00
{
var query = _buildCollection.Find(new BsonDocument()).Sort(sortByOrder).Skip(skip);
2017-01-23 06:50:19 +08:00
if (limit > 0)
{
2017-01-23 06:50:19 +08:00
query = query.Limit(limit);
}
2017-01-23 06:50:19 +08:00
return await query.ToListAsync();
}
public async Task<IReadOnlyDictionary<ProjectFamily, FrontPage>> SelectFrontPage()
2017-01-23 06:50:19 +08:00
{
var families = new Dictionary<ProjectFamily, FrontPage>();
2017-01-23 06:50:19 +08:00
2018-07-26 15:09:55 +08:00
var query = _buildCollection.Aggregate(new AggregateOptions
{
AllowDiskUse = true
})
.Match(new BsonDocument
2017-05-13 19:17:19 +08:00
{
2017-01-23 06:50:19 +08:00
{
"$or", new BsonArray
2017-01-23 06:50:19 +08:00
{
new BsonDocument
{
{
nameof(Build.Family), new BsonDocument
{
{ "$gte", CURRENT_RELEASE }
}
}
},
new BsonDocument
{
2018-10-27 05:41:11 +08:00
{ nameof(Build.Family), CURRENT_LONG_TERM }
}
2017-01-23 06:50:19 +08:00
}
}
})
.Group(new BsonDocument
2017-01-23 06:50:19 +08:00
{
{
"_id", new BsonDocument
{
{ nameof(Build.Family), $"${nameof(Build.Family)}" },
{ nameof(Build.LabUrl), $"${nameof(Build.LabUrl)}" },
{ nameof(Build.SourceType), $"${nameof(Build.SourceType)}" }
}
},
{
"items", new BsonDocument
2017-01-23 06:50:19 +08:00
{
{
"$push", new BsonDocument
{
{ nameof(Build.Id), "$_id" },
{ nameof(Build.MajorVersion), $"${nameof(Build.MajorVersion)}" },
{ nameof(Build.MinorVersion), $"${nameof(Build.MinorVersion)}" },
{ nameof(Build.Number), $"${nameof(Build.Number)}" },
{ nameof(Build.Revision), $"${nameof(Build.Revision)}" },
{ nameof(Build.Lab), $"${nameof(Build.Lab)}" },
{ nameof(Build.BuildTime), $"${nameof(Build.BuildTime)}" }
}
2017-01-23 06:50:19 +08:00
}
}
}
});
var dbResults = await query.ToListAsync();
2017-05-26 04:21:40 +08:00
var results = (from g in dbResults
select new
{
Key = new
{
Family = (ProjectFamily)g["_id"].AsBsonDocument[nameof(Build.Family)].AsInt32,
LabUrl = g["_id"].AsBsonDocument[nameof(Build.LabUrl)].AsString,
SourceType = (TypeOfSource)g["_id"].AsBsonDocument[nameof(Build.SourceType)].AsInt32
},
Items = from i in g["items"].AsBsonArray
2018-02-07 06:16:37 +08:00
select new FrontPageBuild
{
Id = i[nameof(Build.Id)].AsGuid,
MajorVersion = (uint)i[nameof(Build.MajorVersion)].AsInt32,
MinorVersion = (uint)i[nameof(Build.MinorVersion)].AsInt32,
Number = (uint)i[nameof(Build.Number)].AsInt32,
Revision = (uint?)i[nameof(Build.Revision)].AsNullableInt32,
Lab = i[nameof(Build.Lab)].AsString,
BuildTime = i[nameof(Build.BuildTime)].ToNullableUniversalTime()
}
}).ToArray();
IEnumerable<ProjectFamily> listOfFamilies =
results.GroupBy(g => g.Key.Family).Select(g => g.Key).OrderByDescending(k => k);
foreach (ProjectFamily family in listOfFamilies)
{
var fp = new FrontPage
2017-01-23 06:50:19 +08:00
{
2018-10-27 05:41:11 +08:00
CurrentCanary = results.Where(g => g.Key.Family == family
&& !g.Key.LabUrl.Contains("xbox")
&& !g.Key.LabUrl.Contains("analog"))
.SelectMany(g => g.Items)
.OrderByDescending(b => b.BuildTime)
.FirstOrDefault(),
CurrentInsider = results
2018-10-27 05:41:11 +08:00
.Where(g => g.Key.Family == family
&& !g.Key.LabUrl.Contains("xbox")
&& !g.Key.LabUrl.Contains("analog")
2018-10-27 05:41:11 +08:00
&& (g.Key.SourceType == TypeOfSource.PublicRelease
|| g.Key.SourceType == TypeOfSource.UpdateGDR))
.SelectMany(g => g.Items)
.OrderByDescending(b => b.BuildTime)
.FirstOrDefault(),
2017-05-26 04:21:40 +08:00
CurrentRelease = results
2018-10-27 05:41:11 +08:00
.Where(g => (int)g.Key.Family <= CURRENT_RELEASE
&& g.Key.Family == family
&& g.Key.LabUrl.Contains("_release")
&& !g.Key.LabUrl.Contains("xbox")
&& !g.Key.LabUrl.Contains("analog")
2018-10-27 05:41:11 +08:00
&& (g.Key.SourceType == TypeOfSource.PublicRelease
|| g.Key.SourceType == TypeOfSource.UpdateGDR))
.SelectMany(g => g.Items)
.OrderByDescending(b => b.BuildTime)
.FirstOrDefault(),
CurrentXbox =
results.Where(g
2018-10-27 05:41:11 +08:00
=> (int)g.Key.Family >= CURRENT_XBOX
&& g.Key.Family == family
&& g.Key.LabUrl.Contains("xbox"))
.SelectMany(g => g.Items)
.OrderByDescending(b => b.BuildTime)
.FirstOrDefault(),
CurrentAnalog =
results.Where(g
2018-10-27 05:41:11 +08:00
=> (int)g.Key.Family >= CURRENT_RELEASE
&& g.Key.Family == family
&& g.Key.LabUrl.Contains("analog"))
.SelectMany(g => g.Items)
.OrderByDescending(b => b.BuildTime)
.FirstOrDefault()
};
families.Add(family, fp);
}
2017-01-23 06:50:19 +08:00
return families;
2017-01-23 06:50:19 +08:00
}
public async Task<IReadOnlyCollection<Build>> SelectBuildsByStringSearch(string term, int limit = -1)
2017-01-23 06:50:19 +08:00
{
var query = _buildCollection.Aggregate()
.Match(b => b.FullBuildString != null)
.Match(b => b.FullBuildString != "")
.Match(b => b.FullBuildString.ToLower().Contains(term.ToLower()));
2017-01-23 06:50:19 +08:00
if (limit > 0)
{
query = query.Limit(limit);
}
2017-01-23 06:50:19 +08:00
return await query.ToListAsync();
}
public async Task<Build> SelectBuildByFullBuildString(string build) => await _buildCollection
.Find(Builders<Build>.Filter.Eq(b => b.FullBuildString, build))
.SingleOrDefaultAsync();
private async Task<IReadOnlyCollection<Build>> SelectBuildsBySortOption(BsonDocument sort, int limit, int skip)
2017-01-23 06:50:19 +08:00
{
var query = _buildCollection.Find(new BsonDocument()).Sort(sort).Skip(skip);
2017-01-23 06:50:19 +08:00
if (limit > 0)
{
2017-01-23 06:50:19 +08:00
query = query.Limit(limit);
}
2017-01-23 06:50:19 +08:00
return await query.ToListAsync();
}
public Task<IReadOnlyCollection<Build>> SelectBuildsByCompileDate(int limit = -1, int skip = 0)
=> SelectBuildsBySortOption(sortByCompileDate, limit, skip);
2017-01-23 06:50:19 +08:00
public Task<IReadOnlyCollection<Build>> SelectBuildsByAddedDate(int limit = -1, int skip = 0)
=> SelectBuildsBySortOption(sortByAddedDate, limit, skip);
2017-01-23 06:50:19 +08:00
public Task<IReadOnlyCollection<Build>> SelectBuildsByLeakedDate(int limit = -1, int skip = 0)
=> SelectBuildsBySortOption(sortByLeakedDate, limit, skip);
2017-01-23 06:50:19 +08:00
public async Task Insert(Build item)
{
item.Id = Guid.NewGuid();
2017-04-16 22:46:02 +08:00
item.RegenerateCachedProperties();
2017-01-23 06:50:19 +08:00
await _buildCollection.InsertOneAsync(item);
}
public async Task InsertAll(IEnumerable<Build> items)
{
var generatedItems = new List<Build>();
foreach (Build item in items)
{
item.Id = Guid.NewGuid();
2017-04-16 22:46:02 +08:00
item.RegenerateCachedProperties();
2017-01-23 06:50:19 +08:00
generatedItems.Add(item);
}
await _buildCollection.InsertManyAsync(generatedItems);
}
public async Task Update(Build item)
{
Build old = await SelectById(item.Id);
item.Added = old.Added;
item.Modified = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
2017-04-16 22:46:02 +08:00
item.RegenerateCachedProperties();
2017-01-23 06:50:19 +08:00
await _buildCollection.ReplaceOneAsync(Builders<Build>.Filter.Eq(b => b.Id, item.Id), item);
}
2017-08-28 19:18:10 +08:00
public async Task RegenerateCachedProperties()
{
var builds = await Select();
2017-08-28 19:18:10 +08:00
foreach (Build bd in builds)
{
bd.RegenerateCachedProperties();
await _buildCollection.ReplaceOneAsync(Builders<Build>.Filter.Eq(b => b.Id, bd.Id), bd);
}
}
2017-01-23 06:50:19 +08:00
public async Task DeleteById(Guid id)
{
await _buildCollection.DeleteOneAsync(Builders<Build>.Filter.Eq(b => b.Id, id));
}
}
}