pg-promise join array with promise result - pg-promise

I've spent the last few days learning node.js and pg-promise, and I'm trying to optimize my code to be as efficient and fast as possible. I'm not sure if I'm using pg-promise correctly, but I've been reviewing these posts and guides.
Nested queries with pg-promises
get JOIN table as array of results with PostgreSQL/NodeJS
Combine nested loop queries to parent array result - pg-promise
Data Imports
select=>insert
Ok, all of that said. The problem I'm coming across right now is the UPDATE statement isn't firing correctly, and I'm pretty confident that it's because the date field isn't correct. I'm not sure how to pass it in the correct date right now. The tulind function returns an array of value in the same order and count as was input. I need to update a table with those value with an index based on ticker + date.
My ideal state would be to use the 'massive-insert' strategy from the Data Imports link But I'm not quite sure how to implement that before solving this date issue.
Anyway, thank you beforehand for any help and let me know what other details I can provide to assist!
var tulind = require('tulind')
const argv = require('yargs').argv
let run_date = argv.t
const db = require('./pg_db.js')
//step 1: get list of tickers
//step 2: get the past 100 days of close_price to fed into tulind
//step 2: generate TI values from tulind
//step 3: update values back into postgres
db.tx(t => {
//select all eligible stocks that need to have their SMA10 value updated, could be 6k+ records.
return t.many(`select ticker from stocks.stock_md where date(last_sma10) <= date('${run_date}') or last_sma10 is null limit 1`)
.then(ticker_list => {
//console.log(ticker_list)
//select 100 past data points to feed into the tulind function for each individual stock ticker
return t.many(`select ticker, date, close_price from stocks.all_stocks where ticker='${ticker_list[0].ticker}' order by date desc limit 50`)
.then(eod_data => {
let cprice = eod_data.map(item => item.close_price)
//calculate the 10 day SMA for all stocks and update stock.all_stocks
//tulind will return an array of values for as many data points as is fed into the function
tulind.indicators.sma.indicator([cprice],[10], function(err, sma10_results) {
//eod_data.push("sma10: ", sma10_results) //this definitely doesn't work
const queries = eod_data.map(d=>t.none(`UPDATE stocks.all_stocks SET sma10='$4' WHERE ticker='$1' AND date='$2'`, eod_data))
//console.log(eod_data)
})
})
.catch(error => {
console.log(error)
})
return t.batch(queries);
})
.catch(error => {
console.log(error)
})
})
.then(data => {
// SUCCESS
})
.catch(error => {
// ERROR
})
<table border="1"><tbody><tr><th>ticker</th><th>open_price</th><th>close_price</th><th>date</th><th>SMA5</th><th>sma10</th><th>volume</th><th>signal</th><th>high</th><th>low</th><th>macd12</th><th>macd_signal9</th><th>macd26</th><th>obv</th><th>rsi7</th><th>rsi14</th></tr><tr><td>SEB</td><td>200.0000</td><td>200.0000</td><td>2000-01-03</td><td> </td><td> </td><td>500</td><td> </td><td>200.0000</td><td>200.0000</td><td> </td><td> </td><td> </td><td>-173264</td><td> </td><td> </td></tr><tr><td>SEB</td><td>200.0000</td><td>200.0000</td><td>2000-01-04</td><td> </td><td> </td><td>1000</td><td> </td><td>200.0000</td><td>200.0000</td><td> </td><td> </td><td> </td><td>-173264</td><td> </td><td> </td></tr><tr><td>SEB</td><td>201.0000</td><td>199.0000</td><td>2000-01-05</td><td> </td><td> </td><td>300</td><td> </td><td>201.0000</td><td>199.0000</td><td> </td><td> </td><td> </td><td>-174264</td><td> </td><td> </td></tr><tr><td>SEB</td><td>197.0000</td><td>194.0000</td><td>2000-01-06</td><td> </td><td> </td><td>1200</td><td> </td><td>197.1000</td><td>194.0000</td><td> </td><td> </td><td> </td><td>-174564</td><td> </td><td> </td></tr><tr><td>SEB</td><td>194.0000</td><td>194.0000</td><td>2000-01-07</td><td>197.4000000000029</td><td> </td><td>0</td><td> </td><td>194.0000</td><td>194.0000</td><td> </td><td> </td><td> </td><td>-174564</td><td> </td><td> </td></tr><tr><td>SEB</td><td>193.3000</td><td>193.1000</td><td>2000-01-10</td><td>196.0200000000029</td><td> </td><td>300</td><td> </td><td>193.3000</td><td>193.1000</td><td> </td><td> </td><td> </td><td>-174564</td><td> </td><td> </td></tr><tr><td>SEB</td><td>193.0000</td><td>193.0000</td><td>2000-01-11</td><td>194.6200000000029</td><td> </td><td>300</td><td> </td><td>193.0000</td><td>193.0000</td><td> </td><td> </td><td> </td><td>-174864</td><td> </td><td> </td></tr><tr><td>SEB</td><td>193.0000</td><td>191.0000</td><td>2000-01-12</td><td>193.0200000000029</td><td> </td><td>500</td><td> </td><td>193.0000</td><td>188.3000</td><td> </td><td> </td><td> </td><td>-175164</td><td> </td><td> </td></tr><tr><td>SEB</td><td>192.0000</td><td>192.0000</td><td>2000-01-13</td><td>192.6200000000029</td><td> </td><td>500</td><td> </td><td>192.0000</td><td>188.0000</td><td> </td><td> </td><td> </td><td>-174664</td><td> </td><td> </td></tr><tr><td>SEB</td><td>193.0000</td><td>189.0000</td><td>2000-01-14</td><td>191.6200000000029</td><td>194.50999999998996</td><td>600</td><td> </td><td>193.0000</td><td>186.0000</td><td> </td><td> </td><td> </td><td>-175164</td><td> </td><td> </td></tr><tr><td>SEB</td><td>188.0000</td><td>188.0000</td><td>2000-01-18</td><td>190.60000000000292</td><td>193.30999999998994</td><td>100</td><td> </td><td>188.0000</td><td>188.0000</td><td> </td><td> </td><td> </td><td>-175764</td><td> </td><td> </td></tr><tr><td>SEB</td><td>187.0000</td><td>192.0000</td><td>2000-01-19</td><td>190.40000000000293</td><td>192.50999999998996</td><td>300</td><td> </td><td>192.0000</td><td>187.0000</td><td> </td><td> </td><td> </td><td>-175664</td><td> </td><td> </td></tr><tr><td>SEB</td><td>194.0000</td><td>189.0000</td><td>2000-01-20</td><td>190.00000000000293</td><td>191.50999999998996</td><td>1100</td><td> </td><td>200.0000</td><td>185.5000</td><td> </td><td> </td><td> </td><td>-175964</td><td> </td><td> </td></tr><tr><td>SEB</td><td>189.0000</td><td>189.0000</td><td>2000-01-21</td><td>189.40000000000293</td><td>191.00999999998993</td><td>300</td><td> </td><td>189.0000</td><td>185.0000</td><td> </td><td> </td><td> </td><td>-175964</td><td> </td><td> </td></tr><tr><td>SEB</td><td>191.0000</td><td>194.8000</td><td>2000-01-24</td><td>190.5600000000029</td><td>191.0899999999899</td><td>1000</td><td> </td><td>198.0000</td><td>191.0000</td><td> </td><td> </td><td> </td><td>-175664</td><td> </td><td> </td></tr></tbody></table>
CREATE TABLE stocks.stock_md (
ticker varchar NOT NULL,
company_name varchar NULL,
sector varchar NULL,
industry varchar NULL,
daily_update varchar NULL,
last_update date NULL,
exchange varchar NULL,
last_obv date NULL,
last_sma5 date NULL,
last_sma10 date NULL,
last_rsi7 date NULL,
last_rsi14 date NULL
)
CREATE TABLE stocks.all_stocks (
ticker varchar NOT NULL,
open_price varchar NULL,
close_price varchar NULL,
"date" varchar NOT NULL,
"SMA5" varchar NULL,
sma10 varchar NULL,
volume varchar NULL,
signal varchar NULL,
high varchar NULL,
low varchar NULL,
macd12 varchar NULL,
macd_signal9 varchar NULL,
macd26 varchar NULL,
obv varchar NULL,
rsi7 varchar NULL,
rsi14 varchar NULL,
CONSTRAINT all_stocks_pk PRIMARY KEY (ticker, date)
)

