mergeEnvMappings.tsx 878 B

1234567891011121314151617181920212223242526
  1. import type {MonitorBucketEnvMapping, StatsBucket} from '../types';
  2. import {CHECKIN_STATUS_PRECEDENT} from './constants';
  3. /**
  4. * Given two env mappings e.g. {prod: {ok: 1, missed: 0, etc..}}
  5. * combines job status counts
  6. */
  7. export function mergeEnvMappings(
  8. envMappingA: MonitorBucketEnvMapping,
  9. envMappingB: MonitorBucketEnvMapping
  10. ): MonitorBucketEnvMapping {
  11. const combinedEnvs = new Set([
  12. ...Object.keys(envMappingA),
  13. ...Object.keys(envMappingB),
  14. ]);
  15. return [...combinedEnvs].reduce<MonitorBucketEnvMapping>((mergedEnvs, env) => {
  16. const mergedStatusCounts: Partial<StatsBucket> = {};
  17. for (const status of CHECKIN_STATUS_PRECEDENT) {
  18. mergedStatusCounts[status] =
  19. (envMappingA[env]?.[status] ?? 0) + (envMappingB[env]?.[status] ?? 0);
  20. }
  21. mergedEnvs[env] = mergedStatusCounts as StatsBucket;
  22. return mergedEnvs;
  23. }, {});
  24. }