2017-09-26 14 views
0

メトリック数式をデータベースに保存する最も良い方法は何ですか?最初は、生の列を視覚化ツールに投げて、私のメトリックを計算しました。私はすぐに、クライアントの要件などのために標準ルールに多くの(有効な)例外があることを学びました。私は現在、ETL /データベースレイヤの間に分子と分母の列を作成する必要があるかどうかを検討しています。視覚化ツール。後で計算するテーブルに数式を保存していますか?

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.eval.html

恩赦の書式が、私は以下の3つの列を持っている:私は、Pythonを使用して検討していた

はPostgresのテーブルに格納される文字列を読み取るために評価します。 1つの列が特定のプロジェクトに関連付けられ、次に2つのメトリックの例が表示されます。

id       productive_time      productive_status 
165 "productive_time = talk_time + hold_time + after_call_work_time" "productive_status = status_3_time + status_4_time + status_5_time" 
1911 "productive_time = talk_time + hold_time + after_call_work_time + ring_time" "productive_status = status_7_time + status_8_time" 

そして、可視化層において、メトリック計算は、単に潜在的な計算の数十を有すると比較SUM(productive_time)/SUM(call_count)であろう。

他のベストプラクティスはありますか?

代わりに大量のCASE WHEN文を使用することが考えられます。しかし文字通り数百に及ぶ数のidsがカバーされています。彼らの95%は同じでしょう。

編集:

​​

for x in range(0, len(df['inbound_time_formula'].unique())): 
    df.loc[df['inbound_time_formula'] == df['inbound_time_formula'].unique()[x], 'inbound_time'] = df.eval(df['inbound_time_formula'].unique()[x], inplace=True) 

私は、データフレームをdf.evalしようとしたが、全体のデータフレームではなく、式が存在しているだけの行に適用するように見えます。

答えて

0

ルール:

t=# create table rl(id serial,tm text, sm text); 
CREATE TABLE 
t=# insert into rl(tm,sm) values('a+b-c','a*b +c'); 
INSERT 0 1 

データ:

t=# create table dt(i serial,a int,b int, c int); 
CREATE TABLE 
t=# insert into dt(a,b,c) select 1,2,3; 
INSERT 0 1 

例:あなたは、定義により、注射を避けるカントとして

t=# create or replace function rlf(rid int,did int) returns table (rsm int,rtm int) as $$ 
begin 
return query execute format('select '||(select sm from rl where id=rid)||', '||(select tm from rl where id=rid)||' from dt where i=%s',did); 
end; 
$$ language plpgsql 
; 
CREATE FUNCTION 
t=# select * from rlf(1,1); 
rsm | rtm 
-----+----- 
    5 | 0 
(1 row) 

アプローチは非常に多くの疑問がある - あなたは解析されません。ルール - それをそのまま実行...

+0

リスクを軽減するために、私は確かにDB内ではなく、メモリ内で評価を行っています(そうするためにはPythonを使用します)。単一の価値を測定するN方法を扱う際に、一般的なベストプラクティスはありますか? – trench

+0

まあ、ルール列にチェック制約を追加して、数字、特殊文字、コールル名だけを許可することができます –

関連する問題