Related

How to split array list into multiple rows and columns in scala

I am reading this XML file using scala and I had to define custom schema's for each table attribute as they are different.
<CATALOG>
<TABLE at_name="Furniture">
<ROWDATA>
<ROW typeid="0" caseid="0" key="1" code="0"/>
<ROW typeid="1" caseid="0" key="1" code="0"/>
<ROW typeid="1" caseid="1" key="1" code="0"/>
</ROWDATA>
</TABLE>
<TABLE at_name="Cutlery">
<ROWDATA>
<ROW cutleryTypeid="0" color="Blue" code="0"/>
<ROW cutleryTypeid="0" color="Blue" code="0"/>
</ROWDATA>
</TABLE>
<TABLE at_name="Apparel">
<ROWDATA>
<ROW ApparelTypeid="1" color="Blue" barcode="0111"/>
<ROW ApparelTypeid="0" color="Blue" barcode"1122"/>
</ROWDATA>
</TABLE>
<CATALOG>
I now have the following array list created like this(for attribute furniture):
val gl = DF.select("ROWDATA").rdd.map(r => r(0)).collect()
for(element<-gl)
{
println(element)
}
When I print the elements I get this:
[WrappedArray([0,0,1,0], [1,0,1,0], [1,1,1,0])]
I want this transformed into two outputs.
Output 1:
id elements
1 [0,0,1,0]
2 [1,0,1,0]
3 [1,0,1,0]
Then I want convert it like this:
Output 2:
id col1 col2 col3 col4
1 0 0 1 0
2 1 0 1 0
3 1 0 1 0
Any help on this please?

