Browse Source

Add station aliases to database

master
Kenneth Bruen 2 years ago
parent
commit
2465a90305
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 17
      server/Models/Database/StationAlias.cs
  2. 51
      server/Services/Implementations/Database.cs

17
server/Models/Database/StationAlias.cs

@ -0,0 +1,17 @@
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Server.Models.Database;
public record StationAlias(
[property: BsonId]
[property: BsonRepresentation(BsonType.ObjectId)]
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
string? Id,
string Name,
[property: BsonRepresentation(BsonType.ObjectId)]
string? ListingId
) {
public StationAlias() : this(null, "", null) { }
}

51
server/Services/Implementations/Database.cs

@ -33,9 +33,10 @@ public class Database : Server.Services.Interfaces.IDatabase {
"{ $addFields: { stoppedAtCount: { $size: \"$stoppedAtBy\" } } }", "{ $addFields: { stoppedAtCount: { $size: \"$stoppedAtBy\" } } }",
"{ $sort: { stoppedAtCount: -1 } }", "{ $sort: { stoppedAtCount: -1 } }",
"{ $unset: \"stoppedAtCount\" }" "{ $unset: \"stoppedAtCount\" }"
)) ))
.ToList(); .ToList();
public IReadOnlyList<TrainListing> Trains => trainListingsCollection.FindSync(_ => true).ToList(); public IReadOnlyList<TrainListing> Trains => trainListingsCollection.FindSync(_ => true).ToList();
public IReadOnlyList<StationAlias> StationAliases => stationAliasCollection.FindSync(_ => true).ToList();
private static readonly string DbDir = Environment.GetEnvironmentVariable("DB_DIR") ?? Path.Join(Environment.CurrentDirectory, "db"); private static readonly string DbDir = Environment.GetEnvironmentVariable("DB_DIR") ?? Path.Join(Environment.CurrentDirectory, "db");
private static readonly string DbFile = Path.Join(DbDir, "db.json"); private static readonly string DbFile = Path.Join(DbDir, "db.json");
@ -46,6 +47,7 @@ public class Database : Server.Services.Interfaces.IDatabase {
private readonly IMongoCollection<DbRecord> dbRecordCollection; private readonly IMongoCollection<DbRecord> dbRecordCollection;
private readonly IMongoCollection<TrainListing> trainListingsCollection; private readonly IMongoCollection<TrainListing> trainListingsCollection;
private readonly IMongoCollection<StationListing> stationListingsCollection; private readonly IMongoCollection<StationListing> stationListingsCollection;
private readonly IMongoCollection<StationAlias> stationAliasCollection;
private readonly AsyncThrottle throttle; private readonly AsyncThrottle throttle;
private readonly Dictionary<string, string> trainObjectIds = new(); private readonly Dictionary<string, string> trainObjectIds = new();
@ -62,8 +64,11 @@ public class Database : Server.Services.Interfaces.IDatabase {
dbRecordCollection = db.GetCollection<DbRecord>("db"); dbRecordCollection = db.GetCollection<DbRecord>("db");
trainListingsCollection = db.GetCollection<TrainListing>("trainListings"); trainListingsCollection = db.GetCollection<TrainListing>("trainListings");
stationListingsCollection = db.GetCollection<StationListing>("stationListings"); stationListingsCollection = db.GetCollection<StationListing>("stationListings");
stationAliasCollection = db.GetCollection<StationAlias>("stationAliases");
Migration(); Migration();
Task.Run(async () => await Initialize());
} }
private void Migration() { private void Migration() {
@ -170,6 +175,13 @@ public class Database : Server.Services.Interfaces.IDatabase {
} }
} }
private async Task Initialize() {
await foreach (var entry in await stationAliasCollection.FindAsync(_ => true)) {
if (entry?.ListingId is null) continue;
stationObjectIds.Add(entry.Name, entry.ListingId);
}
}
private readonly SemaphoreSlim insertTrainLock = new (1, 1); private readonly SemaphoreSlim insertTrainLock = new (1, 1);
public async Task<string> FoundTrain(string rank, string number, string company) { public async Task<string> FoundTrain(string rank, string number, string company) {
number = string.Join("", number.TakeWhile(c => c is >= '0' and <= '9')); number = string.Join("", number.TakeWhile(c => c is >= '0' and <= '9'));
@ -226,7 +238,16 @@ public class Database : Server.Services.Interfaces.IDatabase {
} }
); );
if (update.IsAcknowledged && update.ModifiedCount > 0) { if (update.IsAcknowledged && update.ModifiedCount > 0) {
stationObjectIds[name] = update.UpsertedId.AsObjectId.ToString(); var listingId = update.UpsertedId.AsObjectId.ToString();
stationObjectIds[name] = listingId;
await stationAliasCollection.UpdateOneAsync(
Builders<StationAlias>.Filter.Eq("name", name),
Builders<StationAlias>.Update.Combine(
Builders<StationAlias>.Update.SetOnInsert("name", name),
Builders<StationAlias>.Update.SetOnInsert("listingId", listingId)
),
new UpdateOptions { IsUpsert = true }
);
} }
} }
finally { finally {
@ -312,9 +333,9 @@ public class Database : Server.Services.Interfaces.IDatabase {
var trainNumber = await FoundTrain(trainData.Rank, trainData.Number, trainData.Operator); var trainNumber = await FoundTrain(trainData.Rank, trainData.Number, trainData.Operator);
await FoundTrainAtStations( await FoundTrainAtStations(
trainData.Groups trainData.Groups
.SelectMany(g => g.Stations) .SelectMany(g => g.Stations)
.Select(trainStop => trainStop.Name) .Select(trainStop => trainStop.Name)
.Distinct(), .Distinct(),
trainNumber trainNumber
); );
} }
@ -341,16 +362,16 @@ public class Database : Server.Services.Interfaces.IDatabase {
} }
} }
public async Task OnItineraries(IReadOnlyList<IItinerary> itineraries) { public async Task OnItineraries(IReadOnlyList<IItinerary> itineraries) {
foreach (var itinerary in itineraries) { foreach (var itinerary in itineraries) {
foreach (var train in itinerary.Trains) { foreach (var train in itinerary.Trains) {
await FoundTrainAtStations( await FoundTrainAtStations(
train.IntermediateStops.Concat(new[] { train.From, train.To }), train.IntermediateStops.Concat(new[] { train.From, train.To }),
train.TrainNumber train.TrainNumber
); );
} }
} }
} }
} }
public record DbRecord( public record DbRecord(

Loading…
Cancel
Save