mock平台

statisMockModel.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Created by gxl.gao on 2017/10/24.
  3. */
  4. const yapi = require('yapi.js');
  5. const baseModel = require('models/base.js');
  6. class statisMockModel extends baseModel {
  7. getName() {
  8. return 'statis_mock';
  9. }
  10. getSchema() {
  11. return {
  12. interface_id: { type: Number, required: true },
  13. project_id: { type: Number, required: true },
  14. group_id: { type: Number, required: true },
  15. time: Number, //'时间戳'
  16. ip: String,
  17. date: String
  18. };
  19. }
  20. countByGroupId(id){
  21. return this.model.countDocuments({
  22. group_id: id
  23. })
  24. }
  25. save(data) {
  26. let m = new this.model(data);
  27. return m.save();
  28. }
  29. getTotalCount() {
  30. return this.model.countDocuments({});
  31. }
  32. async getDayCount(timeInterval) {
  33. let end = timeInterval[1];
  34. let start = timeInterval[0];
  35. let data = [];
  36. const cursor = this.model.aggregate([
  37. {
  38. $match: {
  39. date: { $gt: start, $lte: end }
  40. }
  41. },
  42. {
  43. $group: {
  44. _id: '$date', //$region is the column name in collection
  45. count: { $sum: 1 }
  46. }
  47. },
  48. {
  49. $sort: { _id: 1 }
  50. }
  51. ]).cursor({}).exec();
  52. await cursor.eachAsync(doc => data.push(doc));
  53. return data;
  54. }
  55. list() {
  56. return this.model.find({}).select('date').exec();
  57. }
  58. up(id, data) {
  59. data.up_time = yapi.commons.time();
  60. return this.model.updateOne({
  61. _id: id
  62. }, data, { runValidators: true });
  63. }
  64. }
  65. module.exports = statisMockModel;