运行 用于检查批次是否完成并为 Kusto 中的每个完成批次应用计算的调度程序?

Run a scheduler to check if batch is finished and apply calculations for every finished batch in Kusto?

我有以下在 kusto 中随机生成的数据。

 let data = datatable(BatchNumber: int,Timestamp:datetime, Power1:int, Power2: int, Speed1: int, Speed2: int, Enabled1: bool, Enabled2: bool)
        [
         1, datetime(2022-02-18 10:00:00 AM), 100, 200, 50, 80, false, true,
         1, datetime(2022-02-18 10:01:00 AM), 100, 200, 50, 80, true, true,
         1, datetime(2022-02-18 10:02:00 AM), 100, 200, 50, 80, false, true,
         1, datetime(2022-02-18 10:03:00 AM), 100, 200, 50, 80, true, true,
         1, datetime(2022-02-18 10:04:00 AM), 100, 200, 50, 80, false, true,
         1, datetime(2022-02-18 10:05:00 AM), 100, 200, 50, 80, true, true,
         1, datetime(2022-02-18 10:06:00 AM), 100, 200, 50, 80, false, true,
         1, datetime(2022-02-18 10:07:00 AM), 100, 200, 50, 80, true, true,
         2, datetime(2022-02-18 10:08:00 AM), 100, 200, 50, 80, false, true,
         2, datetime(2022-02-18 10:09:00 AM), 100, 200, 50, 80, true, true,
         2, datetime(2022-02-18 10:10:00 AM), 100, 200, 50, 80, false, true,
         2, datetime(2022-02-18 10:11:00 AM), 100, 200, 50, 80, true, true,
         2, datetime(2022-02-18 10:12:00 AM), 100, 200, 50, 80, false, true,
         2, datetime(2022-02-18 10:13:00 AM), 100, 200, 50, 80, true, true,
         2, datetime(2022-02-18 10:14:00 AM), 100, 200, 50, 80, false, true,
         2, datetime(2022-02-18 10:15:00 AM), 100, 200, 50, 80, true, true,
         2, datetime(2022-02-18 10:15:00 AM), 100, 200, 50, 80, false, true
        ];

我在这里想要实现的是检查批处理何时完成。 这样做的逻辑是获取批号的最后一个值并检查它后面的值。如果下一个值与之前的值不同,则上一批已完成。因此,在这种情况下,批号 = 1 已完成,但批号 2 尚未完成。但是,在这种情况下,我不应该检查特定的批号,因为我们是在实时获取值。脚本应该自己知道批处理何时完成,可能是通过执行每 5 分钟运行一次的调度程序来根据我上面解释的逻辑(粗体)检查批处理是否完成,以及何时知道批处理完成,它应该预测这个批号,Batch_Date(在这种情况下,批号 = 1,日期是 2022-02-18 10:07:00 AM)和总功率(仅基于一些计算这批成品)

例如 BatchNumber = 1 的预期结果:

在这种情况下,一旦脚本知道批号 2 也已完成,将使用该批号添加一条新记录。让我们假设在批号 = 3 的数据集中添加了一个附加行,因此预期结果将是:

所以每当脚本知道一个批处理完成时,它应该直接将其添加为新记录,如屏幕截图所示,投影新批处理、完成日期以及一些专门针对新添加的批处理的计算。

我有点困惑如何在 Kusto 中执行此操作?是否可能?我不知道在这种情况下是否需要调度程序,或者是否有更好更有效的方法?

如果目标是报告,我们会利用 materialized views

演示

.create table data (BatchNumber: int,Timestamp:datetime, Power1:int, Power2: int, Speed1: int, Speed2: int, Enabled1: bool, Enabled2: bool)

.create-or-alter materialized-view data_mv on table data
{
    data
    | summarize BatchData = max(Timestamp), TotalPower = sum(coalesce(Power1,0) + coalesce(Power2,0)) by BatchNumber
}

.create-or-alter materialized-view data_max_BatchNumber_mv on table data
{
    data
    | summarize BatchNumber = max(BatchNumber) by dummy = 1
}

.create-or-alter function completed_batches_f ()
{
    let max_BatchNumber = toscalar(data_max_BatchNumber_mv | project BatchNumber);
    data_mv
    | where BatchNumber < max_BatchNumber
}

.ingest inline into table data <|
1, datetime(2022-02-18 10:00:00 AM), 100, 200, 50, 80, false, true
1, datetime(2022-02-18 10:01:00 AM), 100, 200, 50, 80, true, true
1, datetime(2022-02-18 10:02:00 AM), 100, 200, 50, 80, false, true
1, datetime(2022-02-18 10:03:00 AM), 100, 200, 50, 80, true, true
1, datetime(2022-02-18 10:04:00 AM), 100, 200, 50, 80, false, true

completed_batches_f
BatchNumber BatchData TotalPower

.ingest inline into table data <|
1, datetime(2022-02-18 10:05:00 AM), 100, 200, 50, 80, true, true
1, datetime(2022-02-18 10:06:00 AM), 100, 200, 50, 80, false, true
1, datetime(2022-02-18 10:07:00 AM), 100, 200, 50, 80, true, true
2, datetime(2022-02-18 10:08:00 AM), 100, 200, 50, 80, false, true
2, datetime(2022-02-18 10:09:00 AM), 100, 200, 50, 80, true, true
2, datetime(2022-02-18 10:10:00 AM), 100, 200, 50, 80, false, true
2, datetime(2022-02-18 10:11:00 AM), 100, 200, 50, 80, true, true

completed_batches_f
BatchNumber BatchData TotalPower
1 2022-02-18T10:07:00Z 2400
.ingest inline into table data <|
2, datetime(2022-02-18 10:12:00 AM), 100, 200, 50, 80, false, true
2, datetime(2022-02-18 10:13:00 AM), 100, 200, 50, 80, true, true
2, datetime(2022-02-18 10:14:00 AM), 100, 200, 50, 80, false, true
2, datetime(2022-02-18 10:15:00 AM), 100, 200, 50, 80, true, true
2, datetime(2022-02-18 10:15:00 AM), 100, 200, 50, 80, false, true

completed_batches_f
BatchNumber BatchData TotalPower
1 2022-02-18T10:07:00Z 2400

.ingest inline into table data <|
3, datetime(2022-02-18 10:16:00 AM), 100, 200, 50, 80, false, true

completed_batches_f
BatchNumber BatchData TotalPower
1 2022-02-18T10:07:00Z 2400
2 2022-02-18T10:15:00Z 2700