Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var chalk = require('chalk');
- var _ = require('lodash');
- // what am i?
- var PROP_NAME = 'temp';
- // how many clones of me?
- var PROPS_COUNT = 20;
- // how many data in each clone?
- var HISTORY_LENGTH = 20;
- var TARGET_THNGS = {
- prod: 'UXKGt8PNhneNwcfHcbaD9Mdg',
- test: 'UXpGtQRqCyp6p6DFpWKhKeXg'
- };
- var TARGET_SETTINGS = {
- prod: {
- host: 'api.evrythng.com',
- headers: {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- 'Authorization': 'LYfaSfGWrxuvUPtIoaSafIxhRWLamuTmUpMOOk15Wd4UNZcdtNoCEKvTIDkzxBJuSJOWcpS0xiiyyN0x'
- }
- },
- test: {
- host: 'api-test.evrythng.net',
- headers: {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- 'Authorization': 'WShV6OkSb7plW4ahKqzdJuKeqlPHkNALQyrerIkye59NpC1uH2ia3fvRmO2hCFiXjFsMJBzAqias9QOt'
- }
- }
- };
- updateProperties();
- function updateProperties() {
- ['prod', 'test'].forEach(function(target) {
- var data = generateData();
- var options = getRequestOptions(TARGET_SETTINGS[target], TARGET_THNGS[target]);
- console.log(options);
- console.log(data);
- var req = http.request(options, function(res) {
- res.setEncoding('utf8');
- res.on('data', function(data) {
- console.log('response //', chalk.green(res.statusCode), data);
- });
- });
- req.write(JSON.stringify(data));
- req.end();
- });
- }
- function getRequestOptions(proto, thng) {
- var clone = _.clone(proto);
- clone.path = thngPath(thng);
- clone.method = 'PUT';
- return clone;
- }
- function thngPath(thngId) {
- return `/thngs/${thngId}/properties`;
- }
- function fakeData(name, length) {
- var arr = [];
- var initialDate = (new Date).valueOf() - randIn(50000, 100000);
- var i;
- for (i = 0; i < length; i++) {
- arr.push({
- key: name,
- value: randIn(20, 25),
- timestamp: initialDate++
- });
- }
- return arr;
- }
- function generateData() {
- var carry = [];
- var moreToAdd = PROPS_COUNT;
- while (moreToAdd--) {
- carry = carry.concat(fakeData(PROP_NAME + '-' + moreToAdd, HISTORY_LENGTH));
- }
- return carry;
- }
- function randIn(from, to) {
- return Math.floor(Math.random() * (to - from) + from);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement