Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- - Here are requirements:
- - (Task 1) Source of data: Read/Fetch the three columns (Date, MRN and SurveySelection) of data from csv file located at "c:\Survey\"
- folder - this file gets updated daily with 'date stamp' on its file name.
- - (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"
- - (Task 3) Update: If value of "MRN__c" is already there (by checking inside "SurveySelection__c" table),
- Update or Upsert(Salesforce terminology) all three columns (Date, MRN and SurveySelection) of data from csv file
- - (Task 4) Insert: If "MRN__c" is new/empty (by checking inside "SurveySelection__c" table),
- Insert all three columns (Date, MRN and SurveySelection) of data from csv file
- """
- import datetime, glob, os.path, re, string, sys
- import csv, os
- # JD: Step (1) Authentication to Salesforce
- from SfSimpleSf2 import *
- import time, pyodbc
- def unix_time(dtc): # Probably just getting Unix time
- epoch = datetime.datetime.utcfromtimestamp(0)
- delta = dtc - epoch
- return delta.total_seconds()
- # Function to put date parameter and used to execute
- def processDataUpsert(InFiles, dt1, dt2):
- #dt1Str = dt1.strftime("%Y%m%d") # Create variable/holder for dt1 and covert using strftime
- #dt2Str = dt2.strftime("%Y%m%d") # Create variable/holder for dt2 and covert using strftime
- nowStamp = datetime.datetime.now() # create variable nowStamp
- #tStampStr = nowStamp.strftime("%Y%m%d_%H%M%S") # convert nowStamp into different date format Ex. "20170122"
- #################################################################################
- # >> Task 1, Fetch the data from MRN column after reading thru each csv file
- # Locate INPUT folder
- InFiles = glob.glob('c:\Survey\SurveySelection*.csv') #Unix type of path name
- # Logic to go thru all different csv files and pick the ones in the date ranges
- # List and Timeframe
- InFileslist = [] # To hold a list of all files, list uses [], tuple uses ()
- for InFile in InFiles:
- thenStamp = os.path.getmtime(InFile) # return the time of last modification of path
- # (Needed) algorithm to determine time frame (right now, it is 60 days time frame)
- if (thenStamp >= unix_time(dt1)) and (thenStamp <= unix_time(dt2)):
- InFileslist.append(InFile)
- # UserRead what is inside csv file (data stays with csv file)
- # Go through all files in the List & Read, Read, Read..
- for filename in InFileslist:
- file = open(filename, 'r') # open a file and use it for reading ('r')
- lines = file.readlines() # readline() reads until EOF and returns a list containing the lines
- ###########################################################################################################
- # >> Task 2, Validation of data
- # Querying into SF database
- retA = sf.query_all("select MRN__c from SurveySelection__c") ## getting MRN data in SF
- recA = retA["records"] ## JD: Not sure but it instantiates new object recA from rectA
- sfMrnList = [(a['MRN__c']) for a in recA] ## JD: ???
- qF = "select MRN__c from SurveySelection__c"
- aF = sf.query_all(qF) #???
- rF = aF["records"] #???
- mrn = []
- for recF in rF:
- formId = recF["MRN__c"]
- formDict[formSh] = formId
- mrn.append(recF) ## ????
- ###########################################################################################################
- # >> 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
- if mrn not in sfMrnList: # if mrn is not empty
- # This is where data gets Upserted/Updated
- result = sf.SurveySelection_c.upsert({'mrn__c': mrn})
- ###########################################################################################################
- # >> 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
- if mrn in sfMrnList: # if mrn is empty
- # This is where data gets Inserted (not sure about expression though)
- result = sf.SurveySelection_c.create({'mrn__c': mrn})
- ##############################################################################################################
- # This is just a procedure to: these code look fine.
- if __name__ == "__main__":
- # Setting up time frame
- dateBegin = datetime.datetime.now() + datetime.timedelta(-60) ##I am looking at last 60 days. this would be too much.
- dateEnd = datetime.datetime.now() + datetime.timedelta(-1)
- mDateBegin = dateBegin.replace(hour=0, minute=0, second=0, microsecond=0)
- mDateEnd = dateEnd.replace(hour=23, minute=59, second=59, microsecond=999999)
- # Execute this function
- processDataUpsert(InFiles, mDateBegin, mDateEnd)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement