Skip to content

Commit 5215b7d

Browse files
committed
*
1 parent d2bfebe commit 5215b7d

File tree

4 files changed

+83
-104
lines changed

4 files changed

+83
-104
lines changed

SmartImage.Lib 3/Results/SearchResultItem.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections;
1+
using System.Runtime.CompilerServices;
2+
3+
using System.Collections;
24
using System.Drawing;
35
using System.Dynamic;
46
using Flurl.Http;
@@ -8,6 +10,7 @@
810
using Novus.FileTypes.Uni;
911
using SmartImage.Lib.Utilities;
1012

13+
[assembly: InternalsVisibleTo("SmartImage.Lib Unit Test")]
1114
namespace SmartImage.Lib.Results;
1215

1316
public sealed record SearchResultItem : IDisposable, IComparable<SearchResultItem>, IComparable

SmartImage.Lib 3/SearchClient.cs

Lines changed: 6 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,10 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
158158
Task<SearchResult> task = await Task.WhenAny(tasks);
159159
tasks.Remove(task);
160160

161-
// Debug.WriteLine($"{task.Id} {task.Status}");
162161
SearchResult result = await task;
163162

164-
// ProcessResult(result);
165-
166163
results[i] = result;
167164
i++;
168-
169-
// results.Add(result);
170165
}
171166

172167
ResultChannel?.Writer.Complete();
@@ -176,10 +171,6 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
176171

