Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. var http = require('http');
  2. var chalk = require('chalk');
  3. var _ = require('lodash');
  4.  
  5. // what am i?
  6. var PROP_NAME = 'temp';
  7. // how many clones of me?
  8. var PROPS_COUNT = 20;
  9. // how many data in each clone?
  10. var HISTORY_LENGTH = 20;
  11.  
  12. var TARGET_THNGS = {
  13. prod: 'UXKGt8PNhneNwcfHcbaD9Mdg',
  14. test: 'UXpGtQRqCyp6p6DFpWKhKeXg'
  15. };
  16.  
  17. var TARGET_SETTINGS = {
  18. prod: {
  19. host: 'api.evrythng.com',
  20. headers: {
  21. 'Content-Type': 'application/json',
  22. 'Accept': 'application/json',
  23. 'Authorization': 'LYfaSfGWrxuvUPtIoaSafIxhRWLamuTmUpMOOk15Wd4UNZcdtNoCEKvTIDkzxBJuSJOWcpS0xiiyyN0x'
  24. }
  25. },
  26. test: {
  27. host: 'api-test.evrythng.net',
  28. headers: {
  29. 'Content-Type': 'application/json',
  30. 'Accept': 'application/json',
  31. 'Authorization': 'WShV6OkSb7plW4ahKqzdJuKeqlPHkNALQyrerIkye59NpC1uH2ia3fvRmO2hCFiXjFsMJBzAqias9QOt'
  32. }
  33. }
  34. };
  35.  
  36. updateProperties();
  37.  
  38. function updateProperties() {
  39. ['prod', 'test'].forEach(function(target) {
  40. var data = generateData();
  41. var options = getRequestOptions(TARGET_SETTINGS[target], TARGET_THNGS[target]);
  42.  
  43. console.log(options);
  44. console.log(data);
  45.  
  46. var req = http.request(options, function(res) {
  47. res.setEncoding('utf8');
  48. res.on('data', function(data) {
  49. console.log('response //', chalk.green(res.statusCode), data);
  50. });
  51. });
  52.  
  53. req.write(JSON.stringify(data));
  54. req.end();
  55. });
  56. }
  57.  
  58. function getRequestOptions(proto, thng) {
  59. var clone = _.clone(proto);
  60. clone.path = thngPath(thng);
  61. clone.method = 'PUT';
  62.  
  63. return clone;
  64. }
  65.  
  66. function thngPath(thngId) {
  67. return `/thngs/${thngId}/properties`;
  68. }
  69.  
  70. function fakeData(name, length) {
  71. var arr = [];
  72. var initialDate = (new Date).valueOf() - randIn(50000, 100000);
  73. var i;
  74.  
  75. for (i = 0; i < length; i++) {
  76. arr.push({
  77. key: name,
  78. value: randIn(20, 25),
  79. timestamp: initialDate++
  80. });
  81. }
  82.  
  83. return arr;
  84. }
  85.  
  86. function generateData() {
  87. var carry = [];
  88. var moreToAdd = PROPS_COUNT;
  89.  
  90. while (moreToAdd--) {
  91. carry = carry.concat(fakeData(PROP_NAME + '-' + moreToAdd, HISTORY_LENGTH));
  92. }
  93.  
  94. return carry;
  95. }
  96.  
  97. function randIn(from, to) {
  98. return Math.floor(Math.random() * (to - from) + from);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement