Skip to content

Instantly share code, notes, and snippets.

@Tustin
Last active April 13, 2017 19:24
Show Gist options
  • Save Tustin/3f1ae878eb3252467bd3ec408415505c to your computer and use it in GitHub Desktop.
Save Tustin/3f1ae878eb3252467bd3ec408415505c to your computer and use it in GitHub Desktop.
<?php
class TunablesDiff {
//tunable value existed before, but it's value was changed
const TUNABLE_VALUE_CHANGED = 1;
//tunable value didn't exist before and was just added
const TUNABLE_NEW = 2;
//tunable was removed from the new version
const TUNABLE_REMOVED = 3;
public static function SortTunables($tunableJson) {
$temp = json_decode($tunableJson, true);
$tempTunables = [];
foreach ($temp['tunables'] as $key => $tunableType) {
foreach ($tunableType as $tunableValue) {
$tempTunables[$key] = $tunableValue['value'];
}
}
return $tempTunables;
}
public static function CompareTunables($newTunablesArray, $oldTunablesArray) {
$changedTunables = [];
foreach ($newTunablesArray as $newTunablesName => $newTunablesValue) {
//tunable is new, so add it to the list as a new one
if (!array_key_exists($newTunablesName, $oldTunablesArray)) {
$changedTunables[$newTunablesName] = [
"newValue" => $newTunablesValue,
"type" => self::TUNABLE_NEW
];
} else {
//tunable exists but was changed, so add it as a changed value
if ($newTunablesValue != $oldTunablesArray[$newTunablesName]) {
$changedTunables[$newTunablesName] = [
"newValue" => $newTunablesValue,
"oldValue" => $oldTunablesArray[$newTunablesName],
"type" => self::TUNABLE_VALUE_CHANGED
];
}
}
}
foreach ($oldTunablesArray as $oldTunablesName => $oldTunablesValue) {
if (!array_key_exists($oldTunablesName, $newTunablesArray)) {
$changedTunables[$oldTunablesName] = [
"oldValue" => $oldTunablesValue,
"type" => self::TUNABLE_REMOVED
];
}
}
return $changedTunables;
}
}
//pulled from ros cloud
$newTunables = TunablesDiff::SortTunables(file_get_contents('newest.json'));
//pull the last stored json from database or whatever
$oldTunables = TunablesDiff::SortTunables(file_get_contents('old.json'));
$diff = TunablesDiff::CompareTunables($newTunables, $oldTunables);
foreach ($diff as $changedTunableName => $changedTunable) {
if ($changedTunable['type'] == TunablesDiff::TUNABLE_NEW) {
echo "$changedTunableName has been added with value $changedTunable[newValue]\n";
} else if ($changedTunable['type'] == TunablesDiff::TUNABLE_VALUE_CHANGED) {
echo "$changedTunableName is now $changedTunable[newValue] (was $changedTunable[oldValue])\n";
} else if ($changedTunable['type'] == TunablesDiff::TUNABLE_REMOVED) {
echo "$changedTunableName was removed (old value was $changedTunable[oldValue])\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment