---
# System prepended metadata

title: 新興MM

---

# 新興MM

```python
import os
import sys
import configparser
from sasctl.services import model_repository as mr
from sasctl import Session

class SASModelManager():
    def __init__(self, config_path=None, host='', username='', password='', ca_path=''):
        if config_path:
            config = configparser.ConfigParser()
            config.read(config_path)
            self.host = config['Session']['host']
            self.username = config['Session']['username']
            self.password = config['Session']['password']
        else:
            self.host = host
            self.username = username
            self.password = password
        os.environ['CAS_CLIENT_SSL_CA_LIST'] = config['Env']['capath'] if config_path else ca_path

    def check_repository(self, repo_name):
        repo = mr.get_repository(repo_name)
        # if not building repository yet, exit
        if not repo:
            print("You have not build this repository ({}) yet. Please go to MM then build {} repository.".format(repo_name, repo_name))
            sys.exit(0)
        return repo

    def check_project(self, project_name, repo_name):
        repo = self.check_repository(repo_name)
        project_list = self.get_project_list()
        if project_name in project_list:
            print(f"{project_name} exists. Get the project.")
            return mr.get_project(project_name)
        
        print(f"{project_name} does not exist, creating the project")
        return mr.create_project({'name': project_name}, repo)
    
    def upload_model(self, repo_name, project_name, model_name, modeler, function, algorithm, tool, location, properties):
        with Session(self.host, self.username, self.password):
            project = self.check_project(project_name, repo_name)
            model_list = self.get_model_list()
            if model_name not in model_list:
                mr.create_model(
                    model={'name': model_name},
                    project=project,
                    modeler=modeler,
                    function=function,
                    algorithm=algorithm,
                    tool=tool,
                    location=location,
                    properties=properties
                )
            else:
                print(f"Model[{model_name}] exists, creating new version...")
                model = mr.create_model_version(model_name)
                model['modeler']=modeler
                model['function']=function
                model['algorithm']=algorithm
                model['location']=location
                for i in range(len(model['properties'])):
                    if model['properties'][i]['name'] in properties.keys():
                        model['properties'][i]['value']=properties[model['properties'][i]['name']]

                mr.update_model(model)

    def get_project_list(self):
        return [p['name'] for p in mr.list_projects()]

    def get_model_list(self):
        return [m['name'] for m in mr.list_models()]
```


```
sasmm.upload_model(repo_name="Public", 
                   project_name="Iris2", 
                   model_name="Iris Regression", 
                   modeler="Jason", 
                   function="Classification", 
                   algorithm="Logistic Regression", 
                   tool="Python 3", 
                   location="/jupyter_workspace/Unimicron/Jason/model_train.pt", 
                   properties={'Accuracy': model.score(X_test, y_test)})
```



```
kubectl get pods | grep launcher

kubectl exec -it <launcher-pod> --bash

ls -l /nfsshare01/sas_data_access
```

