heisbuba commited on
Commit
0f9fba5
·
verified ·
1 Parent(s): cf53228

Update src/services/journal_engine.py

Browse files
Files changed (1) hide show
  1. src/services/journal_engine.py +34 -6
src/services/journal_engine.py CHANGED
@@ -3,7 +3,8 @@ import io
3
  import os
4
  import re
5
  from googleapiclient.discovery import build
6
- from googleapiclient.http import MediaByteArrayUpload, MediaIoBaseDownload
 
7
  from google.oauth2.credentials import Credentials
8
  from ..config import get_user_keys, update_user_keys
9
 
@@ -35,17 +36,45 @@ class JournalEngine:
35
  @staticmethod
36
  def save_to_drive(service, file_id, journal_data):
37
  """Uploads the updated journal list back to the hidden AppData folder."""
38
- media = MediaByteArrayUpload(
39
- json.dumps(journal_data).encode('utf-8'),
40
- mimetype='application/json'
 
 
41
  )
42
  service.files().update(fileId=file_id, media_body=media).execute()
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  @staticmethod
45
  def parse_pnl(pnl_str):
46
  """Helper to extract a numeric value from strings like '+$500' or '-10%'."""
47
  try:
48
- # Remove currency symbols and find the first number
49
  clean = re.sub(r'[^\d\.-]', '', str(pnl_str))
50
  return float(clean) if clean else 0.0
51
  except ValueError:
@@ -63,7 +92,6 @@ class JournalEngine:
63
  winrate = (len(wins) / total) * 100 if total > 0 else 0
64
  best_trade = max(journal_data, key=lambda x: cls.parse_pnl(x.get('pnl', 0)), default={})
65
 
66
- # Identify the most frequent bias tag
67
  biases = [t.get('bias', 'Neutral') for t in journal_data if t.get('bias')]
68
  main_bias = max(set(biases), key=biases.count) if biases else "Neutral"
69
 
 
3
  import os
4
  import re
5
  from googleapiclient.discovery import build
6
+ # [FIX] Import MediaIoBaseUpload instead of the non-existent MediaByteArrayUpload
7
+ from googleapiclient.http import MediaIoBaseUpload, MediaIoBaseDownload
8
  from google.oauth2.credentials import Credentials
9
  from ..config import get_user_keys, update_user_keys
10
 
 
36
  @staticmethod
37
  def save_to_drive(service, file_id, journal_data):
38
  """Uploads the updated journal list back to the hidden AppData folder."""
39
+ # [FIX] Use MediaIoBaseUpload with a BytesIO stream
40
+ media = MediaIoBaseUpload(
41
+ io.BytesIO(json.dumps(journal_data).encode('utf-8')),
42
+ mimetype='application/json',
43
+ resumable=True
44
  )
45
  service.files().update(fileId=file_id, media_body=media).execute()
46
 
47
+ @staticmethod
48
+ def initialize_journal(service):
49
+ """Finds or creates 'journal.json' in the appDataFolder."""
50
+ response = service.files().list(
51
+ spaces='appDataFolder',
52
+ fields='files(id, name)',
53
+ pageSize=1
54
+ ).execute()
55
+
56
+ files = response.get('files', [])
57
+ if files:
58
+ return files[0]['id']
59
+
60
+ # Create it if it doesn't exist
61
+ file_metadata = {
62
+ 'name': 'journal.json',
63
+ 'parents': ['appDataFolder']
64
+ }
65
+ # [FIX] Use MediaIoBaseUpload here too
66
+ media = MediaIoBaseUpload(
67
+ io.BytesIO(json.dumps([]).encode('utf-8')),
68
+ mimetype='application/json',
69
+ resumable=True
70
+ )
71
+ file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
72
+ return file.get('id')
73
+
74
  @staticmethod
75
  def parse_pnl(pnl_str):
76
  """Helper to extract a numeric value from strings like '+$500' or '-10%'."""
77
  try:
 
78
  clean = re.sub(r'[^\d\.-]', '', str(pnl_str))
79
  return float(clean) if clean else 0.0
80
  except ValueError:
 
92
  winrate = (len(wins) / total) * 100 if total > 0 else 0
93
  best_trade = max(journal_data, key=lambda x: cls.parse_pnl(x.get('pnl', 0)), default={})
94
 
 
95
  biases = [t.get('bias', 'Neutral') for t in journal_data if t.get('bias')]
96
  main_bias = max(set(biases), key=biases.count) if biases else "Neutral"
97