Skip to content

Commit 704bd1a

Browse files
committed
updates to cache and output the instance display name in otlp output
1 parent 7fd16c1 commit 704bd1a

2 files changed

Lines changed: 70 additions & 7 deletions

File tree

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/iotdbutils/DeviceModelInstancesCache.java

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,25 @@ Software and the Larger Work(s), and to sublicense the foregoing rights on
6565
@Singleton
6666
public class DeviceModelInstancesCache {
6767
private static final String INSTANCE_ID_COLUMN_NAME = "instanceid";
68+
private static final String INSTANCE_ID_COLUMN_DISPLAY_NAME = "displayname";
6869
private static final String EXTERNAL_KEY_COLUMN_NAME = "externalkey";
6970
private static final String MODEL_ID_COLUMN_NAME = "modelid";
7071
private static final String MODEL_NAME_COLUMN_NAME = "modelname";
7172
// these are used for bulk pre-loading of entries
7273
public final static String SELECT_MODEL_IDS_AND_MODEL_NAMES = "SELECT JSON_VALUE (dtm.data, '$._id' ) AS modelid, JSON_VALUE(dtm.data, '$.displayName' ) AS modelname FROM digital_twin_models dtm";
73-
public final static String SELECT_MODEL_ID_EXTERNAL_KEY_AND_INSTANCE_ID = "SELECT JSON_VALUE (dti.data, '$._id' ) AS instanceid, JSON_VALUE (dti.data, '$.digitalTwinModelId' ) AS modelid, JSON_VALUE (dti.data, '$.externalKey' ) AS externalkey FROM digital_twin_instances dti";
74+
public final static String SELECT_MODEL_ID_EXTERNAL_KEY_DISPLAY_NAME_AND_INSTANCE_ID = "SELECT JSON_VALUE (dti.data, '$._id' ) AS instanceid, JSON_VALUE (dti.data, '$.digitalTwinModelId' ) AS modelid, JSON_VALUE (dti.data, '$.externalKey' ) AS externalkey, JSON_VALUE(dti.data, '$.displayName' ) AS displayname FROM digital_twin_instances dti";
7475
// we we don't know about this then we will try an individual load
7576
public final static String SELECT_MODEL_NAME_BY_MODEL_ID = "SELECT JSON_VALUE(dtm.data, '$.displayName' ) AS modelname FROM digital_twin_models dtm WHERE JSON_VALUE (dtm.data, '$._id' ) = ? ";
7677
public final static String SELECT_MODEL_ID_BY_MODEL_NAME = "SELECT JSON_VALUE (dtm.data, '$._id' ) AS modelid FROM digital_twin_models dtm WHERE JSON_VALUE(dtm.data, '$.displayName' ) = ? ";
77-
public final static String SELECT_MODEL_ID_AND_EXTERNAL_KEY_BY_INSTANCE_ID = "SELECT JSON_VALUE (dti.data, '$.digitalTwinModelId' ) AS modelid, JSON_VALUE (dti.data, '$.externalKey' ) AS externalkey FROM digital_twin_instances dti WHERE JSON_VALUE(dti.data, '$._id' ) = ?";
78+
public final static String SELECT_MODEL_ID_DISPLAY_NAME_AND_EXTERNAL_KEY_BY_INSTANCE_ID = "SELECT JSON_VALUE (dti.data, '$.digitalTwinModelId' ) AS modelid, JSON_VALUE (dti.data, '$.externalKey' ) AS externalkey, JSON_VALUE(dti.data, '$.displayName' ) FROM digital_twin_instances dti WHERE JSON_VALUE(dti.data, '$._id' ) = ?";
7879

7980
private final String schemaName;
8081
private final DBConnectionSupplier dbConnectionSupplier;
8182
private Connection connection;
8283

8384
private final Map<String, String> instanceIdToModelId = Collections.synchronizedMap(new HashMap<>());
8485
private final Map<String, String> instanceIdToExternalKey = Collections.synchronizedMap(new HashMap<>());
86+
private final Map<String, String> instanceIdToInstanceName = Collections.synchronizedMap(new HashMap<>());
8587
private final Map<String, String> instanceIdToModelName = Collections.synchronizedMap(new HashMap<>());
8688
private final Map<String, String> externalKeyToInstanceId = Collections.synchronizedMap(new HashMap<>());
8789
private final Map<String, String> modelIdToModelName = Collections.synchronizedMap(new HashMap<>());
@@ -147,7 +149,8 @@ public void configure() throws Exception {
147149
// set this up so we can re-use it later if we need to query for an instance we
148150
// didn't know about
149151
log.fine("Creating prepared statements");
150-
selectModelIdByInstanceIdPS = connection.prepareStatement(SELECT_MODEL_ID_AND_EXTERNAL_KEY_BY_INSTANCE_ID);
152+
selectModelIdByInstanceIdPS = connection
153+
.prepareStatement(SELECT_MODEL_ID_DISPLAY_NAME_AND_EXTERNAL_KEY_BY_INSTANCE_ID);
151154
selectModelNameByModelIdPS = connection.prepareStatement(SELECT_MODEL_NAME_BY_MODEL_ID);
152155
selectModelIdByModelNamePS = connection.prepareStatement(SELECT_MODEL_ID_BY_MODEL_NAME);
153156
log.fine("Prepared statements created");
@@ -349,15 +352,17 @@ private String loadModelByModelName(@NotNull @NotEmpty String modelName)
349352
private void preloadExistingInstances() throws SQLException {
350353
// get all of the results
351354
try (Statement s = connection.createStatement();
352-
ResultSet rs = s.executeQuery(SELECT_MODEL_ID_EXTERNAL_KEY_AND_INSTANCE_ID)) {
355+
ResultSet rs = s.executeQuery(SELECT_MODEL_ID_EXTERNAL_KEY_DISPLAY_NAME_AND_INSTANCE_ID)) {
353356
while (rs.next()) {
354357
String modelIdExistingInstance = rs.getString(MODEL_ID_COLUMN_NAME);
355358
String instanceIdExistingInstance = rs.getString(INSTANCE_ID_COLUMN_NAME);
356359
String externalKeyExistingInstance = rs.getString(EXTERNAL_KEY_COLUMN_NAME);
360+
String instanceDisplayName = rs.getString(INSTANCE_ID_COLUMN_DISPLAY_NAME);
357361
String modelName = modelIdToModelName.get(modelIdExistingInstance);
358362
instanceIdToModelId.put(instanceIdExistingInstance, modelIdExistingInstance);
359-
log.info("Added instance id " + instanceIdExistingInstance + " to modelId " + modelIdExistingInstance
360-
+ " mapping");
363+
instanceIdToInstanceName.put(instanceIdExistingInstance, instanceDisplayName);
364+
log.info("Added instance id " + instanceIdExistingInstance + " named " + instanceDisplayName
365+
+ " to modelId " + modelIdExistingInstance + " mapping");
361366
instanceIdToModelName.put(instanceIdExistingInstance, modelName);
362367
log.info("Added instance id " + instanceIdExistingInstance + " to modelName " + modelName + " mapping");
363368
instanceIdToExternalKey.put(instanceIdExistingInstance, externalKeyExistingInstance);
@@ -421,6 +426,54 @@ public String getModelIdByInstanceId(@NotNull @NotEmpty String instanceId, boole
421426
}
422427
}
423428

429+
/**
430+
* try to get the modelId from the cache or if there is no cachedata
431+
*
432+
* @param instanceId the instance to locate
433+
* @param cacheMissingResults if true and we already have looked but not found
434+
* this then don't look again
435+
* @return the instance display name is there is one
436+
* @throws MissingInstanceException throws an exception if we can't locate the
437+
* instance (either in the known missing cache
438+
* if cacheMissingResults is true, or in the
439+
* IoT service otherwise)
440+
* @throws SQLException if there was a problem querying the iot
441+
* service
442+
*/
443+
public String getInstanceDisplayNameByInstanceId(@NotNull @NotEmpty String instanceId, boolean cacheMissingResults)
444+
throws MissingInstanceException, SQLException {
445+
boolean knownMissing = foundMissingInstanceIds.contains(instanceId);
446+
// are we looking at the cache ?
447+
if (cacheMissingResults && knownMissing) {
448+
// we know it's missing, and we are not checking an other time
449+
throw new MissingInstanceException(
450+
"No instance found in cache and not checking again for instanceid" + instanceId);
451+
}
452+
// do we already have the info ? note that empty string and null are valid
453+
// responses here.
454+
if (instanceIdToExternalKey.containsKey(instanceId)) {
455+
// we have the key, the model could be a string, null blank etc if one hasn't
456+
// been set, but that's still valid.
457+
return instanceIdToExternalKey.get(instanceId);
458+
}
459+
// we don't have a cached version
460+
// let's try and locate it
461+
try {
462+
InstanceKeyInfo ike = loadInstanceByInstanceId(instanceId);
463+
// OK we have something, was it previously tagged as knownMissing ? if so remove
464+
// the id
465+
if (knownMissing) {
466+
foundMissingInstanceIds.remove(instanceId);
467+
}
468+
return ike.getInstanceDisplayName();
469+
} catch (MissingInstanceException e) {
470+
// cache the missing result for later use
471+
foundMissingInstanceIds.add(instanceId);
472+
// then throw the exception
473+
throw e;
474+
}
475+
}
476+
424477
/**
425478
* try to get the external key from the cache or if there is no cachedata
426479
*
@@ -522,12 +575,14 @@ private InstanceKeyInfo loadInstanceByInstanceId(@NotNull @NotEmpty String insta
522575
if (rs.next()) {
523576
String modelId = rs.getString(MODEL_ID_COLUMN_NAME);
524577
String externalKey = rs.getString(EXTERNAL_KEY_COLUMN_NAME);
578+
String instanceDisplayName = rs.getString(INSTANCE_ID_COLUMN_DISPLAY_NAME);
525579
String modelName = modelIdToModelName.get(modelId);
526580
instanceIdToModelId.put(instanceId, modelId);
527581
instanceIdToModelName.put(instanceId, modelName);
528582
instanceIdToExternalKey.put(instanceId, externalKey);
583+
instanceIdToInstanceName.put(instanceId, instanceDisplayName);
529584
externalKeyToInstanceId.put(externalKey, instanceId);
530-
return new InstanceKeyInfo(instanceId, modelId, externalKey);
585+
return new InstanceKeyInfo(instanceId, modelId, externalKey, instanceDisplayName);
531586
} else {
532587
throw new MissingInstanceException("No instance found for instance id " + instanceId);
533588
}
@@ -544,6 +599,7 @@ private class InstanceKeyInfo {
544599
String instanceId;
545600
String modelId;
546601
String externalKey;
602+
String instanceDisplayName;
547603
}
548604

549605
public void unconfigure() throws Exception {

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/outputs/http/normalizeddata/timeseriesdb/TimeSeriesDBOutputOTLP.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ private void addResourceAttributes(NormalizedDataMetricsDataBuilder builder, Nor
163163
String modelId = deviceModelInstancesCache.getModelIdByInstanceId(instanceId, true);
164164
builder.resourceAttribute("iot.digital_twin.model_id", modelId);
165165
log.info("Added model ID resource attribute of " + modelId);
166+
String instanceDisplayName = deviceModelInstancesCache.getInstanceDisplayNameByInstanceId(instanceId, true);
167+
if (instanceDisplayName != null) {
168+
builder.resourceAttribute("iot.digital_twin.display_name", instanceDisplayName);
169+
log.info("Added instance display name of " + instanceDisplayName);
170+
} else {
171+
log.info("Can't locate instance display name for instance " + instanceId);
172+
}
166173
String modelName;
167174
try {
168175
modelName = deviceModelInstancesCache.getModelNameByModelId(modelId, true);

0 commit comments

Comments
 (0)