model_stats.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* a simple plugin that outputs some general information about a model... the point here is to demo how to use _mmmk from a plugin */
  2. const {
  3. __errorContinuable,
  4. __httpReq,
  5. __wHttpReq,
  6. __postInternalErrorMsg, __postMessage,
  7. __sequenceNumber,
  8. __successContinuable,
  9. __uri_to_id
  10. } = require("../__worker");
  11. const _do = require("../___do");
  12. const _utils = require('../utils');
  13. const _mmmk = require("../mmmk");
  14. module.exports = {
  15. 'interfaces' : [{'method':'GET', 'url=':'/stats'}],
  16. 'localcontext' : {},
  17. 'types2instances' :
  18. function()
  19. {
  20. var model = _utils.jsonp(_mmmk.read()),
  21. t2i = {};
  22. console.warn(model, typeof(model));
  23. for( var id in model.nodes )
  24. {
  25. var node = model.nodes[id];
  26. t2i[node['$type']] = t2i[node['$type']] || 0;
  27. t2i[node['$type']]++;
  28. }
  29. return _utils.jsons(t2i,null,'\t');
  30. },
  31. 'csworker' :
  32. function(resp,method,uri,reqData,wcontext)
  33. {
  34. var self = this;
  35. _do.chain(
  36. [__wHttpReq(method, uri+'?wid='+wcontext.__aswid, reqData)])(
  37. function()
  38. {
  39. __postMessage(
  40. {'statusCode':200,
  41. 'changelog':
  42. [{'op':'SYSOUT',
  43. 'text':'CS MODEL STATS\n'+self.types2instances()}],
  44. 'sequence#':__sequenceNumber(),
  45. 'respIndex':resp});
  46. },
  47. function(err) {__postInternalErrorMsg(resp,err);}
  48. );
  49. },
  50. 'asworker' :
  51. function(resp,method,uri,reqData,wcontext)
  52. {
  53. __postMessage(
  54. {'statusCode':200,
  55. 'changelog':
  56. [{'op':'SYSOUT',
  57. 'text':'AS MODEL STATS\n'+this.types2instances()}],
  58. 'sequence#':__sequenceNumber(),
  59. 'respIndex':resp});
  60. }
  61. };