genPogPerf

来源:互联网 发布:nginx lua waf 编辑:程序博客网 时间:2024/06/16 12:49
#!/usr/bin/perl

#usage: perl genPogPerf.pl outputPsaFileName cfgFilePath
#for example: perl genPogPerf.pl prd.txt pog.cfg
#Input file format: raw .psa file exported from Prospacesoftware.
#    while all the fieldsare separated by comma: entry type,field1,field2,...
#Output format: rowid,required fields separated by comma.like
#Description: This script is used to pre-process the raw .psafile before loading in DataStage jobs.
# The script splits the file into several files based on theentry type (Planogram, Product, Fixture and Position).
#          All the other types ofrecords will be discarded.  
# The pieces of smaller files will be further processed by theDataStage jobs and loaded into Database.

# TODO
# 1. Generate a product list
# 2. Generate the planogram records
# 3. Generate the fixture records
# 4. Generate the position records

use strict;
use File::Basename;
use File::Spec;
use File::stat;

# Config these parameters to generate expected number ofrecords
my $numOfProducts=100;
my $numOfPlanogram=100;
my $numOfFixturePerPOG=50;
my $numOfPositionPerFixture=100;

# The length of DPCI before prefix with TGT
my $lengthOfDPCI=10;

# Expected number of mapping fields in config file
my $numOfPogCols = 5;
my $numOfFixCols = 12;
my $numOfPosCols = 17;
my $numOfPrdCols = 9;

my @dpciList=();

&initDPCIList();

# Check the input parameters

my $requiredParameterNumber = 2; # Please modify thisparameter in case new parameter is added.
my $actualInputParameterNumber = scalar @ARGV;
print "Input parameters: ", (join ' ', @ARGV), "\n"if($requiredParameterNumber == $actualInputParameterNumber) or die"Required number of parameters: $requiredParameterNumber; Actualnumber of parameters: $actualInputParameterNumber.";

# Check the files and directories
my $outputFile = shift;
#print "Input file: $outputFile\n" if(-r $outputFile) or die"The $outputFile is not readable.<$!>";
my $fileName = basename $outputFile;
print "Basename: $fileName\n";

my $cfgFile = shift;
print "Config File: $cfgFile\n" if (-r $cfgFile) or die "The$cfgFile is not readable.<$!>";


# Start - Read fields Mapping info from config file

my %typesMap = ();
my $type;

my (%pogMap, %fixMap, %posMap, %prdMap);
# Hash key: Record Type; Hash value: Reference of fieldmapping hash.
$typesMap{'Planogram'} = \%pogMap;
$typesMap{'Fixture'} = \%fixMap;
$typesMap{'Position'} = \%posMap;
$typesMap{'Product'} = \%prdMap;

open(FH_CONFIG, "<$cfgFile") || die "Unable toopen the field map config file : $cfgFile<$!>";

while(<FH_CONFIG>) {
    chomp;
    next if /^#/;      # skip comments
    next if /^\s*$/;    # skip empty lines
if(/^\s*\[\s*(\w+)\s*\]\s*$/){# This is the record type block,like [Planogram]
$type = $1;
next;
}
    # Store field name askey, index as value.
if (/^\s*(\w+)\s*=\s*(\w+)\s*$/){ # Format: FieldName =Index
$typesMap{$type}->{$1} = $2;
}

close FH_CONFIG;
# End - Read fields Mapping info from config file


# Key columns:
# Planogram: PlanogramKey
# Fixture: FixtureName
# Position: DPCI
# Product: DPCI

# Open the files
open(FH, ">$outputFile") || die "Unable to openthe file : $outputFile <$!>";

# Generate Products
&genProduct();

# Generate Planograms
&genPlanogram();

print "Start processing the file: $fileName..\n";


sub genProduct{
foreach my $dpci (@dpciList){
my @prd=();
my $randNum=0;
$prd[0]='Product';
foreach my $key (keys %prdMap){
$randNum = int(rand(100));
if($key eq 'DPCI'){
$prd[$prdMap{$key}]=$dpci;
}
else{
$prd[$prdMap{$key}]=$randNum;
}
}
my $product = join ',',@prd;
print FH "$product\n";
}

}
sub genPlanogram{
my $randNum=0;
for(my $i=0;$i<$numOfPlanogram;$i++){
my @pog=();
$pog[0]='Planogram';
my $pogNum=&prefixPOG($i);
foreach my $key (keys %pogMap){
$randNum = int(rand(100));
if($key eq 'PlanogramKey'){
$pog[$pogMap{$key}]=$pogNum;
}
elsif($key eq 'Name' || $key eq 'Description'){
$pog[$pogMap{$key}]="POG_".$pogNum;
}
else{
$pog[$pogMap{$key}]=$randNum;
}
}
my $planogram = join ',',@pog;
print FH "$planogram\n";
&genFixture();
}
}


sub genFixture{
my $randNum=0;
for(my $i=0;$i<$numOfFixturePerPOG;$i++){
my @fix=();
$fix[0]='Fixture';
my $fixtureName=&prefixFix($i);
foreach my $key (keys %fixMap){
$randNum = int(rand(100));
if($key eq 'FixtureName'){
$fix[$fixMap{$key}]=$fixtureName;
}
elsif($key eq 'FixtureType'){
$fix[$fixMap{$key}]=0;
}
else{
$fix[$fixMap{$key}]=$randNum;
}
}
my $fixture = join ',',@fix;
print FH "$fixture\n";
&genPosition();
}
}

sub genPosition{
my $randNum=0;
for(my$i=0;$i<$numOfPositionPerFixture;$i++){
my @pos=();
$pos[0]='Position';
my $randIdx = int(rand(scalar @dpciList))%(scalar@dpciList);
my $dpic=$dpciList[$randIdx];
foreach my $key (keys %posMap){
$randNum = int(rand(100));
if($key eq 'DPCI'){
$pos[$posMap{$key}]=$dpic;
}
else{
$pos[$posMap{$key}]=$randNum;
}
}
my $position = join ',',@pos;
print FH "$position\n";
}
}


print "Complete generating the psa file: $fileName.\n";



sub initDPCIList{
for(my $i=0;$i<$numOfProducts;$i++){
$dpciList[$i]=&prefixDPCI($i);
}
}

# Prefix the passed-in string
sub prefixDPCI{
my $str = shift;
if ($lengthOfDPCI - length($str) > 0) {  
$str = ('0' x ($lengthOfDPCI- length($str))).$str;
}
$str;
}

# Prefix the passed-in string
sub prefixPOG{
my $str = shift;
if (7 - length($str) > 0) {  
$str = 'P'.('0' x (6- length($str))).$str;
}
$str;
}

# Prefix the passed-in string
sub prefixFix{
my $str = shift;
$str = 'Fix'.('0' x (5- length($str))).$str;
$str;
}

sub trim{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

0 0
原创粉丝点击