All files / src usagelog.js

87.5% Statements 28/32
50% Branches 2/4
90% Functions 9/10
86.67% Lines 26/30

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 1059x 9x 9x 9x         1x 1x             4x 2x         2x               2x         2x   2x 2x 2x     2x               1x 1x                   1x     1x 2x 2x     1x   1x 2x 2x                                                 9x            
const INSTR_CONTRACT_NAME = 'instr.ore';
const INSTR_USAGE_CONTRACT_NAME = 'usagelog.ore';
const INSTR_TABLE_NAME = 'tokens';
const LOG_COUNT_TABLE_NAME = 'counts';
 
/* Private */
 
async function getInstrumentByOwner(owner) {
  const instruments = await this.findInstruments(owner);
  return instruments;
}
 
/* Public */
 
async function getInstrumentsByRight(instrumentList, rightName) {
  // Gets all the instruments with a particular right
  const instruments = await instrumentList.filter(instrument => this.getRight(instrument, rightName) !== undefined);
  return instruments;
}
 
async function getCallStats(instrumentId, rightName) {
  // calls the usagelog contract to get the total number of calls against a particular right
  const result = await this.eos.rpc.get_table_rows({
    code: INSTR_USAGE_CONTRACT_NAME,
    json: true,
    scope: instrumentId,
    table: LOG_COUNT_TABLE_NAME,
    limit: -1
  });
 
  const rightProperties = {
    totalCalls: 0,
    totalCpuUsage: 0
  };
 
  const rightObject = await result.rows.find(right => right.right_name === rightName);
 
  Eif (rightObject !== undefined) {
    rightProperties.totalCalls = rightObject.total_count;
    rightProperties.totalCpuUsage = rightObject.total_cpu;
  }
 
  return rightProperties;
}
 
async function getRightStats(rightName, owner) {
  // Returns the total cpu and calls against a particular right across all the instruments. If owner specified, then returns the total calls and cpu usage for the owner.
  let instruments;
  let rightProperties;
 
  Eif (owner) {
    instruments = await getInstrumentByOwner.bind(this)(owner);
  } else {
    instruments = await this.getAllTableRows({
      code: INSTR_CONTRACT_NAME,
      scope: INSTR_CONTRACT_NAME,
      table: INSTR_TABLE_NAME,
      limit: -1
    });
  }
 
  instruments = await getInstrumentsByRight.bind(this)(instruments, rightName);
 
  // Get the total cpu calls and cpu count across all the instruments
  const results = instruments.map(async (instrumentObject) => {
    rightProperties = await getCallStats.bind(this)(instrumentObject.id, rightName);
    return rightProperties;
  });
 
  const value = await Promise.all(results);
 
  return {
    totalCpuUsage: value.reduce((a, b) => a + parseFloat(b.totalCpuUsage), 0),
    totalCalls: value.reduce((a, b) => a + parseFloat(b.totalCalls), 0)
  };
}
 
async function updateUsageLog(verifierEndpoint, instrumentId, rightName, oreAccessToken, instrumentCallCost) {
  // Post the usage details for an instrument to the verifier usage log handler
  // Verifier then updates the usage for the instrument on the ORE blockchain
  const signature = await this.sign(instrumentId);
  const options = {
    method: 'POST',
    body: JSON.stringify({
      rightName,
      oreAccessToken,
      signature,
      voucherId: instrumentId,
      amount: instrumentCallCost
    }),
    headers: {
      'Content-Type': 'application/json'
    }
  };
  await fetch(`${verifierEndpoint}/update-usage`, options);
}
 
 
module.exports = {
  getCallStats,
  getRightStats,
  getInstrumentsByRight,
  updateUsageLog
};