updateThreatWeights method

  1. @override
Future<void> updateThreatWeights()
override

STORE :Global:threats

Implementation

@override
Future<void> updateThreatWeights() async {
  log('[UPDATE THREAT WEIGHTS]');
  log("CHECK PARENT NODE: :GLOBAL:PROFILES");
  try {
    // ignore: unused_local_variable
    toolbox_api.Node profileChecker = await getNode(':Global:profiles');
  } catch (e) {
    /// CREATE NODE FOR TYPE of TLP WHITE EVENT
    toolbox_api.Node profileChecker =
        toolbox_api.NodeImpl(':Global:profiles', _replicationAPI);
    toolbox_api.Visibility? checkerVisible =
        toolbox_api.VisibilityExtension.valueOf("white");
    if (checkerVisible != null) {
      profileChecker.visibility = checkerVisible;
    }
    await _storageController.add(profileChecker);
  }

  log("CHECK PARENT NODES SUB NODE - DIGITALLY DEPENDENT, DIGITALLY BASED, DIGITAL ENABLER");
  List<String> profiles = [
    "digitally dependent",
    "digitally based",
    "digitally enabler"
  ];
  List<toolbox_api.Node> profileNodes = [];
  for (var profile in profiles) {
    /// search criteria for each profile
    toolbox_api.SearchCriteria profileCriteria = toolbox_api.SearchCriteria(
        searchPath: ':Global:profiles:', key: 'name', value: profile);
    List<toolbox_api.Node> scProf =
        await _storageController.search(profileCriteria);
    if (scProf.isNotEmpty) {
      profileNodes.add(scProf[0]);
    } else {
      /// CREATE NEW
      String newUUID = Uuid().v4();
      toolbox_api.Node newProfile =
          toolbox_api.NodeImpl(':Global:profiles:$newUUID', _replicationAPI);
      newProfile.addOrUpdateValue(toolbox_api.NodeValueImpl('name', profile));
      await _storageController.add(newProfile);
      profileNodes.add(newProfile);
    }
  }
  List<ThreatWeights> weights = await cloud.getThreatWeights();
  for (var weight in weights) {
    ThreatDict? data = weight.threatDict;
    if (data != null) {
      Map<String, dynamic> mapper = data.toJson();
      for (var d in mapper.entries) {
        String key = d.key;
        List<double> value = d.value;
        //mapper.forEach((key, value) async {
        toolbox_api.SearchCriteria criteria = toolbox_api.SearchCriteria(
            searchPath: ':Global:threats:',
            key: 'GEIGER_threat',
            value: '${key[0].toUpperCase()}${key.substring(1)}');
        List<toolbox_api.Node> scNode =
            await _storageController.search(criteria);
        List<double> val = value;
        String path;
        if (scNode.isNotEmpty) {
          for (var element in scNode) {
            toolbox_api.Node newThreat = element;
            path = newThreat.name;
            toolbox_api.NodeValue? nameVal =
                (await newThreat.getValue('name'));
            toolbox_api.NodeValue? threatVal =
                (await newThreat.getValue('GEIGER_threat'));
            if (nameVal!.value == threatVal!.value) {
              int i = 0;
              for (var v in val) {
                profileNodes[i].addOrUpdateValue(
                    toolbox_api.NodeValueImpl(path, v.toString()));
                i++;
              }
            }
          }
        }
      }
      //});
    }
  }

  /// store updated nodes in toolbox
  log("STORE/UPDATE PROFILE NODES");
  for (var node in profileNodes) {
    await _storageController.addOrUpdate(node);
  }
}