Question on SQLite database query on INNER join

I'm new for SQLite DB, I have three tables, M_Clinic, M_Healees and T_Treatments.. In following query, I have used two inner joins from T_Treatments (Transaction) to two of the master refernce tables M_Clinic and M_Healees.. There is a record on both T_Treatments and M_Healees with Healee_ID column with data value 1. However for some reason it is not pulling the record out.. If I do the same query without "where T_Treatments.Healee_ID = 1", the query pulls out the record.. Am I missing anything?
select M_Healees.Healee_ID,
M_Healees.Name,
M_Healees.PhoneNumber,
M_Healees.HealthInformation,
M_Healees.Address,
M_Healees.Clinic_ID,
M_Clinic.Name,
M_Clinic.Address,
T_Treatments.TreatmentDate,
T_Treatments.Symptoms,
T_Treatments.ACUPoint
from T_Treatments
INNER JOIN M_Clinic ON M_Healees. Clinic_ID = M_Clinic. Clinic_ID
INNER JOIN M_Healees ON M_Healees.Healee_ID = T_Treatments.Healee_ID
where T_Treatments.Healee_ID = 1
These are the data on my tables:
TABLE : M_clinic
<table><tr><td>Clinic_ID|</td><td>Name|</td><td>Address|</td></tr>
<tr><td>PK|</td><td>Salem Clinic|</td><td>No.52, Seerangan Street-2, Dhadhagapatti gate, Salem|</td></tr>
</table>
TABLE : M_Healees
<table><tr><td>Healee_ID|</td><td>Name|</td><td>PhoneNumber|</td><td>HealthInformation|</td><td>Address|</td><td>Clinic_ID|</td></tr>
<tr><td>1|</td><td>Narmada|</td><td>9988|</td><td>Body Pains|</td><td>Nethimedu|</td><td>PK|</td></tr>
</table>
TABLE : M_Treatments
<table><tr><td>Healee_ID|</td><td>TreatmentDate|</td><td>Symptoms|</td><td>ACUPoint|</td></tr>
<tr><td>1|</td><td>10/08/2019|</td><td>Body Pain|</td><td>LU5|</td></tr>
</table>

