Performance improvements
All checks were successful
release / Build and push Docker image (push) Successful in 1m11s

This commit is contained in:
2025-01-04 23:06:13 +03:30
parent 59410c1458
commit 787a9dd09b
3 changed files with 20 additions and 9 deletions

View File

@@ -18,10 +18,15 @@ public class HandleStartMessage
(callbackQuery != null ? "Callback" : "Start command")); (callbackQuery != null ? "Callback" : "Start command"));
// choose all media attachments that are approved // choose all media attachments that are approved
var mediaAttachmentsToSelect = await _db.AsQueryable()
.Where(post => post.MediaAttachments.Any(media => media.Approved)) // OLD QUERY
.ToListAsync(); // var mediaAttachmentsToSelect = await _db.AsQueryable()
// .Where(post => post.MediaAttachments.Any(media => media.Approved))
// .ToListAsync();
var filter = Builders<Post>.Filter.ElemMatch(post => post.MediaAttachments, Builders<MediaAttachment>.Filter.Eq(media => media.Approved, true));
var mediaAttachmentsToSelect = await _db.Find(filter).ToListAsync();
// select random approved media attachment // select random approved media attachment
var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)]; var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)];
// send media attachment // send media attachment

View File

@@ -37,6 +37,7 @@ public class ProcessPosts
$"<a href=\"" + post.Url + "\"> Mastodon </a>", ParseMode.Html $"<a href=\"" + post.Url + "\"> Mastodon </a>", ParseMode.Html
, replyMarkup: new InlineKeyboardMarkup().AddButton("Approve", $"approve-{media.MediaId}").AddButton("Reject", $"reject-{media.MediaId}")); , replyMarkup: new InlineKeyboardMarkup().AddButton("Approve", $"approve-{media.MediaId}").AddButton("Reject", $"reject-{media.MediaId}"));
validPosts.Add(post); validPosts.Add(post);
newPosts++;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -44,7 +45,7 @@ public class ProcessPosts
media.PreviewUrl); media.PreviewUrl);
} }
newPosts++;
} }
// Insert post // Insert post

View File

@@ -1,10 +1,10 @@
using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using MongoDB.Driver; using MongoDB.Driver;
using MongoDB.Driver.Linq;
using mstdnCats.Models; using mstdnCats.Models;
namespace mstdnCats.Services; namespace mstdnCats.Services;
@@ -47,11 +47,16 @@ public class ServerStartup
endpoints.MapGet("/api/gimme", async context => endpoints.MapGet("/api/gimme", async context =>
{ {
// Api endpoint // Api endpoint
// Choose all media attachments that are approved // Measure execution time
var mediaAttachmentsToSelect = await _db.AsQueryable() var stopwatch = Stopwatch.StartNew();
.Where(post => post.MediaAttachments.Any(media => media.Approved))
.ToListAsync();
// Choose all posts media attachments that are approved
var filter = Builders<Post>.Filter.ElemMatch(post => post.MediaAttachments, Builders<MediaAttachment>.Filter.Eq(media => media.Approved, true));
var mediaAttachmentsToSelect = await _db.Find(filter).ToListAsync();
// Stop and print execution time
stopwatch.Stop();
Console.WriteLine($"Query executed in: {stopwatch.ElapsedMilliseconds} ms");
// Select random approved media attachment // Select random approved media attachment
var selectedPost = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)]; var selectedPost = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)];