Skip to content

Commit 9257e58

Browse files
committed
Bugs fixed
1 parent bbcba30 commit 9257e58

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed

example/test2.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const _udf = require('../index.js');
22
const conn = require('./db.js');
33

4-
let nodeUDF = new _udf({
5-
con: conn
6-
});
4+
let nodeUDF = new _udf();
75

86
let {
97
convertToTradingSymbol,
@@ -38,7 +36,7 @@ const test2 = async () => {
3836
try {
3937
let sql = 'select * from users_db';
4038
let result = await nodeUDF.sqlQuery(sql, [], false, conn);
41-
let r = nodeUDF.convertToTable(result, ['id', 'client', 'password', 'api_key'], true, false, " | ", decimalLengths = 0, stringSplitter = ",", bunkColumn = 0, isfixedformat = false);
39+
let r = nodeUDF.convertToTable(result, ['id', 'client', 'password', 'api_key'], true, false, " | ", decimalLengths = 0, stringSplitter = ",", bunkColumn = 0, isfixedformat = false, false);
4240

4341
console.log(r);
4442

@@ -47,4 +45,15 @@ const test2 = async () => {
4745
}
4846
}
4947

48+
49+
50+
// const NodeCache = require("node-cache");
51+
// const myCache = new NodeCache()
52+
53+
async function test3() {
54+
let x = await nodeUDF.getsetData(['k'], 100);
55+
56+
console.log(x);
57+
}
58+
5059
test2()

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const BroadCastMessage = require('./telegram/broadcast');
88
const Pushlog = require('./telegram/logs');
99
const Quickfn = require('./telegram/quickfn');
1010
const BhavCopy = require('./public_api/bhavcopy');
11+
const Cache = require("./misc/cache");
1112

12-
class UDF extends Many(StringUDF, Table, InputValidation, ConvertToSymbol, SqlUDF, BroadCastMessage, Pushlog, Quickfn,BhavCopy) {
13+
class UDF extends Many(StringUDF, Table, InputValidation, ConvertToSymbol, SqlUDF, BroadCastMessage, Pushlog, Quickfn, BhavCopy, Cache) {
1314
constructor(params) {
1415
super(params)
1516
this.params = params;

misc/cache.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const NodeCache = require("node-cache");
2+
3+
class Cache extends NodeCache {
4+
constructor(params) {
5+
super(params);
6+
this.params = params;
7+
}
8+
9+
getData = (keys) => {
10+
let _this = this;
11+
let cache = Array.isArray(keys) ? _this.mget(keys) : _this.get(keys);
12+
13+
// let cache =
14+
15+
// Object.assign(cache, {
16+
// _key: val
17+
// });
18+
// }
19+
20+
return [cache];
21+
}
22+
23+
24+
getsetData = async (arr, ttl, cb, arg) => {
25+
let _this = this;
26+
let all_data = [await _this.mget(arr)];
27+
console.log(_this.keys());
28+
29+
let _ttl = (i) => Array.isArray(ttl) ? ttl[i] : ttl;
30+
let process_data = all_data.map((data, i) => {
31+
if (Object.keys(data).length == 0) {
32+
//get the data
33+
let _data = new Date();
34+
//cb(arg);
35+
36+
//store this
37+
_this.set(arr[i], _data, _ttl(i));
38+
39+
//return data
40+
return _data;
41+
} else {
42+
return data;
43+
}
44+
})
45+
console.log(_this.keys());
46+
47+
return process_data;
48+
}
49+
}
50+
51+
module.exports = Cache;

package-lock.json

Lines changed: 35 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-udf",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "imdkbj private nodejs udf",
55
"main": "index.js",
66
"private": true,
@@ -14,6 +14,7 @@
1414
"crypto-random-string": "^3.3.0",
1515
"extends-classes": "^1.0.5",
1616
"mysql": "^2.18.1",
17+
"node-cache": "^5.1.2",
1718
"request": "^2.88.2",
1819
"telegraf": "^3.38.0",
1920
"unzipper": "^0.10.11"

string_udf/table.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,36 @@ class Table extends StringUDF {
4848
super()
4949
}
5050

51+
cb(replyError, txt) {
52+
if (replyError) {
53+
return Promise.reject(txt);
54+
} else {
55+
return 'NA';
56+
}
57+
}
58+
5159
// ************************************************************************************************
5260
// convertToTable
5361
//
5462
// This will convert string/object/array into a tabular format.
5563
// It will have 7 arguments.
5664

57-
// convertToTable('1,2,3,4',null,false,false) ;//Output will be - 1|2|3|4
65+
// convertToTable('1,2,3,4',null,false) ;//Output will be - 1|2|3|4
5866
// convertToTable([[1.12,2,3,4]],null,false,false,"|",2) ;//Output will be - 1.12|2.00|3.00|4.00
59-
// convertToTable('1-2-3-4',null,false,false,"||",0,"-") ;//Output will be - 1||2||3||4
60-
// convertToTable([[1,2,3,4]],null,false,false) ;//Output will be - 1|2|3|4
67+
// convertToTable('1-2-3-4',null,false,"||",0,"-") ;//Output will be - 1||2||3||4
68+
// convertToTable([[1,2,3,4]],null,false) ;//Output will be - 1|2|3|4
6169
// convertToTable([[1,2,3,4]],['col1','col2','col3','col4'],false,false) ;//Output will be following
6270
// col1|col2|col3|col4
6371
// -------------------
6472
// 1 |2 |3 |4
73+
// convertToTable(sqlResult,['col1','col2','col3','col4']) ;//Direct sql result also can be converted.
6574
// ************************************************************************************************
66-
convertToTable = (array_for_table, hdr = null, isObj = true, isArrayReturn = false, sep = '|', decimalLengths = 0, stringSplitter = ",", bunkColumn = 0, isfixedformat = true) => {
75+
convertToTable = (array_for_table, hdr = null, isObj = true, isArrayReturn = false, sep = '|', decimalLengths = 0, stringSplitter = ",", bunkColumn = 0, isfixedformat = true, replyError = true) => {
76+
let _this = this;
6777
try {
6878
let array = isObj ? convert_obj_arr(array_for_table) : Array.isArray(array_for_table) ? parse_copy(array_for_table) : [array_for_table.split(stringSplitter)];
69-
if (array.length == 0)
70-
return Promise.reject('Invalid data passed in 1st argument.');
79+
if (array.length == 0) return _this.cb(replyError, 'Invalid data passed in 1st argument.')
80+
// return Promise.reject('Invalid data passed in 1st argument.');
7181

7282
//slice column
7383
if (bunkColumn > 0) {
@@ -78,7 +88,9 @@ class Table extends StringUDF {
7888
let col_length = JSON.parse(JSON.stringify(array));
7989

8090
let total_cols = col_length[0].length;
81-
if (col_length[0].constructor !== Array) return Promise.reject("Input is invalid for table conversion.")
91+
if (col_length[0].constructor !== Array) return _this.cb(replyError, "Input is invalid for table conversion.")
92+
93+
//Promise.reject("Input is invalid for table conversion.")
8294

8395
//negative values arr
8496
let col_negative = [...get_negative(col_length, total_cols)];
@@ -88,18 +100,15 @@ class Table extends StringUDF {
88100

89101
//find max each col
90102
if (hdr != null) {
91-
if (!Array.isArray(hdr)) return Promise.reject("Header can be either null or an array");
92-
if (hdr.length != col_length[0].length) return Promise.reject("Header column count mismatched.");
103+
if (!Array.isArray(hdr)) return _this.cb(replyError, "Header can be either null or an array");
104+
if (hdr.length != col_length[0].length) return _this.cb(replyError, "Header column count mismatched.");
93105

94106
col_length.push(hdr);
95107
}
96108

97109
let col_length_arr = col_length.map((r) => r.map((c) => lengths(c, decimalLengths)));
98110
let col_max = [...max_string_lengths(col_length_arr, total_cols)];
99111

100-
// console.log(col_negative, col_positive, col_length_arr, col_max);
101-
102-
103112
const format_row = (arrayValue, index) => {
104113
var rowTxt = arrayValue;
105114

@@ -133,7 +142,7 @@ class Table extends StringUDF {
133142

134143
let space_arr = _space_arr.map((r) => map_arr(r));
135144

136-
//add hdr
145+
//add header
137146
if (hdr != null) {
138147
let _hdr = map_arr(hdr);
139148
let find_max_length = Math.max(...space_arr.map(r => r.length), _hdr.length);
@@ -146,7 +155,7 @@ class Table extends StringUDF {
146155
final_table = isfixedformat ? `<code>${final_table}</code>` : final_table;
147156
return final_table;
148157
} catch (e) {
149-
return Promise.reject(e);
158+
return _this.cb(replyError, e);
150159
}
151160
}
152161
}

0 commit comments

Comments
 (0)