177172
if (Config.PriorityEngines == SearchEngineOptions.Auto) {
178173

179-
// var sri = results.SelectMany(r => r.Results).ToArray();
180-
// var result = Optimize(sri).FirstOrDefault() ?? sri.FirstOrDefault();
181-
//todo
182-
183174
try {
184175

185176
var ordered = results.Select(x => x.GetBestResult())
@@ -236,17 +227,6 @@ private void ProcessResult(SearchResult result)
236227

237228
private static void OpenResult([MN] Url url1)
238229
{
239-
// #if DEBUG && !TEST
240-
/*
241-
#pragma warning disable CA1822
242-
243-
// ReSharper disable once MemberCanBeMadeStatic.Local
244-
Logger.LogDebug("Not opening {url}", url1);
245-
return;
246-
247-
#pragma warning restore CA1822
248-
*/
249-
// #else
250230

251231
if (url1 == null) {
252232
return;
@@ -259,46 +239,29 @@ private static void OpenResult([MN] Url url1)
259239
// var b = Open(url1, out var proc);
260240

261241
if (b && proc is { }) {
262-
/*var o = proc.WaitForExit(TimeSpan.FromSeconds(3));
263-
264-
if (o) {
265-
Debug.WriteLine($"{proc}");
266-
}*/
267242
proc.Dispose();
268243
}
269244

270-
// Process.Start(url1);
271-
// HttpUtilities.TryOpenUrl(url1);
272-
// #endif
273-
274245
}
275246

276247
private void OpenResult([MN] SearchResultItem result)
277248
{
278-
// #if DEBUG && !TEST
279-
/*#pragma warning disable CA1822
249+
#if DEBUG && !TEST
250+
#pragma warning disable CA1822
280251

281252
// ReSharper disable once MemberCanBeMadeStatic.Local
282253
Logger.LogDebug("Not opening result {result}", result);
283254
return;
284255

285-
#pragma warning restore CA1822*/
286-
// #else
287-
256+
#pragma warning restore CA1822
257+
#endif
288258
OnOpen?.Invoke(this, result);
289259

290260
if (result != null) {
291261

292-
if (Config.OpenRaw) {
293-
OpenResult(result.Root.GetRawResultItem().Url);
294-
}
295-
else {
296-
OpenResult(result.Url);
297-
// OpenResult(result);
298-
299-
}
262+
var url = Config.OpenRaw ? result.Root.GetRawResultItem().Url : result.Url;
263+
OpenResult(url);
300264
}
301-
// #endif
302265

303266
}
304267

@@ -313,9 +276,6 @@ public List<Task<SearchResult>> GetSearchTasks(SearchQuery query, TaskScheduler
313276
Task<SearchResult> res = e.GetResultAsync(query, token: token)
314277
.ContinueWith((r) =>
315278
{
316-
317-
// Debug.Assert(r.IsCompleted);
318-
// Debug.WriteLine($"{r.Id} :: {r.Status}");
319279
ProcessResult(r.Result);
320280
return r.Result;
321281

@@ -365,63 +325,6 @@ public BaseSearchEngine TryGetEngine(SearchEngineOptions o)
365325
return Engines.FirstOrDefault(e => e.EngineOption == o);
366326
}
367327

368-
/*public static ValueTask<IReadOnlyList<SearchResultItem>> Filter(IEnumerable<SearchResultItem> sri)
369-
{
370-
var sri2 = sri.AsParallel().DistinctBy(e => e.Url).ToList();
371-
372-
return ValueTask.FromResult<IReadOnlyList<SearchResultItem>>(sri2);
373-
}
374-
375-
public static IReadOnlyList<SearchResultItem> Optimize(IEnumerable<SearchResultItem> sri)
376-
{
377-
var items = sri.Where(r => SearchQuery.IsValidSourceType(r.Url))
378-
.OrderByDescending(r => r.Score)
379-
.ThenByDescending(r => r.Similarity)
380-
.ToArray();
381-
382-
try {
383-
/*var c = items.Where(r => r.Root.Engine.EngineOption == SearchEngineOptions.TraceMoe
384-
/*&& r.Similarity <= TraceMoeEngine.FILTER_THRESHOLD#2#);
385-
items = items.Except(c).ToArray();#1#
386-
387-
}
388-
catch (Exception e) {
389-
Logger.LogError("{Error}", e.Message);
390-
}
391-
finally { }
392-
393-
return items.AsReadOnly();
394-
}
395-
396-
public static async Task<IReadOnlyList<UniSource>> GetDirectImagesAsync(IEnumerable<SearchResultItem> sri)
397-
{
398-
//
399-
var filter = Optimize(sri)
400-
.DistinctBy(r => r.Url)
401-
// .Where(r => r.Score >= SearchResultItem.SCORE_THRESHOLD) // probably can be removed/reduced
402-
.Select(async r =>
403-
{
404-
bool b = await r.LoadUniAsync();
405-
return r.Uni;
406-
})
407-
.ToList();
408-
409-
var di = new List<UniSource>();
410-
411-
while (filter.Any()) {
412-
var t1 = await Task.WhenAny(filter);
413-
filter.Remove(t1);
414-
var uf = await t1;
415-
416-
if (uf != null) {
417-
di.AddRange(uf);
418-
}
419-
420-
}
421-
422-
return di.AsReadOnly();
423-
}*/
424-
425328
public void Dispose()
426329
{
427330
foreach (BaseSearchEngine engine in Engines) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Kantan.Utilities;
2+
using Novus.Utilities;
3+
4+
namespace SmartImage.Lib.Utilities;
5+
6+
// todo
7+
8+
public class FieldValueMap
9+
{
10+
11+
public string Name { get; }
12+
13+
public object Value { get; }
14+
15+
public string FieldName { get; }
16+
17+
public static FieldValueMap Find<T>(T inst, string name)
18+
{
19+
var type = inst?.GetType() ?? typeof(T);
20+
return Find(inst, name, type);
21+
}
22+
23+
private static FieldValueMap Find<T>(T inst, string name, Type type)
24+
{
25+
var fld = type.GetAnyResolvedField(name);
26+
27+
if (fld is not null) {
28+
return new FieldValueMap(name, fld.GetValue(inst), fld.Name);
29+
}
30+
31+
return null;
32+
}
33+
34+
public static FieldValueMap[] Find<T>(T inst, string[] names)
35+
{
36+
var type = inst?.GetType() ?? typeof(T);
37+
38+
var values = new FieldValueMap[names.Length];
39+
int i = 0;
40+
41+
foreach (string name in names) {
42+
var val = Find(inst, name, type);
43+
44+
if (val is not null) {
45+
values[i++] = val;
46+
}
47+
}
48+
49+
Array.Resize(ref values, i);
50+
51+
return values;
52+
}
53+
54+
public static FieldValueMap[] Find<T, T2>(T inst, T2 names) where T2 : struct, Enum
55+
{
56+
var names2 = names.GetSetFlags().Select(x => x.ToString()).ToArray();
57+
return Find(inst, names2);
58+
}
59+
60+
public FieldValueMap(string name, object value, string fieldName)
61+
{
62+
Name = name;
63+
Value = value;
64+
FieldName = fieldName;
65+
}
66+
67+
public override string ToString()
68+
{
69+
return $"{nameof(Name)}: {Name} | {nameof(FieldName)}: {FieldName} | {nameof(Value)}: {Value}";
70+
}
71+
72+
}

SmartImage.Rdx/IntegrationCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using SmartImage.Lib.Utilities;
6+
using Spectre.Console;
67
using Spectre.Console.Cli;
78

89
namespace SmartImage.Rdx;

0 commit comments

Comments
 (0)