This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclicks.service.js
More file actions
130 lines (106 loc) · 4.87 KB
/
Copy pathclicks.service.js
File metadata and controls
130 lines (106 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { Client, types, auth } = require('dse-driver');
const protocolVersion = types.protocolVersion;
const PlainTextAuthProvider = auth.PlainTextAuthProvider;
// Don't try using v5-beta with the current driver (for now)
const options = { protocolOptions: { maxVersion: protocolVersion.v4 } };
// Username and password (from .env file) or from cluster1-superuser secret
const username = process.env.DSE_USERNAME || process.env.username;
const password = process.env.DSE_PASSWORD || process.env.password;
const secureConnectBundle = process.env.DSE_SECURE_CONNECT_BUNDLE;
let keyspace = process.env.DSE_KEYSPACE;
if (!keyspace) {
keyspace = 'ks1'; // hard-coded default from CQL examples
console.warn("DSE_KEYSPACE is not set in env! Using default keyspace name 'ks1'.");
}
console.log("DSE_KEYSPACE: ", keyspace);
console.log("DSE_USERNAME: ", username);
console.log("DSE_PASSWORD is set? ", !!password);
console.log("DSE_SECURE_CONNECT_BUNDLE is set? ", !!secureConnectBundle);
if (secureConnectBundle) { // IBM Cloud Databases for DataStax
// Remote secure connection to DBaaS (e.g. IBM Cloud)
console.log("Connecting with secure connect bundle.");
options.cloud = { secureConnectBundle };
options.credentials = { username, password };
options.keyspace = keyspace;
} else { // cass-operator on OpenShift
// cass-operator service accessible within the cluster
options.contactPoints = ['cluster1-dc1-service'];
console.log("Connecting to: ", options.contactPoints);
options.localDataCenter = 'dc1';
options.keyspace = keyspace;
options.authProvider = new PlainTextAuthProvider(username, password);
}
const client = new Client(options);
client.connect(function (err) {
if (err) {
console.error("!!! DATABASE CONNECT ERROR: ", err.message);
} else {
console.log('Connected to cluster with %d host(s): %j',
client.hosts.length, client.hosts.keys());
// Create the table if it doesn't already exist
execCQL(
`CREATE TABLE IF NOT EXISTS clickstream (
customer_id INT,
time_stamp TIMESTAMP,
click_event_type TEXT,
product_name TEXT,
product_category TEXT,
product_price DECIMAL,
total_price_of_basket DECIMAL,
total_number_of_items_in_basket INT,
total_number_of_distinct_items_in_basket INT,
session_duration INT,
PRIMARY KEY ((customer_id, click_event_type), time_stamp)
)`);
}
});
function execCQL(query, params) {
const options = { prepare: true };
// Execute a query
if (client.execute) {
client.execute(query, params, options)
.then(r => console.log("execCQL sent: ", query, params))
.catch(r => console.log("execCQL error: ", r.message));
} else {
console.log('Not connected to a database for execCQL');
}
}
async function trackPageBrowsing(sessionPromise, page) {
const session = await sessionPromise
console.log("SESSION: ",session);
let customer_id = session?.sub || 0;
console.log("CUSTOMER ID: ", customer_id);
console.log("BROWSING PAGE: ", page);
const query = `INSERT into clickstream (
customer_id, time_stamp, click_event_type, product_name, product_category, product_price,
total_price_of_basket, total_number_of_items_in_basket, total_number_of_distinct_items_in_basket,
session_duration
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
// NOTE: 0 for some things we are not using for now.
const params = [parseInt(customer_id), Math.round(Date.now()/1000), 'browsing', '', page, 0, 0, 0, 0, 0];
execCQL(query, params);
}
async function addToCart(customer_id, product, category, price) {
console.log("Add to cart : ", customer_id, product, category );
const query = `INSERT into clickstream (
customer_id, time_stamp, click_event_type, product_name, product_category, product_price,
total_price_of_basket, total_number_of_items_in_basket, total_number_of_distinct_items_in_basket,
session_duration
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
// NOTE: 0 for some things we are not using for now.
const params = [parseInt(customer_id), Math.round(Date.now()/1000), 'add-to-cart', product, category, price, 0, 1, 0, 0];
execCQL(query, params);
}
async function getActivity(customer_id) {
console.log("CUSTOMER ID: ", customer_id);
console.log("Get activity : ", customer_id );
const query = "SELECT * from clickstream where customer_id = ? ALLOW FILTERING";
const params = [parseInt(customer_id)];
const options = { prepare: true };
return await client.execute(query, params, options)
}
export const clickService = {
addToCart,
trackPageBrowsing,
getActivity
};