Hi,
How can I get all the services and alarms related to it inside a GQI?
Scenario:
I want to get all the services and also the element details present inside my view on GQI
Hi,
To retrieve the services/elements within a view you can use the built-in View Relations data source, filter on View ID and then join on the built-in Get Services and Get Elements data sources.
If you also need the details of the elements within the services, you could create an Ad Hoc data source "Service relations" where each row contains a Service ID and an Child ID that maps a Service to one of its children (see minimal implementation below).
It sends a GetLiteServiceInfo message to the GQIDMS with the ViewID property filled in which retrieves LiteServiceInfoEvent objects for each service containing the Children property.
You can then create a query starting from the Service Relations data source, join on the Get Services built-in data source and join on the Get Elements built-in data source.
To get all active alarms under a view, you can also create an Ad Hoc data source "View alarms" that retrieves the alarms filtered by view.
It works similar to the Ad Hoc Service Alarms data source on GitHub, but with a view filter instead. See also minimal implementation below.
Minimal implementation of "Service relations"
[GQIMetaData(Name = "ServiceRelations")] public sealed class ServiceRelations : IGQIDataSource, IGQIOnInit, IGQIInputArguments{
private readonly GQIStringColumn _serviceIdColumn = new GQIStringColumn("Service ID");
private readonly GQIStringColumn _childIdColumn = new GQIStringColumn("Child ID");
private readonly GQIIntArgument _viewIdArgument = new GQIIntArgument("View ID");
private GQIDMS _dms;
public OnInitOutputArgs OnInit(OnInitInputArgs args)
{
_dms = args.DMS;
return default;
}
public GQIArgument[] GetInputArguments()
{
return new[] { _viewIdArgument };
}
private int _viewId;
public OnArgumentsProcessedOutputArgs OnArgumentsProcessed(OnArgumentsProcessedInputArgs args)
{
if (!args.TryGetArgumentValue(_viewIdArgument, out _viewId))
_viewId = -1;
return default;
}
public GQIColumn[] GetColumns()
{
return new[]
{
_serviceIdColumn,
_childIdColumn,
};
}
public GQIPage GetNextPage(GetNextPageInputArgs args)
{
var rows = GetServices()
.SelectMany(CreateRows)
.ToArray();
return new GQIPage(rows);
}
private IEnumerable<LiteServiceInfoEvent> GetServices()
{
var request = new GetLiteServiceInfo { ViewID = _viewId };
return _dms.SendMessages(request)
.Cast<LiteServiceInfoEvent>();
}
private IEnumerable<GQIRow> CreateRows(LiteServiceInfoEvent service)
{
if (service.Children is null)
return Array.Empty<GQIRow>();
var serviceId = ElementID.GetKey(service.DataMinerID, service.ElementID);
return service.Children.Select(child => CreateRow(serviceId, child));
}
private GQIRow CreateRow(string serviceId, LiteServiceChildInfo child)
{
var childId = ElementID.GetKey(child.DataMinerID, child.ElementID);
return new GQIRow(new[]
{
new GQICell { Value = serviceId },
new GQICell { Value = childId },
});
}
Minimal implementation of "View alarms"
[GQIMetaData(Name = "ViewAlarms")] public sealed class ViewAlarms : IGQIDataSource, IGQIOnInit, IGQIInputArguments{
private readonly GQIStringColumn _severityColumn = new GQIStringColumn("Severity");
private readonly GQIStringColumn _elementColumn = new GQIStringColumn("Element");
private readonly GQIStringColumn _parameterColumn = new GQIStringColumn("Parameter");
private readonly GQIStringColumn _valueColumn = new GQIStringColumn("Value");
private readonly GQIIntArgument _viewIdArgument = new GQIIntArgument("View ID");
private GQIDMS _dms;
public OnInitOutputArgs OnInit(OnInitInputArgs args)
{
_dms = args.DMS;
return default;
}
public GQIArgument[] GetInputArguments()
{
return new[] { _viewIdArgument };
}
private int _viewId;
public OnArgumentsProcessedOutputArgs OnArgumentsProcessed(OnArgumentsProcessedInputArgs args)
{
if (!args.TryGetArgumentValue(_viewIdArgument, out _viewId))
_viewId = -1;
return default;
}
public GQIColumn[] GetColumns()
{
return new[]
{
_severityColumn,
_elementColumn,
_parameterColumn,
_valueColumn,
};
}
public GQIPage GetNextPage(GetNextPageInputArgs args)
{
var rows = GetAlarms()
.Select(CreateRow)
.ToArray();
return new GQIPage(rows);
}
private AlarmEventMessage[] GetAlarms()
{
var viewFilterItem = new AlarmFilterItemView(AlarmFilterCompareType.Equality, new[] { _viewId });
var alarmFilter = new AlarmFilter(viewFilterItem);
var request = new GetActiveAlarmsMessage { Filter = alarmFilter };
var response = _dms.SendMessage(request) as ActiveAlarmsResponseMessage;
return response.ActiveAlarms;
}
private GQIRow CreateRow(AlarmEventMessage alarm)
{
return new GQIRow(new[]
{
new GQICell { Value = alarm.Severity },
new GQICell { Value = alarm.ElementName },
new GQICell { Value = alarm.ParameterName },
new GQICell { Value = alarm.Value },
});
}
}
On the GetLiteServiceInfo you can use the View property instead of the ViewID property and the AlarmFilterItemView also accepts view names as strings instead of view IDs as integers.
Thanks, Is there any want to get by name, as view name is unique?