Ready to test pulling random assets

This commit is contained in:
2024-11-23 11:53:11 -07:00
parent 2391462500
commit c8325aafca
16 changed files with 270 additions and 232 deletions

View File

@ -1,5 +1,5 @@
using ImmichToSlideshow.Models.Immich;
using ImmichToSlideshow.Models;
using ImmichToSlideshow.Models.Immich;
using Npgsql;
using System.Collections.ObjectModel;
using System.Data;
@ -8,17 +8,12 @@ using System.Text.Json;
namespace ImmichToSlideshow.Services;
public class AssetService
public class AssetService(AppSettings appSettings)
{
private readonly AppSettings _AppSettings;
private readonly AppSettings _AppSettings = appSettings;
public AssetService(AppSettings appSettings) =>
_AppSettings = appSettings;
private static readonly List<Asset> _AssetsRepository = [];
private static string GetCommandText()
private static string GetColumnsCommandText()
{ // cSpell:disable
List<string> results = new();
// results.Add(" SELECT COALESCE(SUM(checksum_failures), 0) ");
@ -27,97 +22,136 @@ public class AssetService
// results.Add(" FROM information_schema.tables t ");
// results.Add(" WHERE table_schema='public' ");
// results.Add(" AND table_type='BASE TABLE' ");
// results.Add(" SELECT json_agg(c) ");
// results.Add(" FROM information_schema.columns c ");
results.Add(" SELECT json_agg(c) ");
results.Add(" FROM information_schema.columns c ");
// results.Add(" WHERE table_name ='assets' ");
// results.Add(" WHERE table_name ='libraries' ");
// results.Add(" WHERE table_name ='asset_files' ");
results.Add(" WHERE table_name ='asset_files' ");
return string.Join(Environment.NewLine, results);
} // cSpell:enable
private static string GetOwnerIdActiveImageCommandText()
{ // cSpell:disable
List<string> results = new();
results.Add(" SELECT json_agg(j) ");
results.Add(" FROM ( ");
results.Add(" SELECT a.* ");
results.Add(" , f.\"path\" ");
results.Add(" SELECT a.\"ownerId\" ");
results.Add(" FROM assets a ");
// results.Add(" FROM asset_files f ");
results.Add(" INNER ");
results.Add(" JOIN asset_files f ");
results.Add(" ON a.\"id\" = f.\"assetId\" ");
results.Add(" AND f.\"type\" = 'preview' ");
results.Add(" WHERE a.\"status\" = 'active' ");
// results.Add(" WHERE f.\"assetId\" = '4c1933ce-f5b3-4348-bcc3-978f99823d70' ");
results.Add(" AND a.\"isExternal\" = true ");
results.Add(" AND a.\"isOffline\" = false ");
results.Add(" AND a.\"isVisible\" = true ");
// results.Add(" AND a.\"id\" = '4c1933ce-f5b3-4348-bcc3-978f99823d70' ");
// results.Add(" AND a.\"originalFileName\"");
// results.Add(" LIKE '%still%' ");
// results.Add(" AND a.\"originalFileName\" = '979270910999.jpg' ");
results.Add(" AND a.\"type\" = 'IMAGE' ");
results.Add(" GROUP");
results.Add(" BY a.\"ownerId\" ");
results.Add(" ) j ");
return string.Join(Environment.NewLine, results);
} // cSpell:enable
private static int? ExecuteNonQuery(string connectionString, string commandText)
{
int? result;
if (string.IsNullOrEmpty(connectionString))
result = null;
else
{
using NpgsqlConnection npgsqlConnection = new(connectionString);
npgsqlConnection.Open();
using NpgsqlCommand npgsqlCommand = new(commandText, npgsqlConnection);
result = npgsqlCommand.ExecuteNonQuery();
}
return result;
}
private static string GetAssetActiveImagePreviewNotDuplicateCommandText()
{ // cSpell:disable
List<string> results = new();
results.Add(" SELECT json_agg(j) ");
results.Add(" FROM ( ");
results.Add(" SELECT a.\"id\" ");
results.Add(" , a.\"deviceAssetId\" ");
// results.Add(" , a.\"ownerId\" ");
// results.Add(" , a.\"deviceId\" ");
// results.Add(" , a.\"type\" ");
results.Add(" , a.\"originalPath\" ");
// results.Add(" , a.\"fileCreatedAt\" ");
// results.Add(" , a.\"fileModifiedAt\" ");
// results.Add(" , a.\"isFavorite\" ");
// results.Add(" , a.\"duration\" ");
// results.Add(" , a.\"encodedVideoPath\" ");
// results.Add(" , a.\"checksum\" ");
// results.Add(" , a.\"isVisible\" ");
// results.Add(" , a.\"livePhotoVideoId\" ");
// results.Add(" , a.\"updatedAt\" ");
// results.Add(" , a.\"createdAt\" ");
// results.Add(" , a.\"isArchived\" ");
results.Add(" , a.\"originalFileName\" ");
// results.Add(" , a.\"sidecarPath\" ");
// results.Add(" , a.\"thumbhash\" ");
// results.Add(" , a.\"isOffline\" ");
// results.Add(" , a.\"libraryId\" ");
// results.Add(" , a.\"isExternal\" ");
// results.Add(" , a.\"deletedAt\" ");
// results.Add(" , a.\"localDateTime\" ");
// results.Add(" , a.\"stackId\" ");
results.Add(" , a.\"duplicateId\" ");
// results.Add(" , a.\"status\" ");
results.Add(" , f.\"path\" ");
results.Add(" FROM assets a ");
results.Add(" INNER ");
results.Add(" JOIN asset_files f ");
results.Add(" ON a.\"id\" = f.\"assetId\" ");
results.Add(" WHERE a.\"status\" = 'active' ");
results.Add(" AND a.\"type\" = 'IMAGE' ");
results.Add(" AND f.\"type\" = 'preview' ");
results.Add(" AND a.\"duplicateId\" is null ");
results.Add(" AND a.\"isExternal\" = true ");
results.Add(" AND a.\"isOffline\" = false ");
results.Add(" AND a.\"isVisible\" = true ");
results.Add(" AND a.\"ownerId\" = @ownerId ");
results.Add(" ) j ");
return string.Join(Environment.NewLine, results);
} // cSpell:enable
private static StringBuilder GetForJsonPath(string connectionString, string commandText)
private static StringBuilder GetForJsonPath(string connectionString, string commandText, NpgsqlParameter[] npgsqlParameters)
{
StringBuilder stringBuilder = new();
using NpgsqlConnection npgsqlConnection = new(connectionString);
npgsqlConnection.Open();
using NpgsqlCommand npgsqlCommand = new(commandText, npgsqlConnection);
npgsqlCommand.Parameters.AddRange(npgsqlParameters);
NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
while (npgsqlDataReader.Read())
_ = stringBuilder.Append(npgsqlDataReader.GetString(0));
return stringBuilder;
}
public ReadOnlyCollection<Asset>? Get()
public string? GetColumns()
{
string commandText = GetCommandText();
if (commandText.Length == 1)
{
int? result = ExecuteNonQuery(_AppSettings.ConnectionString, commandText);
if (result is null)
{ }
}
StringBuilder stringBuilder = GetForJsonPath(_AppSettings.ConnectionString, commandText);
if (commandText.Length == 1)
File.WriteAllText(".vscode/jsonl/.jsonl", stringBuilder.ToString());
string json = stringBuilder.ToString();
string commandText = GetColumnsCommandText();
NpgsqlParameter[] npgsqlParameters = [];
StringBuilder stringBuilder = GetForJsonPath(_AppSettings.ConnectionString, commandText, npgsqlParameters);
string json = stringBuilder.ToString();
if (json.Length == 1)
File.WriteAllText(".vscode/jsonl/.jsonl", json);
return json;
}
public string? GetOwnerIds()
{
string commandText = GetOwnerIdActiveImageCommandText();
NpgsqlParameter[] npgsqlParameters = [];
StringBuilder stringBuilder = GetForJsonPath(_AppSettings.ConnectionString, commandText, npgsqlParameters);
string json = stringBuilder.ToString();
if (json.Length == 1)
File.WriteAllText(".vscode/jsonl/.jsonl", json);
return json;
}
public string? GetAssets(Guid ownerId)
{
string commandText = GetAssetActiveImagePreviewNotDuplicateCommandText();
NpgsqlParameter[] npgsqlParameters = [new NpgsqlParameter(nameof(ownerId), ownerId)];
StringBuilder stringBuilder = GetForJsonPath(_AppSettings.ConnectionString, commandText, npgsqlParameters);
string json = stringBuilder.ToString();
if (json.Length == 1)
File.WriteAllText(".vscode/jsonl/assets.jsonl", json);
return json;
}
public ReadOnlyCollection<string>? GetRandomPaths(Guid ownerId)
{
string commandText = GetAssetActiveImagePreviewNotDuplicateCommandText();
NpgsqlParameter[] npgsqlParameters = [new NpgsqlParameter(nameof(ownerId), ownerId)];
StringBuilder stringBuilder = GetForJsonPath(_AppSettings.ConnectionString, commandText, npgsqlParameters);
string json = stringBuilder.ToString();
Random random = new();
string ownerIdValue = ownerId.ToString();
Asset[]? assets = JsonSerializer.Deserialize(json, AssetCollectionSourceGenerationContext.Default.AssetArray);
return assets?.AsReadOnly();
string[]? paths = assets is null ? null : (from l in assets orderby random.NextSingle() select l.Path.Split(ownerIdValue)[1][1..]).ToArray();
return paths?.AsReadOnly();
}
// 1. fetch user
// 1. fetch asset
// 1. check wether the user reached the
// 1. update the user
// 1. save the asset
public void Create(Asset asset)
{
// Guid userId,
if (asset is null)
throw new ArgumentNullException(nameof(asset));
// User user = _UsersRepository.Find(x => x.Id == userId)
// ?? throw new InvalidOperationException();
// user.AddAsset(asset);
_AssetsRepository.Add(asset);
}
public Asset? Get(Guid assetId) =>
_AssetsRepository.Find(l => l.Id == assetId.ToString());
}