All files / src environment.js

97.98% Statements 97/99
83.33% Branches 50/60
86.67% Functions 13/15
97.98% Lines 97/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263                1x             57x   57x                       62x     62x 62x       98x   98x               98x 2x               96x 43x           53x 53x   53x 35x 35x             18x 18x 18x 18x 18x   18x 30x 30x           30x 30x 6x 6x       18x             49x 33x     16x 2x     14x 2x     12x 2x     10x       49x 49x                       22x 22x 22x   22x 22x 1x     21x 21x   21x         21x           21x   21x 21x 21x           6x 6x 6x 6x 6x 1x     5x 5x   5x         5x           5x   5x 5x 5x           7x 7x 7x   7x 7x 1x     6x 6x   6x         6x           6x   6x 6x 6x       21x 21x 21x   21x 21x 4x     17x 17x   17x         17x           17x   17x 17x 17x      
import {logger} from './logger'
import AirshipObject from './object'
import Population from './population'
import Stat from './stat'
import Flag from './flag'
 
export default class Environment {
  identify(obj) {
    this.object = obj
  }
 
  async maybeIngest() {}
 
  _identifyObject(obj) {
    const airshipObj =
      obj instanceof AirshipObject ? obj : new AirshipObject(obj)
 
    return airshipObj
  }
 
  _saveStat() {}
 
  _saveExposure() {}
 
  async publish(objs) {} // eslint-disable-line no-unused-vars
 
  shutdown() {}
 
  flag(flagName) {
    const flag = this.router
      ? this.router.getFlag(flagName)
      : new Flag(flagName)
    flag.setDelegate(this)
    return flag
  }
 
  _getAllocation(flag, airshipObj) {
    const offTreatment = flag.offTreatment
 
    Iif (flag.isArchived()) {
      logger(`The flag "${flag.codename}" has been archived`)
      return {
        treatment: offTreatment,
        eligible: false
      }
    }
 
    if (flag.isPaused) {
      return {
        treatment: offTreatment,
        eligible: false
      }
    }
 
    // If the airshipObj is not a valid obj,
    // then return the offTreatment/false
    if (!airshipObj) {
      return {
        treatment: offTreatment,
        eligible: false
      }
    }
 
    const id = airshipObj.getId()
    const override = flag.overrides[id]
 
    if (override) {
      const treatment = flag.treatmentsMap[override.treatmentId]
      return {
        treatment: treatment,
        eligible: !treatment.isOffTreatment,
        fromOverride: true
      }
    }
 
    const obj = airshipObj.getRawObject()
    const useUniverses = flag.getType() === 'experiment'
    const populations = flag.populations
    let treatment = null
    let eligible = false
 
    for (let i = 0; i < populations.length; i++) {
      const p = new Population(populations[i])
      const gateValues = p.getGateValues(
        obj,
        this.router.getEnv(),
        flag,
        useUniverses
      )
      eligible = eligible || gateValues.eligible
      if (gateValues.treatment) {
        treatment = gateValues.treatment
        break
      }
    }
 
    return {
      treatment: treatment || offTreatment,
      eligible: eligible
    }
  }
 
  _resolveAllocations(alloc1, alloc2) {
    if (alloc1.fromOverride) {
      return alloc1
    }
 
    if (alloc2.fromOverride) {
      return alloc2
    }
 
    if (!alloc1.treatment.isOffTreatment) {
      return alloc1
    }
 
    if (!alloc2.treatment.isOffTreatment) {
      return alloc2
    }
 
    return alloc1
  }
 
  _getExposure(flag, airshipObj, alloc, methodCalled) {
    const obj = airshipObj.getRawObject()
    return {
      flag: flag.codename,
      type: obj.type,
      id: obj.id,
      treatment: alloc.treatment.codename,
      methodCalled: methodCalled,
      eligible: alloc.eligible,
      timeExposed: new Date().toISOString()
    }
  }
 
  getTreatment(flag, obj) {
    const stat = new Stat('duration__get_treatment', Stat.TYPE_DURATION)
    stat.start()
    obj = obj || this.object
 
    const airshipObj = this._identifyObject(obj)
    if (!airshipObj.isValid() || flag.isUncategorized()) {
      return 'off'
    }
 
    const allocation = this._getAllocation(flag, airshipObj)
    const groupAllocation = this._getAllocation(flag, airshipObj.getGroup())
 
    const finalAllocation = this._resolveAllocations(
      allocation,
      groupAllocation
    )
 
    const expo = this._getExposure(
      flag,
      airshipObj,
      finalAllocation,
      'get_treatment'
    )
    this._saveExposure(expo)
 
    stat.stop()
    this._saveStat(stat)
    return finalAllocation.treatment.isGhost
      ? (flag.offTreatment && flag.offTreatment.codename) || 'off'
      : finalAllocation.treatment.codename
  }
 
  getPayload(flag, obj) {
    const stat = new Stat('duration__get_payload', Stat.TYPE_DURATION)
    stat.start()
    obj = obj || this.object
    const airshipObj = this._identifyObject(obj)
    if (!airshipObj.isValid() || flag.isUncategorized()) {
      return null
    }
 
    const allocation = this._getAllocation(flag, airshipObj)
    const groupAllocation = this._getAllocation(flag, airshipObj.getGroup())
 
    const finalAllocation = this._resolveAllocations(
      allocation,
      groupAllocation
    )
 
    const expo = this._getExposure(
      flag,
      airshipObj,
      finalAllocation,
      'get_payload'
    )
    this._saveExposure(expo)
 
    stat.stop()
    this._saveStat(stat)
    return finalAllocation.treatment.isGhost
      ? (flag.offTreatment && flag.offTreatment.payload) || null
      : finalAllocation.treatment.payload
  }
 
  isEligible(flag, obj) {
    const stat = new Stat('duration__is_eligible', Stat.TYPE_DURATION)
    stat.start()
    obj = obj || this.object
 
    const airshipObj = this._identifyObject(obj)
    if (!airshipObj.isValid() || flag.isUncategorized()) {
      return false
    }
 
    const allocation = this._getAllocation(flag, airshipObj)
    const groupAllocation = this._getAllocation(flag, airshipObj.getGroup())
 
    const finalAllocation = this._resolveAllocations(
      allocation,
      groupAllocation
    )
 
    const expo = this._getExposure(
      flag,
      airshipObj,
      finalAllocation,
      'is_eligible'
    )
    this._saveExposure(expo)
 
    stat.stop()
    this._saveStat(stat)
    return finalAllocation.eligible
  }
 
  isEnabled(flag, obj) {
    const stat = new Stat('duration__is_enabled', Stat.TYPE_DURATION)
    stat.start()
    obj = obj || this.object
 
    const airshipObj = this._identifyObject(obj)
    if (!airshipObj.isValid() || flag.isUncategorized()) {
      return false
    }
 
    const allocation = this._getAllocation(flag, airshipObj)
    const groupAllocation = this._getAllocation(flag, airshipObj.getGroup())
 
    const finalAllocation = this._resolveAllocations(
      allocation,
      groupAllocation
    )
 
    const expo = this._getExposure(
      flag,
      airshipObj,
      finalAllocation,
      'is_enabled'
    )
    this._saveExposure(expo)
 
    stat.stop()
    this._saveStat(stat)
    return !finalAllocation.treatment.isOffTreatment
  }
}