-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #409 from Sreejit-K/metrics_Integration_For_ticker…
…_funcitonality Metrics service Integration for ticker functionality and DB migration Script in Donor service
- Loading branch information
Showing
14 changed files
with
274 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const config = require('../configs/config'); | ||
const { Kafka, Partitioners } = require('kafkajs'); | ||
|
||
// Connect and send the event | ||
async function emitEventToKafka(topic, telemetryEvent) { | ||
|
||
const kafka = new Kafka({ | ||
clientId: 'dB-Migration', | ||
brokers: config.KAFKA_BROKER_FOR_METRICS_SERVICE?.split(","), // List your Kafka brokers | ||
createPartitioner: Partitioners.LegacyPartitioner | ||
}); | ||
|
||
const producer = kafka.producer(); | ||
|
||
await producer.connect(); | ||
await producer.send({ | ||
topic, | ||
messages: [ | ||
{ | ||
value: JSON.stringify(telemetryEvent) | ||
} | ||
] | ||
}); | ||
|
||
await producer.disconnect(); | ||
} | ||
|
||
module.exports = { | ||
emitEventToKafka | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const { default: axios } = require('axios'); | ||
const config = require('../configs/config'); | ||
const consants = require('../configs/constants'); | ||
const redis = require('../services/redis.service'); | ||
const abhaProfile = require('./abhaProfile.service'); | ||
const { ConfigSource } = require('kafkajs'); | ||
const utils =require('../utils/utils'); | ||
|
||
const lengthOfKey = 15; | ||
async function getMetrics(req, res) { | ||
try { | ||
let schemaType = req.params.schemaType; | ||
if (!schemaType) schemaType = "Pledge"; | ||
let metricsURL = `${config.METRICS_URL}/v1/metrics`; | ||
const key = abhaProfile.getKeyBasedOnEntityName(schemaType); | ||
let dataFromMetricsSerice = ((await axios.get(metricsURL)).data); | ||
let keys = await redis.getAllKeys(key); | ||
keys = keys.filter((i) => i.length === lengthOfKey); | ||
if (Object.keys(dataFromMetricsSerice).length > 0 ) { | ||
dataFromMetricsSerice[schemaType.toString().toLowerCase()]["UNPLEDGED"] = await getUnpledgedCount( keys); | ||
} else { | ||
dataFromMetricsSerice[schemaType.toString().toLowerCase()] = {}; | ||
dataFromMetricsSerice[schemaType.toString().toLowerCase()]["UNPLEDGED"] = await getUnpledgedCount( keys); | ||
} | ||
res.json(dataFromMetricsSerice); | ||
} catch (err) { | ||
const error = utils.getErrorObject(err) | ||
res.status(error.status).send(error) | ||
} | ||
} | ||
|
||
async function getUnpledgedCount (keys) { | ||
let count = 0 ; | ||
for (let i=0 ; i< keys.length; i++) { | ||
let value = await redis.getKey(keys[i]); | ||
if ( value === consants.PLEDGE_STATUS.UNPLEDGED) count++; | ||
} | ||
return count; | ||
} | ||
|
||
module.exports ={ | ||
getMetrics, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { default: axios } = require('axios'); | ||
const config = require('../configs/config'); | ||
const op = require('object-path'); | ||
const kafkaClient =require('../services/kafka.producer'); | ||
|
||
|
||
const topicName = `${config.METRICS_TOPIC}`; | ||
|
||
async function migrateDataToCH (entityName, entityId, accessToken) { | ||
|
||
try { | ||
let searchURL= `${config.REGISTRY_URL}/api/v1/${entityName}/${entityId}`; | ||
console.log(searchURL); | ||
let getTheDataFromRegistry = await axios.get(searchURL, { | ||
headers: { | ||
'Authorization': `Bearer ${accessToken}`, | ||
} | ||
}); | ||
if (getTheDataFromRegistry.data && getTheDataFromRegistry.data != '') { | ||
let transformedData = transformTheData(getTheDataFromRegistry.data, entityName); | ||
let telemertryObject = getTelemtryObject( entityName , entityId,transformedData); | ||
await kafkaClient.emitEventToKafka(topicName, telemertryObject) | ||
console.log("Migration was successfull!"); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
console.log(error?.response?.data); | ||
throw new Error("Error While DB Migration : ", error); | ||
} | ||
} | ||
|
||
|
||
function transformTheData (data, entityName) { | ||
let transformedData = data | ||
let getInternalFeilds = require(`../schemas/${entityName}.json`); | ||
if (getInternalFeilds?._osConfig?.internalFields) getInternalFeilds = getInternalFeilds?._osConfig?.internalFields | ||
else throw new Error("Couldnt fetch the schema / internal feilds - check the schema directory!"); | ||
for (let i=0 ; i < getInternalFeilds.length; i++) { | ||
let attribute = getInternalFeilds[i].split('$.')[1]; | ||
op.del(transformedData, attribute); | ||
} | ||
return transformedData; | ||
} | ||
|
||
function getTelemtryObject (entityName , entityId , filteredData) { | ||
return { | ||
"eid" : "ADD", | ||
"ets" : Date.now(), | ||
"ver" : "3.1", | ||
"mid" : "22e83ab6-f8c5-47af-a84b-c7113e1feb76", | ||
"actor" : { | ||
"id" : "", | ||
"type" : "USER" | ||
}, | ||
"object" : { | ||
"id" : entityId, | ||
"type" : entityName | ||
}, | ||
"edata" : filteredData | ||
} | ||
} | ||
|
||
module.exports ={ | ||
migrateDataToCH, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.