RLei.RedashUtils 1.0.2

dotnet add package RLei.RedashUtils --version 1.0.2
                    
NuGet\Install-Package RLei.RedashUtils -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RLei.RedashUtils" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RLei.RedashUtils" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="RLei.RedashUtils" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RLei.RedashUtils --version 1.0.2
                    
#r "nuget: RLei.RedashUtils, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RLei.RedashUtils@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RLei.RedashUtils&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=RLei.RedashUtils&version=1.0.2
                    
Install as a Cake Tool

RLei.RedashUtils

用于从 Redash 获取查询结果的 .NET 工具库。

功能

  • 获取查询元数据(列信息、参数定义)
  • 执行查询并获取数据(支持参数化查询)
  • 获取单条查询详情
  • 导出查询结果到 Excel(支持多查询多 Sheet)
  • 自动处理分页参数(_pageIndex / _pageSize)
  • 自动同步查询参数(补全缺失的默认值)
  • 自动轮询异步查询任务状态
  • 动态可见列配置(基于条件表达式过滤列)
  • JSON 列自动解析
  • # 前缀列自动过滤

安装

dotnet add package RLei.RedashUtils

配置

在 appsettings.json 中添加:

{
  "RedashSettings": {
    "BaseUrl": "https://your-redash-instance.com",
    "ApiKey": "your-api-key",
    "QueryDelayMs": 100,
    "TimeoutSeconds": 30
  }
}

配置项说明

参数 类型 默认值 说明
BaseUrl string "" Redash 服务基础 URL
ApiKey string "" Redash API 密钥
QueryDelayMs int 100 异步任务轮询间隔(毫秒)
TimeoutSeconds int 30 HTTP 请求超时时间(秒)

使用

注册服务

从配置文件自动绑定(推荐):

// Program.cs
builder.Services.AddRedashService(builder.Configuration);

手动配置(用于测试或特殊场景):

services.AddRedashService(settings =>
{
    settings.BaseUrl = "https://your-redash-instance.com";
    settings.ApiKey = "your-api-key";
});

获取查询元数据

var metadata = await redashService.GetQueryMetadataAsync(queryId: 123);

// 带参数(用于可见列动态过滤)
var metadata = await redashService.GetQueryMetadataAsync(
    queryId: 123,
    parameters: new Dictionary<string, object> { ["status"] = "active" });

执行查询

// 无参数
var result = await redashService.ExecuteQueryAsync(queryId: 123);

// 带参数
var result = await redashService.ExecuteQueryAsync(
    queryId: 123,
    parameters: new Dictionary<string, object> { ["date"] = "2026-01-01" },
    maxAge: 300);

// 分页查询
var result = await redashService.ExecuteQueryAsync(
    queryId: 123,
    parameters: new Dictionary<string, object>
    {
        ["_pageIndex"] = 1,
        ["_pageSize"] = 20
    });

// 处理结果
if (result.Success)
{
    var total = result.Total;      // 总记录数
    var totalPage = result.TotalPage;  // 总页数
    foreach (var row in result.Data) { ... }
}

获取单条详情

var detail = await redashService.GetDetailAsync(
    queryId: 123,
    parameters: new Dictionary<string, object> { ["id"] = 456 });

if (detail.Success && detail.Data != null)
{
    // detail.Data 是 Dictionary<string, object>
    var name = detail.Data["name"]?.ToString();
}

导出 Excel

// 导出单个查询
var bytes = await excelExportService.ExportQueryToExcelAsync(queryId: 123);

// 导出多个查询(每个查询一个 Sheet)
var bytes = await excelExportService.ExportQueriesToExcelAsync(
    queryIds: new List<int> { 123, 456, 789 },
    parameters: new Dictionary<string, object> { ["date"] = "2026-01-01" });

响应模型

QueryResponse(列表数据)

public class QueryResponse
{
    public bool Success { get; set; }
    public List<Dictionary<string, object>>? Data { get; set; }
    public string? Error { get; set; }
    public int? Total { get; set; }      // 总记录数
    public int? TotalPage { get; set; }  // 总页数
}

QueryDetailResponse(单条数据)

public class QueryDetailResponse
{
    public bool Success { get; set; }
    public Dictionary<string, object>? Data { get; set; }
    public string? Error { get; set; }
}

QueryMetadata(元数据)

public class QueryMetadata
{
    public string? Name { get; set; }
    public List<ColumnInfo>? Columns { get; set; }
    public List<QueryParameter>? Parameters { get; set; }
}

高级功能

分页查询

传入 _pageIndex(从 1 开始)和 _pageSize 参数,库自动转换为 Redash 需要的 _pageSkip + _pageSize 组合:

var result = await redashService.ExecuteQueryAsync(
    queryId: 123,
    parameters: new Dictionary<string, object>
    {
        ["_pageIndex"] = 2,   // 第 2 页
        ["_pageSize"] = 20    // 每页 20 条
    });

// result.Total = 总记录数
// result.TotalPage = 总页数

动态可见列

在 Redash 查询的 options 中配置 visible_columns,根据参数动态显示/隐藏列:

{
  "visible_columns": [
    { "name": "status", "condition": "status == \"active\"" },
    { "name": "amount", "condition": "showAmount == true" },
    { "name": "date", "condition": "startDate != null" }
  ]
}

条件表达式使用 C# 语法(基于 RulesEngine),求值为 true 时显示列,为 false 时隐藏。

常用表达式示例:

场景 表达式
字符串相等 status == "active"
数值比较 amount > 100
布尔判断 isEnabled == true
非空判断 name != null
包含判断 type.Contains("vip")

参数自动同步

执行查询时,库会自动与 Redash 定义的参数做同步:

  • 多传的参数会被移除
  • 缺少的参数会根据类型自动填充默认值

JSON 列自动解析

当列的 type 或 displayAs 为 "json" 时,自动将字符串值解析为 ExpandoObject。

Excel 导出限制

  • 单次最多导出 20 个查询
  • 总行数不超过 50 万行
  • 超出时抛出 InvalidOperationException

目标框架

  • .NET 10.0+

依赖

  • Newtonsoft.Json
  • Microsoft.Extensions.Http
  • Microsoft.Extensions.Options
  • MiniExcel
  • RulesEngine

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 121 6/24/2026
1.0.1 112 6/24/2026
1.0.0 125 6/23/2026