History

GrafanaAdapters: Improved operation of unique label validation.

J. Ritchie Carroll committed 6 years ago

Commit 42e79fb1 parent a63554a1 file · +34 −28Browse files

Unified · Split

Source/Libraries/Adapters/GrafanaAdapters/GrafanaDataSourceBase.cs+34−28
@@ -838,23 +838,22 @@ namespace GrafanaAdapters · 837 unmodified lines
838 {838 {
839 return Task.Factory.StartNew(() =>839 return Task.Factory.StartNew(() =>
840 {840 {
841
842 const string SignalIDQuery = "SELECT TOP 1 SignalID FROM ActiveMeasurement WHERE PointTag = '{0}'";841 const string SignalIDQuery = "SELECT TOP 1 SignalID FROM ActiveMeasurement WHERE PointTag = '{0}'";
843 842
844 foreach (Target target in request.targets)843 foreach (Target target in request.targets)
845 target.target = target.target?.Trim() ?? "";844 target.target = target.target?.Trim() ?? "";
846 845
847 if (request.targets.Where(item => item.target != "").Count() < 1)846 if (request.targets.All(item => string.IsNullOrWhiteSpace(item.target)))
848 return new List<GrafanaAlarm>();847 return new List<GrafanaAlarm>();
849 848
850 List<string> pointTags = request.targets.Select(item => item.target.Split(';').ToList()).Aggregate((acc, list) => { return acc.Concat(list).ToList(); });849 List<string> pointTags = request.targets.Select(item => item.target.Split(';').ToList()).Aggregate((acc, list) => acc.Concat(list).ToList());
851 string query = string.Join("),(", pointTags.Select(item => string.Format(SignalIDQuery, item.Trim())));850 string query = string.Join("),(", pointTags.Select(item => string.Format(SignalIDQuery, item.Trim())));
852 851
853 query = "((" + query + "))";852 query = "((" + query + "))";
854 853
855 using (AdoDataConnection connection = new AdoDataConnection("systemSettings"))854 using (AdoDataConnection connection = new AdoDataConnection("systemSettings"))
856 {855 {
857 query = string.Format("SignalID in {0}", query);856 query = $"SignalID in {query}";
858 return new TableOperations<GrafanaAlarm>(connection).QueryRecordsWhere(query).ToList();857 return new TableOperations<GrafanaAlarm>(connection).QueryRecordsWhere(query).ToList();
859 }858 }
860 },859 },
@@ -1177,12 +1176,12 @@ namespace GrafanaAdapters · 316 unmodified lines
1177 if (label.StartsWith("\"") || label.StartsWith("'"))1176 if (label.StartsWith("\"") || label.StartsWith("'"))
1178 label = label.Substring(1, label.Length - 2);1177 label = label.Substring(1, label.Length - 2);
1179 1178
1180 DataSourceValueGroup[] groups = dataset.ToArray();1179 DataSourceValueGroup[] valueGroups = dataset.ToArray();
1181 string[] seriesLabels = new string[groups.Length];1180 string[] seriesLabels = new string[valueGroups.Length];
1182 1181
1183 for (int i = 0; i < groups.Length; i++)1182 for (int i = 0; i < valueGroups.Length; i++)
1184 {1183 {
1185 string target = groups[i].RootTarget;1184 string target = valueGroups[i].RootTarget;
1186 1185
1187 seriesLabels[i] = TargetCache<string>.GetOrAdd($"{label}@{target}", () =>1186 seriesLabels[i] = TargetCache<string>.GetOrAdd($"{label}@{target}", () =>
1188 {1187 {
@@ -1210,27 +1209,34 @@ namespace GrafanaAdapters · 21 unmodified lines
1210 1209
1211 // ReSharper disable once AccessToModifiedClosure1210 // ReSharper disable once AccessToModifiedClosure
1212 if (derivedLabel.Equals(label, StringComparison.Ordinal))1211 if (derivedLabel.Equals(label, StringComparison.Ordinal))
1213 derivedLabel = $"{label}{(groups.Length > 1 ? $" {i + 1}" : "")}";1212 derivedLabel = $"{label}{(valueGroups.Length > 1 ? $" {i + 1}" : "")}";
1214 1213
1215 return derivedLabel;1214 return derivedLabel;
1216 });1215 });
1217 }1216 }
1218 1217
1219 // Verify that all series labels are unique1218 // Verify that all series labels are unique
1220 if (seriesLabels.Length > 1 && seriesLabels.All(seriesLabel => seriesLabel.Equals(seriesLabels[0], StringComparison.OrdinalIgnoreCase)))1219 if (seriesLabels.Length > 1)
1221 {1220 {
1222 for (int i = 1; i < seriesLabels.Length; i++)1221 HashSet<string> uniqueLabelSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
1223 seriesLabels[i] = $"{seriesLabels[i]}{new string('\u00A0', i)}";1222
1223 for (int i = 0; i < seriesLabels.Length; i++)
1224 {
1225 while (uniqueLabelSet.Contains(seriesLabels[i]))
1226 seriesLabels[i] = $"{seriesLabels[i]}\u00A0";
1227
1228 uniqueLabelSet.Add(seriesLabels[i]);
1229 }
1224 }1230 }
1225 1231
1226 for (int i = 0; i < groups.Length; i++)1232 for (int i = 0; i < valueGroups.Length; i++)
1227 {1233 {
1228 yield return new DataSourceValueGroup1234 yield return new DataSourceValueGroup
1229 {1235 {
1230 Target = seriesLabels[i],1236 Target = seriesLabels[i],
1231 RootTarget = groups[i].RootTarget,1237 RootTarget = valueGroups[i].RootTarget,
1232 SourceTarget = sourceTarget,1238 SourceTarget = sourceTarget,
1233 Source = groups[i].Source,1239 Source = valueGroups[i].Source,
1234 DropEmptySeries = dropEmptySeries1240 DropEmptySeries = dropEmptySeries
1235 };1241 };
1236 }1242 }
@@ -1241,7 +1247,7 @@ namespace GrafanaAdapters · 4 unmodified lines
1241 {1247 {
1242 case GroupOperation.Set:1248 case GroupOperation.Set:
1243 // Flatten all series into a single enumerable1249 // Flatten all series into a single enumerable
1244 DataSourceValueGroup result = new DataSourceValueGroup1250 DataSourceValueGroup setValueGroup = new DataSourceValueGroup
1245 {1251 {
1246 Target = $"Set{seriesFunction}({string.Join(", ", parameters)}{(parameters.Length > 0 ? ", " : "")}{queryExpression})",1252 Target = $"Set{seriesFunction}({string.Join(", ", parameters)}{(parameters.Length > 0 ? ", " : "")}{queryExpression})",
1247 RootTarget = queryExpression,1253 RootTarget = queryExpression,
@@ -1253,12 +1259,12 @@ namespace GrafanaAdapters · 5 unmodified lines
1253 // Handle edge-case set operations - for these functions there is data in the target series as well1259 // Handle edge-case set operations - for these functions there is data in the target series as well
1254 if (seriesFunction == SeriesFunction.Minimum || seriesFunction == SeriesFunction.Maximum || seriesFunction == SeriesFunction.Median)1260 if (seriesFunction == SeriesFunction.Minimum || seriesFunction == SeriesFunction.Maximum || seriesFunction == SeriesFunction.Median)
1255 {1261 {
1256 DataSourceValue dataValue = result.Source.First();1262 DataSourceValue dataValue = setValueGroup.Source.First();
1257 result.Target = $"Set{seriesFunction} = {dataValue.Target}";1263 setValueGroup.Target = $"Set{seriesFunction} = {dataValue.Target}";
1258 result.RootTarget = dataValue.Target;1264 setValueGroup.RootTarget = dataValue.Target;
1259 }1265 }
1260 1266
1261 yield return result;1267 yield return setValueGroup;
1262 1268
1263 break;1269 break;
1264 case GroupOperation.Slice:1270 case GroupOperation.Slice:
@@ -1266,27 +1272,27 @@ namespace GrafanaAdapters · 1 unmodified line
1266 parameters = parameters.Skip(1).ToArray();1272 parameters = parameters.Skip(1).ToArray();
1267 1273
1268 // Flatten all series into a single enumerable1274 // Flatten all series into a single enumerable
1269 foreach (DataSourceValueGroup seriesResult in ExecuteSeriesFunctionOverTimeSlices(scanner, seriesFunction, parameters))1275 foreach (DataSourceValueGroup sliceValueGroup in ExecuteSeriesFunctionOverTimeSlices(scanner, seriesFunction, parameters))
1270 {1276 {
1271 yield return new DataSourceValueGroup1277 yield return new DataSourceValueGroup
1272 {1278 {
1273 Target = $"Slice{seriesFunction}({string.Join(", ", parameters)}{(parameters.Length > 0 ? ", " : "")}{seriesResult.Target ?? queryExpression})",1279 Target = $"Slice{seriesFunction}({string.Join(", ", parameters)}{(parameters.Length > 0 ? ", " : "")}{sliceValueGroup.Target ?? queryExpression})",
1274 RootTarget = seriesResult.Target ?? queryExpression,1280 RootTarget = sliceValueGroup.Target ?? queryExpression,
1275 SourceTarget = sourceTarget,1281 SourceTarget = sourceTarget,
1276 Source = seriesResult.Source,1282 Source = sliceValueGroup.Source,
1277 DropEmptySeries = dropEmptySeries1283 DropEmptySeries = dropEmptySeries
1278 };1284 };
1279 }1285 }
1280 1286
1281 break;1287 break;
1282 default:1288 default:
1283 foreach (DataSourceValueGroup dataValues in dataset)1289 foreach (DataSourceValueGroup valueGroup in dataset)
1284 yield return new DataSourceValueGroup1290 yield return new DataSourceValueGroup
1285 {1291 {
1286 Target = $"{seriesFunction}({string.Join(", ", parameters)}{(parameters.Length > 0 ? ", " : "")}{dataValues.Target})",1292 Target = $"{seriesFunction}({string.Join(", ", parameters)}{(parameters.Length > 0 ? ", " : "")}{valueGroup.Target})",
1287 RootTarget = dataValues.RootTarget ?? dataValues.Target,1293 RootTarget = valueGroup.RootTarget ?? valueGroup.Target,
1288 SourceTarget = sourceTarget,1294 SourceTarget = sourceTarget,
1289 Source = ExecuteSeriesFunctionOverSource(dataValues.Source, seriesFunction, parameters),1295 Source = ExecuteSeriesFunctionOverSource(valueGroup.Source, seriesFunction, parameters),
1290 DropEmptySeries = dropEmptySeries1296 DropEmptySeries = dropEmptySeries
1291 };1297 };
1292 1298

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t