mock平台

test.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const fs = require('fs-extra');
  2. const yapi = require('../../server/yapi.js');
  3. const commons = require('../../server/utils/commons');
  4. const dbModule = require('../../server/utils/db.js');
  5. const userModel = require('../../server/models/user.js');
  6. const mongoose = require('mongoose');
  7. yapi.commons = commons;
  8. yapi.connect = dbModule.connect();
  9. const convert2Decimal = num => (num > 9 ? num : `0${num}`);
  10. const formatYMD = (val, joinStr = '-') => {
  11. let date = val;
  12. if (typeof val !== 'object') {
  13. val = val * 1000;
  14. date = new Date(val);
  15. }
  16. return `${[
  17. date.getFullYear(),
  18. convert2Decimal(date.getMonth() + 1),
  19. convert2Decimal(date.getDate())
  20. ].join(joinStr)}`;
  21. };
  22. function run() {
  23. let time = yapi.commons.time() - 10000000;
  24. let data = i => {
  25. time = time - yapi.commons.rand(10000, 1000000);
  26. return {
  27. interface_id: 94,
  28. project_id: 25,
  29. group_id: 19,
  30. time: time,
  31. ip: '1.1.1.1',
  32. date: formatYMD(time)
  33. };
  34. };
  35. yapi.connect
  36. .then(function() {
  37. let logCol = mongoose.connection.db.collection('statis_mock');
  38. let arr = [];
  39. for (let i = 0; i < 11; i++) {
  40. if (arr.length >= 5) {
  41. logCol.insert(arr);
  42. arr = [];
  43. }
  44. arr.push(data(i));
  45. }
  46. })
  47. .catch(function(err) {
  48. throw new Error(err.message);
  49. });
  50. }
  51. run();