DokuWiki: Side-By-Side diff: Table with two cols in code block in table cell

I want to create a table in DokuWiki which has two rows.
On the left side is a code block (old code), and on the right side there is an other code block (new code).
How to format this in dokuwiki?
If it would be html, it would be easy:
<table>
<tr>
<td>
<pre>
...
</pre>
</td>
<td>
<pre>
...
</pre>
</td>
</tr>
</table>
Optional: Syntax highlighting for Python would be nice.
This should do the work:
| <code>Row 1 Col 1</code> | <code>Row 1 Col 2</code> |
Or for a simple monospaced text use '':
| ''%%Row 1 Col 1%%'' | ''%%Row 1 Col 2%%'' |
EDIT Add %% %% makes string to be not interpreted as DokuWiki syntax.
Here the documentation: https://www.dokuwiki.org/wiki:syntax
||border=1
||!Table heading 1||!Table heading 2||
||Cell 1.1||Cell 1.2||
||Cell 2.1||Cell 2.2||

Compare Calculated Date with Date Dimension in MDX

I've the following requirement:
There's a Fund Dimension and Date Dimension and Bid Price Measure. I've to show the Fund Code, Fund Series, Fund Name in Row and Bid Price in columns for only three Days i.e Today, 7 Days Back and Yesterday. I've calculated These days. How can I show the Dates on Row and get data for only those dates?
WITH
MEMBER [DateTest].[Today] As
FORMAT (Now(),'yyyy-MM-dd')
MEMBER [DateTest].[Yesterday] As
FORMAT (Now()-1,'yyyy-MM-dd')
MEMBER [DateTest].[LastWeek] As
FORMAT (Now()-7,'yyyy-MM-dd')
SELECT NON EMPTY { [Measures].[Fund Bid Price 6dp - Fund Ccy] } ON COLUMNS,
NON EMPTY { (
Except([Fund].[Fund Code].[Fund Code].ALLMEMBERS,
filter([Fund].[Fund Code].[Fund Code].ALLMEMBERS ,
(left( [Fund].[Fund Code].currentmember.name,1)="P"
OR left( [Fund].[Fund Code].currentmember.name,1)="Q"
OR left( [Fund].[Fund Code].currentmember.name,1)="W"
)
)
) --Excluding fund code which are starting with 'P','Q' or 'W'
*EXCEPT([Share Class].[Series Number].Children, [Share Class].[Series Number].&[11] ) --Excluding "X"
* [Fund].[Fund Name].[Fund Name].ALLMEMBERS
* [Fund].[Fund Type].[Fund Type].ALLMEMBERS
//* {[Business Date].[Year].&[2012],[Business Date].[Year].&[2014]}
//* ([Business Date].[Date].[Date].ALLMEMBERS )
)
} ON ROWS
FROM
(SELECT
([Business Date].[Year].&[1998] : [Business Date].[Year].&[2014] ) ON COLUMNS
FROM [IDM Investment Data] )
![enter image description here][1]

Show table rownumber

I have a script which type is="text/x-tmpl"
This script generates the rows and columns into the tbody.
What I try to do is to create a column which have a input to show the number of row of each item.
example:
row 1: thumbnail - Filename - 1
row 2: thumbnail - Filenaem - 2
...
...
I want that the user can change his row number.
The problem is that always show "0".
Here is the code:
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td><input type="text" value="{%=[i]%}"></td> /**My input to show the number of row**/
</tr>
{% } %}