• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

super3 / feed / 16867361384

10 Aug 2025 11:04PM UTC coverage: 84.221% (-0.6%) from 84.818%
16867361384

push

github

developeratexample
Optimize filter-context API by eliminating internal HTTP calls

Replace internal HTTP request to /api/filter-queue/add with direct
storage operations. This improves performance and reliability by:
- Reducing network overhead
- Eliminating potential timeout issues
- Simplifying the request flow

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

305 of 383 branches covered (79.63%)

Branch coverage included in aggregate %.

0 of 13 new or added lines in 1 file covered. (0.0%)

533 of 612 relevant lines covered (87.09%)

9.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/api/filter-context.js
1
const { getStorage } = require('../lib/storage');
×
2
const { success, methodNotAllowed, badRequest, serverError } = require('../lib/utils/error-handler');
×
3
const logger = require('../lib/logger');
×
4

5
// Helper function to add posts to queue directly
6
async function addToQueue(posts, keyword, storage) {
NEW
7
  const timestamp = Date.now();
×
NEW
8
  const queueItems = [];
×
9

NEW
10
  for (const post of posts) {
×
NEW
11
    const queueKey = `queue:filter:${timestamp}:${post.id}`;
×
NEW
12
    const queueItem = {
×
13
      postId: post.id,
14
      title: post.title,
15
      selftext: post.selftext,
16
      keyword: keyword,
17
      timestamp: timestamp,
18
      status: 'pending',
19
      addedAt: new Date().toISOString()
20
    };
21
    
NEW
22
    await storage.set(queueKey, queueItem);
×
NEW
23
    queueItems.push({ key: queueKey, ...queueItem });
×
24
  }
25

NEW
26
  const stats = await storage.get('queue:stats') || { total: 0, pending: 0, processed: 0, failed: 0 };
×
NEW
27
  stats.total += posts.length;
×
NEW
28
  stats.pending += posts.length;
×
NEW
29
  await storage.set('queue:stats', stats);
×
30

NEW
31
  return { count: posts.length, items: queueItems };
×
32
}
33

34
module.exports = async (req, res) => {
×
35
  res.setHeader('Access-Control-Allow-Origin', '*');
×
36
  res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
×
37
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
×
38

39
  if (req.method === 'OPTIONS') {
×
40
    return res.status(200).end();
×
41
  }
42

43
  const storage = getStorage();
×
44
  await storage.init();
×
45

46
  if (req.method === 'POST') {
×
47
    try {
×
48
      const { keyword, context, posts } = req.body;
×
49

50
      if (!keyword || !context) {
×
51
        return badRequest(res, 'Keyword and context are required');
×
52
      }
53

54
      if (!posts || !Array.isArray(posts)) {
×
55
        return badRequest(res, 'Posts array is required');
×
56
      }
57

58
      const postsToQueue = posts.map(post => ({
×
59
        id: post.id,
60
        title: post.title,
61
        selftext: post.selftext || ''
×
62
      }));
63

64
      // Directly add to queue instead of making HTTP call
NEW
65
      const result = await addToQueue(postsToQueue, `${keyword} (context: ${context})`, storage);
×
66

67
      for (const post of posts) {
×
68
        const keys = await storage.keys(`posts:${keyword}:*`);
×
69
        for (const key of keys) {
×
70
          const data = await storage.get(key);
×
71
          if (!data || !data.posts) continue;
×
72
          
73
          const postIndex = data.posts.findIndex(p => p.id === post.id);
×
74
          if (postIndex !== -1) {
×
75
            data.posts[postIndex] = {
×
76
              ...data.posts[postIndex],
77
              filterContext: context,
78
              filterStatus: 'queued',
79
              queuedAt: new Date().toISOString()
80
            };
81
            await storage.set(key, data);
×
82
            break;
×
83
          }
84
        }
85
      }
86

87
      logger.info(`Queued ${posts.length} posts for filtering with context: ${context}`);
×
88
      
89
      return success(res, {
×
90
        message: 'Posts queued for filtering',
91
        count: result.count,
92
        queueItems: result.items
93
      });
94

95
    } catch (error) {
96
      return serverError(res, error, { 
×
97
        context: 'Failed to queue posts for filtering'
98
      });
99
    }
100
  }
101

102
  if (req.method === 'GET') {
×
103
    try {
×
104
      const { postId } = req.query;
×
105
      
106
      if (!postId) {
×
107
        const stats = await storage.get('queue:stats') || { total: 0, pending: 0, processed: 0, failed: 0 };
×
108
        return success(res, { stats });
×
109
      }
110

111
      const resultKey = `queue:results:${postId}`;
×
112
      const result = await storage.get(resultKey);
×
113
      
114
      if (!result) {
×
115
        const queueKeys = await storage.keys(`queue:filter:*:${postId}`);
×
116
        if (queueKeys.length > 0) {
×
117
          const queueItem = await storage.get(queueKeys[0]);
×
118
          if (queueItem) {
×
119
            return success(res, {
×
120
              postId,
121
              status: queueItem.status,
122
              queuedAt: queueItem.addedAt
123
            });
124
          }
125
        }
126
        
127
        return success(res, {
×
128
          postId,
129
          status: 'not_queued'
130
        });
131
      }
132

133
      return success(res, {
×
134
        postId,
135
        status: 'completed',
136
        relevant: result.relevant,
137
        reasoning: result.reasoning,
138
        confidence: result.confidence,
139
        completedAt: result.completedAt
140
      });
141

142
    } catch (error) {
143
      return serverError(res, error, { 
×
144
        context: 'Failed to get filter results'
145
      });
146
    }
147
  }
148

149
  return methodNotAllowed(res, ['POST', 'GET', 'OPTIONS']);
×
150
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc