Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active December 14, 2020 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/b0b0144e887ac9e04dd180b126d5f38b to your computer and use it in GitHub Desktop.
Save icebeam7/b0b0144e887ac9e04dd180b126d5f38b to your computer and use it in GitHub Desktop.
Búsqueda de tags coincidentes
// Se requiere un paquete Nuget adicional
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
// Espacios de nombre requeridos
using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
// La clase Analisis es la tabla que almacena los resultados del análisis
public class Analisis : TableEntity
{
public string Nombre { get; set; }
public string Descripcion { get; set; }
public string Tags { get; set; }
}
public static async Task<IActionResult> Run(HttpRequest req, CloudTable inputTable, ILogger log)
{
// El nombre de la imagen (= partition key)
string pk = req.Query["nombre"];
// Devuelve un solo registro (partition key + row key)
TableOperation operacion = TableOperation.Retrieve<Analisis>(pk, "1");
TableResult resultado = await inputTable.ExecuteAsync(operacion);
Analisis analisis = (Analisis)resultado.Result;
// Accedemos a los tags del registro localizado y los convertimos a lista
List<string> tags = analisis.Tags.Split(',').ToList();
// Recorreremos toda la tabla de Analisis
TableContinuationToken token = null;
TableQuery<Analisis> query = new TableQuery<Analisis>();
CancellationToken ct = default(CancellationToken);
// Aqui acumularemos los resultados
List<Analisis> imagenes = new List<Analisis>();
do
{
// El recorrido se hace por bloques
TableQuerySegment<Analisis> pagina = await inputTable.ExecuteQuerySegmentedAsync<Analisis>(query, token);
token = pagina.ContinuationToken;
// Por cada analisis del bloque
foreach(var registro in pagina)
{
// Obtenemos la lista de tags
List<string> tagsRegistro = registro.Tags.Split(',').ToList();
// Buscamos si coincide un tag al menos
// En caso afirmativo, lo mandamos a la lista de resultados
if (tagsRegistro.Any(tr => tags.Any(t => t == tr)))
imagenes.Add(registro);
}
}while (token != null && !ct.IsCancellationRequested);
// Se devuelve la colección de imágenes que contienen al menos un tag coincidente
return new OkObjectResult(imagenes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment