ocr-job-stats.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @node ocr-job-stats
  3. * @name OCR Job Stats
  4. * @category ocr
  5. * @version 1.0.1
  6. * @description Get OCR job queue statistics
  7. * @icon bar-chart-2
  8. */
  9. const configSchema = {
  10. type: 'object',
  11. properties: {
  12. baseUrl: {
  13. type: 'string',
  14. title: 'API Base URL',
  15. default: 'http://localhost:3000',
  16. description: 'Base URL of the OCR API'
  17. },
  18. credentialId: {
  19. type: 'string',
  20. title: 'Credential',
  21. description: 'Bearer or API Key credential for authentication',
  22. dynamicOptions: {
  23. source: 'credentials',
  24. filter: { type: 'api_key' }
  25. }
  26. }
  27. },
  28. required: ['baseUrl']
  29. };
  30. const inputSchema = {
  31. type: 'object',
  32. properties: {}
  33. };
  34. const outputSchema = {
  35. type: 'object',
  36. properties: {
  37. stats: {
  38. type: 'object',
  39. properties: {
  40. pending: { type: 'number' },
  41. processing: { type: 'number' },
  42. completed: { type: 'number' },
  43. failed: { type: 'number' }
  44. }
  45. }
  46. }
  47. };
  48. const outputs = [
  49. { name: 'main', displayName: 'Stats', type: 'object', color: '#f59e0b' }
  50. ];
  51. module.exports = {
  52. configSchema,
  53. inputSchema,
  54. outputSchema,
  55. outputs,
  56. async execute(config, input, context) {
  57. const baseUrl = (config.baseUrl || 'http://localhost:3000').replace(/\/$/, '');
  58. // Build headers
  59. const headers = {};
  60. smartbotic.log.info(`OCR Job Stats - credentialId: ${config.credentialId || 'NOT SET'}`);
  61. if (config.credentialId) {
  62. const auth = smartbotic.credentials.get(config.credentialId);
  63. smartbotic.log.info(`Credential lookup result: ${JSON.stringify(auth)}`);
  64. if (!auth.success) {
  65. throw new Error(`Failed to load credential: ${auth.error}`);
  66. }
  67. headers[auth.headerName] = auth.headerValue;
  68. smartbotic.log.info(`Auth header set: ${auth.headerName}`);
  69. } else {
  70. smartbotic.log.warn('No credential selected');
  71. }
  72. smartbotic.log.info(`OCR Get Job Stats from ${baseUrl}`);
  73. const response = smartbotic.http.request({
  74. method: 'GET',
  75. url: `${baseUrl}/api/jobs/stats`,
  76. headers: headers,
  77. timeout: 30000
  78. });
  79. if (response.status < 200 || response.status >= 300) {
  80. const errorMsg = typeof response.data === 'string' ? response.data : JSON.stringify(response.data);
  81. if (response.status === 401 && !config.credentialId) { throw new Error('Authentication required - please select a credential in node settings'); } throw new Error(`OCR API error (${response.status}): ${errorMsg}`);
  82. }
  83. const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
  84. return {
  85. stats: data.stats || data
  86. };
  87. }
  88. };