Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. - Here are requirements:
  6.  
  7. - (Task 1) Source of data: Read/Fetch the three columns (Date, MRN and SurveySelection) of data from csv file located at "c:\Survey\"
  8. folder - this file gets updated daily with 'date stamp' on its file name.
  9.  
  10. - (Task 2) Validation of data: data from "MRN__c" column gets checked to see if there is any new or existing one from Table called "SurveySelection__c"
  11.  
  12. - (Task 3) Update: If value of "MRN__c" is already there (by checking inside "SurveySelection__c" table),
  13. Update or Upsert(Salesforce terminology) all three columns (Date, MRN and SurveySelection) of data from csv file
  14.  
  15. - (Task 4) Insert: If "MRN__c" is new/empty (by checking inside "SurveySelection__c" table),
  16. Insert all three columns (Date, MRN and SurveySelection) of data from csv file
  17. """
  18.  
  19. import datetime, glob, os.path, re, string, sys
  20. import csv, os
  21.  
  22. # JD: Step (1) Authentication to Salesforce
  23. from SfSimpleSf2 import *
  24. import time, pyodbc
  25.  
  26. def unix_time(dtc): # Probably just getting Unix time
  27. epoch = datetime.datetime.utcfromtimestamp(0)
  28. delta = dtc - epoch
  29. return delta.total_seconds()
  30.  
  31. # Function to put date parameter and used to execute
  32. def processDataUpsert(InFiles, dt1, dt2):
  33. #dt1Str = dt1.strftime("%Y%m%d") # Create variable/holder for dt1 and covert using strftime
  34. #dt2Str = dt2.strftime("%Y%m%d") # Create variable/holder for dt2 and covert using strftime
  35. nowStamp = datetime.datetime.now() # create variable nowStamp
  36. #tStampStr = nowStamp.strftime("%Y%m%d_%H%M%S") # convert nowStamp into different date format Ex. "20170122"
  37.  
  38.  
  39. #################################################################################
  40. # >> Task 1, Fetch the data from MRN column after reading thru each csv file
  41.  
  42. # Locate INPUT folder
  43. InFiles = glob.glob('c:\Survey\SurveySelection*.csv') #Unix type of path name
  44.  
  45.  
  46. # Logic to go thru all different csv files and pick the ones in the date ranges
  47. # List and Timeframe
  48. InFileslist = [] # To hold a list of all files, list uses [], tuple uses ()
  49. for InFile in InFiles:
  50. thenStamp = os.path.getmtime(InFile) # return the time of last modification of path
  51.  
  52. # (Needed) algorithm to determine time frame (right now, it is 60 days time frame)
  53. if (thenStamp >= unix_time(dt1)) and (thenStamp <= unix_time(dt2)):
  54. InFileslist.append(InFile)
  55.  
  56.  
  57. # UserRead what is inside csv file (data stays with csv file)
  58. # Go through all files in the List & Read, Read, Read..
  59. for filename in InFileslist:
  60. file = open(filename, 'r') # open a file and use it for reading ('r')
  61. lines = file.readlines() # readline() reads until EOF and returns a list containing the lines
  62.  
  63. ###########################################################################################################
  64.  
  65. # >> Task 2, Validation of data
  66.  
  67. # Querying into SF database
  68. retA = sf.query_all("select MRN__c from SurveySelection__c") ## getting MRN data in SF
  69. recA = retA["records"] ## JD: Not sure but it instantiates new object recA from rectA
  70. sfMrnList = [(a['MRN__c']) for a in recA] ## JD: ???
  71.  
  72. qF = "select MRN__c from SurveySelection__c"
  73. aF = sf.query_all(qF) #???
  74. rF = aF["records"] #???
  75.  
  76. mrn = []
  77. for recF in rF:
  78. formId = recF["MRN__c"]
  79. formDict[formSh] = formId
  80. mrn.append(recF) ## ????
  81.  
  82.  
  83. ###########################################################################################################
  84. # >> Task 3 >> If there is already MRN__c inside SurveySelection__c, then just upsert/update three columns (Date__c, MRN__c, SurveySelection__c) of data inside SurveySelection__c table
  85.  
  86. if mrn not in sfMrnList: # if mrn is not empty
  87. # This is where data gets Upserted/Updated
  88. result = sf.SurveySelection_c.upsert({'mrn__c': mrn})
  89.  
  90. ###########################################################################################################
  91. # >> Task 4 >> If there is no MRN__c inside SurveySelection__c, then insert three columns (Date__c, MRN__c, SurveySelection__c) of data inside SurveySelection__c tabl
  92.  
  93. if mrn in sfMrnList: # if mrn is empty
  94. # This is where data gets Inserted (not sure about expression though)
  95. result = sf.SurveySelection_c.create({'mrn__c': mrn})
  96.  
  97.  
  98. ##############################################################################################################
  99.  
  100. # This is just a procedure to: these code look fine.
  101. if __name__ == "__main__":
  102.  
  103. # Setting up time frame
  104. dateBegin = datetime.datetime.now() + datetime.timedelta(-60) ##I am looking at last 60 days. this would be too much.
  105. dateEnd = datetime.datetime.now() + datetime.timedelta(-1)
  106.  
  107. mDateBegin = dateBegin.replace(hour=0, minute=0, second=0, microsecond=0)
  108. mDateEnd = dateEnd.replace(hour=23, minute=59, second=59, microsecond=999999)
  109.  
  110.  
  111. # Execute this function
  112. processDataUpsert(InFiles, mDateBegin, mDateEnd)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement