/// <summary> /// Returns a collection of mappings that match the given filter expression. /// </summary> /// <param name="filterExpression">The filter expression to be matched.</param> /// <returns>The collection of mappings that match the given expression.</returns> public IEnumerable <TypeMapping> EnumerateTypeMappings(string filterExpression) { string tableName; string whereExpression; string sortField; int takeCount; if ((object)m_mappingTable == null) { m_mappingTable = GetMappingTable(); } if (!AdapterBase.ParseFilterExpression(filterExpression, out tableName, out whereExpression, out sortField, out takeCount)) { return(filterExpression .Split(';') .Select(str => str.Trim()) .Where(str => !string.IsNullOrEmpty(str)) .Select(GetTypeMapping)); } if (!tableName.Equals("Mappings", StringComparison.OrdinalIgnoreCase)) { return(Enumerable.Empty <TypeMapping>()); } return(m_mappingTable .Select(whereExpression, sortField) .Take(takeCount) .Select(row => row.Field <string>("MappingIdentifier")) .Select(GetTypeMapping)); }
/// <summary> /// Parses query expression from annotation for annotation type. /// </summary> /// <param name="annotation">Grafana annotation.</param> /// <param name="useFilterExpression">Determines if query is using a filter expression.</param> /// <returns>Parsed annotation type for query expression from <paramref name="annotation"/>.</returns> public static AnnotationType ParseQueryType(this Annotation annotation, out bool useFilterExpression) { if (annotation == null) { throw new ArgumentNullException(nameof(annotation)); } string query = annotation.query ?? ""; Tuple <AnnotationType, bool> result = TargetCache <Tuple <AnnotationType, bool> > .GetOrAdd(query, () => { AnnotationType type = AnnotationType.Undefined; bool parsedFilterExpression = false; if (AdapterBase.ParseFilterExpression(query, out string tableName, out string _, out string _, out int _)) { parsedFilterExpression = true; switch (tableName.ToUpperInvariant()) { case "RAISEDALARMS": type = AnnotationType.RaisedAlarms; break; case "CLEAREDALARMS": type = AnnotationType.ClearedAlarms; break; default: throw new InvalidOperationException("Invalid FILTER table for annotation query expression."); } }
/// <summary> /// Parses source definitions for an annotation query. /// </summary> /// <param name="annotation">Grafana annotation.</param> /// <param name="type">Annotation type.</param> /// <param name="source">Metadata of source definitions.</param> /// <param name="useFilterExpression">Determines if query is using a filter expression.</param> /// <returns>Parsed source definitions from <paramref name="annotation"/>.</returns> public static Dictionary <string, DataRow> ParseSourceDefinitions(this Annotation annotation, AnnotationType type, DataSet source, bool useFilterExpression) { if ((object)annotation == null) { throw new ArgumentNullException(nameof(annotation)); } if ((object)source == null) { throw new ArgumentNullException(nameof(source)); } if (type == AnnotationType.Undefined) { throw new InvalidOperationException("Unrecognized type or syntax for annotation query expression."); } string query = annotation.query ?? ""; return(TargetCache <Dictionary <string, DataRow> > .GetOrAdd(query, () => { DataRow[] rows; if (useFilterExpression) { string tableName, expression, sortField; int takeCount; if (AdapterBase.ParseFilterExpression(query, out tableName, out expression, out sortField, out takeCount)) { rows = source.Tables[tableName.Translate()].Select(expression, sortField).Take(takeCount).ToArray(); } else { throw new InvalidOperationException("Invalid FILTER syntax for annotation query expression."); } } else { // Assume all records if no filter expression was provided rows = source.Tables[type.TableName().Translate()].Rows.Cast <DataRow>().ToArray(); } Dictionary <string, DataRow> definitions = new Dictionary <string, DataRow>(StringComparer.OrdinalIgnoreCase); foreach (DataRow row in rows) { MeasurementKey key = GetTargetFromGuid(row[type.TargetFieldName()].ToString()); if (key != MeasurementKey.Undefined) { definitions[key.TagFromKey(source)] = row; } } return definitions; })); }
/// <summary> /// Parses query expression from annotation for annotation type. /// </summary> /// <param name="annotation">Grafana annotation.</param> /// <param name="useFilterExpression">Determines if query is using a filter expression.</param> /// <returns>Parsed annotation type for query expression from <paramref name="annotation"/>.</returns> public static AnnotationType ParseQueryType(this Annotation annotation, out bool useFilterExpression) { if ((object)annotation == null) { throw new ArgumentNullException(nameof(annotation)); } string query = annotation.query ?? ""; Tuple <AnnotationType, bool> result = TargetCache <Tuple <AnnotationType, bool> > .GetOrAdd(query, () => { AnnotationType type = AnnotationType.Undefined; string tableName, expression, sortField; int takeCount; bool parsedFilterExpression = false; if (AdapterBase.ParseFilterExpression(query, out tableName, out expression, out sortField, out takeCount)) { parsedFilterExpression = true; switch (tableName.ToUpperInvariant()) { case "RAISEDALARMS": type = AnnotationType.RaisedAlarms; break; case "CLEAREDALARMS": type = AnnotationType.ClearedAlarms; break; default: throw new InvalidOperationException("Invalid FILTER table for annotation query expression."); } } else if (query.StartsWith("#RaisedAlarms", StringComparison.OrdinalIgnoreCase)) { type = AnnotationType.RaisedAlarms; } else if (query.StartsWith("#ClearedAlarms", StringComparison.OrdinalIgnoreCase)) { type = AnnotationType.ClearedAlarms; } if (type == AnnotationType.Undefined) { throw new InvalidOperationException("Unrecognized type or syntax for annotation query expression."); } return(new Tuple <AnnotationType, bool>(type, parsedFilterExpression)); }); useFilterExpression = result.Item2; return(result.Item1); }