1
+ const Metrics = require ( '../models/metrics' ) ;
1
2
const client = require ( 'prom-client' ) ;
3
+ const jwt = require ( 'jsonwebtoken' ) ;
2
4
3
5
// Create a Registry to register metrics
4
6
const register = new client . Registry ( ) ;
@@ -25,9 +27,113 @@ const trackRequests = (req, res, next) => {
25
27
} ;
26
28
27
29
// Metrics endpoint handler
30
+ /*
28
31
const getMetrics = async (req, res) => {
29
32
res.set('Content-Type', register.contentType);
30
33
res.send(await register.metrics());
31
34
};
35
+ */
32
36
33
- module . exports = { trackRequests, getMetrics } ;
37
+ const getMetrics = async ( req , res ) => {
38
+ try {
39
+ // Get the Authorization header
40
+ const authHeader = req . headers [ 'authorization' ] ;
41
+ if ( ! authHeader || ! authHeader . startsWith ( 'Bearer ' ) ) {
42
+ return res . status ( 401 ) . json ( { error : 'Unauthorized: No token provided' } ) ;
43
+ }
44
+
45
+ // Extract the token
46
+ const token = authHeader . split ( ' ' ) [ 1 ] ;
47
+
48
+ // Verify and decode the token
49
+ const decoded = jwt . verify ( token , process . env . JWT_SECRET ) ;
50
+ const userId = decoded . userId ; // Ensure your JWT payload contains a `userId`
51
+
52
+ if ( ! userId ) {
53
+ return res . status ( 400 ) . json ( { error : 'Invalid token: userId missing' } ) ;
54
+ }
55
+
56
+ // Set content type and retrieve metrics
57
+ res . set ( 'Content-Type' , register . contentType ) ;
58
+ const metricsText = await register . metrics ( ) ;
59
+ const metrics = parseMetrics ( metricsText ) ; // Helper function to parse metrics.
60
+
61
+ // Save metrics to the database
62
+ await Metrics . saveMetricsToDatabase ( userId , metrics ) ;
63
+
64
+ // Send the metrics as a response
65
+ res . set ( 'Content-Type' , register . contentType ) ;
66
+ res . send ( metricsText ) ;
67
+ } catch ( error ) {
68
+ console . error ( "Error in getMetrics:" , error ) ;
69
+ res . status ( 500 ) . send ( { error : "Failed to process metrics" } ) ;
70
+ }
71
+ } ;
72
+
73
+ function parseMetrics ( metricsText ) {
74
+ const lines = metricsText . split ( "\n" ) ;
75
+ const metrics = [ ] ;
76
+
77
+ lines . forEach ( line => {
78
+ // Look for metric lines that follow the format: 'metric_name value'
79
+ if ( ! line . startsWith ( "#" ) && line . trim ( ) !== "" ) {
80
+ const [ metricName , metricValue ] = line . split ( / \s + / ) ;
81
+ if ( metricName && metricValue ) {
82
+ metrics . push ( { metricName, metricValue } ) ;
83
+ }
84
+ }
85
+ } ) ;
86
+
87
+ return metrics ;
88
+ }
89
+
90
+
91
+ const createMetric = async ( req , res ) => {
92
+ const { userId, metricName, metricValue } = req . body ;
93
+ try {
94
+ const metric = await Metrics . create ( { userId, metricName, metricValue } ) ;
95
+ res . status ( 201 ) . json ( metric ) ;
96
+ } catch ( error ) {
97
+ res . status ( 500 ) . json ( { error : 'Failed to create metric' } ) ;
98
+ }
99
+ } ;
100
+
101
+ const getMetricsByUser = async ( req , res ) => {
102
+ const { userId } = req . params ;
103
+ try {
104
+ const metrics = await Metrics . findByUserId ( userId ) ;
105
+ res . status ( 200 ) . json ( metrics ) ;
106
+ } catch ( error ) {
107
+ res . status ( 500 ) . json ( { error : 'Failed to fetch metrics' } ) ;
108
+ }
109
+ } ;
110
+
111
+ const updateMetric = async ( req , res ) => {
112
+ const { id } = req . params ;
113
+ const { metricName, metricValue } = req . body ;
114
+ try {
115
+ const metric = await Metrics . update ( id , { metricName, metricValue } ) ;
116
+ res . status ( 200 ) . json ( metric ) ;
117
+ } catch ( error ) {
118
+ res . status ( 500 ) . json ( { error : 'Failed to update metric' } ) ;
119
+ }
120
+ } ;
121
+
122
+ const deleteMetric = async ( req , res ) => {
123
+ const { id } = req . params ;
124
+ try {
125
+ await Metrics . delete ( id ) ;
126
+ res . status ( 200 ) . json ( { message : 'Metric deleted successfully' } ) ;
127
+ } catch ( error ) {
128
+ res . status ( 500 ) . json ( { error : 'Failed to delete metric' } ) ;
129
+ }
130
+ } ;
131
+
132
+ module . exports = {
133
+ trackRequests,
134
+ getMetrics,
135
+ createMetric,
136
+ getMetricsByUser,
137
+ updateMetric,
138
+ deleteMetric,
139
+ } ;
0 commit comments