Skip to content

Commit

Permalink
[Fleet] Make status runtime query more robust (#211639)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Feb 18, 2025
1 parent f84ea79 commit 0180301
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('buildStatusRuntimeField', () => {
"status": Object {
"script": Object {
"lang": "painless",
"source": " long lastCheckinMillis = doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (doc['active'].size() > 0 && doc['active'].value == false) { emit('unenrolled'); } else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
"source": " long lastCheckinMillis = doc.containsKey('last_checkin') && doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (!doc.containsKey('active') || (doc['active'].size() > 0 && doc['active'].value == false)) { emit('unenrolled'); } else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
},
"type": "keyword",
},
Expand All @@ -36,7 +36,7 @@ describe('buildStatusRuntimeField', () => {
"status": Object {
"script": Object {
"lang": "painless",
"source": " long lastCheckinMillis = doc['my.prefix.last_checkin'].size() > 0 ? doc['my.prefix.last_checkin'].value.toInstant().toEpochMilli() : ( doc['my.prefix.enrolled_at'].size() > 0 ? doc['my.prefix.enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (doc['my.prefix.active'].size() > 0 && doc['my.prefix.active'].value == false) { emit('unenrolled'); } else if (doc.containsKey('audit_unenrolled_reason') && doc['my.prefix.audit_unenrolled_reason'].size() > 0 && doc['my.prefix.audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['my.prefix.audit_unenrolled_reason'].size() > 0 && doc['my.prefix.audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['my.prefix.policy_revision_idx'].size() == 0 || ( doc['my.prefix.upgrade_started_at'].size() > 0 && doc['my.prefix.upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['my.prefix.last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['my.prefix.unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['my.prefix.last_checkin_status'].size() > 0 && doc['my.prefix.last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['my.prefix.last_checkin_status'].size() > 0 && doc['my.prefix.last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
"source": " long lastCheckinMillis = doc.containsKey('my.prefix.last_checkin') && doc['my.prefix.last_checkin'].size() > 0 ? doc['my.prefix.last_checkin'].value.toInstant().toEpochMilli() : ( doc['my.prefix.enrolled_at'].size() > 0 ? doc['my.prefix.enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (!doc.containsKey('my.prefix.active') || (doc['my.prefix.active'].size() > 0 && doc['my.prefix.active'].value == false)) { emit('unenrolled'); } else if (doc.containsKey('audit_unenrolled_reason') && doc['my.prefix.audit_unenrolled_reason'].size() > 0 && doc['my.prefix.audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['my.prefix.audit_unenrolled_reason'].size() > 0 && doc['my.prefix.audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['my.prefix.policy_revision_idx'].size() == 0 || ( doc['my.prefix.upgrade_started_at'].size() > 0 && doc['my.prefix.upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['my.prefix.last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['my.prefix.unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['my.prefix.last_checkin_status'].size() > 0 && doc['my.prefix.last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['my.prefix.last_checkin_status'].size() > 0 && doc['my.prefix.last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
},
"type": "keyword",
},
Expand All @@ -56,7 +56,7 @@ describe('buildStatusRuntimeField', () => {
"status": Object {
"script": Object {
"lang": "painless",
"source": " long lastCheckinMillis = doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (doc['active'].size() > 0 && doc['active'].value == false) { emit('unenrolled'); } else if (lastCheckinMillis > 0 && doc.containsKey('policy_id') && doc['policy_id'].size() > 0 && ['policy-1'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567590123L) {emit('inactive');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
"source": " long lastCheckinMillis = doc.containsKey('last_checkin') && doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (!doc.containsKey('active') || (doc['active'].size() > 0 && doc['active'].value == false)) { emit('unenrolled'); } else if (lastCheckinMillis > 0 && doc.containsKey('policy_id') && doc['policy_id'].size() > 0 && ['policy-1'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567590123L) {emit('inactive');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
},
"type": "keyword",
},
Expand All @@ -76,7 +76,7 @@ describe('buildStatusRuntimeField', () => {
"status": Object {
"script": Object {
"lang": "painless",
"source": " long lastCheckinMillis = doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (doc['active'].size() > 0 && doc['active'].value == false) { emit('unenrolled'); } else if (lastCheckinMillis > 0 && doc.containsKey('policy_id') && doc['policy_id'].size() > 0 && ['policy-1','policy-2'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567590123L) {emit('inactive');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
"source": " long lastCheckinMillis = doc.containsKey('last_checkin') && doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (!doc.containsKey('active') || (doc['active'].size() > 0 && doc['active'].value == false)) { emit('unenrolled'); } else if (lastCheckinMillis > 0 && doc.containsKey('policy_id') && doc['policy_id'].size() > 0 && ['policy-1','policy-2'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567590123L) {emit('inactive');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
},
"type": "keyword",
},
Expand All @@ -100,7 +100,7 @@ describe('buildStatusRuntimeField', () => {
"status": Object {
"script": Object {
"lang": "painless",
"source": " long lastCheckinMillis = doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (doc['active'].size() > 0 && doc['active'].value == false) { emit('unenrolled'); } else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
"source": " long lastCheckinMillis = doc.containsKey('last_checkin') && doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (!doc.containsKey('active') || (doc['active'].size() > 0 && doc['active'].value == false)) { emit('unenrolled'); } else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
},
"type": "keyword",
},
Expand All @@ -124,7 +124,7 @@ describe('buildStatusRuntimeField', () => {
"status": Object {
"script": Object {
"lang": "painless",
"source": " long lastCheckinMillis = doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (doc['active'].size() > 0 && doc['active'].value == false) { emit('unenrolled'); } else if (lastCheckinMillis > 0 && doc.containsKey('policy_id') && doc['policy_id'].size() > 0 && ['policy-1','policy-2'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567590123L || ['policy-3'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567490123L) {emit('inactive');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
"source": " long lastCheckinMillis = doc.containsKey('last_checkin') && doc['last_checkin'].size() > 0 ? doc['last_checkin'].value.toInstant().toEpochMilli() : ( doc['enrolled_at'].size() > 0 ? doc['enrolled_at'].value.toInstant().toEpochMilli() : -1 ); if (!doc.containsKey('active') || (doc['active'].size() > 0 && doc['active'].value == false)) { emit('unenrolled'); } else if (lastCheckinMillis > 0 && doc.containsKey('policy_id') && doc['policy_id'].size() > 0 && ['policy-1','policy-2'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567590123L || ['policy-3'].contains(doc['policy_id'].value) && lastCheckinMillis < 1234567490123L) {emit('inactive');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'uninstall'){emit('uninstalled');} else if (doc.containsKey('audit_unenrolled_reason') && doc['audit_unenrolled_reason'].size() > 0 && doc['audit_unenrolled_reason'].value == 'orphaned'){emit('orphaned');} else if ( lastCheckinMillis > 0 && lastCheckinMillis < 1234567590123L ) { emit('offline'); } else if ( doc['policy_revision_idx'].size() == 0 || ( doc['upgrade_started_at'].size() > 0 && doc['upgraded_at'].size() == 0 ) ) { emit('updating'); } else if (doc['last_checkin'].size() == 0) { emit('enrolling'); } else if (doc['unenrollment_started_at'].size() > 0) { emit('unenrolling'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'error' ) { emit('error'); } else if ( doc['last_checkin_status'].size() > 0 && doc['last_checkin_status'].value.toLowerCase() == 'degraded' ) { emit('degraded'); } else { emit('online'); }",
},
"type": "keyword",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,18 @@ function _buildSource(
});

return `
long lastCheckinMillis = ${field('last_checkin')}.size() > 0
long lastCheckinMillis = doc.containsKey(${fieldPath('last_checkin')}) && ${field(
'last_checkin'
)}.size() > 0
? ${field('last_checkin')}.value.toInstant().toEpochMilli()
: (
${field('enrolled_at')}.size() > 0
? ${field('enrolled_at')}.value.toInstant().toEpochMilli()
: -1
);
if (${field('active')}.size() > 0 && ${field('active')}.value == false) {
if (!doc.containsKey(${fieldPath('active')}) || (${field('active')}.size() > 0 && ${field(
'active'
)}.value == false)) {
emit('unenrolled');
}
${agentIsInactiveCondition ? `else if (${agentIsInactiveCondition}) {emit('inactive');}` : ''}
Expand Down

0 comments on commit 0180301

Please sign in to comment.