コード例 #1
0
def update_history(history, upgrade_policies):
    """Update history with information from clusters
    with upgrade policies.

    Args:
        history (dict): history in the following format:
        {
          "check_in": "2021-08-29 18:01:27.730441",
          "versions": {
            "version1": {
                "workloads": {
                    "workload1": {
                        "soak_days": 21,
                        "reporting": [
                            "cluster1",
                            "cluster2"
                        ]
                    },
                        "workload2": {
                        "soak_days": 6,
                        "reporting": [
                            "cluster3"
                        ]
                    }
                }
            }
          }
        }
        upgrade_policies (list): query results of clusters upgrade policies
    """
    default_workload_history = {
        'soak_days': 0.0,
        'reporting': [],
    }

    now = datetime.utcnow()
    check_in = parser.parse(get_or_init(history, 'check_in', str(now)))
    versions = get_or_init(history, 'versions', {})

    # we iterate over clusters upgrade policies and update the version history
    for item in upgrade_policies:
        current_version = item['current_version']
        version_history = get_or_init(versions, current_version, {})
        version_workloads = get_or_init(version_history, 'workloads', {})
        cluster = item['cluster']
        workloads = item['workloads']
        # we keep the version history per workload
        for w in workloads:
            workload_history = get_or_init(
                version_workloads, w,
                copy.deepcopy(default_workload_history))

            reporting = workload_history['reporting']
            # if the cluster is already reporting - accumulate it.
            # if not - add it to the reporting list (first report)
            if cluster in reporting:
                workload_history['soak_days'] += \
                    (now - check_in).total_seconds() / 86400  # seconds in day
            else:
                workload_history['reporting'].append(cluster)

    history['check_in'] = str(now)
コード例 #2
0
 def test_get_or_init_get(self):
     d = {"k": "v"}
     self.assertEqual(ds.get_or_init(d, "k", "notv"), "v")
コード例 #3
0
 def test_get_or_init_get(self):
     d = {'k': 'v'}
     self.assertEqual(ds.get_or_init(d, 'k', 'notv'), 'v')
コード例 #4
0
 def test_get_or_init_init(self):
     d = {}
     self.assertEqual(ds.get_or_init(d, "k", "v"), "v")
コード例 #5
0
 def test_get_or_init_init(self):
     d = {}
     self.assertEqual(ds.get_or_init(d, 'k', 'v'), 'v')