```
import pymssql
import pandas as pd
import functools

def connect(factory):
    if factory=="YM":
        host='10.22.65.120'
        db='dc'
        user='dc'
        pwd='dc'
    elif factory=="SY":
        host="10.30.40.110"
        db="dc"
        user="s3pc"
        pwd="pc12345"
    elif factory=="HF":
        host="10.44.66.47"
        db="SFIBDI"
        user="sfibdi"
        pwd="pc12345"

    cnxn = pymssql.connect(
        host=host,
        user=user,
        password=pwd,
        database=db)
    return cnxn

def check_lot(factory, cnxn, hours=24):
	if factory=="YM":
	    sql = """
		SELECT A.ProcCode, 'tot_cnt_{day}' as daycnt,COUNT(DISTINCT A.lotnum) as 'cnt'
		FROM acme.dbo.pdl_ckhistory AS A(NOLOCK)
		WHERE A.Line = 55
		 AND A.ProcCode in ('FVI06','AOI02')
		 AND A.AftStatus = 'CheckIn'
		 AND A.BefStatus <> 'CheckIn'
		 AND A.isCancel IS NULL
		 AND A.Changetime BETWEEN DATEADD(HOUR, -{hours}, GETDATE()) AND GETDATE()
		GROUP BY A.ProcCode
	    """.format(day=int(hours/24),hours=hours)
	elif factory =="SY":
	    sql = """
		SELECT CASE WHEN A.Line=5 THEN 'S1'
		WHEN A.Line=6 THEN 'S2'
		WHEN A.Line=7 THEN 'S3'
		WHEN A.Line=54 THEN 'S2A' END AS Factory, 
		A.ProcCode, 'tot_cnt_{day}' as daycnt,COUNT(DISTINCT A.lotnum) as 'cnt'
		FROM acme.dbo.pdl_ckhistory AS A(NOLOCK)
		WHERE A.Line in (5,6,7,54)
		 AND A.ProcCode in ('FVI06','AOI02')
		 AND A.AftStatus = 'CheckIn'
		 AND A.BefStatus <> 'CheckIn'
		 AND A.isCancel IS NULL
		 AND A.Changetime BETWEEN DATEADD(HOUR, -{hours}, GETDATE()) AND GETDATE()
		GROUP BY A.Line, A.ProcCode
	    """.format(day=int(hours/24),hours=hours)
	elif factory=="HF":
		sql = """
		SELECT CASE WHEN A.Line=28 THEN 'H1'
		WHEN A.Line=29 THEN 'H3' END AS Factory, 
		A.ProcCode, 'tot_cnt_{day}' as daycnt,COUNT(DISTINCT A.lotnum) as 'cnt'
		FROM dbo.view_PDL_CKHistory AS A(NOLOCK)
		WHERE A.Line in (28, 29)
		     AND A.ProcCode in ('FVI06','AOI02')
		     AND A.AftStatus = 'CheckIn'
		     AND A.BefStatus <> 'CheckIn'
		     AND A.isCancel IS NULL
		     AND A.Changetime BETWEEN DATEADD(HOUR, -{hours}, GETDATE()) AND GETDATE()
		GROUP BY A.Line, A.ProcCode
		""".format(day=int(hours/24),hours=hours)
	df = pd.read_sql(sql, cnxn)
	"""
	cursor = cnxn.cursor(as_dict=True)
	cursor.execute(sql)
	row = cursor.fetchone()
	cnt_val = row['cnt'] if row else -1
	cursor.close()
	"""
	return df

"""
ym_cnxn=connect(factory="YM")
total_cnt_1day = check_lot(ym_cnxn, hours=24)
total_cnt_7day = check_lot(ym_cnxn, hours=24*7)
total_cnt_30day = check_lot(ym_cnxn, hours=24*30)
print(total_cnt_1day, total_cnt_7day, total_cnt_30day)
ym=functools.reduce(lambda left, right: pd.merge(left, right, on='ProcCode'), 
	[total_cnt_1day,total_cnt_7day,total_cnt_30day])
ym["Factory"]="YM"
ym_cnxn.close()
"""

def factory_pipeline(factory):
	# Build SQL connection
	cnxn = connect(factory=factory)
	total_cnt_1day = check_lot(factory, cnxn, hours=24)
	total_cnt_7day = check_lot(factory, cnxn, hours=24*7)
	total_cnt_30day = check_lot(factory, cnxn, hours=24*30)
	df_concat=pd.concat([total_cnt_1day, total_cnt_7day, total_cnt_30day])
	if factory=="SY" or factory=="HF":
		df_all = pd.pivot(df_concat, index=['Factory','ProcCode'], columns='daycnt', values='cnt')
	else:
		df_all = pd.pivot(df_concat, index='ProcCode', columns='daycnt', values='cnt')
		df_all['Factory'] = factory
	df_all = df_all.reset_index()
	return df_all

ym = factory_pipeline("YM")
SAS.df2sd(ym,'msym')
sy = factory_pipeline("SY")
SAS.df2sd(sy,'mssy')
hf = factory_pipeline("HF")
SAS.df2sd(hf, 'mshf')
```